Added self-handling event types
This commit is contained in:
parent
1d64ddb7f5
commit
fe1ce08a6c
@ -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)
|
||||
|
||||
}
|
@ -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<ViewModel : CompatViewModel, Binding : ViewDataBinding> :
|
||||
@ -24,4 +25,10 @@ abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBin
|
||||
delegate.onResume()
|
||||
}
|
||||
|
||||
override fun onEventDispatched(event: ViewEvent) {
|
||||
super.onEventDispatched(event)
|
||||
|
||||
delegate.onEventExecute(event, this)
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,15 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.graphics.Insets
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.skoumal.teanity.viewevents.ViewEvent
|
||||
import com.topjohnwu.magisk.model.events.ActivityExecutor
|
||||
import com.topjohnwu.magisk.model.events.ContextExecutor
|
||||
import com.topjohnwu.magisk.model.events.FragmentExecutor
|
||||
import timber.log.Timber
|
||||
|
||||
class CompatDelegate internal constructor(
|
||||
private val view: CompatView<*>
|
||||
@ -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()
|
||||
|
@ -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<ViewModel : CompatViewModel, Binding : ViewDataBinding>
|
||||
@ -24,4 +25,10 @@ abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBin
|
||||
delegate.onResume()
|
||||
}
|
||||
|
||||
override fun onEventDispatched(event: ViewEvent) {
|
||||
super.onEventDispatched(event)
|
||||
|
||||
delegate.onEventExecute(event, this)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user