Skip to content

Commit

Permalink
Consolidate self update change requests into main flavour
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Apr 8, 2022
1 parent 686b4d6 commit 1b4c751
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 124 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/mxt/anitrend/extension/ContextExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,8 @@ fun Context.logFile(): File {
val log = File(logDirectory(), "${packageName}.log")
if (!log.exists()) log.mkdirs()
return log
}

fun Context.supportsAutoUpdates(): Boolean {
return resources.getBoolean(R.bool.display_update_channel_pref)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ open class BasePresenter(context: Context?) : CommonPresenter(context) {
.addMigration(Migrations.MIGRATION_109_134)
.addMigration(Migrations.MIGRATION_135_136)
.addMigration(Migrations.MIGRATION_18400_18500)
.addMigration(Migrations.MIGRATION_1090700_1090800)
.build(context)
return migrationUtil.applyMigration()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mxt.anitrend.util.migration

import android.content.Context
import com.mxt.anitrend.util.Settings
import timber.log.Timber

/**
* Creates a new migration between [startVersion] and [endVersion].
Expand All @@ -13,7 +14,9 @@ abstract class Migration(
val startVersion: Int,
val endVersion: Int
) {
abstract fun applyMigration(context: Context, settings: Settings)
open fun applyMigration(context: Context, settings: Settings) {
Timber.i("Applying migrations for $this")
}

/**
* Indicates whether some other object is "equal to" this one. Implementations must fulfil the following
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/com/mxt/anitrend/util/migration/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package com.mxt.anitrend.util.migration

import android.content.Context
import android.os.Build
import androidx.annotation.CallSuper
import androidx.core.content.edit
import com.mxt.anitrend.R
import com.mxt.anitrend.analytics.AnalyticsLogging
import com.mxt.anitrend.data.DatabaseHelper
import com.mxt.anitrend.extension.koinOf
import com.mxt.anitrend.extension.supportsAutoUpdates
import com.mxt.anitrend.model.api.retro.WebFactory
import com.mxt.anitrend.util.JobSchedulerUtil
import com.mxt.anitrend.util.Settings
Expand All @@ -15,8 +18,8 @@ import timber.log.Timber
object Migrations {

val MIGRATION_101_108 = object : Migration(101, 108) {
@CallSuper
override fun applyMigration(context: Context, settings: Settings) {
Timber.i("Applying migrations for $this")
settings.edit {
clear()
apply()
Expand All @@ -27,8 +30,8 @@ object Migrations {
}

val MIGRATION_109_134 = object : Migration(109, 134) {
@CallSuper
override fun applyMigration(context: Context, settings: Settings) {
Timber.i("Applying migrations for $this")
settings.edit {
clear()
apply()
Expand All @@ -43,20 +46,31 @@ object Migrations {
}

val MIGRATION_135_136 = object : Migration(135, 136) {
@CallSuper
override fun applyMigration(context: Context, settings: Settings) {
Timber.i("Applying migrations for $this")
settings.isCrashReportsEnabled = true
}
}

val MIGRATION_18400_18500 = object : Migration(18400, 18500) {
@CallSuper
override fun applyMigration(context: Context, settings: Settings) {
Timber.i("Applying migrations for $this")
settings.edit {
// A small error in the italian translation which was using the
// string below as a key for notification preference
remove("Notifiche di nuovo messaggio")
}
}
}

val MIGRATION_1090700_1090800 = object : Migration(1090700, 1090800) {
@CallSuper
override fun applyMigration(context: Context, settings: Settings) {
settings.edit {
if (!context.supportsAutoUpdates()) {
settings.updateChannel = null
}
}
}
}
}
140 changes: 22 additions & 118 deletions app/src/main/java/com/mxt/anitrend/view/activity/index/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.mxt.anitrend.view.activity.index

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_DENIED
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand All @@ -17,29 +15,28 @@ import androidx.annotation.StringRes
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.viewpager.widget.ViewPager
import androidx.work.WorkInfo
import androidx.work.WorkManager
import butterknife.ButterKnife
import com.afollestad.materialdialogs.DialogAction
import com.google.android.material.navigation.NavigationView
import com.mxt.anitrend.R
import com.mxt.anitrend.adapter.pager.index.*
import com.mxt.anitrend.analytics.contract.ISupportAnalytics
import com.mxt.anitrend.base.custom.activity.ActivityBase
import com.mxt.anitrend.base.custom.activity.checkUpdate
import com.mxt.anitrend.base.custom.activity.launchUpdateWorker
import com.mxt.anitrend.base.custom.async.WebTokenRequest
import com.mxt.anitrend.base.custom.consumer.BaseConsumer
import com.mxt.anitrend.base.custom.view.image.AvatarIndicatorView
import com.mxt.anitrend.base.custom.view.image.HeaderImageView
import com.mxt.anitrend.base.interfaces.event.BottomSheetChoice
import com.mxt.anitrend.extension.*
import com.mxt.anitrend.extension.LAZY_MODE_UNSAFE
import com.mxt.anitrend.extension.getCompatDrawable
import com.mxt.anitrend.extension.koinOf
import com.mxt.anitrend.extension.startNewActivity
import com.mxt.anitrend.model.entity.anilist.User
import com.mxt.anitrend.presenter.base.BasePresenter
import com.mxt.anitrend.service.DownloaderService
import com.mxt.anitrend.util.DialogUtil
import com.mxt.anitrend.util.KeyUtil
import com.mxt.anitrend.util.NotifyUtil
Expand Down Expand Up @@ -353,60 +350,7 @@ class MainActivity : ActivityBase<User, BasePresenter>(), View.OnClickListener,
})
showBottomSheet()
}
R.id.nav_check_update -> when (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)) {
PERMISSION_GRANTED -> {
mBottomSheet = BottomSheetMessage.Builder()
.setText(R.string.drawer_update_text)
.setTitle(R.string.drawer_update_title)
.setPositiveText(R.string.Yes)
.setNegativeText(R.string.No)
.buildWithCallback(object : BottomSheetChoice {
override fun onPositiveButton() {
val versionBase = presenter.database.remoteVersion
if (versionBase != null && versionBase.isNewerVersion)
DownloaderService.downloadNewVersion(
this@MainActivity,
versionBase
)
else presenter.checkForUpdates(false)
}

override fun onNegativeButton() {

}
})
showBottomSheet()
}
PERMISSION_DENIED -> if (ActivityCompat.shouldShowRequestPermissionRationale(
this@MainActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
)
DialogUtil.createMessage(
this@MainActivity,
R.string.title_permission_write,
R.string.text_permission_write
) { _, which ->
when (which) {
DialogAction.POSITIVE -> ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_PERMISSION
)
DialogAction.NEGATIVE -> NotifyUtil.makeText(
this@MainActivity,
R.string.canceled_by_user,
Toast.LENGTH_SHORT
).show()
else -> {}
}
}
else
requestPermissionIfMissing(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
R.id.nav_check_update -> checkUpdate()
else -> { }
}
}
Expand Down Expand Up @@ -445,17 +389,21 @@ class MainActivity : ActivityBase<User, BasePresenter>(), View.OnClickListener,
}

override fun makeRequest() {
WorkManager.getInstance(this)
.getWorkInfosByTagLiveData(
KeyUtil.WorkUpdaterId
).observe(this) { workInfoList ->
val workInfo = workInfoList.firstOrNull { workInfo ->
workInfo.state == WorkInfo.State.SUCCEEDED
}
if (workInfo != null)
onUpdateChecked(
workInfo.outputData.getBoolean(KeyUtil.WorkUpdaterSilentId, false)
)
launchUpdateWorker(menuItems)
}

private fun checkNewInstallation() {
if (presenter.settings.isUpdated) {
DialogUtil.createChangeLog(this)
presenter.settings.setUpdated()
}
if (presenter.settings.isFreshInstall) {
presenter.settings.isFreshInstall = false
mBottomSheet = BottomSheetMessage.Builder()
.setText(R.string.app_intro_guide)
.setTitle(R.string.app_intro_title)
.setNegativeText(R.string.Ok).build()
showBottomSheet()
}
}

Expand All @@ -470,32 +418,6 @@ class MainActivity : ActivityBase<User, BasePresenter>(), View.OnClickListener,
}
}

private fun onLatestUpdateInstalled() {
NotifyUtil.createAlerter(
this@MainActivity,
getString(R.string.title_update_infodadat),
getString(R.string.app_no_date),
R.drawable.ic_cloud_done_white_24dp,
R.color.colorStateGreen
)
}

private fun onUpdateChecked(silent: Boolean) {
val remoteVersion = presenter.database.remoteVersion

if (remoteVersion != null) {
if (remoteVersion.isNewerVersion) {
// If a new version of the application is available on GitHub
val mAppUpdateWidget = menuItems.findItem(R.id.nav_check_update)
.actionView.findViewById<TextView>(R.id.app_update_info)
mAppUpdateWidget.text = getString(R.string.app_update, remoteVersion.version)
mAppUpdateWidget.visibility = View.VISIBLE
} else if (!silent) {
onLatestUpdateInstalled()
}
}
}

private fun setupUserItems() {
presenter.database.currentUser?.apply {
mUserName.text = name
Expand All @@ -520,24 +442,6 @@ class MainActivity : ActivityBase<User, BasePresenter>(), View.OnClickListener,
mHomeFeed.isVisible = true
}

/**
* Checks to see if this instance is a new installation
*/
private fun checkNewInstallation() {
if (presenter.settings.isUpdated) {
DialogUtil.createChangeLog(this)
presenter.settings.setUpdated()
}
if (presenter.settings.isFreshInstall) {
presenter.settings.isFreshInstall = false
mBottomSheet = BottomSheetMessage.Builder()
.setText(R.string.app_intro_guide)
.setTitle(R.string.app_intro_title)
.setNegativeText(R.string.Ok).build()
showBottomSheet()
}
}

override fun onClick(view: View) {
if (view.id == R.id.banner_clickable) {
if (presenter.settings.isAuthenticated) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
android:id="@+id/nav_check_update"
android:icon="@drawable/ic_cloud_download_white_24dp"
android:title="@string/drawer_title_update"
android:visible="@bool/display_update_channel_pref"
app:actionLayout="@layout/action_view_update" />
</menu>
</item>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
app:entryValues="@array/pref_values_channel_entries"
app:key="@string/pref_key_update_channel"
app:title="@string/pref_title_update_channel"
app:useSimpleSummaryProvider="true" />
app:useSimpleSummaryProvider="true"
app:isPreferenceVisible="@bool/display_update_channel_pref"/>

<SwitchPreference
app:defaultValue="false"
Expand Down

0 comments on commit 1b4c751

Please sign in to comment.