diff --git a/app/src/main/java/com/topjohnwu/magisk/model/events/EventExecutors.kt b/app/src/main/java/com/topjohnwu/magisk/model/events/EventExecutors.kt new file mode 100644 index 000000000..8039a38e6 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/model/events/EventExecutors.kt @@ -0,0 +1,23 @@ +package com.topjohnwu.magisk.model.events + +import android.content.Context +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.Fragment + +interface ContextExecutor { + + operator fun invoke(context: Context) + +} + +interface ActivityExecutor { + + operator fun invoke(activity: AppCompatActivity) + +} + +interface FragmentExecutor { + + operator fun invoke(fragment: Fragment) + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt index 8aef13008..4ba995450 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt @@ -3,6 +3,7 @@ package com.topjohnwu.magisk.redesign.compat import android.os.Bundle import android.view.View import androidx.databinding.ViewDataBinding +import com.skoumal.teanity.viewevents.ViewEvent import com.topjohnwu.magisk.ui.base.MagiskActivity abstract class CompatActivity : @@ -24,4 +25,10 @@ abstract class CompatActivity @@ -12,6 +19,20 @@ class CompatDelegate internal constructor( view.viewModel.requestRefresh() } + fun onEventExecute(event: ViewEvent, activity: AppCompatActivity) { + (event as? ContextExecutor)?.invoke(activity) + (event as? ActivityExecutor)?.invoke(activity) + (event as? FragmentExecutor)?.let { + Timber.e("Cannot run ${FragmentExecutor::class.java.simpleName} in Activity. Consider adding ${ContextExecutor::class.java.simpleName} as fallback.") + } + } + + fun onEventExecute(event: ViewEvent, fragment: Fragment) { + (event as? ContextExecutor)?.invoke(fragment.requireContext()) + (event as? FragmentExecutor)?.invoke(fragment) + (event as? ActivityExecutor)?.invoke(fragment.requireActivity() as AppCompatActivity) + } + fun ensureInsets() { ViewCompat.setOnApplyWindowInsetsListener(view.viewRoot) { _, insets -> insets.asInsets() diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt index 52bd1c65b..8089f9cd8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt @@ -3,6 +3,7 @@ package com.topjohnwu.magisk.redesign.compat import android.os.Bundle import android.view.View import androidx.databinding.ViewDataBinding +import com.skoumal.teanity.viewevents.ViewEvent import com.topjohnwu.magisk.ui.base.MagiskFragment abstract class CompatFragment @@ -24,4 +25,10 @@ abstract class CompatFragment