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 BetterDialog<T> : DialogFragment() { | |
companion object { | |
val KEY_ARGS = "KEY_ARGS" | |
val KEY_CALLED_FROM_FRAGMENT = "KEY_CALLED_FROM_FRAGMENT" | |
val TAG = "DIALOG_TAG" | |
} | |
@Suppress("UNCHECKED_CAST") | |
private val args: DialogBuilderImpl.DialogArgs<T> by lazy { | |
checkNotNull(arguments?.getSerializable(KEY_ARGS) as DialogBuilderImpl.DialogArgs<T>) { "Args are 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
private class DialogBuilderImpl<T>(val context: Context) : DialogBuilder<T> { | |
data class DialogArgs<T>( | |
val title: String? = null, | |
val message: String? = null, | |
val yesButtonAction: Serializable? = null, | |
val noButtonAction: Serializable? = null, | |
val yesButtonText: String? = null, | |
val noButtonText: String? = null | |
) : Serializable |
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
private typealias DialogCallback<T> = T.() -> Unit | |
interface DialogBuilder<T> { | |
fun title(title: String) | |
fun title(titleResId: Int) | |
fun message(message: String) | |
fun message(messageResId: Int) |
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 BetterActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val button = findViewById<Button>(R.id.button) | |
button.setOnClickListener { showBetterDialog() } | |
val normalButton = findViewById<Button>(R.id.normalButton) | |
normalButton.setOnClickListener { launchNormalActivity() } |
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 NormalActivity : AppCompatActivity(), NormalDialogFragment.NormalDialogListener { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_normal) | |
val button = findViewById<Button>(R.id.normalButton) | |
button.setOnClickListener { showNormalDialog() } | |
} | |
private fun showNormalDialog() = |
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
/** | |
* Provides a single instance of a class requiring a context. Can be inherited by a companion | |
* object to easily make it a singleton | |
*/ | |
abstract class SingletonWithContext<T> { | |
@Volatile | |
private var instance: T? = null | |
fun getInstance(context: Context): T = | |
instance ?: synchronized(this) { |
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
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.CoordinatorLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.design.widget.AppBarLayout | |
android:id="@+id/appbar" | |
android:layout_width="match_parent" |
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
private fun scanForActivity(context: Context?): FragmentActivity? = when (context) { | |
is FragmentActivity -> context | |
is ContextWrapper -> scanForActivity(context.baseContext) | |
else -> throw IllegalArgumentException("Context must be a FragmentActivity!") | |
} |
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
@Suppress("UNCHECKED_CAST") | |
private inline fun <reified T : ViewModel> createViewModel(key: String, crossinline initializer: () -> T): T { | |
val factory = object : ViewModelProvider.NewInstanceFactory() { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T = initializer() as T | |
} | |
return ViewModelProviders.of(activity, factory).get(key, T::class.java) | |
} |
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 MyView(context: Context) : FrameLayout(context) { | |
private val viewModel by lazy { setupViewModel() } | |
private val activity by lazy { scanForActivity(context) } | |
private fun scanForActivity(context: Context?): FragmentActivity = when (context) { | |
is FragmentActivity -> context | |
is ContextWrapper -> scanForActivity(context.baseContext) | |
else -> throw IllegalArgumentException("Context must be a FragmentActivity!") | |
} |
NewerOlder