-
Notifications
You must be signed in to change notification settings - Fork 94
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
6 changed files
with
212 additions
and
0 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
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
123 changes: 123 additions & 0 deletions
123
android/app/src/main/java/now/fortuitous/thanos/launchother/LaunchOtherAppRuleActivity.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,123 @@ | ||
package now.fortuitous.thanos.launchother | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.google.accompanist.swiperefresh.SwipeRefresh | ||
import com.google.accompanist.swiperefresh.SwipeRefreshIndicator | ||
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import github.tornaco.android.thanos.module.compose.common.ComposeThemeActivity | ||
import github.tornaco.android.thanos.module.compose.common.DisposableEffectWithLifeCycle | ||
import github.tornaco.android.thanos.module.compose.common.theme.TypographyDefaults | ||
import github.tornaco.android.thanos.module.compose.common.widget.ListItem | ||
import github.tornaco.android.thanos.module.compose.common.widget.TextInputDialog | ||
import github.tornaco.android.thanos.module.compose.common.widget.ThanoxSmallAppBarScaffold | ||
import github.tornaco.android.thanos.module.compose.common.widget.rememberTextInputState | ||
|
||
@AndroidEntryPoint | ||
class LaunchOtherAppRuleActivity : ComposeThemeActivity() { | ||
companion object { | ||
@JvmStatic | ||
fun start(context: Context) { | ||
val starter = Intent(context, LaunchOtherAppRuleActivity::class.java) | ||
context.startActivity(starter) | ||
} | ||
} | ||
|
||
@Composable | ||
override fun Content() { | ||
val viewModel = hiltViewModel<LaunchOtherAppRuleViewModel>() | ||
val state by viewModel.state.collectAsState() | ||
|
||
val inputDialog = rememberTextInputState( | ||
title = stringResource(id = github.tornaco.android.thanos.R.string.menu_title_rules), | ||
message = "" | ||
) { | ||
viewModel.add(it) | ||
} | ||
TextInputDialog(state = inputDialog) | ||
|
||
ThanoxSmallAppBarScaffold( | ||
title = { | ||
Text( | ||
text = stringResource(id = github.tornaco.android.thanos.R.string.menu_title_rules), | ||
style = TypographyDefaults.appBarTitleTextStyle() | ||
) | ||
}, | ||
onBackPressed = { | ||
thisActivity().finish() | ||
}, | ||
actions = { | ||
IconButton(onClick = { | ||
inputDialog.show() | ||
}) { | ||
Icon( | ||
painter = painterResource(id = github.tornaco.android.thanos.R.drawable.ic_add_fill), | ||
contentDescription = "Add" | ||
) | ||
} | ||
} | ||
) { paddings -> | ||
|
||
DisposableEffectWithLifeCycle(onResume = { | ||
viewModel.refresh() | ||
}) | ||
|
||
LaunchedEffect(viewModel) { | ||
viewModel.refresh() | ||
} | ||
|
||
SwipeRefresh( | ||
state = rememberSwipeRefreshState(state.isLoading), | ||
onRefresh = { viewModel.refresh() }, | ||
indicatorPadding = paddings, | ||
clipIndicatorToPadding = false, | ||
indicator = { state, refreshTriggerDistance -> | ||
SwipeRefreshIndicator( | ||
state = state, | ||
refreshTriggerDistance = refreshTriggerDistance, | ||
scale = true, | ||
arrowEnabled = false, | ||
contentColor = MaterialTheme.colorScheme.primary | ||
) | ||
} | ||
) { | ||
LazyColumn( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(paddings) | ||
) { | ||
items(state.ruleItems) { | ||
ListItem(modifier = Modifier, title = it.rule, | ||
action1 = { | ||
IconButton(onClick = { | ||
viewModel.remove(it.rule) | ||
}) { | ||
Icon( | ||
painter = painterResource(id = github.tornaco.android.thanos.R.drawable.ic_delete_bin_2_fill), | ||
contentDescription = "Delete" | ||
) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
android/app/src/main/java/now/fortuitous/thanos/launchother/LaunchOtherAppRuleViewModel.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,60 @@ | ||
package now.fortuitous.thanos.launchother | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import github.tornaco.android.thanos.core.app.ThanosManager | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
data class RuleItem(val rule: String) | ||
|
||
data class OpsState( | ||
val isLoading: Boolean, | ||
val ruleItems: List<RuleItem> | ||
) | ||
|
||
@SuppressLint("StaticFieldLeak") | ||
@HiltViewModel | ||
class LaunchOtherAppRuleViewModel @Inject constructor(@ApplicationContext private val context: Context) : | ||
ViewModel() { | ||
|
||
private val _state = | ||
MutableStateFlow( | ||
OpsState( | ||
isLoading = true, | ||
ruleItems = emptyList() | ||
) | ||
) | ||
val state = _state.asStateFlow() | ||
|
||
private val thanos by lazy { ThanosManager.from(context) } | ||
private val supervisor by lazy { thanos.activityStackSupervisor } | ||
|
||
fun refresh() { | ||
_state.value = _state.value.copy(isLoading = true) | ||
viewModelScope.launch { | ||
val rules = withContext(Dispatchers.IO) { | ||
supervisor.allLaunchOtherAppRules.map { RuleItem(it) } | ||
} | ||
_state.value = _state.value.copy(ruleItems = rules, isLoading = false) | ||
} | ||
} | ||
|
||
fun add(rule: String) { | ||
supervisor.addLaunchOtherAppRule(rule) | ||
refresh() | ||
} | ||
|
||
fun remove(rule: String) { | ||
supervisor.deleteLaunchOtherAppRule(rule) | ||
refresh() | ||
} | ||
} |
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