From 256a4197c93054981bba18f1e64672d302664213 Mon Sep 17 00:00:00 2001 From: len Date: Sun, 21 May 2017 13:42:06 +0200 Subject: [PATCH] Replace changelog dialog with controller, move migration logic to a separate class --- .../java/eu/kanade/tachiyomi/Migrations.kt | 53 +++++++++++++ .../ui/main/ChangelogDialogController.kt | 32 ++++++++ .../ui/main/ChangelogDialogFragment.kt | 74 ------------------- .../kanade/tachiyomi/ui/main/MainActivity.kt | 6 +- 4 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/Migrations.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogController.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogFragment.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt new file mode 100644 index 000000000..de568c725 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt @@ -0,0 +1,53 @@ +package eu.kanade.tachiyomi + +import eu.kanade.tachiyomi.data.library.LibraryUpdateJob +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import eu.kanade.tachiyomi.data.updater.UpdateCheckerJob +import java.io.File + +object Migrations { + + /** + * Performs a migration when the application is updated. + * + * @param preferences Preferences of the application. + * @return true if a migration is performed, false otherwise. + */ + fun upgrade(preferences: PreferencesHelper): Boolean { + val context = preferences.context + val oldVersion = preferences.lastVersionCode().getOrDefault() + if (oldVersion < BuildConfig.VERSION_CODE) { + preferences.lastVersionCode().set(BuildConfig.VERSION_CODE) + + if (oldVersion == 0) return false + + if (oldVersion < 14) { + // Restore jobs after upgrading to evernote's job scheduler. + if (BuildConfig.INCLUDE_UPDATER && preferences.automaticUpdates()) { + UpdateCheckerJob.setupTask() + } + LibraryUpdateJob.setupTask() + } + if (oldVersion < 15) { + // Delete internal chapter cache dir. + File(context.cacheDir, "chapter_disk_cache").deleteRecursively() + } + if (oldVersion < 19) { + // Move covers to external files dir. + val oldDir = File(context.externalCacheDir, "cover_disk_cache") + if (oldDir.exists()) { + val destDir = context.getExternalFilesDir("covers") + if (destDir != null) { + oldDir.listFiles().forEach { + it.renameTo(File(destDir, it.name)) + } + } + } + } + return true + } + return false + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogController.kt new file mode 100644 index 000000000..4d24b7e20 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogController.kt @@ -0,0 +1,32 @@ +package eu.kanade.tachiyomi.ui.main + +import android.app.Dialog +import android.content.Context +import android.os.Bundle +import android.util.AttributeSet +import com.afollestad.materialdialogs.MaterialDialog +import eu.kanade.tachiyomi.BuildConfig +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.ui.base.controller.DialogController +import it.gmariotti.changelibs.library.view.ChangeLogRecyclerView + +class ChangelogDialogController : DialogController() { + + override fun onCreateDialog(savedState: Bundle?): Dialog { + val activity = activity!! + val view = WhatsNewRecyclerView(activity) + return MaterialDialog.Builder(activity) + .title(if (BuildConfig.DEBUG) "Notices" else "Changelog") + .customView(view, false) + .positiveText(android.R.string.yes) + .build() + } + + class WhatsNewRecyclerView(context: Context) : ChangeLogRecyclerView(context) { + override fun initAttrs(attrs: AttributeSet?, defStyle: Int) { + mRowLayoutId = R.layout.changelog_row_layout + mRowHeaderLayoutId = R.layout.changelog_header_layout + mChangeLogFileResourceId = if (BuildConfig.DEBUG) R.raw.changelog_debug else R.raw.changelog_release + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogFragment.kt deleted file mode 100644 index 08ab144b1..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/ChangelogDialogFragment.kt +++ /dev/null @@ -1,74 +0,0 @@ -package eu.kanade.tachiyomi.ui.main - -import android.app.Dialog -import android.content.Context -import android.os.Bundle -import android.support.v4.app.DialogFragment -import android.support.v4.app.FragmentManager -import android.util.AttributeSet -import com.afollestad.materialdialogs.MaterialDialog -import eu.kanade.tachiyomi.BuildConfig -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.library.LibraryUpdateJob -import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.getOrDefault -import eu.kanade.tachiyomi.data.updater.UpdateCheckerJob -import it.gmariotti.changelibs.library.view.ChangeLogRecyclerView -import java.io.File - -class ChangelogDialogFragment : DialogFragment() { - - companion object { - fun show(context: Context, preferences: PreferencesHelper, fm: FragmentManager) { - val oldVersion = preferences.lastVersionCode().getOrDefault() - if (oldVersion < BuildConfig.VERSION_CODE) { - preferences.lastVersionCode().set(BuildConfig.VERSION_CODE) - ChangelogDialogFragment().show(fm, "changelog") - - // TODO better upgrades management - if (oldVersion == 0) return - - if (oldVersion < 14) { - // Restore jobs after upgrading to evernote's job scheduler. - if (BuildConfig.INCLUDE_UPDATER && preferences.automaticUpdates()) { - UpdateCheckerJob.setupTask() - } - LibraryUpdateJob.setupTask() - } - if (oldVersion < 15) { - // Delete internal chapter cache dir. - File(context.cacheDir, "chapter_disk_cache").deleteRecursively() - } - if (oldVersion < 19) { - // Move covers to external files dir. - val oldDir = File(context.externalCacheDir, "cover_disk_cache") - if (oldDir.exists()) { - val destDir = context.getExternalFilesDir("covers") - if (destDir != null) { - oldDir.listFiles().forEach { - it.renameTo(File(destDir, it.name)) - } - } - } - } - } - } - } - - override fun onCreateDialog(savedState: Bundle?): Dialog { - val view = WhatsNewRecyclerView(context) - return MaterialDialog.Builder(activity) - .title(if (BuildConfig.DEBUG) "Notices" else "Changelog") - .customView(view, false) - .positiveText(android.R.string.yes) - .build() - } - - class WhatsNewRecyclerView(context: Context) : ChangeLogRecyclerView(context) { - override fun initAttrs(attrs: AttributeSet?, defStyle: Int) { - mRowLayoutId = R.layout.changelog_row_layout - mRowHeaderLayoutId = R.layout.changelog_header_layout - mChangeLogFileResourceId = if (BuildConfig.DEBUG) R.raw.changelog_debug else R.raw.changelog_release - } - } -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index aa613d8c3..5b825f9c4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -10,6 +10,7 @@ import android.support.v7.graphics.drawable.DrawerArrowDrawable import android.view.ViewGroup import com.bluelinelabs.conductor.* import com.bluelinelabs.conductor.changehandler.FadeChangeHandler +import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.base.activity.BaseActivity @@ -136,10 +137,11 @@ class MainActivity : BaseActivity() { syncActivityViewWithController(router.backstack.lastOrNull()?.controller()) - // TODO changelog controller if (savedInstanceState == null) { // Show changelog if needed - ChangelogDialogFragment.show(this, preferences, supportFragmentManager) + if (Migrations.upgrade(preferences)) { + ChangelogDialogController().showDialog(router) + } } }