Change ActivityExecutor signature

This commit is contained in:
topjohnwu 2020-08-19 04:40:35 -07:00
parent 551a478fdc
commit 5ed4071f74
8 changed files with 37 additions and 47 deletions

View File

@ -2,7 +2,6 @@ package com.topjohnwu.magisk.arch
import android.content.Context import android.content.Context
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.topjohnwu.magisk.core.base.BaseActivity
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
/** /**
@ -20,7 +19,7 @@ interface ContextExecutor {
} }
interface ActivityExecutor { interface ActivityExecutor {
operator fun invoke(activity: BaseActivity) operator fun invoke(activity: BaseUIActivity<*, *>)
} }
interface FragmentExecutor { interface FragmentExecutor {

View File

@ -7,14 +7,14 @@ import androidx.annotation.RequiresPermission
import androidx.navigation.NavDirections import androidx.navigation.NavDirections
import com.topjohnwu.magisk.MainDirections import com.topjohnwu.magisk.MainDirections
import com.topjohnwu.magisk.arch.ActivityExecutor import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.base.BaseActivity
class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor { class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor {
@RequiresPermission(allOf = [Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE]) @RequiresPermission(allOf = [Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE])
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "application/zip" intent.type = "application/zip"
activity.startActivityForResult(intent, Const.ID.FETCH_ZIP) activity.startActivityForResult(intent, Const.ID.FETCH_ZIP)

View File

@ -6,7 +6,6 @@ import com.google.android.material.snackbar.Snackbar
import com.topjohnwu.magisk.arch.ActivityExecutor import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.utils.TransitiveText import com.topjohnwu.magisk.utils.TransitiveText
class SnackbarEvent private constructor( class SnackbarEvent private constructor(
@ -35,7 +34,7 @@ class SnackbarEvent private constructor(
builder: Snackbar.() -> Unit = {} builder: Snackbar.() -> Unit = {}
) = Snackbar.make(view, message, length).apply(builder).show() ) = Snackbar.make(view, message, length).apply(builder).show()
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
if (activity is BaseUIActivity<*, *>) { if (activity is BaseUIActivity<*, *>) {
snackbar(activity.snackbarView, snackbar(activity.snackbarView,
msg.getText(activity.resources).toString(), msg.getText(activity.resources).toString(),

View File

@ -11,7 +11,7 @@ import com.topjohnwu.magisk.view.MarkDownWindow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ViewActionEvent(val action: BaseActivity.() -> Unit) : ViewEvent(), ActivityExecutor { class ViewActionEvent(val action: BaseActivity.() -> Unit) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) = action(activity) override fun invoke(activity: BaseUIActivity<*, *>) = action(activity)
} }
class OpenChangelogEvent(val item: Repo) : ViewEventWithScope(), ContextExecutor { class OpenChangelogEvent(val item: Repo) : ViewEventWithScope(), ContextExecutor {
@ -27,7 +27,7 @@ class PermissionEvent(
private val callback: (Boolean) -> Unit private val callback: (Boolean) -> Unit
) : ViewEvent(), ActivityExecutor { ) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) = override fun invoke(activity: BaseUIActivity<*, *>) =
activity.withPermissions(*permissions.toTypedArray()) { activity.withPermissions(*permissions.toTypedArray()) {
onSuccess { onSuccess {
callback(true) callback(true)
@ -39,25 +39,25 @@ class PermissionEvent(
} }
class BackPressEvent : ViewEvent(), ActivityExecutor { class BackPressEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
activity.onBackPressed() activity.onBackPressed()
} }
} }
class DieEvent : ViewEvent(), ActivityExecutor { class DieEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
activity.finish() activity.finish()
} }
} }
class RecreateEvent : ViewEvent(), ActivityExecutor { class RecreateEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
activity.recreate() activity.recreate()
} }
} }
class RequestFileEvent : ViewEvent(), ActivityExecutor { class RequestFileEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
Intent(Intent.ACTION_GET_CONTENT) Intent(Intent.ACTION_GET_CONTENT)
.setType("*/*") .setType("*/*")
.addCategory(Intent.CATEGORY_OPENABLE) .addCategory(Intent.CATEGORY_OPENABLE)
@ -76,7 +76,7 @@ class RequestFileEvent : ViewEvent(), ActivityExecutor {
class NavigationEvent( class NavigationEvent(
private val directions: NavDirections private val directions: NavDirections
) : ViewEvent(), ActivityExecutor { ) : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
(activity as? BaseUIActivity<*, *>)?.apply { (activity as? BaseUIActivity<*, *>)?.apply {
directions.navigate() directions.navigate()
} }

View File

@ -1,8 +1,8 @@
package com.topjohnwu.magisk.events.dialog package com.topjohnwu.magisk.events.dialog
import com.topjohnwu.magisk.arch.ActivityExecutor import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.core.utils.BiometricHelper
class BiometricDialog( class BiometricDialog(
@ -16,7 +16,7 @@ class BiometricDialog(
builder(Builder()) builder(Builder())
} }
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
BiometricHelper.authenticate( BiometricHelper.authenticate(
activity, activity,
onError = listenerOnFailure, onError = listenerOnFailure,

View File

@ -1,50 +1,46 @@
package com.topjohnwu.magisk.events.dialog package com.topjohnwu.magisk.events.dialog
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.MagiskDialog
import java.lang.ref.WeakReference
class DarkThemeDialog : DialogEvent(), ActivityExecutor { class DarkThemeDialog : DialogEvent() {
private var activity: WeakReference<Activity>? = null
override fun invoke(activity: BaseActivity) {
this.activity = WeakReference(activity)
}
override fun build(dialog: MagiskDialog) { override fun build(dialog: MagiskDialog) {
val activity = dialog.context.unwrap()
dialog.applyTitle(R.string.settings_dark_mode_title) dialog.applyTitle(R.string.settings_dark_mode_title)
.applyMessage(R.string.settings_dark_mode_message) .applyMessage(R.string.settings_dark_mode_message)
.applyButton(MagiskDialog.ButtonType.POSITIVE) { .applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = R.string.settings_dark_mode_light titleRes = R.string.settings_dark_mode_light
icon = R.drawable.ic_day icon = R.drawable.ic_day
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_NO) } onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_NO, activity) }
} }
.applyButton(MagiskDialog.ButtonType.NEUTRAL) { .applyButton(MagiskDialog.ButtonType.NEUTRAL) {
titleRes = R.string.settings_dark_mode_system titleRes = R.string.settings_dark_mode_system
icon = R.drawable.ic_day_night icon = R.drawable.ic_day_night
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) } onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, activity) }
} }
.applyButton(MagiskDialog.ButtonType.NEGATIVE) { .applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = R.string.settings_dark_mode_dark titleRes = R.string.settings_dark_mode_dark
icon = R.drawable.ic_night icon = R.drawable.ic_night
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_YES) } onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_YES, activity) }
}
.onDismiss {
activity?.clear()
activity = null
} }
} }
private fun selectTheme(mode: Int) { private fun Context.unwrap(): Activity {
return when(this) {
is Activity -> this
is ContextWrapper -> baseContext.unwrap()
else -> error("Cannot happen")
}
}
private fun selectTheme(mode: Int, activity: Activity) {
Config.darkTheme = mode Config.darkTheme = mode
activity?.get()?.recreate() activity.recreate()
} }
} }

View File

@ -1,16 +1,16 @@
package com.topjohnwu.magisk.events.dialog package com.topjohnwu.magisk.events.dialog
import android.content.Context import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.ContextExecutor import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.view.MagiskDialog import com.topjohnwu.magisk.view.MagiskDialog
abstract class DialogEvent : ViewEvent(), ContextExecutor { abstract class DialogEvent : ViewEvent(), ActivityExecutor {
protected lateinit var dialog: MagiskDialog protected lateinit var dialog: MagiskDialog
override fun invoke(context: Context) { override fun invoke(activity: BaseUIActivity<*, *>) {
dialog = MagiskDialog(context).apply(this::build).reveal() dialog = MagiskDialog(activity).apply(this::build).reveal()
} }
abstract fun build(dialog: MagiskDialog) abstract fun build(dialog: MagiskDialog)

View File

@ -5,13 +5,9 @@ import androidx.databinding.Bindable
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.ActivityExecutor import com.topjohnwu.magisk.arch.*
import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.arch.itemBindingOf
import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.core.download.DownloadSubject.Manager import com.topjohnwu.magisk.core.download.DownloadSubject.Manager
import com.topjohnwu.magisk.core.download.RemoteFileService import com.topjohnwu.magisk.core.download.RemoteFileService
import com.topjohnwu.magisk.core.model.MagiskJson import com.topjohnwu.magisk.core.model.MagiskJson
@ -118,7 +114,7 @@ class HomeViewModel(
val showTest = false val showTest = false
fun onTestPressed() = object : ViewEvent(), ActivityExecutor { fun onTestPressed() = object : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseActivity) { override fun invoke(activity: BaseUIActivity<*, *>) {
/* Entry point to trigger test events within the app */ /* Entry point to trigger test events within the app */
} }
}.publish() }.publish()