Skip to content

Commit

Permalink
Move out self updating outside of playstore variant
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed Apr 8, 2022
1 parent 6fc113f commit 181a76f
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mxt.anitrend.base.custom.activity

import android.content.Context
import android.view.Menu
import androidx.fragment.app.FragmentActivity
import timber.log.Timber
import com.mxt.anitrend.view.activity.index.MainActivity

private fun Context.unsupportedFeature() {
Timber.i("$packageName does not support checking updates, migrate to play services")
}

private fun FragmentActivity.onLatestUpdateInstalled() {
unsupportedFeature()
}

private fun FragmentActivity.onUpdateChecked(silent: Boolean, menuItems: Menu) {
unsupportedFeature()
}

fun FragmentActivity.launchUpdateWorker(menuItems: Menu) {
unsupportedFeature()
}

fun MainActivity.checkUpdate() {
unsupportedFeature()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.mxt.anitrend.base.custom.activity

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.view.Menu
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.work.WorkInfo
import androidx.work.WorkManager
import com.afollestad.materialdialogs.DialogAction
import com.mxt.anitrend.R
import com.mxt.anitrend.base.interfaces.event.BottomSheetChoice
import com.mxt.anitrend.service.DownloaderService
import com.mxt.anitrend.util.DialogUtil
import com.mxt.anitrend.util.KeyUtil
import com.mxt.anitrend.util.NotifyUtil
import timber.log.Timber
import com.mxt.anitrend.view.activity.index.MainActivity
import com.mxt.anitrend.view.sheet.BottomSheetMessage

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

private fun MainActivity.onUpdateChecked(silent: Boolean, menuItems: Menu) {
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()
}
}
}

fun MainActivity.launchUpdateWorker(menuItems: Menu) {
WorkManager.getInstance(this)
.getWorkInfosByTagLiveData(
KeyUtil.WorkUpdaterId
).observe(this) { workInfoList ->
workInfoList.firstOrNull { workInfo ->
workInfo.state == WorkInfo.State.SUCCEEDED
}?.run {
onUpdateChecked(
outputData.getBoolean(
KeyUtil.WorkUpdaterSilentId,
false
),
menuItems
)
}
}
}

fun MainActivity.checkUpdate() {
when (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)) {
PackageManager.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@checkUpdate,
versionBase
)
else presenter.checkForUpdates(false)
}

override fun onNegativeButton() {

}
})
showBottomSheet()
}
PackageManager.PERMISSION_DENIED -> if (ActivityCompat.shouldShowRequestPermissionRationale(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
)
DialogUtil.createMessage(
this,
R.string.title_permission_write,
R.string.text_permission_write
) { _, which ->
when (which) {
DialogAction.POSITIVE -> ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
ActivityBase.REQUEST_PERMISSION
)
DialogAction.NEGATIVE -> NotifyUtil.makeText(
this,
R.string.canceled_by_user,
Toast.LENGTH_SHORT
).show()
else -> {}
}
}
else
requestPermissionIfMissing(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
}

0 comments on commit 181a76f

Please sign in to comment.