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 / 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 / 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 / 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 / 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")
}
)
}
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 / ParcelableExample.kt
Created February 12, 2022 18:42
ParcelableExample.kt
@Parcelize
data class Example(val id: String, val name: String) : Parcelable
@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 / 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 / FirstScreenViewModel.kt
Created August 8, 2021 15:48
FirstScreenViewModel.kt
class FirstScreenViewModel(
private val navigator: Navigator
) : ViewModel {
...
fun navigateExample() {
navigator.navigate(NavigationActions.FirstScreen.firstScreenToSecondScreen())
}
@k0siara
k0siara / LifecycleAwareState.kt
Created August 8, 2021 15:44
LifecycleAwareState.kt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.flow.Flow
@Composable