Last active
January 9, 2021 18:13
-
-
Save Psijic/561eea45891b9d45f7b1f1008b472bd3 to your computer and use it in GitHub Desktop.
Features Separating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DynamicConfig { | |
companion object { | |
// Here could be your feature toggle request | |
var isTheFeatureAvailable = true | |
var isOtherFeatureAvailable = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Featured { | |
fun doOnActivityCreated(action: (savedInstanceState: Bundle?) -> Unit) | |
fun doOnActivityResult(action: (requestCode: Int, resultCode: Int, | |
data: Intent?) -> Unit) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainFragmentAdvanced : FeatureFragment() { | |
private var theFeature: TheFinalFeature<MainFragmentAdvanced>? = null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) { | |
//... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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