Skip to content

Instantly share code, notes, and snippets.

View pandulapeter's full-sized avatar

Pandula Péter pandulapeter

  • Cluj-Napoca
View GitHub Profile
@pandulapeter
pandulapeter / SharedPrefManager.kt
Last active September 19, 2018 01:22
SharedPrefManager
class SharedPrefManager(context: Context) {
private val preferences = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
var booleanField by PreferenceFieldDelegate.Boolean("boolean_field")
var intField by PreferenceFieldDelegate.Int("int_field")
var stringField by PreferenceFieldDelegate.String("string_field")
private sealed class PreferenceFieldDelegate<T>(protected val key: kotlin.String) : ReadWriteProperty<SharedPrefManager, T> {
class Boolean(key: kotlin.String) : PreferenceFieldDelegate<kotlin.Boolean>(key) {
@pandulapeter
pandulapeter / BundleDelegate.kt
Last active February 20, 2018 13:34
BundleDelegate
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) {
@pandulapeter
pandulapeter / IntervalDelegate.kt
Created February 20, 2018 13:33
IntervalDelegate
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)
@pandulapeter
pandulapeter / SealedClass.kt
Created February 20, 2018 13:37
Sealed class
sealed class Screen {
abstract val screenName: String
object Menu : Screen() {
override val screenName = "Menu screen"
}
object Leaderboard : Screen() {
override val screenName = "Leaderboard screen"
}
@pandulapeter
pandulapeter / GameActivity.kt
Last active February 20, 2018 13:49
Example usage of BundleDelegate
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) {
@pandulapeter
pandulapeter / MusicPlayerFragment.kt
Created February 20, 2018 13:50
Example usage of BundleDelegate
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 {
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)
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
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
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