All Projects → gurleensethi → LoggerPreferences

gurleensethi / LoggerPreferences

Licence: MIT License
Get to know which class changed the value in SharedPreferences.

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to LoggerPreferences

o-fish-android
Android app for the Officer's Fishery Information Sharing Hub (O-FISH). The mobile app allows fisheries officers to document and share critical information gathered during a routine vessel inspection.
Stars: ✭ 19 (-5%)
Mutual labels:  android-development
Android-Shortify
An Android library used for making an Android application more faster with less amount of code. Shortify for Android provides basic functionalities of view and resource binding, view customization, JSON parsing, AJAX, various readymade dialogs and much more.
Stars: ✭ 21 (+5%)
Mutual labels:  android-development
ToDo
Android application to quickly add tasks and reminders.
Stars: ✭ 13 (-35%)
Mutual labels:  android-development
ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (+95%)
Mutual labels:  android-development
android-custom-view
No description or website provided.
Stars: ✭ 15 (-25%)
Mutual labels:  android-development
CurvedBottomNavigationView
we will learn How to draw custom shapes in Curved Bottom Navigation View
Stars: ✭ 42 (+110%)
Mutual labels:  android-development
jshodan
Powerful Shodan API client using RxJava and Retrofit
Stars: ✭ 56 (+180%)
Mutual labels:  android-development
feature-flag-android
A Gradle plugin to achieve feature flag based development for Android applications.
Stars: ✭ 82 (+310%)
Mutual labels:  android-development
Login-SignupUI-FirebasePhoneauth
New Repo
Stars: ✭ 43 (+115%)
Mutual labels:  android-development
android-trinity
android-trinity is tiny proactive framework with much of the scaffolding code required to start a new Android Application.
Stars: ✭ 44 (+120%)
Mutual labels:  android-development
Weather-Guru-MVP
Sample Material-design Android weather application build with MVP architectural approach using Dagger2, RxJava2, Retrofit2, Event-Bus, GreenDao, Butterknife, Lottie etc.
Stars: ✭ 15 (-25%)
Mutual labels:  android-development
ULogViewer
Cross-Platform Universal Log Viewer.
Stars: ✭ 64 (+220%)
Mutual labels:  android-development
SuperToolbar
Android native Toolbar on steroids 💪
Stars: ✭ 52 (+160%)
Mutual labels:  android-development
mktool
Unpack and repack the android boot.img and recovery.img easily.
Stars: ✭ 52 (+160%)
Mutual labels:  android-development
youtube-android
Source code as seen on my YouTube videos!
Stars: ✭ 35 (+75%)
Mutual labels:  android-development
HouseAds2
A library ( V2 ) for cross promoting own apps within own apps - for Android
Stars: ✭ 23 (+15%)
Mutual labels:  android-development
Xama.JTPorts.ShowcaseView
Xamarin.Android Native showcase view.
Stars: ✭ 17 (-15%)
Mutual labels:  android-development
Android-SQLite-Tutorial
This is a simple project of Android SQLite Relational Database. You can check my Bengali Blog Post on this topic
Stars: ✭ 34 (+70%)
Mutual labels:  android-development
android-boilerplate
Android Boilerplate à la ustwo
Stars: ✭ 43 (+115%)
Mutual labels:  android-development
svg2vector
Online batch converter of SVG images to Android vector drawable XML resource files
Stars: ✭ 39 (+95%)
Mutual labels:  android-development

LoggerPreferences

Get to know which class changed the value in SharedPreferences.

Installation

In the root level build.gradle file add:

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

Now add the dependency:

dependencies {
    compile 'com.github.gurleensethi:LoggerPreferences:v1.0.0'
}

Usage

Initialize

LoggerPreferences will log any action that is taken with SharedPreferences, be it changing values or retrieving them. To enable logging, you have to initalize LoggerPreferences.

LoggerPreferences.init(true, true);

The first parameter tells LoggerPreferences to log everytime when a value is changed in SharedPreferences and the second parameter tells to log everytime a value is retrieved.

Getting a Reference

There are two ways you can obtain a reference. First is by providing a Context, filename (Stirng) and mode (int).

//LoggerPreferences.get(Context, fileName, mode);
LoggerPreferences preferences = LoggerPreferences.get(this, "sp_file", Context.MODE_PRIVATE)
        .with(this);

