diff --git a/app/build.gradle b/app/build.gradle index 139d01c..556bd68 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,6 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' android { compileSdkVersion 29 @@ -27,7 +29,8 @@ dependencies { androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation project(':fastsave') implementation 'com.jakewharton:butterknife:8.8.1' - annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' + kapt 'com.jakewharton:butterknife-compiler:8.8.1' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" //DataBaseDebugger debugImplementation 'com.amitshekhar.android:debug-db:1.0.3' } diff --git a/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.java deleted file mode 100644 index fd502b2..0000000 --- a/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.appizona.yehiahd.fastsaveexample; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumented test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.appizona.yehia.fastsaveexample", appContext.getPackageName()); - } -} diff --git a/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..5b9efbd --- /dev/null +++ b/app/src/androidTest/java/com/appizona/yehiahd/fastsaveexample/ExampleInstrumentedTest.kt @@ -0,0 +1,22 @@ +package com.appizona.yehiahd.fastsaveexample + +import android.support.test.InstrumentationRegistry +import android.support.test.runner.AndroidJUnit4 +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.Assert.assertEquals + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + assertEquals("com.appizona.yehia.fastsaveexample", appContext.packageName) + } +} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.java b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.java deleted file mode 100644 index 2b092b9..0000000 --- a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.java +++ /dev/null @@ -1,163 +0,0 @@ -package com.appizona.yehiahd.fastsaveexample; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.TextView; - -import com.appizona.yehiahd.fastsave.FastSave; - -import java.util.ArrayList; -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; - -public class MainActivity extends AppCompatActivity implements View.OnClickListener { - - - @BindView(R.id.edit_text) - EditText editText; - @BindView(R.id.save_int) - Button saveInt; - @BindView(R.id.save_float) - Button saveFloat; - @BindView(R.id.save_long) - Button saveLong; - @BindView(R.id.save_string) - Button saveString; - @BindView(R.id.save_boolean) - Button saveBoolean; - @BindView(R.id.get_int) - Button getInt; - @BindView(R.id.get_float) - Button getFloat; - @BindView(R.id.get_long) - Button getLong; - @BindView(R.id.get_string) - Button getString; - @BindView(R.id.get_boolean) - Button getBoolean; - @BindView(R.id.save_object) - Button saveObject; - @BindView(R.id.get_object) - Button getObject; - @BindView(R.id.save_objects_list) - Button saveObjectsList; - @BindView(R.id.get_objects_list) - Button getObjectsList; - @BindView(R.id.value_TV) - TextView valueTV; - @BindView(R.id.clear_all) - Button clearAll; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - ButterKnife.bind(this); - - saveInt.setOnClickListener(this); - saveBoolean.setOnClickListener(this); - saveLong.setOnClickListener(this); - saveFloat.setOnClickListener(this); - saveString.setOnClickListener(this); - saveObject.setOnClickListener(this); - saveObjectsList.setOnClickListener(this); - clearAll.setOnClickListener(this); - - getObject.setOnClickListener(this); - getObjectsList.setOnClickListener(this); - - } - - @Override - public void onClick(View view) { - switch (view.getId()) { - case R.id.save_int: - FastSave.getInstance().saveInt("int_key", 22); - break; - - case R.id.save_boolean: - FastSave.getInstance().saveBoolean("boolean_key", true); - break; - - case R.id.save_long: - FastSave.getInstance().saveLong("long_key", 2222); - - break; - - case R.id.save_float: - FastSave.getInstance().saveFloat("float_key", 22.0f); - - break; - - case R.id.save_string: - FastSave.getInstance().saveString("string_key", "My Age is 22"); - - break; - - case R.id.save_object: - Person person = new Person() - .setId(22) - .setName("Yehia") - .setEmployee(true); - FastSave.getInstance().saveObject("object_key", person); - break; - - case R.id.save_objects_list: - List list = getListOfObjects(); - FastSave.getInstance().saveObjectsList("objects_list_key", list); - - break; - - case R.id.clear_all: - FastSave.getInstance().clearSession(); - break; - - case R.id.get_object: - Person p = FastSave.getInstance().getObject("object_key", Person.class); - valueTV.setText(p.getName()); - break; - - case R.id.get_objects_list: - List pl = FastSave.getInstance().getObjectsList("objects_list_key", Person.class); - valueTV.setText(pl.get(1).getName()); - break; - - - } - } - - private List getListOfObjects() { - List list = new ArrayList<>(); - Person person1 = new Person() - .setId(22) - .setName("Yehia") - .setEmployee(true); - - Person person2 = new Person() - .setId(44) - .setName("Mohamed") - .setEmployee(false); - - Person person3 = new Person() - .setId(30) - .setName("Android") - .setEmployee(true); - - Person person4 = new Person() - .setId(33) - .setName("Google") - .setEmployee(true); - - list.add(person1); - list.add(person2); - list.add(person3); - list.add(person4); - - return list; - } -} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.kt b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.kt new file mode 100644 index 0000000..e094863 --- /dev/null +++ b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MainActivity.kt @@ -0,0 +1,152 @@ +package com.appizona.yehiahd.fastsaveexample + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.view.View +import android.widget.Button +import android.widget.EditText +import android.widget.TextView +import butterknife.BindView +import butterknife.ButterKnife +import com.appizona.yehiahd.fastsave.FastSave + +class MainActivity : AppCompatActivity(), View.OnClickListener { + + @BindView(R.id.edit_text) + lateinit var editText: EditText + + @BindView(R.id.save_int) + lateinit var saveInt: Button + + @BindView(R.id.save_float) + lateinit var saveFloat: Button + + @BindView(R.id.save_long) + lateinit var saveLong: Button + + @BindView(R.id.save_string) + lateinit var saveString: Button + + @BindView(R.id.save_boolean) + lateinit var saveBoolean: Button + + @BindView(R.id.get_int) + lateinit var getInt: Button + + @BindView(R.id.get_float) + lateinit var getFloat: Button + + @BindView(R.id.get_long) + lateinit var getLong: Button + + @BindView(R.id.get_string) + lateinit var getString: Button + + @BindView(R.id.get_boolean) + lateinit var getBoolean: Button + + @BindView(R.id.save_object) + lateinit var saveObject: Button + + @BindView(R.id.get_object) + lateinit var getObject: Button + + @BindView(R.id.save_objects_list) + lateinit var saveObjectsList: Button + + @BindView(R.id.get_objects_list) + lateinit var getObjectsList: Button + + @BindView(R.id.value_TV) + lateinit var valueTV: TextView + + @BindView(R.id.clear_all) + lateinit var clearAll: Button + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + ButterKnife.bind(this) + + saveInt.setOnClickListener(this) + saveBoolean.setOnClickListener(this) + saveLong.setOnClickListener(this) + saveFloat.setOnClickListener(this) + saveString.setOnClickListener(this) + saveObject.setOnClickListener(this) + saveObjectsList.setOnClickListener(this) + clearAll.setOnClickListener(this) + + getObject.setOnClickListener(this) + getObjectsList.setOnClickListener(this) + } + + override fun onClick(view: View) { + when (view.id) { + R.id.save_int -> FastSave.getInstance().saveInt("int_key", 22) + + R.id.save_boolean -> FastSave.getInstance().saveBoolean("boolean_key", true) + + R.id.save_long -> FastSave.getInstance().saveLong("long_key", 2222) + + R.id.save_float -> FastSave.getInstance().saveFloat("float_key", 22.0f) + + R.id.save_string -> FastSave.getInstance().saveString("string_key", "My Age is 22") + + R.id.save_object -> { + val person = Person() + .setId(22) + .setName("Yehia") + .setEmployee(true) + FastSave.getInstance().saveObject("object_key", person) + } + + R.id.save_objects_list -> { + val list = getListOfObjects() + FastSave.getInstance().saveObjectsList("objects_list_key", list) + } + + R.id.clear_all -> FastSave.getInstance().clearSession() + + R.id.get_object -> { + val p = FastSave.getInstance().getObject("object_key", Person::class.java) + valueTV.text = p?.name + } + + R.id.get_objects_list -> { + val pl = FastSave.getInstance().getObjectsList("objects_list_key", Person::class.java) + valueTV.text = pl?.get(1)?.name + } + } + } + + private fun getListOfObjects(): List { + val list = ArrayList() + val person1 = Person() + .setId(22) + .setName("Yehia") + .setEmployee(true) + + val person2 = Person() + .setId(44) + .setName("Mohamed") + .setEmployee(false) + + val person3 = Person() + .setId(30) + .setName("Android") + .setEmployee(true) + + val person4 = Person() + .setId(33) + .setName("Google") + .setEmployee(true) + + list.add(person1) + list.add(person2) + list.add(person3) + list.add(person4) + + return list + } +} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.java b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.java deleted file mode 100644 index 1f02ec6..0000000 --- a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.appizona.yehiahd.fastsaveexample; - -import android.app.Application; - -import com.appizona.yehiahd.fastsave.FastSave; - -public class MyApplication extends Application { - @Override - public void onCreate() { - super.onCreate(); - FastSave.init(getApplicationContext()); - } -} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.kt b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.kt new file mode 100644 index 0000000..9394d5b --- /dev/null +++ b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/MyApplication.kt @@ -0,0 +1,11 @@ +package com.appizona.yehiahd.fastsaveexample + +import android.app.Application +import com.appizona.yehiahd.fastsave.FastSave + +class MyApplication : Application() { + override fun onCreate() { + super.onCreate() + FastSave.init(applicationContext) + } +} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.java b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.java deleted file mode 100644 index 2dfd139..0000000 --- a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.appizona.yehiahd.fastsaveexample; - -public class Person { - - private int id; - private String name; - private boolean isEmployee; - - public int getId() { - return id; - } - - public Person setId(int id) { - this.id = id; - return this; - } - - public String getName() { - return name; - } - - public Person setName(String name) { - this.name = name; - return this; - } - - public boolean isEmployee() { - return isEmployee; - } - - public Person setEmployee(boolean employee) { - isEmployee = employee; - return this; - } - - @Override - public String toString() { - return "id = " + id + " name = " + name + " isEmployee = " + isEmployee; - } -} diff --git a/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.kt b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.kt new file mode 100644 index 0000000..277d890 --- /dev/null +++ b/app/src/main/java/com/appizona/yehiahd/fastsaveexample/Person.kt @@ -0,0 +1,29 @@ +package com.appizona.yehiahd.fastsaveexample + +class Person { + var id: Int = 0 + private set + var name: String? = null + private set + var isEmployee: Boolean = false + private set + + fun setId(id: Int): Person { + this.id = id + return this + } + + fun setName(name: String): Person { + this.name = name + return this + } + + fun setEmployee(employee: Boolean): Person { + this.isEmployee = employee + return this + } + + override fun toString(): String { + return "id = $id name = $name isEmployee = $isEmployee" + } +} diff --git a/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.java b/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.java deleted file mode 100644 index f533200..0000000 --- a/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.appizona.yehiahd.fastsaveexample; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.kt b/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.kt new file mode 100644 index 0000000..bcde28a --- /dev/null +++ b/app/src/test/java/com/appizona/yehiahd/fastsaveexample/ExampleUnitTest.kt @@ -0,0 +1,16 @@ +package com.appizona.yehiahd.fastsaveexample + +import org.junit.Test +import org.junit.Assert.assertEquals + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} diff --git a/build.gradle b/build.gradle index 4e8009d..0a980fc 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,15 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + ext.kotlin_version = '1.9.22' + repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.2.0' - + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/fastsave/build.gradle b/fastsave/build.gradle index cfb52bb..e42d7e6 100644 --- a/fastsave/build.gradle +++ b/fastsave/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' android { compileSdkVersion 29 @@ -23,4 +24,5 @@ android { dependencies { //Gson implementation 'com.google.code.gson:gson:2.8.9' + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } diff --git a/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.java b/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.java deleted file mode 100644 index 3c3625d..0000000 --- a/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.appizona.yehiahd.fastsave; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumented test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.appizona.yehia.fastsave.test", appContext.getPackageName()); - } -} diff --git a/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.kt b/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..26c2e91 --- /dev/null +++ b/fastsave/src/androidTest/java/com/appizona/yehiahd/fastsave/ExampleInstrumentedTest.kt @@ -0,0 +1,22 @@ +package com.appizona.yehiahd.fastsave + +import android.support.test.InstrumentationRegistry +import android.support.test.runner.AndroidJUnit4 +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.Assert.assertEquals + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + assertEquals("com.appizona.yehia.fastsave.test", appContext.packageName) + } +} diff --git a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.java b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.java deleted file mode 100644 index dfcc41d..0000000 --- a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.appizona.yehiahd.fastsave; - -public class FastException extends RuntimeException { - - public FastException(String message) { - super(message); - } -} diff --git a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.kt b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.kt new file mode 100644 index 0000000..b761e5c --- /dev/null +++ b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastException.kt @@ -0,0 +1,3 @@ +package com.appizona.yehiahd.fastsave + +class FastException(message: String) : RuntimeException(message) diff --git a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.java b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.java deleted file mode 100644 index 5089d74..0000000 --- a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.java +++ /dev/null @@ -1,211 +0,0 @@ -package com.appizona.yehiahd.fastsave; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.util.Log; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class FastSave { - - private static FastSave instance; - private static SharedPreferences mSharedPreferences; - - private FastSave() { - } - - public static void init(Context context) { - mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - } - - public static void init(SharedPreferences sharedPreferences) { - mSharedPreferences = sharedPreferences; - } - - public static FastSave getInstance() { - if (instance == null) { - validateInitialization(); - synchronized (FastSave.class) { - if (instance == null) { - instance = new FastSave(); - } - } - } - return instance; - } - - public void saveInt(String key, int value) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putInt(key, value); - editor.apply(); - } - - public int getInt(String key, int defaultValue) { - if (isKeyExists(key)) { - return mSharedPreferences.getInt(key, defaultValue); - } - return defaultValue; - } - - public void saveBoolean(String key, boolean value) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putBoolean(key, value); - editor.apply(); - } - - public boolean getBoolean(String key, boolean defaultValue) { - if (isKeyExists(key)) { - return mSharedPreferences.getBoolean(key, defaultValue); - } - return defaultValue; - } - - - public void saveFloat(String key, float value) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putFloat(key, value); - editor.apply(); - } - - public float getFloat(String key, float defaultValue) { - if (isKeyExists(key)) { - return mSharedPreferences.getFloat(key, defaultValue); - } - return defaultValue; - } - - - public void saveLong(String key, long value) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putLong(key, value); - editor.apply(); - } - - public long getLong(String key, long defaultValue) { - if (isKeyExists(key)) { - return mSharedPreferences.getLong(key, defaultValue); - } - return defaultValue; - } - - - public void saveString(String key, String value) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putString(key, value); - editor.apply(); - } - - public String getString(String key, String defaultValue) { - if (isKeyExists(key)) { - return mSharedPreferences.getString(key, defaultValue); - } - return defaultValue; - } - - public void saveObject(String key, T object) { - String objectString = new Gson().toJson(object); - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putString(key, objectString); - editor.apply(); - } - - public T getObject(String key, Class classType) { - if (isKeyExists(key)) { - String objectString = mSharedPreferences.getString(key, null); - if (objectString != null) { - return new Gson().fromJson(objectString, classType); - } - } - return null; - } - - - public void saveObjectsList(String key, List objectList) { - String objectString = new Gson().toJson(objectList); - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.putString(key, objectString); - editor.apply(); - } - - public List getObjectsList(String key, Class classType) { - if (isKeyExists(key)) { - String objectString = mSharedPreferences.getString(key, null); - if (objectString != null) { - - ArrayList t = new Gson().fromJson(objectString, new TypeToken>() { - }.getType()); - - List finalList = new ArrayList<>(); - - for (int i = 0; i < t.size(); i++) { - String s = String.valueOf(t.get(i)); - finalList.add(new Gson().fromJson(s, classType)); - } - - return finalList; - } - } - - return null; - } - - public void clearSession() { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.clear(); - editor.apply(); - } - - public boolean deleteValue(String key) { - if (isKeyExists(key)) { - SharedPreferences.Editor editor = mSharedPreferences.edit(); - editor.remove(key); - editor.apply(); - return true; - } - - return false; - } - - - private static void validateInitialization() { - if (mSharedPreferences == null) - throw new FastException("FastSave Library must be initialized inside your application class by calling FastSave.init(getApplicationContext)"); - } - - public boolean isKeyExists(String key) { - Map map = mSharedPreferences.getAll(); - if (map.containsKey(key)) { - return true; - } else { - Log.e("FastSave", "No element founded in sharedPrefs with the key " + key); - return false; - } - } - - public boolean removeAllExcept(String... keys) { - Boolean isMatch = false; - SharedPreferences.Editor editor = mSharedPreferences.edit(); - for (String key : mSharedPreferences.getAll().keySet()) { - for (String k : keys) { - if (key.equals(k)) { - isMatch = true; - } - } - if (!isMatch) { - editor.remove(key); - } else { - isMatch = false; - } - } - editor.apply(); - - return true; - } - -} diff --git a/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.kt b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.kt new file mode 100644 index 0000000..0f9545c --- /dev/null +++ b/fastsave/src/main/java/com/appizona/yehiahd/fastsave/FastSave.kt @@ -0,0 +1,173 @@ +package com.appizona.yehiahd.fastsave + +import android.content.Context +import android.content.SharedPreferences +import android.preference.PreferenceManager +import android.util.Log +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken + +class FastSave private constructor() { + + companion object { + @Volatile + private var instance: FastSave? = null + private var mSharedPreferences: SharedPreferences? = null + + @JvmStatic + fun init(context: Context) { + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + } + + @JvmStatic + fun init(sharedPreferences: SharedPreferences) { + mSharedPreferences = sharedPreferences + } + + @JvmStatic + fun getInstance(): FastSave { + if (instance == null) { + validateInitialization() + synchronized(FastSave::class.java) { + if (instance == null) { + instance = FastSave() + } + } + } + return instance!! + } + + private fun validateInitialization() { + if (mSharedPreferences == null) { + throw FastException("FastSave Library must be initialized inside your application class by calling FastSave.init(getApplicationContext)") + } + } + } + + fun saveInt(key: String, value: Int) { + mSharedPreferences!!.edit().putInt(key, value).apply() + } + + fun getInt(key: String, defaultValue: Int): Int { + return if (isKeyExists(key)) { + mSharedPreferences!!.getInt(key, defaultValue) + } else defaultValue + } + + fun saveBoolean(key: String, value: Boolean) { + mSharedPreferences!!.edit().putBoolean(key, value).apply() + } + + fun getBoolean(key: String, defaultValue: Boolean): Boolean { + return if (isKeyExists(key)) { + mSharedPreferences!!.getBoolean(key, defaultValue) + } else defaultValue + } + + fun saveFloat(key: String, value: Float) { + mSharedPreferences!!.edit().putFloat(key, value).apply() + } + + fun getFloat(key: String, defaultValue: Float): Float { + return if (isKeyExists(key)) { + mSharedPreferences!!.getFloat(key, defaultValue) + } else defaultValue + } + + fun saveLong(key: String, value: Long) { + mSharedPreferences!!.edit().putLong(key, value).apply() + } + + fun getLong(key: String, defaultValue: Long): Long { + return if (isKeyExists(key)) { + mSharedPreferences!!.getLong(key, defaultValue) + } else defaultValue + } + + fun saveString(key: String, value: String) { + mSharedPreferences!!.edit().putString(key, value).apply() + } + + fun getString(key: String, defaultValue: String): String { + return if (isKeyExists(key)) { + mSharedPreferences!!.getString(key, defaultValue) ?: defaultValue + } else defaultValue + } + + fun saveObject(key: String, obj: T) { + val objectString = Gson().toJson(obj) + mSharedPreferences!!.edit().putString(key, objectString).apply() + } + + fun getObject(key: String, classType: Class): T? { + if (isKeyExists(key)) { + val objectString = mSharedPreferences!!.getString(key, null) + if (objectString != null) { + return Gson().fromJson(objectString, classType) + } + } + return null + } + + fun saveObjectsList(key: String, objectList: List) { + val objectString = Gson().toJson(objectList) + mSharedPreferences!!.edit().putString(key, objectString).apply() + } + + fun getObjectsList(key: String, classType: Class): List? { + if (isKeyExists(key)) { + val objectString = mSharedPreferences!!.getString(key, null) + if (objectString != null) { + val list: ArrayList = Gson().fromJson(objectString, object : TypeToken>() {}.type) + val finalList = ArrayList() + for (i in list.indices) { + val s = list[i].toString() + finalList.add(Gson().fromJson(s, classType)) + } + return finalList + } + } + return null + } + + fun clearSession() { + mSharedPreferences!!.edit().clear().apply() + } + + fun deleteValue(key: String): Boolean { + return if (isKeyExists(key)) { + mSharedPreferences!!.edit().remove(key).apply() + true + } else { + false + } + } + + fun isKeyExists(key: String): Boolean { + val map = mSharedPreferences!!.all + return if (map.containsKey(key)) { + true + } else { + Log.e("FastSave", "No element founded in sharedPrefs with the key $key") + false + } + } + + fun removeAllExcept(vararg keys: String): Boolean { + var isMatch: Boolean + val editor = mSharedPreferences!!.edit() + for (key in mSharedPreferences!!.all.keys) { + isMatch = false + for (k in keys) { + if (key == k) { + isMatch = true + } + } + if (!isMatch) { + editor.remove(key) + } + } + editor.apply() + return true + } +} diff --git a/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.java b/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.java deleted file mode 100644 index cbf1e3e..0000000 --- a/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.appizona.yehiahd.fastsave; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.kt b/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.kt new file mode 100644 index 0000000..bf15905 --- /dev/null +++ b/fastsave/src/test/java/com/appizona/yehiahd/fastsave/ExampleUnitTest.kt @@ -0,0 +1,16 @@ +package com.appizona.yehiahd.fastsave + +import org.junit.Test +import org.junit.Assert.assertEquals + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +}