Skip to content

Commit

Permalink
[app] Regular interval ui of the DateTimeEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed May 25, 2022
1 parent 9da5e4e commit fbe68e6
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface IProfileManager {

void addRuleIfNotExists(String ruleJson, in IRuleAddCallback callback, int format);

void publishStringFact(String factValue, long delayMills);
void publishStringFact(int source, String factValue, long delayMills);

void updateRule(int ruleId, String ruleJson, in IRuleAddCallback callback, int format);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static class Default implements github.tornaco.android.thanos.core.profil
@Override public void addRuleIfNotExists(java.lang.String ruleJson, github.tornaco.android.thanos.core.profile.IRuleAddCallback callback, int format) throws android.os.RemoteException
{
}
@Override public void publishStringFact(java.lang.String factValue, long delayMills) throws android.os.RemoteException
@Override public void publishStringFact(int source, java.lang.String factValue, long delayMills) throws android.os.RemoteException
{
}
@Override public void updateRule(int ruleId, java.lang.String ruleJson, github.tornaco.android.thanos.core.profile.IRuleAddCallback callback, int format) throws android.os.RemoteException
Expand Down Expand Up @@ -564,11 +564,13 @@ public static github.tornaco.android.thanos.core.profile.IProfileManager asInter
case TRANSACTION_publishStringFact:
{
data.enforceInterface(descriptor);
java.lang.String _arg0;
_arg0 = data.readString();
long _arg1;
_arg1 = data.readLong();
this.publishStringFact(_arg0, _arg1);
int _arg0;
_arg0 = data.readInt();
java.lang.String _arg1;
_arg1 = data.readString();
long _arg2;
_arg2 = data.readLong();
this.publishStringFact(_arg0, _arg1, _arg2);
reply.writeNoException();
return true;
}
Expand Down Expand Up @@ -1415,17 +1417,18 @@ public java.lang.String getInterfaceDescriptor()
_data.recycle();
}
}
@Override public void publishStringFact(java.lang.String factValue, long delayMills) throws android.os.RemoteException
@Override public void publishStringFact(int source, java.lang.String factValue, long delayMills) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(source);
_data.writeString(factValue);
_data.writeLong(delayMills);
boolean _status = mRemote.transact(Stub.TRANSACTION_publishStringFact, _data, _reply, 0);
if (!_status && getDefaultImpl() != null) {
getDefaultImpl().publishStringFact(factValue, delayMills);
getDefaultImpl().publishStringFact(source, factValue, delayMills);
return;
}
_reply.readException();
Expand Down Expand Up @@ -1743,7 +1746,7 @@ public static github.tornaco.android.thanos.core.profile.IProfileManager getDefa
public java.lang.String getAutoConfigTemplateSelectionId() throws android.os.RemoteException;
public boolean applyConfigTemplateForPackage(java.lang.String packageName, github.tornaco.android.thanos.core.profile.ConfigTemplate template) throws android.os.RemoteException;
public void addRuleIfNotExists(java.lang.String ruleJson, github.tornaco.android.thanos.core.profile.IRuleAddCallback callback, int format) throws android.os.RemoteException;
public void publishStringFact(java.lang.String factValue, long delayMills) throws android.os.RemoteException;
public void publishStringFact(int source, java.lang.String factValue, long delayMills) throws android.os.RemoteException;
public void updateRule(int ruleId, java.lang.String ruleJson, github.tornaco.android.thanos.core.profile.IRuleAddCallback callback, int format) throws android.os.RemoteException;
public void registerRuleChangeListener(github.tornaco.android.thanos.core.profile.IRuleChangeListener listener) throws android.os.RemoteException;
public void unRegisterRuleChangeListener(github.tornaco.android.thanos.core.profile.IRuleChangeListener listener) throws android.os.RemoteException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class ProfileManager {
public static final int RULE_FORMAT_JSON = 0;
public static final int RULE_FORMAT_YAML = 1;

public static final int FACT_SOURCE_SHORTCUT = 1;
public static final int FACT_SOURCE_DATE_TIME = 2;

public static final String PROFILE_AUTO_APPLY_NEW_INSTALLED_APPS_CONFIG_TEMPLATE_PACKAGE_PREFIX
= "thanox.config.template.";

Expand Down Expand Up @@ -201,8 +204,8 @@ public boolean applyConfigTemplateForPackage(String packageName, ConfigTemplate
}

@SneakyThrows
public void publishStringFact(String factValue, long delayMills) {
server.publishStringFact(factValue, delayMills);
public void publishStringFact(int source, String factValue, long delayMills) {
server.publishStringFact(source, factValue, delayMills);
}

@SneakyThrows
Expand Down
2 changes: 1 addition & 1 deletion android/internal/Thanox-Internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* (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.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Remove
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.window.DialogProperties
import github.tornaco.android.thanos.module.compose.common.LargeSpacer
import github.tornaco.android.thanos.module.compose.common.StandardSpacer
import kotlin.time.Duration


class DurationPickerDialogState(
val title: String,
val onDurationPick: (Duration) -> Unit
) {
private var _isShowing: Boolean by mutableStateOf(false)
val isShowing get() = _isShowing

fun show() {
_isShowing = true
}

fun dismiss() {
_isShowing = false
}
}

@Composable
fun rememberDurationPickerDialogState(
title: String,
onDurationPick: (Duration) -> Unit
): DurationPickerDialogState {
return remember {
DurationPickerDialogState(title, onDurationPick)
}
}

@Composable
fun DurationPickerDialog(state: DurationPickerDialogState) {
if (state.isShowing) {
AlertDialog(
title = {
Text(text = state.title)
},
text = {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.Start
) {
Hour()
StandardSpacer()
Minute()
StandardSpacer()
Second()
}
},
confirmButton = {},
onDismissRequest = {
state.dismiss()
},
properties = DialogProperties()
)
}
}

@Composable
private fun Hour() {
Field(
"Hour",
0,
999
)
}

@Composable
private fun Minute() {
Field(
"Minute",
0,
59
)
}

@Composable
private fun Second() {
Field(
"Second",
0,
59
)
}

@Composable
private fun Field(label: String, min: Int = 0, max: Int) {
var value by remember {
mutableStateOf(min)
}
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Text(text = label)
LargeSpacer()

// -
IconButton(onClick = {
if (value > min) {
value -= 1
} else {
value = max
}
}) {
Icon(
imageVector = Icons.Filled.Remove,
contentDescription = "-"
)
}

StandardSpacer()
Text(text = "$value")
StandardSpacer()

// +
IconButton(onClick = {
if (value < max) {
value += 1
} else {
value = min
}
}) {
Icon(
imageVector = Icons.Filled.Add,
contentDescription = "+"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SingleChoiceDialogState(
}

@Composable
fun rememberDialogState(
fun rememberSingleChoiceDialogState(
title: String,
items: List<SingleChoiceItem>,
onItemClick: (String) -> Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package github.tornaco.thanos.android.module.profile;

import static github.tornaco.android.thanos.core.profile.ProfileManager.FACT_SOURCE_SHORTCUT;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -39,7 +41,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
String factValue = getIntent().getStringExtra(EXTRA_PROFILE_FACT_VALUE);
if (!TextUtils.isEmpty(factValue)) {
XLog.i("publish factValue= %s", factValue);
ThanosManager.from(this).ifServiceInstalled(thanosManager -> thanosManager.getProfileManager().publishStringFact(factValue, 0));
ThanosManager.from(this).ifServiceInstalled(thanosManager -> thanosManager.getProfileManager().publishStringFact(FACT_SOURCE_SHORTCUT, factValue, 0));
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,43 @@ import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Timer
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import github.tornaco.android.thanos.module.compose.common.theme.TypographyDefaults
import github.tornaco.android.thanos.module.compose.common.widget.*
import github.tornaco.thanos.android.module.profile.R

private class UIState {
var showCreateRegularIntervalDialog by mutableStateOf(false)
}
private const val ID_TIME_OF_A_DAY = "tod"
private const val ID_REGULAR_INTERVAL = "ri"

@Composable
fun Activity.DateTimeEngineScreen() {
val uiState = rememberUIState()
val durationPickerDialogState = rememberDurationPickerDialogState(title = "Regular interval") {

val dialogState =
rememberDialogState(title = stringResource(id = R.string.module_profile_rule_new),
}


val typeSelectDialogState =
rememberSingleChoiceDialogState(title = stringResource(id = R.string.module_profile_rule_new),
items = listOf(
SingleChoiceItem(id = "", icon = Icons.Filled.Schedule, label = "Time of a day (coming soon)"),
SingleChoiceItem(id = "", icon = Icons.Filled.Timer, label = "Regular interval (coming soon)"),
SingleChoiceItem(
id = ID_TIME_OF_A_DAY,
icon = Icons.Filled.Schedule,
label = "Time of a day (coming soon)"
),
SingleChoiceItem(
id = ID_REGULAR_INTERVAL,
icon = Icons.Filled.Timer,
label = "Regular interval"
),
),
onItemClick = {
uiState.showCreateRegularIntervalDialog = true
when (it) {
ID_REGULAR_INTERVAL -> {
durationPickerDialogState.show()
}
ID_TIME_OF_A_DAY -> {}
}
})

ThanoxSmallAppBarScaffold(title = {
Expand All @@ -66,26 +81,11 @@ fun Activity.DateTimeEngineScreen() {
contentDescription = stringResource(id = R.string.module_profile_rule_new)
)
}) {
dialogState.show()
typeSelectDialogState.show()
}
}) { contentPadding ->

SingleChoiceDialog(state = dialogState)
}
}


@Composable
private fun CreateRegularIntervalDialog(uiState: UIState) {
if (uiState.showCreateRegularIntervalDialog) {
// TODO Impl.
}
}


@Composable
private fun rememberUIState(): UIState {
return remember {
UIState()
SingleChoiceDialog(state = typeSelectDialogState)
DurationPickerDialog(state = durationPickerDialogState)
}
}
}
Loading

0 comments on commit fbe68e6

Please sign in to comment.