Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customizable action on clock widget tap #1280

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.mm20.launcher2.ui.launcher.widgets.clock

import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
Expand Down Expand Up @@ -65,16 +66,20 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.ktx.isAtLeastApiLevel
import de.mm20.launcher2.preferences.ClockWidgetAlignment
import de.mm20.launcher2.preferences.ClockWidgetColors
import de.mm20.launcher2.preferences.ClockWidgetStyle
import de.mm20.launcher2.preferences.GestureAction
import de.mm20.launcher2.preferences.TimeFormat
import de.mm20.launcher2.preferences.ui.ClockWidgetSettings
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.base.LocalTime
import de.mm20.launcher2.ui.component.BottomSheetDialog
import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.component.preferences.Preference
import de.mm20.launcher2.ui.component.preferences.SwitchPreference
import de.mm20.launcher2.ui.ktx.toPixels
import de.mm20.launcher2.ui.launcher.widgets.clock.clocks.AnalogClock
import de.mm20.launcher2.ui.launcher.widgets.clock.clocks.BinaryClock
import de.mm20.launcher2.ui.launcher.widgets.clock.clocks.CustomClock
Expand All @@ -85,6 +90,8 @@ import de.mm20.launcher2.ui.launcher.widgets.clock.clocks.SegmentClock
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.PartProvider
import de.mm20.launcher2.ui.locals.LocalPreferDarkContentOverWallpaper
import de.mm20.launcher2.ui.settings.clockwidget.ClockWidgetSettingsScreenVM
import de.mm20.launcher2.ui.settings.gestures.GesturePreference
import de.mm20.launcher2.ui.settings.gestures.requiresAccessibilityService
import de.mm20.launcher2.ui.utils.isTwentyFourHours
import org.koin.androidx.compose.inject

Expand Down Expand Up @@ -576,6 +583,29 @@ fun ConfigureClockWidgetSheet(
viewModel.setFillHeight(it)
}
)

val options = buildList {
add(stringResource(R.string.gesture_action_none) to GestureAction.NoAction)
add(stringResource(R.string.gesture_action_alarms) to GestureAction.Alarms)
add(stringResource(R.string.gesture_action_launch_app) to GestureAction.Launch(null))
}
val appIconSize = 32.dp.toPixels()
val tapAction by viewModel.tapAction.collectAsStateWithLifecycle(null)
val tapApp by viewModel.tapApp.collectAsState(null)
val tapAppIcon by remember(tapApp?.key) {
viewModel.getIcon(tapApp, appIconSize.toInt())
}.collectAsState(null)
GesturePreference(
title = stringResource(R.string.preference_gesture_tap),
value = tapAction,
onValueChanged = { viewModel.setTapAction(it) },
isOpenSearch = false,
options = options,
app = tapApp,
appIcon = tapAppIcon,
onAppChanged = { viewModel.setTapApp(it) }
)

