Skip to content

Instantly share code, notes, and snippets.

View k0siara's full-sized avatar
🧐
Investigating a bug

Patryk Kosieradzki k0siara

🧐
Investigating a bug
View GitHub Profile
@k0siara
k0siara / NavHostExample.kt
Created February 11, 2022 23:08
NavHostExample.kt
NavHost(startDestination = "profile/{userId}") {
...
composable(
"profile/{userId}",
arguments = listOf(navArgument("userId") { type = NavType.StringType })
) {...}
}
@k0siara
k0siara / NavHostExampleExtraction.kt
Created February 11, 2022 23:11
NavHostExampleExtraction.kt
composable(
"profile?userId={userId}",
arguments = listOf(navArgument("userId") { defaultValue = "me" })
) { backStackEntry ->
Profile(backStackEntry.arguments?.getString("userId")) { friendUserId ->
navController.navigate("profile?userId=$friendUserId")
}
}
@k0siara
k0siara / ParcelableExample.kt
Created February 12, 2022 18:42
ParcelableExample.kt
@Parcelize
data class Example(val id: String, val name: String) : Parcelable
class ExampleNavType : NavType<Example>(isNullableAllowed = false) {
override fun get(bundle: Bundle, key: String): Example? {
return bundle.getParcelable(key)
}
override fun parseValue(value: String): Example {
return Gson().fromJson(value, Example::class.java)
}
override fun put(bundle: Bundle, key: String, value: Example) {
@k0siara
k0siara / ExampleNavHostWithCustomNavType.kt
Last active February 12, 2022 18:56
ExampleNavHostWithCustomNavType.kt
NavHost(...) {
composable("home") {
Home(
onClick = {
val example = Example("1", "Example")
val json = Uri.encode(Gson().toJson(example))
navController.navigate("details/$json")
}
)
}
@k0siara
k0siara / FlowObserver.kt
Created November 4, 2022 10:07
FlowObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@k0siara
k0siara / AlertDialogBuilderExample.kt
Created November 5, 2022 20:35
AlertDialogBuilderExample.kt
AlertDialog.Builder(context)
.setPositiveButton(R.string.ok) { dialog, id ->
// User clicked OK button
}
.setNegativeButton(R.string.cancel) { dialog, id ->
// User cancelled the dialog
}
.create()
.show()
@k0siara
k0siara / CustomAlertDialog.kt
Created November 5, 2022 20:45
CustomAlertDialog.kt
class CustomAlertDialog : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
AlertDialog.Builder(requireContext())
        .setMessage(getString(R.string.order_confirmation))
            .setPositiveButton(getString(R.string.ok)) { _,_ -> }
            .create()
}
}
@k0siara
k0siara / CustomDialog.kt
Last active November 6, 2022 09:58
CustomDialog.kt
class CustomDialog : DialogFragment(R.layout.custom_dialog) {
private val binding by viewBinding<CustomDialogBinding>()
private val viewModel by viewModels<CustomDialogViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Setup UI and other stuff ...
}
}
@k0siara
k0siara / FragmentManagerExample.kt
Created November 5, 2022 20:51
FragmentManagerExample.kt
fun confirmStartGame() {
    val newFragment = StartGameDialogFragment()
    newFragment.show(supportFragmentManager, "game")
}