diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index d703958aa7..46f2e80625 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -1,8 +1,6 @@ package eu.kanade.tachiyomi import android.app.Application -import android.content.Context -import eu.kanade.tachiyomi.data.preference.PreferencesHelper import org.acra.ACRA import org.acra.annotation.ReportsCrashes import timber.log.Timber @@ -17,29 +15,16 @@ import uy.kohesive.injekt.Injekt ) open class App : Application() { - var appTheme = 0 - override fun onCreate() { super.onCreate() Injekt.importModule(AppModule(this)) if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree()) - setupTheme() setupAcra() } - private fun setupTheme() { - appTheme = PreferencesHelper.getTheme(this) - } - protected open fun setupAcra() { ACRA.init(this) } - companion object { - @JvmStatic - fun get(context: Context): App { - return context.applicationContext as App - } - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarm.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarm.kt index 8f2368406c..1c08f967ea 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarm.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarm.kt @@ -7,7 +7,10 @@ import android.content.Context import android.content.Intent import android.os.SystemClock import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.util.alarmManager +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get /** * This class is used to update the library by firing an alarm after a specified time. @@ -25,10 +28,8 @@ class LibraryUpdateAlarm : BroadcastReceiver() { * @param intervalInHours the time in hours when it will be executed. Defaults to the * value stored in preferences. */ - @JvmStatic - @JvmOverloads fun startAlarm(context: Context, - intervalInHours: Int = PreferencesHelper.getLibraryUpdateInterval(context)) { + intervalInHours: Int = Injekt.get().libraryUpdateInterval().getOrDefault()) { // Stop previous running alarms if needed, and do not restart it if the interval is 0. stopAlarm(context) if (intervalInHours == 0) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index c0eaffc84f..45c8e5ddd0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -10,6 +10,8 @@ import eu.kanade.tachiyomi.R */ class PreferenceKeys(context: Context) { + val theme = context.getString(R.string.pref_theme_key) + val rotation = context.getString(R.string.pref_rotation_type_key) val enableTransitions = context.getString(R.string.pref_enable_transitions_key) @@ -78,6 +80,8 @@ class PreferenceKeys(context: Context) { val filterUnread = context.getString(R.string.pref_filter_unread_key) + val automaticUpdateStatus = context.getString(R.string.pref_enable_automatic_updates_key) + fun sourceUsername(sourceId: Int) = "pref_source_username_$sourceId" fun sourcePassword(sourceId: Int) = "pref_source_password_$sourceId" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index ef2e2cce43..8a159c3a7f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -13,7 +13,7 @@ import java.io.IOException fun Preference.getOrDefault(): T = get() ?: defaultValue()!! -class PreferencesHelper(private val context: Context) { +class PreferencesHelper(context: Context) { val keys = PreferenceKeys(context) @@ -32,28 +32,9 @@ class PreferencesHelper(private val context: Context) { } } - companion object { + fun clear() = prefs.edit().clear().apply() - fun getLibraryUpdateInterval(context: Context): Int { - return PreferenceManager.getDefaultSharedPreferences(context).getInt( - context.getString(R.string.pref_library_update_interval_key), 0) - } - - fun getAutomaticUpdateStatus(context: Context): Boolean { - return PreferenceManager.getDefaultSharedPreferences(context).getBoolean( - context.getString(R.string.pref_enable_automatic_updates), false) - } - - @JvmStatic - fun getTheme(context: Context): Int { - return PreferenceManager.getDefaultSharedPreferences(context).getInt( - context.getString(R.string.pref_theme_key), 1) - } - } - - fun clear() { - prefs.edit().clear().apply() - } + fun theme() = prefs.getInt(keys.theme, 1) fun rotation() = rxPrefs.getInteger(keys.rotation, 1) @@ -147,4 +128,6 @@ class PreferencesHelper(private val context: Context) { fun filterUnread() = rxPrefs.getBoolean(keys.filterUnread, false) + fun automaticUpdateStatus() = prefs.getBoolean(keys.automaticUpdateStatus, false) + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloaderAlarm.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloaderAlarm.kt index b0258fd076..66e01deb94 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloaderAlarm.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdateDownloaderAlarm.kt @@ -15,6 +15,8 @@ import eu.kanade.tachiyomi.util.notification import eu.kanade.tachiyomi.util.notificationManager import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get class UpdateDownloaderAlarm : BroadcastReceiver() { @@ -26,9 +28,8 @@ class UpdateDownloaderAlarm : BroadcastReceiver() { * @param context the application context. * @param intervalInHours the time in hours when it will be executed. */ - @JvmStatic - @JvmOverloads - fun startAlarm(context: Context, intervalInHours: Int = 12, isEnabled: Boolean = PreferencesHelper.getAutomaticUpdateStatus(context)) { + fun startAlarm(context: Context, intervalInHours: Int = 12, + isEnabled: Boolean = Injekt.get().automaticUpdateStatus()) { // Stop previous running alarms if needed, and do not restart it if the interval is 0. UpdateDownloaderAlarm.stopAlarm(context) if (intervalInHours == 0 || !isEnabled) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/ActivityMixin.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/ActivityMixin.kt index 41773a9d59..a259c3adb7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/ActivityMixin.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/ActivityMixin.kt @@ -8,8 +8,10 @@ import android.support.v4.content.ContextCompat import android.support.v7.app.ActionBar import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar -import eu.kanade.tachiyomi.App import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get interface ActivityMixin { @@ -22,7 +24,7 @@ interface ActivityMixin { } fun setAppTheme() { - setTheme(when (App.get(getActivity()).appTheme) { + setTheme(when (Injekt.get().theme()) { 2 -> R.style.Theme_Tachiyomi_Dark else -> R.style.Theme_Tachiyomi }) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryActivity.kt index 2f25572c3b..fedf1c27b9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/category/CategoryActivity.kt @@ -50,7 +50,6 @@ class CategoryActivity : BaseRxActivity(), ActionMode.Callbac * * @param context context information. */ - @JvmStatic fun newIntent(context: Context): Intent { return Intent(context, CategoryActivity::class.java) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt index 162379037f..562aade653 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadFragment.kt @@ -67,7 +67,6 @@ class DownloadFragment : BaseRxFragment() { * * @return a new instance of [DownloadFragment]. */ - @JvmStatic fun newInstance(): DownloadFragment { return DownloadFragment() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index d03415cac0..f4849acef5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -95,7 +95,6 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback * * @return a new instance of [LibraryFragment]. */ - @JvmStatic fun newInstance(): LibraryFragment { return LibraryFragment() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersFragment.kt index 35fb685401..a1b2dc0857 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent_updates/RecentChaptersFragment.kt @@ -34,7 +34,6 @@ class RecentChaptersFragment * Create new RecentChaptersFragment. * @return a new instance of [RecentChaptersFragment]. */ - @JvmStatic fun newInstance(): RecentChaptersFragment { return RecentChaptersFragment() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadFragment.kt index 1c7dc15205..6586cb48cb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadFragment.kt @@ -27,9 +27,7 @@ class RecentlyReadFragment : BaseRxFragment() { companion object { /** * Create new RecentChaptersFragment. - * */ - @JvmStatic fun newInstance(): RecentlyReadFragment { return RecentlyReadFragment() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 2e81d577b7..5b6b4292fb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -6,7 +6,6 @@ import android.support.v14.preference.MultiSelectListPreference import android.support.v4.app.TaskStackBuilder import android.support.v7.preference.Preference import android.view.View -import eu.kanade.tachiyomi.App import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.library.LibraryUpdateAlarm import eu.kanade.tachiyomi.ui.main.MainActivity @@ -62,8 +61,6 @@ class SettingsGeneralFragment : SettingsNestedFragment() { } themePreference.setOnPreferenceChangeListener { preference, newValue -> - App.get(activity).appTheme = (newValue as String).toInt() - // Rebuild activity's to apply themes. TaskStackBuilder.create(activity) .addNextIntent(Intent(activity, MainActivity::class.java)) diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 9b0ba28e0b..3543a757e7 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -55,7 +55,7 @@ pref_version pref_build_time - pref_enable_automatic_updates_key + automatic_updates pref_display_catalogue_as_list pref_last_catalogue_source_key diff --git a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt index deddd2c1c6..0895523e1d 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateAlarmTest.kt @@ -5,6 +5,8 @@ import android.content.Context import android.content.Intent import android.os.Build import android.os.SystemClock +import eu.kanade.tachiyomi.App +import eu.kanade.tachiyomi.AppModule import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.CustomRobolectricGradleTestRunner import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -17,6 +19,9 @@ import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import org.robolectric.shadows.ShadowAlarmManager import org.robolectric.shadows.ShadowApplication +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.InjektScope +import uy.kohesive.injekt.registry.default.DefaultRegistrar @Config(constants = BuildConfig::class, sdk = intArrayOf(Build.VERSION_CODES.LOLLIPOP)) @RunWith(CustomRobolectricGradleTestRunner::class) @@ -30,6 +35,8 @@ class LibraryUpdateAlarmTest { fun setup() { app = ShadowApplication.getInstance() context = spy(app.applicationContext) + Injekt = InjektScope(DefaultRegistrar()) + Injekt.importModule(AppModule(context as App)) alarmManager = shadowOf(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) }