Android-Power-Preference

Additional

Language
Java
Version
1.4.0 (Feb 4, 2019)
Created
Dec 6, 2018
Updated
Feb 6, 2019
Owner
Ali Esa Assadi (AliEsaAssadi)
Contributor
Ali Esa Assadi (AliEsaAssadi)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

Android Power Preference

A Powerful library to control and simplify the usage of shared preference in Android.

Download

implementation 'com.aliassadi:power-preference-lib:1.4.0'

Getting started

To accsess preference file there is two option:

  1. Default preference file.
Preference preference = PowerPreference.getDefaultFile();
  1. Specefic preference file by name.
Preference preference = PowerPreference.getFileByName("preferenceName");

Put

To write data to preference file asynchronous.

Put will asynchronously save the preferences without holding the current thread.

Support Integer, Long, Float, Double, String, ArrayList, Map, Object
PowerPreference.getDefaultFile().put("key",value);

Inserting multiple values

PowerPreference.getDefaultFile()
        .put("key", value)
        .put("key", value)
              .
              .
        .put("key", value);

The library support the default implementation of shared preference such as putBoolean() and putString().

Set

To write data to preference file synchronous.

Set will synchronously save the preference while holding the current thread until done and returning a success flag.

boolean result = PowerPreference.getDefaultFile().set("key",value);

Get

To retrieve values from a preference file, call methods such as getInt() and getString(),

String value = PowerPreference.getDefaultFile().getString("key", defaultValue);

Retrive Object:

Object value = PowerPreference.getDefaultFile().getObject("key", Object.class, defaultValue);

Retrive Array Of Objects:

ArrayList<Object> value = PowerPreference.getDefaultFile().getObject("key", Object[].class, defaultValue);

Retrive Map:

HashMap<String, Object> value = PowerPreference.getDefaultFile().getMap("key", HashMap.class, String.class, Object.class);

You can getting a value with out pass a defaultValue the library well return a default from the list see the example above

Library default values

Type Default
Integer, Long, Float, Double 0
String ""
Object null
String value = PowerPreference.getDefaultFile().getString("key");
//If the key dosn't exist the library will return a default value from list in this case an empty string.

You also can choose a default value for each key in you preference file by seDefaults() method see the defaults section for more.

Defaults

Set Default value to be used when reading from shared preference, You can use a different defaults value for every preference file.

There is two option using:

  1. XML
  2. HashMap

Examples:

  • XML:
<?xml version="1.0" encoding="utf-8"?>
<defaultMap>
    <entry>
        <key>key</key>
        <value>true</value>
    </entry>

    <entry>
        <key>key</key>
        <value>Hello World!</value>
    </entry>
    
    <entry>
        <key>key</key>
        <value>5</value>
    </entry>
    
    //...etc
    
</defaultMap>
PowerPreference.getDefaultFile().setDefaults(R.xml.preferences_defaults)
  • HashMap:
Map<String, Object> defaults = new HashMap<>();
defaults.put("key", true);
defaults.put("key", "Hello World!");
defaults.put("key", 123);
defaults.put("key", 111L);
defaults.put("key", 1.1f);
defaults.put("key", 1.111d);
defaults.put("key", new Object());
PowerPreference.getDefaultFile().setDefaults(hashMap);

Other

PowerPreference.getDefaultFile().clear();
PowerPreference.getDefaultFile().remove("key");
PowerPreference.getDefaultFile().contains("key");
PowerPreference.getDefaultFile().getData("key");

For all preference in the app.

PowerPreference.getAllData()
PowerPreference.clearAllData()

Preference Debbuger

By preference debugger you can show all the preference in all the file in your app simply by calling.

PowerPreference.showDebugScreen(true) //true - describe if the value is editable

License

Copyright 2018 Ali Esa Assadi.

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.