Skip to content

Commit

Permalink
[app] alarm engine fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Feb 2, 2023
1 parent 6dedec9 commit a234f1c
Show file tree
Hide file tree
Showing 14 changed files with 982 additions and 398 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package github.tornaco.android.thanos.module.compose.common

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp

@Composable
fun Int.toDp(): Dp {
return with(LocalDensity.current) {
this@toDp.toDp()
}
}

@Composable
fun Dp.toPx(): Float {
return with(LocalDensity.current) {
this@toPx.toPx()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* (C) Copyright 2022 Thanox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package github.tornaco.android.thanos.module.compose.common.widget

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import github.tornaco.android.thanos.module.compose.common.theme.ColorDefaults

@Composable
fun CardContainer(content: @Composable () -> Unit) {
androidx.compose.material.Card(
modifier = Modifier
.padding(horizontal = 16.dp, vertical = 8.dp),
backgroundColor = ColorDefaults.backgroundSurfaceColor(),
shape = RoundedCornerShape(12.dp),
elevation = 0.dp
) {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@

package github.tornaco.android.thanos.module.compose.common.widget

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.*
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.AlertDialogDefaults
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

@OptIn(ExperimentalComposeUiApi::class)
Expand Down Expand Up @@ -66,4 +70,43 @@ fun ThanoxAlertDialog(
usePlatformDefaultWidth = false
)
)
}


@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ThanoxDialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
title: @Composable () -> Unit = {},
buttons: @Composable RowScope.() -> Unit = {},
content: @Composable ColumnScope.() -> Unit,
) {
Dialog(onDismissRequest = onDismissRequest,
properties = DialogProperties(
dismissOnBackPress = properties.dismissOnBackPress,
dismissOnClickOutside = properties.dismissOnClickOutside,
securePolicy = properties.securePolicy,
usePlatformDefaultWidth = false
)) {
Surface(modifier = Modifier.fillMaxWidth(fraction = 0.82f),
shape = AlertDialogDefaults.shape,
color = AlertDialogDefaults.containerColor,
tonalElevation = AlertDialogDefaults.TonalElevation) {

Column(modifier = Modifier
.fillMaxWidth()
.padding(16.dp)) {
title()
Spacer(modifier = Modifier.size(16.dp))
content()
Row(modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically) {
buttons()
}
}

}
}
}
3 changes: 0 additions & 3 deletions android/modules/module_profile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
<activity
android:name=".engine.NewRegularIntervalActivity"
android:exported="false" />
<activity
android:name=".engine.NewAlarmActivity"
android:exported="false" />
<activity
android:name=".example.ProfileExampleActivity"
android:exported="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package github.tornaco.thanos.android.module.profile.engine

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Tag
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight.Companion.W600
import androidx.compose.ui.unit.dp
import github.tornaco.android.thanos.core.alarm.Alarm
import github.tornaco.android.thanos.core.alarm.Repeat
import github.tornaco.android.thanos.core.alarm.TimeOfADay
import github.tornaco.android.thanos.core.alarm.WeekDay
import github.tornaco.android.thanos.module.compose.common.theme.ColorDefaults
import github.tornaco.android.thanos.module.compose.common.widget.StandardSpacer
import github.tornaco.android.thanos.module.compose.common.widget.ThanoxDialog
import github.tornaco.thanos.android.module.profile.R
import github.tornaco.thanos.android.module.profile.engine.timepicker.BottomTimePicker
import java.time.LocalTime

class AlarmSelectorState(
initialTime: LocalTime,
initialRepeat: Array<WeekDay>,
val selected: (Alarm) -> Unit
) {
private var _isShow by mutableStateOf(false)
val isShow get() = _isShow

var time: LocalTime = initialTime

var repeat = mutableStateOf(initialRepeat.toList())

var tag by mutableStateOf("")

fun addRepeat(weekDay: WeekDay) {
repeat.value = repeat.value.toMutableList().apply {
add(weekDay)
}
}

fun removeRepeat(weekDay: WeekDay) {
repeat.value = repeat.value.toMutableList().apply {
remove(weekDay)
}
}

fun show() {
_isShow = true
}

fun dismiss() {
_isShow = false
}
}

@Composable
fun rememberAlarmSelectorState(
initialTime: LocalTime = LocalTime.now(),
initialRepeat: Array<WeekDay> = emptyArray(),
selected: (Alarm) -> Unit
): AlarmSelectorState {
return remember {
AlarmSelectorState(initialTime, initialRepeat, selected)
}
}

@Composable
fun AlarmSelector(state: AlarmSelectorState) {
if (state.isShow) {
ThanoxDialog(onDismissRequest = { state.dismiss() }, title = {
DialogTitle(text = stringResource(id = R.string.module_profile_date_time_alarm))
}, buttons = {
TextButton(onClick = {
state.selected(
Alarm(
label = state.tag,
triggerAt = TimeOfADay(
hour = state.time.hour,
minutes = state.time.minute,
seconds = state.time.second
),
repeat = Repeat(state.repeat.value)
)
)
state.dismiss()
}) {
Text(text = stringResource(id = android.R.string.ok))
}
}, content = {
AlarmContent(state)
})
}
}

@Composable
private fun AlarmContent(state: AlarmSelectorState) {
Column(
verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
) {
Tag(state = state)

BottomTimePicker(is24TimeFormat = true, currentTime = state.time) {
state.time = it
}
RepeatSelector(state)
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun Tag(state: AlarmSelectorState) {
Column(verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.Start) {
TextField(
label = {
Text(text = "Tag")
},
leadingIcon = {
Icon(
imageVector = Icons.Filled.Tag,
contentDescription = "tag"
)
},
value = state.tag, onValueChange = {
state.tag = it
})
Text(
text = stringResource(
id = R.string.module_profile_date_time_tag,
"\ncondition: \"timeTick && tag == \"${state.tag}\"\""
), style = MaterialTheme.typography.labelSmall
)
}
StandardSpacer()
}

@Composable
private fun RepeatSelector(state: AlarmSelectorState) {
Column(
modifier = Modifier.padding(top = 24.dp, bottom = 32.dp),
horizontalAlignment = Alignment.Start
) {
Text(
text = "Repeat",
style = MaterialTheme.typography.bodySmall
)
StandardSpacer()
Row(
modifier = Modifier
.horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
arrayOf(
WeekDay.SUNDAY,
WeekDay.MONDAY,
WeekDay.TUESDAY,
WeekDay.WEDNESDAY,
WeekDay.THURSDAY,
WeekDay.FRIDAY,
WeekDay.SATURDAY
).forEach { weekDay ->
val isSelected = state.repeat.value.contains(weekDay)
Column(
modifier = Modifier
.width(36.dp)
.height(36.dp)
.padding(2.dp)
.clip(CircleShape)
.background(color = if (isSelected) MaterialTheme.colorScheme.primaryContainer else ColorDefaults.backgroundSurfaceColor())
.clickable {
if (isSelected) {
state.removeRepeat(weekDay)
} else {
state.addRepeat(weekDay)
}
}
.padding(4.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = weekDay.name.first().toString().uppercase(),
style = MaterialTheme.typography.bodySmall.copy(fontWeight = W600)
)
}
}
}
}
}


@Composable
fun DialogTitle(text: String) {
Text(
modifier = Modifier.padding(8.dp),
text = text,
style = MaterialTheme.typography.titleLarge
)
}
Loading

0 comments on commit a234f1c

Please sign in to comment.