Second is by providing an object of SharedPreferences directly.

//LoggerPreferences.get(SharedPreferences);
LoggerPreferences preferences = LoggerPreferences.get(getSharedPreferences("sp_file", Context.MODE_PRIVATE))
        .with(this);

You also have to call the function with(Object) and pass in the object which is responsible for interacting(changing/retrieving) with SharedPreferences. with takes in a parameter of type Object. If you don't call with() then by default LoggerPreferences will logged as the file name, so make sure to call with().

API

LoggerPreferences is a wrapper around SharedPreferences. It has all the functions provided by SharedPreferences and works in exactly the same manner.

So if in the MainActivity you change a String value.

preferences.edit()
        .putString("key_string", "new value")
        .apply();

In logcat you will get:

D/LoggerPreferences: [String] MainActivity changed the value of Key[key_string] from [old value] to [new value].

If you are retrieving a String value:

String value = preferences.getString("key_string", null);

In logcat you will get:

D/LoggerPreferences: [String] MainActivity is retrieving the value of Key[key_string] which is set to [new value].

LoggerPreferences has all the functions provided by SharedPreferences and SharedPreferences.Editor.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LoggerPreferences.init(true, true);

        LoggerPreferences preferences = LoggerPreferences.get(this, "sp_file", Context.MODE_PRIVATE)
                .with(this);

        HashSet<String> set = new HashSet<>();
        set.add("New Value");
        set.add("Oh!");
        set.add("I am in a set");

        preferences.edit()
                .putInt("key_int", 123)
                .putString("key_string", "new value")
                .putBoolean("key_boolean", false)
                .putFloat("key_float", 10.0f)
                .putLong("key_long", 112320L)
                .putStringSet("key_set", set)
                .apply();

        preferences.getString("key_string", null);
        preferences.getInt("key_int", 0);
        preferences.getBoolean("key_boolean", false);
        preferences.getLong("key_long", 1000L);
        preferences.getFloat("key_float", 10f);
        preferences.getStringSet("key_set", null);
        preferences.getAll();
        preferences.registerOnSharedPreferenceChangeListener(null);
        preferences.unregisterOnSharedPreferenceChangeListener(null);
    }
}

Logcat:

D/LoggerPreferences: [Int] MainActivity changed the value of Key[key_int] from [123] to [123].
D/LoggerPreferences: [String] MainActivity changed the value of Key[key_string] from [new value] to [new value].
D/LoggerPreferences: [Boolean] MainActivity changed the value of Key[key_boolean] from [true] to [false].
D/LoggerPreferences: [Float] MainActivity changed the value of Key[key_float] from [1.0] to [10.0].
D/LoggerPreferences: [Long] MainActivity changed the value of Key[key_long] from [10000] to [112320].
D/LoggerPreferences: [Set<String>] MainActivity changed the value of Key[key_set] from [[Test, Testing!]] to [I am in a set,Oh!,New Value].

D/LoggerPreferences: [String] MainActivity is retrieving the value of Key[key_string] which is set to [new value].
D/LoggerPreferences: [Int] MainActivity is retrieving the value of Key[key_int] which is set to [123].
D/LoggerPreferences: [Boolean] MainActivity is retrieving the value of Key[key_boolean] which is set to [false].
D/LoggerPreferences: [Long] MainActivity is retrieving the value of Key[key_long] which is set to [112320].
D/LoggerPreferences: [Float] MainActivity is retrieving the value of Key[key_float] which is set to [10.0].
D/LoggerPreferences: [Set<String>] MainActivity is retrieving the value of Key[key_set] which is set to [I am in a set,Oh!,New Value].
D/LoggerPreferences: MainActivity is retrieving all values.
D/LoggerPreferences: MainActivity has registered a OnSharedPreferenceChangeListener.
D/LoggerPreferences: MainActivity has unregistered a OnSharedPreferenceChangeListener.

Tips

LoggerPreferences uses the same tag when logging: LoggerPreferences. In your logcat search LoggerPreferences to get all the logs.

Never log in production!

Support

If you have any idea or need a change in the library or found a bug, please open an issue.

License

Copyright 2018 Gurleen Sethi

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].