Giter Site home page Giter Site logo

martinmbae / powerpreference Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aliasadi/powerpreference

0.0 0.0 0.0 222 KB

๐Ÿ’พ A Powerful library to control and simplify the usage of shared preference in Android.

Home Page: https://aliasadi.github.io/PowerPreference/

License: Apache License 2.0

Java 100.00%

powerpreference's Introduction

Power Preference

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

Android Arsenal Download

Download

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

Getting started

To accsess preference file there is two option:

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

Save Data - Asynchronous(PUT)

To write data to preference file asynchronous.

  1. Insert single value
PowerPreference.getDefaultFile().put("key",value);
  1. Insert multiple values
PowerPreference.getDefaultFile()
        .put("key", value)
        .put("key", value)
              .
              .
        .put("key", value);

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

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

Note:

You can write any type of data Integer, Long, Float, Double, String, ArrayList, Map, Object

Save Data - Synchronous(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);

You can also call set with the type such as setBoolean() and setString().

Read Data

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

  1. String
String value = PowerPreference.getDefaultFile().getString("key", defaultValue);
  1. Object
Object value = PowerPreference.getDefaultFile().getObject("key", Object.class, defaultValue);
  1. Array
ArrayList<Object> value = PowerPreference.getDefaultFile().getObject("key", Object[].class, defaultValue);
  1. Map
HashMap<String, Object> value = PowerPreference.getDefaultFile().getMap("key", HashMap.class, String.class, Object.class);

Note:

You can getting a value with out pass a defaultValue the library well return a default from the list if the key not exist.

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

If the key dosn't exist the library will return an empty string.

Type Default
Integer, Long, Float, Double 0
String ""
Object null

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

Clear Data

  1. Clear data from specefic file
PowerPreference.getDefaultFile().clear();
  1. Clear data from all files
PowerPreference.getAllData()

Remove Data

PowerPreference.getDefaultFile().remove("key");

Get Data

  1. Get all data from specefic file
Map<String, ?> fileData = PowerPreference.getDefaultFile().getData();
  1. Get all data from all files in the app.
List<PreferenceObject> appData = PowerPreference.getAllData()

Default Values

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
<?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)
  1. 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);

Preference Debbuger

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

PowerPreference.showDebugScreen();

License

Copyright 2018 Ali Asadi.

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.

powerpreference's People

Contributors

aliasadi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.