Inject SharedPreferences

This commit is contained in:
Shinokuni 2020-10-23 21:16:48 +02:00
parent afce72bbe1
commit 45c9121946
13 changed files with 48 additions and 51 deletions

View File

@ -1,5 +1,6 @@
package com.readrops.app
import androidx.preference.PreferenceManager
import com.readrops.app.repositories.FreshRSSRepository
import com.readrops.app.repositories.LocalFeedRepository
import com.readrops.app.repositories.NextNewsRepository
@ -48,4 +49,6 @@ val appModule = module {
}
single { GlideApp.with(androidApplication()) }
single { PreferenceManager.getDefaultSharedPreferences(androidContext()) }
}

View File

@ -24,17 +24,17 @@ open class ReadropsApp : Application() {
createNotificationChannels()
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
if (SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME).toBoolean())
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
startKoin {
androidLogger(Level.ERROR)
androidContext(this@ReadropsApp)
modules(apiModule, dbModule, appModule)
}
if (SharedPreferencesManager.readString(SharedPreferencesManager.SharedPrefKey.DARK_THEME).toBoolean())
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
private fun createNotificationChannels() {

View File

@ -178,8 +178,8 @@ public class AddAccountActivity extends AppCompatActivity {
}
private void saveLoginPassword(Account account) {
SharedPreferencesManager.writeValue(this, account.getLoginKey(), account.getLogin());
SharedPreferencesManager.writeValue(this, account.getPasswordKey(), account.getPassword());
SharedPreferencesManager.writeValue(account.getLoginKey(), account.getLogin());
SharedPreferencesManager.writeValue(account.getPasswordKey(), account.getPassword());
account.setLogin(null);
account.setPassword(null);
@ -191,8 +191,8 @@ public class AddAccountActivity extends AppCompatActivity {
binding.addAccountUrl.setText(accountToEdit.getUrl());
binding.addAccountName.setText(accountToEdit.getAccountName());
binding.addAccountLogin.setText(SharedPreferencesManager.readString(this, accountToEdit.getLoginKey()));
binding.addAccountPassword.setText(SharedPreferencesManager.readString(this, accountToEdit.getPasswordKey()));
binding.addAccountLogin.setText(SharedPreferencesManager.readString(accountToEdit.getLoginKey()));
binding.addAccountPassword.setText(SharedPreferencesManager.readString(accountToEdit.getPasswordKey()));
}
private void updateAccount() {

View File

@ -260,8 +260,8 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
Account account = (Account) binding.addFeedAccountSpinner.getSelectedItem();
account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey()));
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
account.setLogin(SharedPreferencesManager.readString(account.getLoginKey()));
account.setPassword(SharedPreferencesManager.readString(account.getPasswordKey()));
viewModel.addFeeds(feedsToInsert, account)
.subscribeOn(Schedulers.io())

View File

@ -216,7 +216,7 @@ public class ItemActivity extends AppCompatActivity {
}
private void openUrl() {
int value = Integer.parseInt(SharedPreferencesManager.readString(this,
int value = Integer.parseInt(SharedPreferencesManager.readString(
SharedPreferencesManager.SharedPrefKey.OPEN_ITEMS_IN));
switch (value) {
case 0:
@ -245,7 +245,7 @@ public class ItemActivity extends AppCompatActivity {
}
private void openInCustomTab() {
boolean darkTheme = Boolean.parseBoolean(SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME));
boolean darkTheme = Boolean.parseBoolean(SharedPreferencesManager.readString(SharedPreferencesManager.SharedPrefKey.DARK_THEME));
int color = itemWithFeed.getBgColor() != 0 ? itemWithFeed.getBgColor() : itemWithFeed.getColor();
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()

View File

@ -119,7 +119,7 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
viewModel = ViewModelCompat.getViewModel(this, MainViewModel.class);
viewModel.setShowReadItems(SharedPreferencesManager.readBoolean(this,
viewModel.setShowReadItems(SharedPreferencesManager.readBoolean(
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES));
viewModel.getItemsWithFeed().observe(this, itemWithFeeds -> {
@ -660,12 +660,12 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
if (item.isChecked()) {
item.setChecked(false);
viewModel.setShowReadItems(false);
SharedPreferencesManager.writeValue(this,
SharedPreferencesManager.writeValue(
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES, false);
} else {
item.setChecked(true);
viewModel.setShowReadItems(true);
SharedPreferencesManager.writeValue(this,
SharedPreferencesManager.writeValue(
SharedPreferencesManager.SharedPrefKey.SHOW_READ_ARTICLES, true);
}
@ -709,16 +709,16 @@ public class MainActivity extends AppCompatActivity implements SwipeRefreshLayou
private void getAccountCredentials(List<Account> accounts) {
for (Account account : accounts) {
if (account.getLogin() == null)
account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey()));
account.setLogin(SharedPreferencesManager.readString(account.getLoginKey()));
if (account.getPassword() == null)
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
account.setPassword(SharedPreferencesManager.readString(account.getPasswordKey()));
}
}
private void startAboutActivity() {
Libs.ActivityStyle activityStyle;
if (Boolean.valueOf(SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME)))
if (Boolean.valueOf(SharedPreferencesManager.readString(SharedPreferencesManager.SharedPrefKey.DARK_THEME)))
activityStyle = Libs.ActivityStyle.DARK;
else
activityStyle = Libs.ActivityStyle.LIGHT_DARK_TOOLBAR;

View File

@ -3,7 +3,6 @@ package com.readrops.app.activities
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
@ -121,7 +120,7 @@ class NotificationPermissionActivity : AppCompatActivity() {
}
private fun displayAutoSynchroPopup() {
val autoSynchroValue = SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.AUTO_SYNCHRO)
val autoSynchroValue = SharedPreferencesManager.readString(SharedPreferencesManager.SharedPrefKey.AUTO_SYNCHRO)
if (autoSynchroValue.toFloat() <= 0) {
MaterialDialog.Builder(this)

View File

@ -61,9 +61,9 @@ public class FeedsFragment extends Fragment {
account = getArguments().getParcelable(ACCOUNT);
if (account.getLogin() == null)
account.setLogin(SharedPreferencesManager.readString(getContext(), account.getLoginKey()));
account.setLogin(SharedPreferencesManager.readString(account.getLoginKey()));
if (account.getPassword() == null)
account.setPassword(SharedPreferencesManager.readString(getContext(), account.getPasswordKey()));
account.setPassword(SharedPreferencesManager.readString(account.getPasswordKey()));
viewModel = SharedViewModelCompat.getSharedViewModel(this, ManageFeedsFoldersViewModel.class);
viewModel.setAccount(account);

View File

@ -61,9 +61,9 @@ public class FoldersFragment extends Fragment {
account = getArguments().getParcelable(ACCOUNT);
if (account.getLogin() == null)
account.setLogin(SharedPreferencesManager.readString(getContext(), account.getLoginKey()));
account.setLogin(SharedPreferencesManager.readString(account.getLoginKey()));
if (account.getPassword() == null)
account.setPassword(SharedPreferencesManager.readString(getContext(), account.getPasswordKey()));
account.setPassword(SharedPreferencesManager.readString(account.getPasswordKey()));
adapter = new FoldersAdapter(this::openFolderOptionsDialog);
viewModel = SharedViewModelCompat.getSharedViewModel(this, ManageFeedsFoldersViewModel.class);

View File

@ -147,8 +147,8 @@ public class AccountSettingsFragment extends PreferenceFragmentCompat {
.positiveText(R.string.validate)
.negativeText(R.string.cancel)
.onPositive(((dialog, which) -> {
SharedPreferencesManager.remove(getContext(), account.getLoginKey());
SharedPreferencesManager.remove(getContext(), account.getPasswordKey());
SharedPreferencesManager.remove(account.getLoginKey());
SharedPreferencesManager.remove(account.getPasswordKey());
viewModel.delete(account)
.subscribeOn(Schedulers.io())

View File

@ -133,7 +133,7 @@ public class LocalFeedRepository extends ARepository {
Collections.sort(items, Item::compareTo);
int maxItems = Integer.parseInt(SharedPreferencesManager.readString(context,
int maxItems = Integer.parseInt(SharedPreferencesManager.readString(
SharedPreferencesManager.SharedPrefKey.ITEMS_TO_PARSE_MAX_NB));
if (maxItems > 0 && items.size() > maxItems) {
items = items.subList(items.size() - maxItems, items.size());

View File

@ -1,19 +1,15 @@
package com.readrops.app.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import org.koin.java.KoinJavaComponent;
public final class SharedPreferencesManager {
private static SharedPreferences getSharedPreferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
public static void writeValue(Context context, String key, Object value) {
SharedPreferences sharedPref = getSharedPreferences(context);
public static void writeValue(String key, Object value) {
SharedPreferences sharedPref = KoinJavaComponent.get(SharedPreferences.class);
SharedPreferences.Editor editor = sharedPref.edit();
if (value instanceof Boolean)
@ -24,32 +20,32 @@ public final class SharedPreferencesManager {
editor.apply();
}
public static void writeValue(Context context, SharedPrefKey sharedPrefKey, Object value) {
writeValue(context, sharedPrefKey.key, value);
public static void writeValue(SharedPrefKey sharedPrefKey, Object value) {
writeValue(sharedPrefKey.key, value);
}
public static int readInt(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
public static int readInt(SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = KoinJavaComponent.get(SharedPreferences.class);
return sharedPreferences.getInt(sharedPrefKey.key, sharedPrefKey.getIntDefaultValue());
}
public static boolean readBoolean(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
public static boolean readBoolean(SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = KoinJavaComponent.get(SharedPreferences.class);
return sharedPreferences.getBoolean(sharedPrefKey.key, sharedPrefKey.getBooleanDefaultValue());
}
public static String readString(Context context, String key) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
public static String readString(String key) {
SharedPreferences sharedPreferences = KoinJavaComponent.get(SharedPreferences.class);
return sharedPreferences.getString(key, null);
}
public static String readString(Context context, SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
public static String readString(SharedPrefKey sharedPrefKey) {
SharedPreferences sharedPreferences = KoinJavaComponent.get(SharedPreferences.class);
return sharedPreferences.getString(sharedPrefKey.key, sharedPrefKey.getStringDefaultValue());
}
public static void remove(Context context, String key) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
public static void remove(String key) {
SharedPreferences sharedPreferences = KoinJavaComponent.get(SharedPreferences.class);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key);

View File

@ -7,7 +7,6 @@ import android.content.Intent
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.readrops.api.services.SyncResult
@ -48,8 +47,8 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
notificationBuilder.setContentText(it.accountName)
notificationManager.notify(SYNC_NOTIFICATION_ID, notificationBuilder.build())
it.login = SharedPreferencesManager.readString(applicationContext, it.loginKey)
it.password = SharedPreferencesManager.readString(applicationContext, it.passwordKey)
it.login = SharedPreferencesManager.readString(it.loginKey)
it.password = SharedPreferencesManager.readString(it.passwordKey)
val repository = get<ARepository>(parameters = { parametersOf(it) })