This file contains hidden or 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
enum class TransitionType { | |
SIBLING, DETAIL, MODAL | |
} | |
inline fun <reified T : Fragment> FragmentManager.handleReplace( | |
tag: String = T::class.java.name, | |
addToBackStack: Boolean = false, | |
@IdRes containerId: Int = R.id.fragment_container, | |
transitionType: TransitionType? = null, | |
crossinline newInstance: () -> T |
This file contains hidden or 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 BaseFragment<B : ViewDataBinding, VM : BaseViewModel>(@LayoutRes private val layoutResourceId: Int) : Fragment() { | |
private var realBinding: B? = null | |
protected val binding: B get() = realBinding ?: throw IllegalStateException("Trying to access the binding outside of the view lifecycle.") | |
protected abstract val viewModel: VM | |
final override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = | |
DataBindingUtil.inflate<B>(inflater, layoutResourceId, container, false).also { | |
realBinding = it | |
}.root |
This file contains hidden or 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 BaseFragment<B : ViewDataBinding, VM : BaseViewModel>(@LayoutRes private val layoutResourceId: Int) : Fragment() { | |
protected var binding by AutoClearedValue<B>() | |
protected abstract val viewModel: VM | |
final override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = | |
DataBindingUtil.inflate<B>(inflater, layoutResourceId, container, false).also { | |
binding = it | |
}.root |
This file contains hidden or 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 AutoClearedValue<T : Any> : ReadWriteProperty<Fragment, T>, LifecycleObserver { | |
private var _value: T? = null | |
override fun getValue(thisRef: Fragment, property: KProperty<*>): T = | |
_value ?: throw IllegalStateException("Trying to call an auto-cleared value outside of the view lifecycle.") | |
override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) { | |
thisRef.viewLifecycleOwner.lifecycle.removeObserver(this) | |
_value = value | |
thisRef.viewLifecycleOwner.lifecycle.addObserver(this) |
This file contains hidden or 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 MusicPlayerFragment : Fragment() { | |
private val playlistId by lazy { arguments.playlistId } | |
private val songIndex by lazy { arguments.songIndex } | |
companion object { | |
private var Bundle.playlistId by BundleDelegate.String("key_playlist_id") | |
private var Bundle.songIndex by BundleDelegate.Int("key_song_index") | |
fun newInstance(playlistId: String, songIndex: Int) = MusicPlayerFragment().apply { | |
arguments = Bundle().apply { |
This file contains hidden or 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 GameActivity : AppCompatActivity() { | |
private var Bundle.score by BundleDelegate.Int("key_score") | |
private var score = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
savedInstanceState?.let { score = it.score } | |
} | |
override fun onSaveInstanceState(outState: Bundle) { |
This file contains hidden or 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
sealed class Screen { | |
abstract val screenName: String | |
object Menu : Screen() { | |
override val screenName = "Menu screen" | |
} | |
object Leaderboard : Screen() { | |
override val screenName = "Leaderboard screen" | |
} |
This file contains hidden or 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 IntervalDelegate( | |
var value: Int, | |
val minValue: Int, | |
val maxValue: Int | |
) : ReadWriteProperty<Any?, Int> { | |
override fun getValue(thisRef: Any?, property: KProperty<*>) = value | |
override fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) { | |
this.value = value.coerceIn(minValue, maxValue) |
This file contains hidden or 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
sealed class BundleDelegate<T>(protected val key: kotlin.String) : ReadWriteProperty<Bundle, T> { | |
class Int(key: kotlin.String) : BundleDelegate<kotlin.Int>(key) { | |
override fun getValue(thisRef: Bundle, property: KProperty<*>) = thisRef.getInt(key) | |
override fun setValue(thisRef: Bundle, property: KProperty<*>, value: kotlin.Int) = thisRef.putInt(key, value) | |
} | |
class String(key: kotlin.String) : BundleDelegate<kotlin.String>(key) { |
NewerOlder