Skip to content

Commit

Permalink
Action sheet style added
Browse files Browse the repository at this point in the history
  • Loading branch information
avdyushin committed Nov 7, 2019
1 parent 4ec7084 commit 10df591
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 9 deletions.
20 changes: 16 additions & 4 deletions alerts/src/androidLibMain/kotlin/Alerts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,26 @@ actual class AlertInterface(
alertDialog = AlertDialog.Builder(context)
.setTitle(alert.title)
.setMessage(alert.message)
.create()
.apply {
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
action.handler()
if (alert.style == Alert.Style.ACTION_LIST) {
val actions = alert.actions.toTypedArray()
val titles = actions.map { it.title }.toTypedArray()
setItems(titles) { _, which ->
val action = alert.actions[which].apply { handler() }
afterHandler(action)
}
}
}
.create()
.apply {
if (alert.style == Alert.Style.ALERT) {
alert.actions.forEach { action ->
setButton(transform(action.style), action.title) { _, _ ->
action.handler()
afterHandler(action)
}
}
}
setOnDismissListener { alertDialog = null }
setOnCancelListener { afterHandler(null) }
show()
Expand Down
22 changes: 20 additions & 2 deletions alerts/src/commonMain/kotlin/Alerts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ typealias AlertActionHandler = () -> Unit
data class Alert(
val title: String?,
val message: String?,
val actions: List<Action>
val actions: List<Action>,
val style: Alert.Style = Style.ALERT
) {

/**
* Alert style
*/
enum class Style {
ALERT, ACTION_LIST
}

/**
* An action than represents a button in the alert
*
Expand Down Expand Up @@ -136,6 +145,7 @@ abstract class BaseAlertBuilder {
private var title: String? = null
private var message: String? = null
private var actions: MutableList<Alert.Action> = mutableListOf()
private var style: Alert.Style = Alert.Style.ALERT
private val mutex = Mutex()

/**
Expand Down Expand Up @@ -192,6 +202,13 @@ abstract class BaseAlertBuilder {
*/
fun addActions(actions: List<Alert.Action>) = apply { this.actions.addAll(actions) }

/**
* Sets a style of the alert
*
* @param style The style of an alert
*/
fun setStyle(style: Alert.Style) = apply { this.style = style }

/**
* Builds an alert using DSL syntax
*
Expand All @@ -218,6 +235,7 @@ abstract class BaseAlertBuilder {
this.title = null
this.message = null
this.actions.clear()
this.style = Alert.Style.ALERT
}

/**
Expand All @@ -230,7 +248,7 @@ abstract class BaseAlertBuilder {
require(title != null || message != null) { "Please set title and/or message for the Alert" }
require(actions.isNotEmpty()) { "Please set at least one Action for the Alert" }

return Alert(title, message, actions)
return Alert(title, message, actions, style)
}

/**
Expand Down
6 changes: 5 additions & 1 deletion alerts/src/iosMain/kotlin/Alerts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ actual class AlertInterface(
Alert.Action.Style.DESTRUCTIVE, Alert.Action.Style.NEUTRAL -> UIAlertActionStyleDestructive
Alert.Action.Style.CANCEL, Alert.Action.Style.NEGATIVE -> UIAlertActionStyleCancel
}
fun transform(style: Alert.Style): UIAlertControllerStyle = when (style) {
Alert.Style.ALERT -> UIAlertControllerStyleAlert
Alert.Style.ACTION_LIST -> UIAlertControllerStyleActionSheet
}
}

override fun dismissAlert(animated: Boolean) {
Expand All @@ -49,7 +53,7 @@ actual class AlertInterface(
UIAlertController.alertControllerWithTitle(
alert.title,
alert.message,
UIAlertControllerStyleAlert
transform(alert.style)
).apply {
alert.actions.forEach { action ->
addAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ class AlertsActivity : AppCompatActivity(R.layout.activity_alerts) {

btn_simple_alert.setOnClickListener { alertFactory.showAlert() }
btn_dismissible_alert.setOnClickListener { alertFactory.showAndDismissAfter(3) }
btn_alert_list.setOnClickListener { alertFactory.showList() }
}
}
6 changes: 6 additions & 0 deletions example/android/src/main/res/layout/activity_alerts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
android:layout_height="wrap_content"
android:text="@string/dismissible_alert"/>

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_alert_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/alert_list"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</androidx.core.widget.NestedScrollView>
1 change: 1 addition & 0 deletions example/android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<string name="app_name">Kaluga Example</string>
<string name="show_alert">Show Alert</string>
<string name="dismissible_alert">Dismissible Alert</string>
<string name="alert_list">Show Alert List</string>
<string name="show_loading_indicator">Show Loading Indicator</string>
</resources>
1 change: 1 addition & 0 deletions example/ios/Demo/Alerts/AlertsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AlertsViewController: UITableViewController {
switch indexPath.row {
case 0: alertFactory.showAlert()
case 1: alertFactory.showAndDismissAfter(timeSecs: 3)
case 2: alertFactory.showList()
default: ()
}
}
Expand Down
21 changes: 19 additions & 2 deletions example/ios/Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15400" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="czt-vE-Q3c">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="czt-vE-Q3c">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15404"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15509"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -228,6 +228,23 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="iW9-HH-Oc0" style="IBUITableViewCellStyleDefault" id="lMI-O8-dAX">
<rect key="frame" x="0.0" y="115" width="414" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="lMI-O8-dAX" id="jWN-Fu-MOI">
<rect key="frame" x="0.0" y="0.0" width="414" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Show Action Sheet" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="iW9-HH-Oc0">
<rect key="frame" x="20" y="0.0" width="374" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
Expand Down
13 changes: 13 additions & 0 deletions example/shared/src/commonMain/kotlin/AlertFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ class AlertFactory(private val builder: AlertBuilder) {
coroutine.cancel()
}
}

fun showList() = GlobalScope.launch(MainQueueDispatcher) {
build {
setTitle("Select an option")
setStyle(Alert.Style.ACTION_LIST)
addActions(listOf(
Alert.Action("Option 1") { log(LogLevel.DEBUG, "Option 1") },
Alert.Action("Option 2") { log(LogLevel.DEBUG, "Option 2") },
Alert.Action("Option 3") { log(LogLevel.DEBUG, "Option 3") },
Alert.Action("Option 4") { log(LogLevel.DEBUG, "Option 4") }
))
}.show()
}
}

0 comments on commit 10df591

Please sign in to comment.