Added self-handling event types

This commit is contained in:
Viktor De Pasquale 2019-10-05 21:53:33 +02:00
parent 1d64ddb7f5
commit fe1ce08a6c
4 changed files with 58 additions and 0 deletions

View File

@ -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)
}

View File

@ -3,6 +3,7 @@ package com.topjohnwu.magisk.redesign.compat
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.databinding.ViewDataBinding import androidx.databinding.ViewDataBinding
import com.skoumal.teanity.viewevents.ViewEvent
import com.topjohnwu.magisk.ui.base.MagiskActivity import com.topjohnwu.magisk.ui.base.MagiskActivity
abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBinding> : abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBinding> :
@ -24,4 +25,10 @@ abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBin
delegate.onResume() delegate.onResume()
} }
override fun onEventDispatched(event: ViewEvent) {
super.onEventDispatched(event)
delegate.onEventExecute(event, this)
}
} }

View File

@ -1,8 +1,15 @@
package com.topjohnwu.magisk.redesign.compat package com.topjohnwu.magisk.redesign.compat
import android.graphics.Insets import android.graphics.Insets
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat 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( class CompatDelegate internal constructor(
private val view: CompatView<*> private val view: CompatView<*>
@ -12,6 +19,20 @@ class CompatDelegate internal constructor(
view.viewModel.requestRefresh() 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() { fun ensureInsets() {
ViewCompat.setOnApplyWindowInsetsListener(view.viewRoot) { _, insets -> ViewCompat.setOnApplyWindowInsetsListener(view.viewRoot) { _, insets ->
insets.asInsets() insets.asInsets()

View File

@ -3,6 +3,7 @@ package com.topjohnwu.magisk.redesign.compat
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.databinding.ViewDataBinding import androidx.databinding.ViewDataBinding
import com.skoumal.teanity.viewevents.ViewEvent
import com.topjohnwu.magisk.ui.base.MagiskFragment import com.topjohnwu.magisk.ui.base.MagiskFragment
abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBinding> abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBinding>
@ -24,4 +25,10 @@ abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBin
delegate.onResume() delegate.onResume()
} }
override fun onEventDispatched(event: ViewEvent) {
super.onEventDispatched(event)
delegate.onEventExecute(event, this)
}
} }