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 androidx.fragment.app.Fragment
import com.topjohnwu.magisk.core.base.BaseActivity
import kotlinx.coroutines.CoroutineScope
/**
@ -20,7 +19,7 @@ interface ContextExecutor {
}
interface ActivityExecutor {
operator fun invoke(activity: BaseActivity)
operator fun invoke(activity: BaseUIActivity<*, *>)
}
interface FragmentExecutor {

View File

@ -7,14 +7,14 @@ import androidx.annotation.RequiresPermission
import androidx.navigation.NavDirections
import com.topjohnwu.magisk.MainDirections
import com.topjohnwu.magisk.arch.ActivityExecutor
import com.topjohnwu.magisk.arch.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.base.BaseActivity
class InstallExternalModuleEvent : ViewEvent(), ActivityExecutor {
@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)
intent.type = "application/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.BaseUIActivity
import com.topjohnwu.magisk.arch.ViewEvent
import com.topjohnwu.magisk.core.base.BaseActivity
import com.topjohnwu.magisk.utils.TransitiveText
class SnackbarEvent private constructor(
@ -35,7 +34,7 @@ class SnackbarEvent private constructor(
builder: Snackbar.() -> Unit = {}
) = Snackbar.make(view, message, length).apply(builder).show()
override fun invoke(activity: BaseActivity) {
override fun invoke(activity: BaseUIActivity<*, *>) {
if (activity is BaseUIActivity<*, *>) {
snackbar(activity.snackbarView,
msg.getText(activity.resources).toString(),

View File

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

View File

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

View File

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

View File

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

View File

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