generated from ajou4095/template-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
132 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
...ation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/common/base/BaseActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,17 @@ | ||
package ac.dnd.bookkeeping.android.presentation.common.base | ||
|
||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import androidx.lifecycle.viewModelScope | ||
import com.ray.rds.window.alert.AlertDialogFragmentProvider | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
abstract class BaseActivity : AppCompatActivity() { | ||
|
||
protected abstract val viewModel: BaseViewModel | ||
|
||
protected val handler = CoroutineExceptionHandler { _, throwable -> | ||
Timber.e(throwable) | ||
AlertDialogFragmentProvider.makeAlertDialog( | ||
title = throwable.javaClass.simpleName, | ||
message = throwable.message | ||
).show() | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initView() | ||
initObserver() | ||
observeViewModelError() | ||
} | ||
|
||
protected open fun initView() = Unit | ||
|
||
protected open fun initObserver() = Unit | ||
|
||
private fun observeViewModelError() { | ||
repeatOnStarted { | ||
viewModel.errorEvent.eventObserve { event -> | ||
handler.handleException(viewModel.viewModelScope.coroutineContext, event.throwable) | ||
} | ||
} | ||
} | ||
|
||
fun DialogFragment.show() { | ||
if ( | ||
!this@BaseActivity.isFinishing | ||
&& !this@BaseActivity.isDestroyed | ||
&& !this@BaseActivity.supportFragmentManager.isDestroyed | ||
&& !this@BaseActivity.supportFragmentManager.isStateSaved | ||
) { | ||
show(this@BaseActivity.supportFragmentManager, javaClass.simpleName) | ||
} | ||
} | ||
|
||
protected fun repeatOnStarted( | ||
block: suspend CoroutineScope.() -> Unit | ||
) { | ||
this.lifecycleScope.launch(handler) { | ||
repeatOnLifecycle(Lifecycle.State.STARTED, block) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...tation/src/main/kotlin/ac/dnd/bookkeeping/android/presentation/common/util/ComposeUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ac.dnd.bookkeeping.android.presentation.common.util | ||
|
||
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel | ||
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve | ||
import ac.dnd.bookkeeping.android.presentation.common.view.DialogScreen | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import io.sentry.Sentry | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
@Composable | ||
fun ErrorObserver( | ||
viewModel: BaseViewModel | ||
) { | ||
val dialogIsShowingState = remember { mutableStateOf(false) } | ||
DialogScreen(dialogIsShowingState) | ||
|
||
LaunchedEffectWithLifecycle(viewModel.errorEvent) { | ||
viewModel.errorEvent.eventObserve { event -> | ||
Timber.d(event.throwable) | ||
Sentry.captureException(event.throwable) | ||
|
||
dialogIsShowingState.value = true | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun LaunchedEffectWithLifecycle( | ||
key1: Any?, | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
block: suspend CoroutineScope.() -> Unit | ||
) { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
LaunchedEffect(key1) { | ||
lifecycleOwner.lifecycleScope.launch( | ||
coroutineContext + context | ||
) { | ||
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED, block) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.