AnimatedVisibility(fillHeight == true) {
var showDropdown by remember { mutableStateOf(false) }
Preference(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package de.mm20.launcher2.ui.launcher.widgets.clock

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.provider.AlarmClock
import androidx.core.app.ActivityOptionsCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.preferences.GestureAction
import de.mm20.launcher2.preferences.ui.ClockWidgetSettings
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.searchable.SavableSearchableRepository
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.AlarmPartProvider
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.BatteryPartProvider
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.DatePartProvider
Expand All @@ -15,16 +20,20 @@ import de.mm20.launcher2.ui.launcher.widgets.clock.parts.MusicPartProvider
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.PartProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class ClockWidgetVM : ViewModel(), KoinComponent {
private val settings: ClockWidgetSettings by inject()
private val searchableRepository: SavableSearchableRepository by inject()

private val partProviders = settings.parts.map {
val providers = mutableListOf<PartProvider>()
Expand Down Expand Up @@ -69,9 +78,40 @@ class ClockWidgetVM : ViewModel(), KoinComponent {
partProviders.value.forEach { it.setTime(time) }
}

private val tapAction = settings.tapAction
.stateIn(viewModelScope, SharingStarted.Eagerly, null)

private val tapApp: StateFlow<SavableSearchable?> = tapAction
.flatMapLatest {
if (it !is GestureAction.Launch || it.key == null) flowOf(null)
else searchableRepository.getByKeys(listOf(it.key!!)).map {
it.firstOrNull()
}
}
.stateIn(viewModelScope, SharingStarted.Eagerly, null)

fun launchClockApp(context: Context) {
context.tryStartActivity(Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
when(tapAction.value) {
is GestureAction.Alarms -> {
context.tryStartActivity(Intent(AlarmClock.ACTION_SHOW_ALARMS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
}

is GestureAction.Launch -> {
val view = (context as Activity).window.decorView
val options = ActivityOptionsCompat.makeScaleUpAnimation(
view,
0,
0,
view.width,
view.height
)
tapApp.value?.launch(context, options.toBundle())
}

else -> {}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ package de.mm20.launcher2.ui.settings.clockwidget

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.mm20.launcher2.icons.IconService
import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.preferences.ClockWidgetAlignment
import de.mm20.launcher2.preferences.ClockWidgetColors
import de.mm20.launcher2.preferences.ClockWidgetStyle
import de.mm20.launcher2.preferences.GestureAction
import de.mm20.launcher2.preferences.TimeFormat
import de.mm20.launcher2.preferences.ui.ClockWidgetSettings
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.searchable.SavableSearchableRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class ClockWidgetSettingsScreenVM : ViewModel(), KoinComponent {
private val settings: ClockWidgetSettings by inject()
private val searchableRepository: SavableSearchableRepository by inject()
private val iconService: IconService by inject()

val compact = settings.compact
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)
fun setCompact(compact: Boolean) {
Expand Down Expand Up @@ -75,6 +87,26 @@ class ClockWidgetSettingsScreenVM : ViewModel(), KoinComponent {
settings.setFillHeight(fillHeight)
}

val tapAction = settings.tapAction
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)
fun setTapAction(action: GestureAction) {
settings.setTapAction(action)
}

val tapApp: Flow<SavableSearchable?> = tapAction
.flatMapLatest {
if (it !is GestureAction.Launch || it.key == null) flowOf(null)
else searchableRepository.getByKeys(listOf(it.key!!)).map {
it.firstOrNull()
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(stopTimeoutMillis = 10000), null)

fun setTapApp(searchable: SavableSearchable?) {
searchable?.let { searchableRepository.insert(it) } ?: return
setTapAction(GestureAction.Launch(searchable.key))
}

val parts = settings.parts
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)

Expand All @@ -100,4 +132,9 @@ class ClockWidgetSettingsScreenVM : ViewModel(), KoinComponent {
fun setAlignment(alignment: ClockWidgetAlignment) {
settings.setAlignment(alignment)
}

fun getIcon(searchable: SavableSearchable?, size: Int): Flow<LauncherIcon?> {
if (searchable == null) return emptyFlow()
return iconService.getIcon(searchable, size)
}
}
2 changes: 2 additions & 0 deletions core/i18n/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,10 @@
<string name="preference_gesture_swipe_left">Swipe left</string>
<string name="preference_gesture_swipe_right">Swipe right</string>
<string name="preference_gesture_double_tap">Double tap</string>
<string name="preference_gesture_tap">Tap</string>
<string name="preference_gesture_long_press">Long press</string>
<string name="preference_gesture_home_button">Home button/gesture</string>
<string name="gesture_action_alarms">Show alarms</string>
<string name="gesture_action_none">Do nothing</string>
<string name="gesture_action_open_search">Open search</string>
<string name="gesture_action_launch_app">Launch app</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ data class LauncherSettingsData internal constructor(
val clockWidgetDatePart: Boolean = true,
val clockWidgetFillHeight: Boolean = true,
val clockWidgetAlignment: ClockWidgetAlignment = ClockWidgetAlignment.Bottom,
val clockTapAction: GestureAction = GestureAction.Alarms,

val homeScreenDock: Boolean = false,

Expand Down Expand Up @@ -373,6 +374,10 @@ sealed interface GestureAction {
@Serializable
@SerialName("launch_searchable")
data class Launch(val key: String?) : GestureAction

@Serializable
@SerialName("alarms")
data object Alarms : GestureAction
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import de.mm20.launcher2.preferences.ClockWidgetAlignment
import de.mm20.launcher2.preferences.ClockWidgetColors
import de.mm20.launcher2.preferences.ClockWidgetStyle
import de.mm20.launcher2.preferences.ClockWidgetStyleEnum
import de.mm20.launcher2.preferences.GestureAction
import de.mm20.launcher2.preferences.LauncherDataStore
import de.mm20.launcher2.preferences.TimeFormat
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -72,6 +73,15 @@ class ClockWidgetSettings internal constructor(
}
}

val tapAction: Flow<GestureAction> = launcherDataStore.data.map { it.clockTapAction }
.distinctUntilChanged()

fun setTapAction(action: GestureAction) {
launcherDataStore.update {
it.copy(clockTapAction = action)
}
}

val dock
get() = launcherDataStore.data.map { it.homeScreenDock }

Expand Down