Coder Social home page Coder Social logo

prettysharedpreferences's Introduction

Version 1.0.2

What's new

  • Thread safe
  • Reduce most of boilplace code.
  • Backward Compatibility:
    • add commit() method for api level < 9.
    • make apply() method safe to call by wrap by try catch and call commit() as the fallback when use in api level < 9.
  • Add clear() for quick remove all set value.
  • Add discard() for drop all changes which was not apply() or commit() yet.

PrettySharedPreferences

Android Arsenal Build Status

PrettySharedPreferences is a lightweight library for help you deal with SharedPreferences more easy and reduce most of boilplace code.

Features

  • Easy to use
  • Reduce most of boilplace code.

Download

JAR

Download the latest JAR

or

Gradle

dependencies {
    compile 'com.github.talenguyen:prettysharedpreferences:1.0.2'
}

or

Maven

<dependency>
  <groupId>com.github.talenguyen</groupId>
  <artifactId>prettysharedpreferences</artifactId>
  <version>1.0.2</version>
</dependency>

Usage

Create a class and extends from PrettySharedPreferences class. Let's say PrefManager.

public class PrefManager extends PrettySharedPreferences<PrefManager> {

    public PrefManager(SharedPreferences sharedPreferences) {
        super(sharedPreferences);
    }

    public StringEditor<PrefManager> username() {
        return getStringEditor("username");
    }
    
    public LongEditor<PrefManager> userId() {
        return getLongEditor("userId");
    }
    
    /**
    * Dynamic key
    */
    public IntegerEditor<PrefManager> int(String key) {
        return getIntegerEditor(key);
    }

    ...
}

Create an instance

    PrefManager prefManager = new PrefManager(getSharedPreferences("MyPref", MODE_PRIVATE));

To put a single value

    prefManager.username().put("tale").apply();

To put multiple values

    prefManager
        .username().put("tale")
        .userId().put(1)
        .int("key1").put(1)
        .int("key2").put(2)
        .apply();

To get value

    String username = prefManager.username().getOr("<default value>");
    long userId = prefManager.userId().getOr(0l);
    int key1 = prefManager.int("key1").getOr(0);

That's it. Enjoy!

License

Copyright 2014 Giang Nguyen

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.

prettysharedpreferences's People

Contributors

talenguyen avatar

Watchers

James Cloos avatar  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.