AndroidUtils

A set of utility classes to hopefully help you save your time.

License

License

Categories

Categories

GUI User Interface
GroupId

GroupId

com.github.guilhe
ArtifactId

ArtifactId

android-utils
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

aar
Description

Description

AndroidUtils
A set of utility classes to hopefully help you save your time.
Project URL

Project URL

https://github.com/GuilhE/android-utils-lib
Source Code Management

Source Code Management

https://github.com/GuilhE/android-utils-lib

Download android-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.guilhe/android-utils/ -->
<dependency>
    <groupId>com.github.guilhe</groupId>
    <artifactId>android-utils</artifactId>
    <version>1.0.3</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.guilhe/android-utils/
implementation 'com.github.guilhe:android-utils:1.0.3'
// https://jarcasting.com/artifacts/com.github.guilhe/android-utils/
implementation ("com.github.guilhe:android-utils:1.0.3")
'com.github.guilhe:android-utils:aar:1.0.3'
<dependency org="com.github.guilhe" name="android-utils" rev="1.0.3">
  <artifact name="android-utils" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.guilhe', module='android-utils', version='1.0.3')
)
libraryDependencies += "com.github.guilhe" % "android-utils" % "1.0.3"
[com.github.guilhe/android-utils "1.0.3"]

Dependencies

runtime (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

SharedPrefsUtils (Kotlin version here!)

Build Status Android Arsenal

Save and load objects from SharedPreferences in a faster and simpler way.

Version 1.x

- November, 2018    - "Rebranding", changed lib name.
- November, 2017    - Gradle update, added Codacy.
- August, 2017      - SharedPrefsUtils.

NOTE: this repo will not be updated you should switch to kotlin version

Getting started

The first step is to include SharedPrefsUtils into your project, for example, as a Gradle compile dependency:

implementation 'com.github.guilhe:SharedPrefsUtils:${LATEST_VERSION}'

Maven Central Download

Sample usage

SharedPrefsUtils

public static boolean putObject(SharedPreferences prefs, String key, Object object) {}
public static <T> T getObject(SharedPreferences prefs, String key, TypeToken<T> type, T defaultValue) {}
public static <T> T getObject(SharedPreferences prefs, String key, Class<T> object, T defaultValue) {}

To save and load primitive types:

SharedPrefsUtils.putObject(prefs, "key", 1);
int a = SharedPrefsUtils.getObject(prefs, "key", int.class, 1);

To save and load object types:

List<T> list = new ArrayList<>();
SharedPrefsUtils.putObject(prefs, "key", list);
list = SharedPrefsUtils.getObject(prefs, "key", new TypeToken<List<T>>(){}, new ArrayList<T>()));

When not using primitive types you should use TypeToken instead of T.class, for example:

    @Test
    public void testOne() throws Exception {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        SharedPrefsUtils.putObject(prefs, "key", list);
        
        assertEquals(list, SharedPrefsUtils.getObject(prefs, "key", new TypeToken<List<Integer>>(){}, new ArrayList<Integer>()));
        assertNotEquals(list, SharedPrefsUtils.getObject(prefs, "key", List.class, new ArrayList<Integer>()));
    }
    
    @Test
    public void testTwo() throws Exception {
        List<MyObjectType> list = new ArrayList<>();
        list.add(new MyObjectType("string", 1, true));
        SharedPrefsUtils.putObject(prefs, "key", list);
        
        assertEquals(list, SharedPrefsUtils.getObject(prefs, "key", new TypeToken<List<MyObjectType>>() {}, new ArrayList<MyObjectType>()));
        assertNotEquals(list, SharedPrefsUtils.getObject(prefs, "key", List.class, new ArrayList<MyObjectType>()));
    }
    
    private static class MyObjectType implements Parcelable {
            private String mFieldString;
            private int mFieldInt;
            private boolean mFieldBoolean;
            ...
    }

Both tests will ran to completion.

Regarding assertNotEquals(list, SharedPrefsUtils.getObject(prefs, "key", List.class, new ArrayList<Integer>())); being true, I guess it's related with the fact that public <T> T fromJson(JsonReader reader, Type typeOfT){} method from Gson.java (line 886) is type unsafe: "Since Type is not parameterized by T, this method is type unsafe and should be used carefully". That's why I believe I'm getting List<Double> instead of List<Integer>.

Also:

SharedPrefsUtils.putObject(prefs, "key", 1);
SharedPrefsUtils.getObject(prefs, "key", boolean.class, false);

Will throw JsonParseException.

Binaries

Additional binaries and dependency information for can be found at https://search.maven.org.

Dependencies

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

LICENSE

Copyright (c) 2017-present, SharedPrefsUtils Contributors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
1.0.3
1.0.2
1.0.1
1.0.0