Coder Social home page Coder Social logo

kishandonga / easyprefs Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 4.0 273 KB

EasyPrefs is a wrapper on the android shared preferences, also it will need only one-time initialization and used in the whole project without context, it supports encryption and decryption with extended APIs.

License: MIT License

Kotlin 91.08% Java 8.92%
android androidlibrary android-library preferences sharedpreferences easy-to-use preferences-api encryptedsharedpreferences sharedpreferences-editor encrypted-preferences

easyprefs's Introduction

Android EasyPrefs

    Android Arsenal

EasyPrefs is a library for the android SharedPreferences or we can say it wrapper on it. It provides easy access to the SharedPreferences API, also it will need only one-time initialization, and is used in the whole project without context also has a facility to provide context if necessary with support encryption and decryption.

It supports all Primitive Data Types including Set.

For, the secure operation use AES encryption <= 20 API Level, and for 21 >= use Android-provided encrypted shared preferences in both cases you get the same output your key and value are stored securely.

This library is developed in Kotlin and supports both the language Kotlin as well as Java with the same practices.

The default file name is prefs_<package_name> and custom file name like prefs_<given_filename> there is no need to give a file extension. it is automatically appended at the end of the name.

Installation

Gradle:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.kishandonga:EasyPrefs:1.2'
}

Examples

Refer, to the Sample Test Suite in the suite package For, more information refer to this test case code here

Initialize App

Prefs.initializeApp(context) this line of code adds into the main Application Class, as like this.

class MainApp : Application() {
    override fun onCreate() {
        super.onCreate()
        Prefs.initializeApp(this)
    }
}

Write Operation

Prefs.write().content("KEY", "VALUE")
	     .content("KEY", "VALUE")
	     .commit() or .apply()
Prefs.write(fileName)
       	     .content("KEY", "VALUE")
	     .content("KEY", "VALUE")
	     .commit() or .apply()
Prefs.write(context)
       	     .content("KEY", "VALUE")
       	     .content("KEY", "VALUE")
       	     .commit() or .apply()
Prefs.write(context, fileName)
       	     .content("KEY", "VALUE")
       	     .content("KEY", "VALUE")
       	     .commit() or .apply()

Read Operation

Prefs.read().content("KEY", "")
Prefs.read(fileName).content("KEY", "")
Prefs.read(context).content("KEY", "")
Prefs.read(context, fileName).content("KEY", "")

Secure Operation

Prefs.securely().write()
       	     .content("KEY", "VALUE")
       	     .content("KEY", "VALUE")
       	     .commit() or .apply()

Prefs.securely().read().content("KEY",  "")

Has Operation

Prefs.has().key("KEY");
Prefs.has(fileName).key("KEY");
Prefs.has(context).key("KEY");
Prefs.has(context, fileName).key("KEY");

Prefs.has().empty();
  • Give boolean value if key exists then true else false.

Remove Operation

Prefs.remove().key("KEY").
Prefs.remove(fileName).key("KEY").
Prefs.remove(context).key("KEY").
Prefs.remove(context, fileName).key("KEY").

commit() or .apply()
  • After the remove operation commit and apply call mandatory.

Clear Operation

Prefs.clear().all()
Prefs.clear(fileName).all()
Prefs.clear(context).all()
Prefs.clear(context, fileName).all()

.commit() or .apply()

Note: for, securely operation use the same Prefs.clear() operation.
  • After the clear operation commit and apply call mandatory.

For, all the read, write, clear, has, remove support context and fileName manually, so you can use your preferences with multiple files with the same API use just need to change the fileName when you call.

If you pass context manually then no need to initialize lib on the application class, For, more information refer here

This library tested on the API levels 20, 26, 29, and 30 if you found any bug or issue raise an issue or submit a PR

Future Scope

  • add sorting on the Set so get direct sorted data.
  • callback extend as we already have in the preferences.
  • provide suggestions to make this lib more helpful.

Contributing

Contributions are welcome! If you find a bug please report it and if you want to add a new feature then please suggest it to me. If you want to contribute code please file an issue or create a branch of the current dev branch and post a pull request.

About me

I'm Kishan Donga and you can connect with me via the below links, I am a developer and I love to create innovations.

LinkedIn @ikd96 Email [email protected] Twitter @ikishan96 Instagram @ikishan96

License

EasyPrefs is released under the MIT License.

easyprefs's People

Contributors

kishandonga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

easyprefs's Issues

Remove

Hi , thank you for your Lib
I can write and read securely() form but I couldn't remove any Key!

Prefs.remove(this)
                        .key("$i")
                        .commit()

Read value from file

This code incorrect when get file from preference

    override fun readOn(
        context: Context,
        fileName: String,
        encType: Encryption
    ): Read {
        return ReadImpl(
            PrefProvider.getPref(context, **getFileName(context)**, encType),
            encType
        )
    }

Could not find com.github.kishandonga:EasyPrefs:1.1.

Sorry I'm new to Android development.

In the build.gradle(Project) I have added this:

repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }

In the build.gradle(App) I added this:

dependencies {
  ...
    implementation 'com.github.kishandonga:EasyPrefs:1.1'
}

When I rebuild the project I get this:

Could not find com.github.kishandonga:EasyPrefs:1.1.
Required by:
project :app

Isn't this like CocoaPods in iOS where the libs get pulled automatically? Do I still have to copy and paste the lib by hand into my project? Is there a better way? Thanks

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.