Skip to content

Commit

Permalink
Merge pull request #357 from skaengus2012/feature/issue342
Browse files Browse the repository at this point in the history
Make Global ErrorHandler
  • Loading branch information
skaengus2012 authored Dec 25, 2024
2 parents 99c3c68 + c062ad4 commit fd48f09
Show file tree
Hide file tree
Showing 65 changed files with 876 additions and 272 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ android {

dependencies {
implementation(projects.core.android)
implementation(projects.core.androidx.compose)
implementation(projects.core.androidx.fragment)
implementation(projects.core.androidx.lifecycle)
implementation(projects.core.androidx.navigationCompose)
implementation(projects.core.androidx.recyclerview)
implementation(projects.core.androidx.transition)
implementation(projects.core.annotation)
implementation(projects.core.component.tag)
implementation(projects.core.component.usermessage)
implementation(projects.core.component.usermessageHandle)
implementation(projects.core.dataDi)
implementation(projects.core.data)
implementation(projects.core.designsystem)
Expand All @@ -90,7 +93,6 @@ dependencies {
implementation(projects.core.statekit)
kapt(projects.statekit.compiler)
implementation(projects.core.translation)
implementation(projects.core.uiCompose)
implementation(projects.core.uitext)

implementation(libs.kotlinx.coroutines.android)
Expand Down
4 changes: 2 additions & 2 deletions app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.nlab.reminder.internal.common.android.startup.init.FlipperInitializer"
android:name="com.nlab.reminder.apps.startup.init.FlipperInitializer"
android:value="androidx.startup" />
<meta-data
android:name="com.nlab.reminder.internal.common.android.startup.init.TimberInitializer"
android:name="com.nlab.reminder.apps.startup.init.TimberInitializer"
android:value="androidx.startup" />
</provider>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.nlab.reminder.internal.common.android.startup.init
package com.nlab.reminder.apps.startup.init

import android.content.Context
import androidx.startup.Initializer
Expand All @@ -27,7 +27,7 @@ import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
import com.facebook.flipper.plugins.leakcanary2.FlipperLeakEventListener
import com.facebook.flipper.plugins.leakcanary2.LeakCanary2FlipperPlugin
import com.facebook.soloader.SoLoader
import com.nlab.reminder.internal.common.android.startup.EmptyDependencies
import com.nlab.reminder.apps.startup.EmptyDependencies
import leakcanary.LeakCanary

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

package com.nlab.reminder.internal.common.android.startup.init
package com.nlab.reminder.apps.startup.init

import android.content.Context
import androidx.startup.Initializer
import com.nlab.reminder.internal.common.android.startup.EmptyDependencies
import com.nlab.reminder.apps.startup.EmptyDependencies
import timber.log.Timber

/**
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".ReminderApplication"
android:name=".PlaneatApplication"
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -47,10 +47,10 @@
android:exported="false"
tools:node="merge">
<meta-data
android:name="com.nlab.reminder.internal.common.android.startup.init.CoilInitializer"
android:name="com.nlab.reminder.apps.startup.init.CoilInitializer"
android:value="androidx.startup" />
<meta-data
android:name="com.nlab.reminder.internal.common.android.startup.init.StateKitPluginInitializer"
android:name="com.nlab.reminder.apps.startup.init.StateKitPluginInitializer"
android:value="androidx.startup" />
</provider>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ import dagger.hilt.android.HiltAndroidApp
*/
@ExcludeFromGeneratedTestReport
@HiltAndroidApp
class ReminderApplication : Application()
class PlaneatApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@file:Suppress("FunctionName")

package com.nlab.reminder.internal.common.android.startup
package com.nlab.reminder.apps.startup

import androidx.startup.Initializer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

@file:Suppress("unused")

package com.nlab.reminder.internal.common.android.startup.init
package com.nlab.reminder.apps.startup.init

import android.content.Context
import androidx.startup.Initializer
import coil.Coil
import coil.ImageLoader
import coil.disk.DiskCache
import coil.memory.MemoryCache
import com.nlab.reminder.internal.common.android.startup.EmptyDependencies
import com.nlab.reminder.apps.startup.EmptyDependencies

/**
* @author Doohyun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,43 @@
*/
@file:Suppress("unused")

package com.nlab.reminder.internal.common.android.startup.init
package com.nlab.reminder.apps.startup.init

import android.content.Context
import androidx.startup.Initializer
import com.nlab.reminder.core.statekit.plugins.StateKitPlugin
import com.nlab.reminder.internal.common.android.startup.EmptyDependencies
import com.nlab.reminder.apps.startup.EmptyDependencies
import com.nlab.reminder.core.component.usermessage.UserMessageException
import com.nlab.reminder.core.component.usermessage.handle.di.getUserMessageBroadcastMonitor
import com.nlab.reminder.core.component.usermessage.handle.impl.UserMessageBroadcastMonitor
import kotlinx.coroutines.CancellationException
import timber.log.Timber

/**
* @author Doohyun
*/
internal class StateKitPluginInitializer : Initializer<Unit> {
override fun create(context: Context) {
StateKitPlugin.addGlobalExceptionHandler { _, throwable -> Timber.tag("StateKitGlobalErr").e(throwable) }
val tag = "StateKitGlobalErr"
val userMessageBroadcastMonitor: UserMessageBroadcastMonitor =
context.getUserMessageBroadcastMonitor()

StateKitPlugin.addGlobalExceptionHandler { _, throwable ->
when (throwable) {
is UserMessageException -> {
Timber.tag(tag).e(throwable.origin)
userMessageBroadcastMonitor.send(userMessage = throwable.userMessage)
}

is CancellationException -> {
// do nothing
}

else -> {
Timber.tag(tag).e(throwable)
}
}
}
}

override fun dependencies() = EmptyDependencies()
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/nlab/reminder/apps/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.nlab.reminder.core.android.widget.Toast
import com.nlab.reminder.core.designsystem.compose.theme.PlaneatTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

/**
* @author Doohyun
*/
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var appToast: Toast

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)

enableEdgeToEdge()
setContent {
val appState = rememberPlaneatAppState()
val appState = rememberPlaneatAppState(
appToast = appToast
)
PlaneatTheme {
PlaneatApp(appState = appState)
}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/nlab/reminder/apps/ui/PlaneatApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ package com.nlab.reminder.apps.ui
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.nlab.reminder.core.component.usermessage.handle.ui.UserMessageHandler

/**
* @author Thalys
*/
@Composable
fun PlaneatApp(
appState: PlaneatAppState
) {
fun PlaneatApp(appState: PlaneatAppState) {
PlaneatNavHost(
modifier = Modifier.fillMaxSize(),
appState = appState
)
UserMessageHandler(
showApplicationToast = { message -> appState.showApplicationToast(message) }
)
}
16 changes: 12 additions & 4 deletions app/src/main/java/com/nlab/reminder/apps/ui/PlaneatAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.nlab.reminder.core.android.widget.Toast

/**
* @author Thalys
*/
@Stable
class PlaneatAppState(
val navController: NavHostController
)
val navController: NavHostController,
private val appToast: Toast,
) {
fun showApplicationToast(message: String) {
appToast.showToast(text = message)
}
}

@Composable
fun rememberPlaneatAppState(
navController: NavHostController = rememberNavController()
navController: NavHostController = rememberNavController(),
appToast: Toast,
): PlaneatAppState = PlaneatAppState(
navController = navController
navController = navController,
appToast = appToast
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.nlab.reminder.domain.feature.home

import com.nlab.reminder.core.component.tag.edit.TagEditState
import com.nlab.reminder.core.text.UiText
import com.nlab.reminder.core.data.model.Tag
import com.nlab.reminder.core.kotlin.NonNegativeLong
import com.nlab.statekit.annotation.UiAction
Expand All @@ -35,11 +34,6 @@ internal sealed class HomeAction private constructor() {

data class TagEditStateSynced(val state: TagEditState?) : HomeAction()

data class UserMessagePosted(val message: UiText) : HomeAction()

@UiAction
data class UserMessageShown(val message: UiText) : HomeAction()

@UiAction
data object Interacted : HomeAction()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.nlab.reminder.domain.feature.home

import com.nlab.reminder.core.translation.StringIds
import com.nlab.reminder.core.text.UiText
import com.nlab.reminder.core.kotlin.onFailure
import com.nlab.reminder.core.component.usermessage.getOrThrowMessage
import com.nlab.statekit.dsl.reduce.DslReduce
import com.nlab.statekit.reduce.Reduce
import com.nlab.reminder.domain.feature.home.HomeAction.*
Expand All @@ -37,8 +35,7 @@ internal fun HomeReduce(environment: HomeEnvironment): HomeReduce = DslReduce {
timetableScheduleCount = action.timetableSchedulesCount,
allScheduleCount = action.allSchedulesCount,
tags = action.sortedTags,
interaction = HomeInteraction.Empty,
userMessages = emptyList()
interaction = HomeInteraction.Empty
)
}
transition<Success> {
Expand Down Expand Up @@ -79,7 +76,7 @@ internal fun HomeReduce(environment: HomeEnvironment): HomeReduce = DslReduce {
suspendEffect<OnTagLongClicked> {
environment.tagEditDelegate
.startEditing(tag = action.tag)
.onFailure { dispatch(UserMessagePosted(UiText(StringIds.tag_not_found))) }
.getOrThrowMessage()
}
}
scope(isMatch = { current.interaction is HomeInteraction.TagEdit }) {
Expand All @@ -89,23 +86,21 @@ internal fun HomeReduce(environment: HomeEnvironment): HomeReduce = DslReduce {
suspendEffect<OnTagRenameConfirmClicked> {
environment.tagEditDelegate
.tryUpdateTagName(current.tags)
.onFailure { dispatch(UserMessagePosted(UiText(StringIds.unknown_error))) }
.getOrThrowMessage()
}
suspendEffect<OnTagReplaceConfirmClicked> {
environment.tagEditDelegate
.mergeTag()
.onFailure { dispatch(UserMessagePosted(UiText(StringIds.unknown_error))) }
.getOrThrowMessage()
}
effect<OnTagReplaceCancelClicked> { environment.tagEditDelegate.cancelMergeTag() }
effect<OnTagDeleteRequestClicked> { environment.tagEditDelegate.startDelete() }
suspendEffect<OnTagDeleteConfirmClicked> {
environment.tagEditDelegate
.deleteTag()
.onFailure { dispatch(UserMessagePosted(UiText(StringIds.unknown_error))) }
.getOrThrowMessage()
}
}
transition<UserMessagePosted> { current.copy(userMessages = current.userMessages + action.message) }
transition<UserMessageShown> { current.copy(userMessages = current.userMessages - action.message) }
transition<Interacted> { current.copy(interaction = HomeInteraction.Empty) }
effect<Interacted> {
if (current.interaction is HomeInteraction.TagEdit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.nlab.reminder.domain.feature.home

import com.nlab.reminder.core.data.model.Tag
import com.nlab.reminder.core.kotlin.NonNegativeLong
import com.nlab.reminder.core.text.UiText

/**
* @author Doohyun
Expand All @@ -32,6 +31,5 @@ internal sealed class HomeUiState private constructor() {
val allScheduleCount: NonNegativeLong,
val tags: List<Tag>,
val interaction: HomeInteraction,
val userMessages: List<UiText>
) : HomeUiState()
}
Loading

0 comments on commit fd48f09

Please sign in to comment.