Musicoo/app/src/main/java/relax/offline/music/fragment/MoBaseFragment.kt
2024-05-24 20:36:01 +08:00

57 lines
1.6 KiB
Kotlin

package relax.offline.music.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import relax.offline.music.sp.AppStore
import relax.offline.music.util.LogTag
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
abstract class MoBaseFragment<T : ViewBinding> : Fragment(), CoroutineScope by MainScope() {
enum class Event {
FragmentOnResume,
}
protected abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> T
protected lateinit var binding: T
protected val TAG = LogTag.VO_FRAGMENT_LOG
protected val appStore by lazy { AppStore(requireContext()) }
protected val events = Channel<Event>(Channel.UNLIMITED)
protected abstract suspend fun onViewCreated()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = bindingInflater(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
launch {
onViewCreated()
}
}
override fun onDestroyView() {
super.onDestroyView()
cancel()
}
override fun onDestroy() {
super.onDestroy()
cancel()
}
override fun onResume() {
super.onResume()
events.trySend(Event.FragmentOnResume)
}
}