From 45c91219460f0b860e9d7e4a57f768de7a71c557 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Fri, 23 Oct 2020 21:16:48 +0200 Subject: [PATCH] Inject SharedPreferences --- .../main/java/com/readrops/app/AppModule.kt | 3 ++ .../main/java/com/readrops/app/ReadropsApp.kt | 10 +++--- .../app/activities/AddAccountActivity.java | 8 ++--- .../app/activities/AddFeedActivity.java | 4 +-- .../readrops/app/activities/ItemActivity.java | 4 +-- .../readrops/app/activities/MainActivity.java | 12 +++---- .../NotificationPermissionActivity.kt | 3 +- .../readrops/app/fragments/FeedsFragment.java | 4 +-- .../app/fragments/FoldersFragment.java | 4 +-- .../settings/AccountSettingsFragment.java | 4 +-- .../app/repositories/LocalFeedRepository.java | 2 +- .../app/utils/SharedPreferencesManager.java | 36 +++++++++---------- .../java/com/readrops/app/utils/SyncWorker.kt | 5 ++- 13 files changed, 48 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/com/readrops/app/AppModule.kt b/app/src/main/java/com/readrops/app/AppModule.kt index 9f111ce5..ee0b5696 100644 --- a/app/src/main/java/com/readrops/app/AppModule.kt +++ b/app/src/main/java/com/readrops/app/AppModule.kt @@ -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()) } } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/ReadropsApp.kt b/app/src/main/java/com/readrops/app/ReadropsApp.kt index 8d856bd8..559bfa33 100644 --- a/app/src/main/java/com/readrops/app/ReadropsApp.kt +++ b/app/src/main/java/com/readrops/app/ReadropsApp.kt @@ -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() { diff --git a/app/src/main/java/com/readrops/app/activities/AddAccountActivity.java b/app/src/main/java/com/readrops/app/activities/AddAccountActivity.java index 7f3653cf..f952c5bd 100644 --- a/app/src/main/java/com/readrops/app/activities/AddAccountActivity.java +++ b/app/src/main/java/com/readrops/app/activities/AddAccountActivity.java @@ -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() { diff --git a/app/src/main/java/com/readrops/app/activities/AddFeedActivity.java b/app/src/main/java/com/readrops/app/activities/AddFeedActivity.java index 2973ecc5..5828ce1a 100644 --- a/app/src/main/java/com/readrops/app/activities/AddFeedActivity.java +++ b/app/src/main/java/com/readrops/app/activities/AddFeedActivity.java @@ -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()) diff --git a/app/src/main/java/com/readrops/app/activities/ItemActivity.java b/app/src/main/java/com/readrops/app/activities/ItemActivity.java index 8d45d45a..14a96800 100644 --- a/app/src/main/java/com/readrops/app/activities/ItemActivity.java +++ b/app/src/main/java/com/readrops/app/activities/ItemActivity.java @@ -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() diff --git a/app/src/main/java/com/readrops/app/activities/MainActivity.java b/app/src/main/java/com/readrops/app/activities/MainActivity.java index 826b7001..e8dac12f 100644 --- a/app/src/main/java/com/readrops/app/activities/MainActivity.java +++ b/app/src/main/java/com/readrops/app/activities/MainActivity.java @@ -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 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; diff --git a/app/src/main/java/com/readrops/app/activities/NotificationPermissionActivity.kt b/app/src/main/java/com/readrops/app/activities/NotificationPermissionActivity.kt index 4c6e0c3d..a0b2ff07 100644 --- a/app/src/main/java/com/readrops/app/activities/NotificationPermissionActivity.kt +++ b/app/src/main/java/com/readrops/app/activities/NotificationPermissionActivity.kt @@ -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) diff --git a/app/src/main/java/com/readrops/app/fragments/FeedsFragment.java b/app/src/main/java/com/readrops/app/fragments/FeedsFragment.java index 82dcf0a9..ff9a31e2 100644 --- a/app/src/main/java/com/readrops/app/fragments/FeedsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/FeedsFragment.java @@ -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); diff --git a/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java b/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java index 9ecb0e63..587660ee 100644 --- a/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/FoldersFragment.java @@ -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); diff --git a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java index 30d91490..b0f1cc95 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/AccountSettingsFragment.java @@ -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()) diff --git a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java index 65aa3553..f1d27653 100644 --- a/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/repositories/LocalFeedRepository.java @@ -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()); diff --git a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java index 0da4ee57..9d42e45b 100644 --- a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java +++ b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java @@ -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); diff --git a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt index e544df2e..e788690b 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncWorker.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncWorker.kt @@ -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(parameters = { parametersOf(it) })