Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Patryk Kosieradzki k0siara

🏠
Working from home
View GitHub Profile
@k0siara
k0siara / ChooseAttachmentFromStorageUseCase.kt
Created Nov 5, 2022
ChooseAttachmentFromStorageUseCase.kt
View ChooseAttachmentFromStorageUseCase.kt
class ChooseAttachmentFromStorageUseCase @Inject constructor(...) {
suspend fun invoke(...) {
val attachmentSource = showPickerBottomSheet(data = ...)
...
}
}
View MyViewModel.kt
class MyViewModel : ViewModel(), ... {
fun onSomeAction() {
viewModelScope.launch {
...
val dialogResult = showTryAgainDialog(data = ...)
...
}
}
}
View HomeFragment.kt
class HomeFragment : Fragment(R.layout.home_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
findNavController()
.currentBackStackEntry
?.savedStateHandle
?.getLiveData<String>("my-result-key")
?.observe(viewLifecycleOwner) { result ->
View MyDialogFragment.kt
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok)) { _, _ ->
findNavController()
.previousBackStackEntry
?.savedStateHandle?.set("my-result-key", "example-data")
}
@k0siara
k0siara / ActivityHandling3Dialogs.kt
Created Nov 5, 2022
ActivityHandling3Dialogs.kt
View ActivityHandling3Dialogs.kt
class MainActivity : FragmentActivity(),
NoticeDialogFragment.NoticeDialogListener,
SecondDialogFragment.Listener,
ThirdDialogFragment.Listener {
private val viewModel by ...
fun showNoticeDialog() {
val dialog = NoticeDialogFragment()
dialog.show(supportFragmentManager, "NoticeDialogFragment")
View NoticeDialogFragment.kt
class NoticeDialogFragment : DialogFragment() {
internal lateinit var listener: NoticeDialogListener
interface NoticeDialogListener {
fun onDialogPositiveClick(dialog: DialogFragment)
fun onDialogNegativeClick(dialog: DialogFragment)
}
override fun onAttach(context: Context) {
super.onAttach(context)
View BetterMyFragment.kt
class MyFragment : Fragment(R.layout.my_layout) {
fun onSomeAction() {
coroutineScope.launch {
// Code A
when (val dialogResult = showDialogAndAwaitResult()) {
DialogResult.Ok -> {
// Code C
}
View MyFragment.kt
class MyFragment : Fragment(R.layout.my_layout) {
fun onSomeAction() {
// Code A
showDialog(
onOkClicked = { ... },
onCancelClicked = { ... }
)
@k0siara
k0siara / HomeFragment.kt
Last active Nov 5, 2022
HomeFragment.kt
View HomeFragment.kt
class HomeFragment : Fragment(R.layout.home_fragment) {
private val binding by viewBinding<HomeFragmentBinding>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {
findNavController().navigate(HomeFragmentDirections.toMyDialog())
}
@k0siara
k0siara / home_nav_graph.xml
Last active Nov 5, 2022
home_nav_graph.xml
View home_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="org.example.HomeFragment"