SP -> core

This commit is contained in:
Milos Kozak 2020-05-04 00:33:27 +02:00
parent 7e6f82d068
commit 99907ed029
6 changed files with 17 additions and 141 deletions

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.dependencyInjection package info.nightscout.androidaps.dependencyInjection
import android.content.Context import android.content.Context
import androidx.preference.PreferenceManager
import dagger.Binds import dagger.Binds
import dagger.Lazy import dagger.Lazy
import dagger.Module import dagger.Module
@ -22,7 +21,6 @@ import info.nightscout.androidaps.queue.CommandQueue
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP 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.FileStorage
import info.nightscout.androidaps.utils.storage.Storage import info.nightscout.androidaps.utils.storage.Storage
import javax.inject.Singleton import javax.inject.Singleton
@ -30,12 +28,6 @@ import javax.inject.Singleton
@Module(includes = [AppModule.AppBindings::class, PluginsModule::class]) @Module(includes = [AppModule.AppBindings::class, PluginsModule::class])
open class AppModule { open class AppModule {
@Provides
@Singleton
fun provideSharedPreferences(context: Context, resourceHelper: ResourceHelper): SP {
return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context), resourceHelper)
}
@Provides @Provides
@Singleton @Singleton
fun provideProfileFunction(injector: HasAndroidInjector, aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePluginProvider, fabricPrivacy: FabricPrivacy): ProfileFunction { fun provideProfileFunction(injector: HasAndroidInjector, aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePluginProvider, fabricPrivacy: FabricPrivacy): ProfileFunction {

View file

@ -19,26 +19,6 @@ public class SP {
return sharedPreferences.getAll(); 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 @Deprecated
static public String getString(String key, String defaultValue) { static public String getString(String key, String defaultValue) {
return sharedPreferences.getString(key, 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 @Deprecated
static public void putString(int resourceID, String value) { static public void putString(int resourceID, String value) {
sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply(); sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply();
@ -159,11 +43,6 @@ public class SP {
sharedPreferences.edit().putString(key, value).apply(); sharedPreferences.edit().putString(key, value).apply();
} }
@Deprecated
static public void remove(int resourceID) {
sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply();
}
@Deprecated @Deprecated
static public void remove(String key) { static public void remove(String key) {
sharedPreferences.edit().remove(key).apply(); sharedPreferences.edit().remove(key).apply();

View file

@ -1,10 +1,13 @@
package info.nightscout.androidaps.core.dependencyInjection package info.nightscout.androidaps.core.dependencyInjection
import android.content.Context import android.content.Context
import android.preference.PreferenceManager
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.resources.ResourceHelperImplementation 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 import javax.inject.Singleton
@Module @Module
@ -15,4 +18,10 @@ open class CoreModule {
fun provideResources(context: Context): ResourceHelper { fun provideResources(context: Context): ResourceHelper {
return ResourceHelperImplementation(context) return ResourceHelperImplementation(context)
} }
@Provides
@Singleton
fun provideSharedPreferences(context: Context, resourceHelper: ResourceHelper): SP {
return SPImplementation(PreferenceManager.getDefaultSharedPreferences(context), resourceHelper)
}
} }

View file

@ -1,15 +1,11 @@
package info.nightscout.androidaps.utils; 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. * Created by mike on 23.06.2016.
*/ */
public class SafeParse { 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) { public static Double stringToDouble(String input) {
Double result = 0d; Double result = 0d;
input = input.replace(",", "."); input = input.replace(",", ".");
@ -19,7 +15,7 @@ public class SafeParse {
try { try {
result = Double.parseDouble(input); result = Double.parseDouble(input);
} catch (Exception e) { } catch (Exception e) {
log.error("Error parsing " + input + " to double"); // log.error("Error parsing " + input + " to double");
} }
return result; return result;
} }
@ -33,21 +29,21 @@ public class SafeParse {
try { try {
result = Integer.parseInt(input); result = Integer.parseInt(input);
} catch (Exception e) { } catch (Exception e) {
log.error("Error parsing " + input + " to int"); // log.error("Error parsing " + input + " to int");
} }
return result; return result;
} }
public static Long stringToLong(String input) { public static Long stringToLong(String input) {
Long result = 0L; Long result = 0L;
input = input.replace(",", "."); input = input.replace(",", ".");
input = input.replace("", "-"); input = input.replace("", "-");
if (input.equals("")) if (input.equals(""))
return 0L; return 0L;
try { try {
result = Long.parseLong(input); result = Long.parseLong(input);
} catch (Exception e) { } catch (Exception e) {
log.error("Error parsing " + input + " to long"); // log.error("Error parsing " + input + " to long");
} }
return result; return result;
} }