Skip to content

Commit

Permalink
Added logger to AlertPresenter so dialog actions can be logged.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristoferAlexander committed Oct 10, 2023
1 parent 470e3fc commit bda35fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions alerts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kotlin {
dependencies {
implementation(project(":architecture", ""))
implementation(project(":base", ""))
implementation(project(":logging", ""))
implementation(project(":resources", ""))
}
}
Expand Down
27 changes: 23 additions & 4 deletions alerts/src/androidLibMain/kotlin/AlertPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import android.widget.LinearLayout
import com.splendo.kaluga.architecture.lifecycle.ActivityLifecycleSubscribable
import com.splendo.kaluga.architecture.lifecycle.LifecycleManagerObserver
import com.splendo.kaluga.base.utils.applyIf
import com.splendo.kaluga.logging.Logger
import com.splendo.kaluga.logging.RestrictedLogLevel
import com.splendo.kaluga.logging.RestrictedLogger
import com.splendo.kaluga.logging.info
import com.splendo.kaluga.resources.dpToPixel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -43,6 +47,7 @@ import kotlinx.coroutines.launch
actual class AlertPresenter(
private val alert: Alert,
private val lifecycleManagerObserver: LifecycleManagerObserver = LifecycleManagerObserver(),
private val logger: Logger,
coroutineScope: CoroutineScope,
) : BaseAlertPresenter(alert), CoroutineScope by coroutineScope {

Expand All @@ -52,6 +57,7 @@ actual class AlertPresenter(
*/
actual class Builder(
private val lifecycleManagerObserver: LifecycleManagerObserver = LifecycleManagerObserver(),
private val logger: Logger = RestrictedLogger(RestrictedLogLevel.None),
) : BaseAlertPresenter.Builder(), ActivityLifecycleSubscribable by lifecycleManagerObserver {

/**
Expand All @@ -62,7 +68,7 @@ actual class AlertPresenter(
* @return The created [AlertPresenter]
*/
actual override fun create(alert: Alert, coroutineScope: CoroutineScope) =
AlertPresenter(alert, lifecycleManagerObserver, coroutineScope)
AlertPresenter(alert, lifecycleManagerObserver, logger, coroutineScope)
}

private companion object {
Expand Down Expand Up @@ -96,9 +102,14 @@ actual class AlertPresenter(
}.collect { contextPresentation ->
when (val dialogPresentation = contextPresentation.second) {
is DialogPresentation.Showing -> contextPresentation.first?.activity?.let {
logger.info(TAG, "Displaying alert dialog with title: ${alert.title}")
presentDialog(it, dialogPresentation)
} ?: run { alertDialog = null }
is DialogPresentation.Hidden -> alertDialog?.dismiss()

is DialogPresentation.Hidden -> {
logger.info(TAG, "Dismissing alert dialog with title: ${alert.title}")
alertDialog?.dismiss()
}
}
}
}
Expand All @@ -123,7 +134,10 @@ actual class AlertPresenter(
.applyIf(alert.style == Alert.Style.ACTION_LIST) {
val titles = alert.actions.map { it.title }.toTypedArray()
setItems(titles) { _, which ->
val action = alert.actions[which].apply { handler() }
val action = alert.actions[which].apply {
logger.info(TAG, "Action ${this.title} was called on dialog with title: ${alert.title}")
handler()
}
presentation.afterHandler(action)
}
}
Expand All @@ -138,6 +152,7 @@ actual class AlertPresenter(
}
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
logger.info(TAG, "Action ${action.title} was called on dialog with title: ${alert.title}")
action.handler()
presentation.afterHandler(action)
}
Expand All @@ -146,6 +161,7 @@ actual class AlertPresenter(
.applyIf(alert.style == Alert.Style.ALERT) {
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
logger.info(TAG, "Action ${action.title} was called on dialog with title: ${alert.title}")
action.handler()
presentation.afterHandler(action)
}
Expand All @@ -156,7 +172,10 @@ actual class AlertPresenter(
alertDialog = null
this@AlertPresenter.presentation.value = DialogPresentation.Hidden
}
setOnCancelListener { presentation.afterHandler(null) }
setOnCancelListener {
logger.info(TAG, "Canceling alert dialog with title: ${alert.title}")
presentation.afterHandler(null)
}
show()
}
presentation.completion()
Expand Down
4 changes: 4 additions & 0 deletions alerts/src/commonMain/kotlin/Alerts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ interface AlertActions {
*/
abstract class BaseAlertPresenter(private val alert: Alert) : AlertActions {

companion object {
const val TAG = "AlertDialog"
}

/**
* Abstract alert builder class, used to create a [BaseAlertPresenter].
*
Expand Down
22 changes: 18 additions & 4 deletions alerts/src/iosMain/kotlin/AlertPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Copyright 2022 Splendo Consulting B.V. The Netherlands

package com.splendo.kaluga.alerts

import com.splendo.kaluga.logging.Logger
import com.splendo.kaluga.logging.RestrictedLogLevel
import com.splendo.kaluga.logging.RestrictedLogger
import com.splendo.kaluga.logging.info
import kotlinx.cinterop.ObjCAction
import kotlinx.coroutines.CoroutineScope
import platform.Foundation.NSString
Expand All @@ -44,7 +48,8 @@ import platform.objc.sel_registerName
actual class AlertPresenter(
private val alert: Alert,
private val parent: UIViewController,
) : BaseAlertPresenter(alert) {
private val logger: Logger,
) : BaseAlertPresenter(alert) {

/** Ref to alert's [UITextField] of type [Alert.Style.TEXT_INPUT] */
private var textField: UITextField? = null
Expand Down Expand Up @@ -85,7 +90,10 @@ actual class AlertPresenter(
* A [BaseAlertPresenter.Builder] for creating an [AlertPresenter]
* @param viewController The [UIViewController] to present any [AlertPresenter] built using this builder.
*/
actual class Builder(private val viewController: UIViewController) : BaseAlertPresenter.Builder() {
actual class Builder(
private val viewController: UIViewController,
private val logger: Logger = RestrictedLogger(RestrictedLogLevel.None)
) : BaseAlertPresenter.Builder() {

/**
* Creates an [AlertPresenter]
Expand All @@ -94,7 +102,7 @@ actual class AlertPresenter(
* @param coroutineScope The [CoroutineScope] managing the alert lifecycle.
* @return The created [AlertPresenter]
*/
actual override fun create(alert: Alert, coroutineScope: CoroutineScope) = AlertPresenter(alert, viewController)
actual override fun create(alert: Alert, coroutineScope: CoroutineScope) = AlertPresenter(alert, viewController, logger)
}

override fun dismissAlert(animated: Boolean) {
Expand All @@ -117,6 +125,7 @@ actual class AlertPresenter(
action.title,
transform(action.style),
) {
logger.info(TAG, "Action ${action.title} was called on dialog with title: ${alert.title}")
action.handler()
afterHandler(action)
},
Expand All @@ -132,6 +141,7 @@ actual class AlertPresenter(
NSString.localizedStringWithFormat("Cancel"),
UIAlertActionStyleCancel,
) {
logger.info(TAG, "Canceling alert dialog with title: ${alert.title}")
afterHandler(null)
},
)
Expand All @@ -143,7 +153,11 @@ actual class AlertPresenter(
}
}
}.run {
parent.presentViewController(this, animated, completion)
logger.info(TAG, "Displaying alert dialog with title: ${alert.title}")
parent.presentViewController(this, animated) {
logger.info(TAG, "Dismissing alert dialog with title: ${alert.title}")
completion()
}
}
}

Expand Down

0 comments on commit bda35fd

Please sign in to comment.