diff --git a/app/src/app/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt b/app/src/app/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt new file mode 100644 index 000000000..a1a636f5a --- /dev/null +++ b/app/src/app/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt @@ -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() +} \ No newline at end of file diff --git a/app/src/github/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt b/app/src/github/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt new file mode 100644 index 000000000..0542e9629 --- /dev/null +++ b/app/src/github/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt @@ -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(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) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/mxt/anitrend/service/DownloaderService.kt b/app/src/github/java/com/mxt/anitrend/service/DownloaderService.kt similarity index 100% rename from app/src/main/java/com/mxt/anitrend/service/DownloaderService.kt rename to app/src/github/java/com/mxt/anitrend/service/DownloaderService.kt