Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ allprojects {
project build.gradle
```groovy
dependencies {
commonMainApi("dev.icerock.moko:errors:0.5.0")
commonMainApi("dev.icerock.moko:errors:0.5.1")
}
```

Expand Down
2 changes: 1 addition & 1 deletion errors-build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {

dependencies {
api("dev.icerock:mobile-multiplatform:0.12.0")
api("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20")
api("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
api("com.android.tools.build:gradle:4.2.1")
api("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package dev.icerock.moko.errors.presenters

import android.view.View
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.google.android.material.snackbar.Snackbar
import dev.icerock.moko.resources.desc.StringDesc

Expand All @@ -14,14 +16,30 @@ actual class SnackBarErrorPresenter actual constructor(
) : ErrorPresenter<StringDesc> {

override fun show(throwable: Throwable, activity: FragmentActivity, data: StringDesc) {
val rootView = activity.findViewById<View>(android.R.id.content)?.rootView
?: activity.window?.decorView?.findViewById<View>(android.R.id.content)
if (rootView != null) {
Snackbar.make(
rootView,
data.toString(activity),
duration.toAndroidCode()
).show()
}
val decorView: View = if (activity.hasWindowFocus()) {
activity.window?.decorView
} else {
val dialogFragment = activity.supportFragmentManager.findActiveDialogFragment()
dialogFragment?.dialog?.window?.decorView
} ?: return
val contentView: View = decorView.findViewById(android.R.id.content) ?: return
val snackbar = Snackbar.make(
contentView,
data.toString(activity),
duration.toAndroidCode()
)
snackbar.show()
}

private fun FragmentManager.findActiveDialogFragment(): DialogFragment? {
val dialogFragment: DialogFragment? = fragments.filterIsInstance<DialogFragment>()
.filter { it.showsDialog }
.filter { it.isResumed }
.filter { it.dialog?.window?.decorView?.hasWindowFocus() == true }
.firstOrNull()

if (dialogFragment != null) return dialogFragment

return fragments.firstNotNullOfOrNull { it.childFragmentManager.findActiveDialogFragment() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ object ExceptionMappersStorage {
* class [T].
*/
inline fun <E : Throwable, reified T : Any> throwableMapper(): (e: E) -> T {
return throwableMapper(T::class)
return ExceptionMappersStorage.throwableMapper(T::class)
}
}

/**
* Factory method that allows getting exception description
*/
inline fun <reified E : Throwable, reified T : Any> E.mapThrowable(): T {
return ExceptionMappersStorage.throwableMapper<E, T>(T::class)(this)
}
/**
* Factory method that allows getting exception description
*/
inline fun <reified E : Throwable, reified T : Any> E.mapThrowable(): T {
return ExceptionMappersStorage.throwableMapper<E, T>(T::class)(this)
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[versions]
kotlinVersion = "1.5.20"
androidAppCompatVersion = "1.2.0"
materialDesignVersion = "1.0.0"
materialDesignVersion = "1.4.0"
androidLifecycleVersion = "2.1.0"
androidCoreTestingVersion = "2.1.0"
coroutinesVersion = "1.5.0-native-mt"
mokoMvvmVersion = "0.11.0"
mokoResourcesVersion = "0.16.0"
mokoErrorsVersion = "0.5.0"
mokoErrorsVersion = "0.5.1"

[libraries]
appCompat = { module = "androidx.appcompat:appcompat", version.ref = "androidAppCompatVersion" }
Expand Down
31 changes: 31 additions & 0 deletions sample/android-app/src/main/java/com/icerockdev/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
package com.icerockdev

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.icerockdev.databinding.ActivityMainBinding
import com.icerockdev.library.SimpleViewModel
import com.icerockdev.library.createSimpleViewModel
Expand Down Expand Up @@ -37,5 +42,31 @@ class MainActivity : MvvmActivity<ActivityMainBinding, SimpleViewModel>() {
binding.alertButton.setOnClickListener {
viewModel.onAlertButtonClick()
}
binding.bottomSheetButton.setOnClickListener {
showBottomSheet()
}
}

private fun showBottomSheet() {
BottomSheet().show(supportFragmentManager, "TAG")
}

class BottomSheet : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bottom_sheet, container)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<Button>(R.id.show_alert).setOnClickListener {
val viewModel: SimpleViewModel = (activity as MainActivity).viewModel
viewModel.onAlertButtonClick()
}
}
}
}
11 changes: 9 additions & 2 deletions sample/android-app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
type="com.icerockdev.library.SimpleViewModel" />
</data>

<FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
Expand All @@ -21,5 +21,12 @@
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Run alert error handler" />
</FrameLayout>

<Button
android:id="@+id/bottom_sheet_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Bottom sheet" />
</LinearLayout>
</layout>
13 changes: 13 additions & 0 deletions sample/android-app/src/main/res/layout/fragment_bottom_sheet.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<Button
android:id="@+id/show_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="show error" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

package com.icerockdev.library

import dev.icerock.moko.errors.MR
import dev.icerock.moko.errors.handler.ExceptionHandler
import dev.icerock.moko.errors.mappers.ExceptionMappersStorage
import dev.icerock.moko.errors.presenters.AlertErrorPresenter
import dev.icerock.moko.errors.presenters.SelectorErrorPresenter
import dev.icerock.moko.errors.presenters.SnackBarDuration
import dev.icerock.moko.errors.presenters.SnackBarErrorPresenter
import dev.icerock.moko.errors.presenters.ToastDuration
import dev.icerock.moko.errors.presenters.ToastErrorPresenter
import dev.icerock.moko.mvvm.livedata.LiveData
import dev.icerock.moko.mvvm.livedata.MutableLiveData
import dev.icerock.moko.mvvm.livedata.readOnly
import dev.icerock.moko.mvvm.viewmodel.ViewModel
import dev.icerock.moko.resources.desc.desc
import kotlinx.coroutines.launch
import kotlin.random.Random

fun createSimpleViewModel(): SimpleViewModel {
val alertErrorPresenter = AlertErrorPresenter(
alertTitle = MR.strings.moko_errors_presenters_alertDialogTitle.desc(),
positiveButtonText = MR.strings.moko_errors_presenters_alertPositiveButton.desc()
val snackBarErrorPresenter = SnackBarErrorPresenter(
duration = SnackBarDuration.SHORT
)
val toastErrorPresenter = ToastErrorPresenter(
duration = ToastDuration.LONG
Expand All @@ -31,7 +29,7 @@ fun createSimpleViewModel(): SimpleViewModel {
exceptionHandler = ExceptionHandler(
errorPresenter = SelectorErrorPresenter { throwable ->
when (throwable) {
is CustomException -> alertErrorPresenter
is CustomException -> snackBarErrorPresenter
else -> toastErrorPresenter
}
},
Expand Down