Skip to content

Instantly share code, notes, and snippets.

@Psijic
Last active January 9, 2021 18:13
Show Gist options
  • Save Psijic/561eea45891b9d45f7b1f1008b472bd3 to your computer and use it in GitHub Desktop.
Save Psijic/561eea45891b9d45f7b1f1008b472bd3 to your computer and use it in GitHub Desktop.
Features Separating
class DynamicConfig {
companion object {
// Here could be your feature toggle request
var isTheFeatureAvailable = true
var isOtherFeatureAvailable = false
}
}
abstract class FeatureFragment : Fragment(), Featured {
private var onActivityCreatedAction: MutableList<((savedInstanceState: Bundle?)
-> Unit)>? = mutableListOf()
private var onActivityResultAction: MutableList<((
requestCode: Int, resultCode: Int, data: Intent?
) -> Unit)>? = mutableListOf()
override fun doOnActivityCreated(action: (savedInstanceState: Bundle?) -> Unit) {
onActivityCreatedAction?.add(action)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
onActivityCreatedAction?.forEach {
it.invoke(savedInstanceState)
}
}
override fun doOnActivityResult(action: (requestCode: Int, resultCode: Int,
data: Intent?) -> Unit) {
onActivityResultAction?.add(action)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
onActivityResultAction?.forEach {
it.invoke(requestCode, resultCode, data)
}
}
}
class MainFragment : Fragment() {
private var theFeature: TheFeature? = null
private val viewModel: MainFragmentViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (DynamicConfig.isTheFeatureAvailable)
theFeature = TheFeature(viewModel, this)
}
private fun someFunctionWhereTheFeatureMethodIsUsed(){
// ...
theFeature?.featureActions()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
theFeature?.onActivityCreated(savedInstanceState)
}
}
class MainFragmentAdvanced : FeatureFragment() {
private var theFeature: TheFinalFeature<MainFragmentAdvanced>? = null
}
class TheFeature(private val viewModel: MainFragmentViewModel,
private val fragment: Fragment) {
fun featureActions() {
// Some actions in the feature
viewModel.doSomeActions()
}
fun onActivityCreated(savedInstanceState: Bundle?) {
Toast.makeText(fragment.requireContext(), "Some feature text",
Toast.LENGTH_LONG).show()
}
}
class TheFinalFeature<T>(
private val viewModel: ViewModel,
private val fragment: T
) where T : Fragment, T : Featured {
init {
fragment.doOnActivityCreated(::onActivityCreated)
fragment.doOnActivityResult(::onActivityResult)
}
private fun onActivityCreated(savedInstanceState: Bundle?) {
//...
}
private fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//...
}
}
class TheUnfinishedFeature(private val viewModel: ViewModel,
private val fragment: Fragment) {
init {
fragment.onActivityCreated = onActivityCreated // How to do it?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment