Coder Social home page Coder Social logo

preferhythm's Introduction

./artwork/logo.png

Preferhythm

Download Build Status license

Automatically generate classes to use Android's SharedPreferences easily and safely.

Requirement

  • Android 4.0.3 (API 15) or later

Install

Add the following gradle dependency exchanging x.x.x for the latest release.

Java

dependencies {
    implementation 'com.kazakago.preferhythm:preferhythm:x.x.x'
    annotationProcessor 'com.kazakago.preferhythm:preferhythm-processor:x.x.x'
}

Kotlin

apply plugin: 'kotlin-kapt'

dependencies {
    implementation 'com.kazakago.preferhythm:preferhythm:x.x.x'
    kapt 'com.kazakago.preferhythm:preferhythm-processor:x.x.x'
}

Supported Type

  • Primitive Type
    • int
    • long
    • float
    • boolean
  • Object Type
    • Integer
    • Long
    • Float
    • Boolean
    • String
    • Set<String>

This is the same as Android's SharedPreferences specification. More details.

Usage

Create a preference model class and add @PrefClass annotation.
Next, define field variable and add @PrefField annotation.

@PrefClass // `@PrefClass` is nessesary for your preferences model class.
class MyPreferences {

    @PrefField // `@PrefField` is nessesary for your preference value.
    int intValue = 3; // default value is `3`.

    @PrefField
    boolean booleanValue; // default value is `false`. (primitive default value)

    @PrefField
    String stringValue; // default value is `null`.

    @NonNull
    @PrefField
    String nonNullStringValue = "foo"; // NonNull annotation with default value.

}

When building, [PREFERENCES_MODEL_NAME] + Manager class is auto generated based on the class with @PrefClass annotation.
Also, Put and get methods are auto created based on the original field name with @PrefField annotation.

public class MainActivity extends Activity {

    // `MyPreferencesManager` class is auto generated.
    private MyPreferencesManager myPreferencesManager = new MyPreferencesManager(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        System.out.print(myPreferencesManager.getIntValue()); // 3 (default value)

        myPreferencesManager.setIntValue(100); // set 100
        myPreferencesManager.commit(); // commit change.

        System.out.print(myPreferencesManager.getIntValue()); // 100
        
        myPreferencesManager.clear(); // clear value.
        myPreferencesManager.commit(); // commit change.

        System.out.print(myPreferencesManager.getIntValue()); // 3 (default value)
    }

}

Refer to the sample module (Java & Kotlin) for details.

Important

Preferences Name

By default, the SharedPreferences name is Class Name.
Therefore, if you change the class name, the saved value can not be retrieved.

If you want to set the SharedPreferences name to an arbitrary value, please add parameters to @PrefField.  

@PrefClass("YOUR_ORIGINAL_PREFERENCES_NAME")
class YourPreferencesClass {

...

}

Key Name

By default, the key name when saving to SharedPreferences is Field Name.
Therefore, if you change the field name, the saved value can not be retrieved.

If you want to set the key name to an arbitrary value, please add parameters to @PrefField.  

@PrefClass
class YourPreferencesClass {

    @PrefField("YOUR_ORIGINAL_KEY_NAME")
    int yourSaveValue;

}

Advanced

Override Put and Get Method.

you can override put and get method in [PREFERENCES_MODEL_NAME] + Manager class.

public class CustomMyPreferencesManager extends MyPreferencesManager {

    public CustomMyPreferencesManager(@NonNull Context context) {
        super(context);
    }
    
    @Nullable
    @Override
    public String getStringValue() {
        // ### Your custom step. (if needed) ###
        // ex) decryption code.
        // ###
        return super.getStringValue();
    }

    @NonNull
    @Override
    public MyPreferencesManager putStringValue(@Nullable String value) {
        // ### Your custom step. (if needed) ###
        // ex) encryption code.
        // ###
        return super.putStringValue(value);
    }

}

Kotlin Support

This library supports Kotlin's Nullable.

@PrefClass
class MyPreferences {

    @PrefField
    val intValue: Int = 3 // NonNull & default value is `3`.

    @PrefField
    val booleanValue: Boolean = false // NonNull & default value is `false`

    @PrefField
    val stringValue: String = "foo" // NonNull & default value is `foo`

    @PrefField
    val nullableStringValue: String? = null // Nullable & default value is `null`

    @PrefField
    val nullableStringWithInitValue: String? = "bar" // Nullable & default value is `bar`

}

License

MIT License

Copyright (c) 2017 KazaKago

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

preferhythm's People

Contributors

kazakago avatar

Watchers

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