From 99907ed0291160fb2136484a6e792467e328ea1c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 4 May 2020 00:33:27 +0200 Subject: [PATCH] SP -> core --- .../dependencyInjection/AppModule.kt | 8 -- .../info/nightscout/androidaps/utils/SP.java | 121 ------------------ .../core/dependencyInjection/CoreModule.kt | 9 ++ .../androidaps/utils/SafeParse.java | 20 ++- .../androidaps/utils/sharedPreferences/SP.kt | 0 .../sharedPreferences/SPImplementation.kt | 0 6 files changed, 17 insertions(+), 141 deletions(-) rename {app => core}/src/main/java/info/nightscout/androidaps/utils/SafeParse.java (67%) rename {app => core}/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt (100%) rename {app => core}/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt (100%) diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index bb968938d5..9c78bd5642 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -1,7 +1,6 @@ package info.nightscout.androidaps.dependencyInjection import android.content.Context -import androidx.preference.PreferenceManager import dagger.Binds import dagger.Lazy import dagger.Module @@ -22,7 +21,6 @@ import info.nightscout.androidaps.queue.CommandQueue import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP -import info.nightscout.androidaps.utils.sharedPreferences.SPImplementation import info.nightscout.androidaps.utils.storage.FileStorage import info.nightscout.androidaps.utils.storage.Storage import javax.inject.Singleton @@ -30,12 +28,6 @@ import javax.inject.Singleton @Module(includes = [AppModule.AppBindings::class, PluginsModule::class]) open class AppModule { - @Provides - @Singleton - fun provideSharedPreferences(context: Context, resourceHelper: ResourceHelper): SP { - return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context), resourceHelper) - } - @Provides @Singleton fun provideProfileFunction(injector: HasAndroidInjector, aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePluginProvider, fabricPrivacy: FabricPrivacy): ProfileFunction { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/SP.java b/app/src/main/java/info/nightscout/androidaps/utils/SP.java index 08ad3a8693..a6124acf10 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/SP.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/SP.java @@ -19,26 +19,6 @@ public class SP { return sharedPreferences.getAll(); } - @Deprecated - static public void clear() { - sharedPreferences.edit().clear().apply(); - } - - @Deprecated - static public boolean contains(String key) { - return sharedPreferences.contains(key); - } - - @Deprecated - static public boolean contains(int resourceId) { - return sharedPreferences.contains(MainApp.gs(resourceId)); - } - - @Deprecated - static public String getString(int resourceID, String defaultValue) { - return sharedPreferences.getString(MainApp.gs(resourceID), defaultValue); - } - @Deprecated static public String getString(String key, String defaultValue) { return sharedPreferences.getString(key, defaultValue); @@ -53,102 +33,6 @@ public class SP { } } - @Deprecated - static public boolean getBoolean(String key, Boolean defaultValue) { - try { - return sharedPreferences.getBoolean(key, defaultValue); - } catch (Exception e) { - return defaultValue; - } - } - - @Deprecated - static public Double getDouble(int resourceID, Double defaultValue) { - return SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())); - } - - @Deprecated - static public Double getDouble(String key, Double defaultValue) { - return SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString())); - } - - @Deprecated - static public int getInt(int resourceID, Integer defaultValue) { - try { - return sharedPreferences.getInt(MainApp.gs(resourceID), defaultValue); - } catch (Exception e) { - return SafeParse.stringToInt(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())); - } - } - - @Deprecated - static public int getInt(String key, Integer defaultValue) { - try { - return sharedPreferences.getInt(key, defaultValue); - } catch (Exception e) { - return SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString())); - } - } - - @Deprecated - static public long getLong(int resourceID, Long defaultValue) { - try { - return sharedPreferences.getLong(MainApp.gs(resourceID), defaultValue); - } catch (Exception e) { - return SafeParse.stringToLong(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString())); - } - } - - @Deprecated - static public long getLong(String key, Long defaultValue) { - try { - return sharedPreferences.getLong(key, defaultValue); - } catch (Exception e) { - return SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString())); - } - } - - @Deprecated - static public void putBoolean(String key, boolean value) { - sharedPreferences.edit().putBoolean(key, value).apply(); - } - - @Deprecated - static public void putBoolean(int resourceID, boolean value) { - sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply(); - } - - @Deprecated - static public void putDouble(String key, double value) { - sharedPreferences.edit().putString(key, Double.toString(value)).apply(); - } - - @Deprecated - static public void putLong(String key, long value) { - sharedPreferences.edit().putLong(key, value).apply(); - } - - @Deprecated - static public void putLong(int resourceID, long value) { - sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply(); - } - - @Deprecated - static public void putInt(String key, int value) { - sharedPreferences.edit().putInt(key, value).apply(); - } - - @Deprecated - static public void putInt(int resourceID, int value) { - sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply(); - } - - @Deprecated - static public void incInt(int resourceID) { - int value = getInt(resourceID, 0) + 1; - sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply(); - } - @Deprecated static public void putString(int resourceID, String value) { sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply(); @@ -159,11 +43,6 @@ public class SP { sharedPreferences.edit().putString(key, value).apply(); } - @Deprecated - static public void remove(int resourceID) { - sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply(); - } - @Deprecated static public void remove(String key) { sharedPreferences.edit().remove(key).apply(); diff --git a/core/src/main/java/info/nightscout/androidaps/core/dependencyInjection/CoreModule.kt b/core/src/main/java/info/nightscout/androidaps/core/dependencyInjection/CoreModule.kt index 3d022dfd6f..b7ec76d301 100644 --- a/core/src/main/java/info/nightscout/androidaps/core/dependencyInjection/CoreModule.kt +++ b/core/src/main/java/info/nightscout/androidaps/core/dependencyInjection/CoreModule.kt @@ -1,10 +1,13 @@ package info.nightscout.androidaps.core.dependencyInjection import android.content.Context +import android.preference.PreferenceManager import dagger.Module import dagger.Provides import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelperImplementation +import info.nightscout.androidaps.utils.sharedPreferences.SP +import info.nightscout.androidaps.utils.sharedPreferences.SPImplementation import javax.inject.Singleton @Module @@ -15,4 +18,10 @@ open class CoreModule { fun provideResources(context: Context): ResourceHelper { return ResourceHelperImplementation(context) } + + @Provides + @Singleton + fun provideSharedPreferences(context: Context, resourceHelper: ResourceHelper): SP { + return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context), resourceHelper) + } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/utils/SafeParse.java b/core/src/main/java/info/nightscout/androidaps/utils/SafeParse.java similarity index 67% rename from app/src/main/java/info/nightscout/androidaps/utils/SafeParse.java rename to core/src/main/java/info/nightscout/androidaps/utils/SafeParse.java index 4e1ee5ceae..0ea68bd1eb 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/SafeParse.java +++ b/core/src/main/java/info/nightscout/androidaps/utils/SafeParse.java @@ -1,15 +1,11 @@ package info.nightscout.androidaps.utils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; - /** * Created by mike on 23.06.2016. */ public class SafeParse { - private static Logger log = StacktraceLoggerWrapper.getLogger(SafeParse.class); + // TODO return logging with dagger +// private static Logger log = StacktraceLoggerWrapper.getLogger(SafeParse.class); public static Double stringToDouble(String input) { Double result = 0d; input = input.replace(",", "."); @@ -19,7 +15,7 @@ public class SafeParse { try { result = Double.parseDouble(input); } catch (Exception e) { - log.error("Error parsing " + input + " to double"); +// log.error("Error parsing " + input + " to double"); } return result; } @@ -33,21 +29,21 @@ public class SafeParse { try { result = Integer.parseInt(input); } catch (Exception e) { - log.error("Error parsing " + input + " to int"); +// log.error("Error parsing " + input + " to int"); } return result; } - public static Long stringToLong(String input) { + public static Long stringToLong(String input) { Long result = 0L; input = input.replace(",", "."); input = input.replace("−", "-"); - if (input.equals("")) - return 0L; + if (input.equals("")) + return 0L; try { result = Long.parseLong(input); } catch (Exception e) { - log.error("Error parsing " + input + " to long"); +// log.error("Error parsing " + input + " to long"); } return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt b/core/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt similarity index 100% rename from app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt rename to core/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SP.kt diff --git a/app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt b/core/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt similarity index 100% rename from app/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt rename to core/src/main/java/info/nightscout/androidaps/utils/sharedPreferences/SPImplementation.kt