Compare commits
8 Commits
05c7f0f60d
...
v1.40.2-ty
| Author | SHA1 | Date | |
|---|---|---|---|
| 291752abcd | |||
| 4836618609 | |||
| c8109d9197 | |||
| 3b3d6473ca | |||
| 6aba977823 | |||
| 229e5d1d32 | |||
| cb07acb577 | |||
| a0f9413d40 |
@@ -34,8 +34,8 @@ android {
|
||||
applicationId = "de.mm20.launcher2"
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
targetSdk = libs.versions.targetSdk.get().toInt()
|
||||
versionCode = System.getenv("VERSION_CODE_OVERRIDE")?.toIntOrNull() ?: 2026091004
|
||||
versionName = "1.40.2-typing.5"
|
||||
versionCode = System.getenv("VERSION_CODE_OVERRIDE")?.toIntOrNull() ?: 2026091006
|
||||
versionName = "1.40.2-typing.7"
|
||||
signingConfig = signingConfigs.getByName("debug")
|
||||
}
|
||||
|
||||
|
||||
@@ -17,33 +17,47 @@ import de.mm20.launcher2.ui.launcher.widgets.clock.parts.MusicPartProvider
|
||||
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.PartProvider
|
||||
import de.mm20.launcher2.ui.launcher.widgets.clock.parts.SmartspacerPartProvider
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
|
||||
class ClockWidgetVM : ViewModel(), KoinComponent {
|
||||
private val settings: ClockWidgetSettings by inject()
|
||||
|
||||
private val partProviders = settings.parts.combine(settings.useSmartspacer) { p, s ->
|
||||
p to s
|
||||
}.map { (parts, smartspacer) ->
|
||||
if (smartspacer && isAtLeastApiLevel(29)) {
|
||||
return@map listOf(SmartspacerPartProvider())
|
||||
}
|
||||
/**
|
||||
* Kept for the lifetime of the ViewModel instead of being rebuilt whenever the clock widget
|
||||
* is subscribed again - which happens on every return to the home screen, because the
|
||||
* launcher stops while it is in the background. Providers cache the data behind their
|
||||
* ranking, so a fresh instance starts at ranking 0 and makes its part disappear until its
|
||||
* query returns; the agenda part would blink on every visit that way.
|
||||
*/
|
||||
private val partProviders = MutableStateFlow<List<PartProvider>>(emptyList())
|
||||
|
||||
val providers = mutableListOf<PartProvider>()
|
||||
if (parts.date) providers += DatePartProvider()
|
||||
if (parts.calendar) providers += CalendarPartProvider()
|
||||
if (parts.music) providers += MusicPartProvider()
|
||||
providers += BatteryPartProvider(parts.battery)
|
||||
if (parts.alarm) providers += AlarmPartProvider()
|
||||
providers
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
settings.parts.combine(settings.useSmartspacer) { p, s -> p to s }
|
||||
.collect { (parts, smartspacer) ->
|
||||
partProviders.value = if (smartspacer && isAtLeastApiLevel(29)) {
|
||||
listOf(SmartspacerPartProvider())
|
||||
} else {
|
||||
buildList {
|
||||
if (parts.date) add(DatePartProvider())
|
||||
if (parts.calendar) add(CalendarPartProvider())
|
||||
if (parts.music) add(MusicPartProvider())
|
||||
add(BatteryPartProvider(parts.battery))
|
||||
if (parts.alarm) add(AlarmPartProvider())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getActiveParts(context: Context): Flow<List<PartProvider>> = channelFlow {
|
||||
partProviders.collectLatest { providers ->
|
||||
|
||||
@@ -5,22 +5,26 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.icu.text.DateFormat
|
||||
import android.icu.util.ULocale
|
||||
import android.provider.AlarmClock
|
||||
import android.text.format.DateUtils
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.getSystemService
|
||||
import de.mm20.launcher2.ktx.tryStartActivity
|
||||
import de.mm20.launcher2.ui.R
|
||||
import de.mm20.launcher2.ui.locals.LocalTimeFormat
|
||||
import de.mm20.launcher2.ui.utils.isTwentyFourHours
|
||||
import java.util.Date
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.channels.trySendBlocking
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -51,7 +55,8 @@ class AlarmPartProvider : PartProvider {
|
||||
if (alarm > it + AlarmWindow) {
|
||||
send(0)
|
||||
} else {
|
||||
send(60)
|
||||
// Highest ranking, so the alarm always sits directly below the clock.
|
||||
send(100)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,11 +82,15 @@ class AlarmPartProvider : PartProvider {
|
||||
@Composable
|
||||
override fun Component(compactLayout: Boolean) {
|
||||
val context = LocalContext.current
|
||||
val timeFormat = LocalTimeFormat.current
|
||||
|
||||
val alarmTime by nextAlarmTime
|
||||
val time by this.time.collectAsState(System.currentTimeMillis())
|
||||
|
||||
alarmTime?.let {
|
||||
alarmTime?.let { alarm ->
|
||||
|
||||
val skeleton = if (timeFormat.isTwentyFourHours(context)) "HH:mm" else "hh:mm a"
|
||||
val alarmText = DateFormat.getInstanceForSkeleton(skeleton, ULocale.getDefault())
|
||||
.format(Date(alarm))
|
||||
|
||||
if (!compactLayout) {
|
||||
|
||||
@@ -99,12 +108,8 @@ class AlarmPartProvider : PartProvider {
|
||||
)
|
||||
Text(
|
||||
modifier = Modifier.padding(start = 12.dp),
|
||||
text = DateUtils.getRelativeTimeSpanString(
|
||||
it,
|
||||
time,
|
||||
DateUtils.MINUTE_IN_MILLIS
|
||||
)
|
||||
.toString(),
|
||||
text = alarmText,
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -123,13 +128,10 @@ class AlarmPartProvider : PartProvider {
|
||||
)
|
||||
Text(
|
||||
modifier = Modifier.padding(start = 12.dp),
|
||||
text = DateUtils.getRelativeTimeSpanString(
|
||||
it,
|
||||
time,
|
||||
DateUtils.MINUTE_IN_MILLIS
|
||||
text = alarmText,
|
||||
style = MaterialTheme.typography.titleLarge.copy(
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
.toString(),
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -69,17 +70,14 @@ import java.util.Date
|
||||
*/
|
||||
private const val MaxRows = 4
|
||||
|
||||
/** An event that starts within this time frame (or is running) raises the ranking. */
|
||||
private const val SoonThreshold = 30 * 60 * 1000L
|
||||
|
||||
/** Re-query interval, so the agenda survives calendar changes that don't notify. */
|
||||
private const val RefreshInterval = 15 * 60 * 1000L
|
||||
|
||||
private const val RankingToday = 30
|
||||
private const val RankingSoon = 70
|
||||
/** Static ranking: the agenda is shown whenever there are events left today. */
|
||||
private const val Ranking = 30
|
||||
|
||||
/** Width of the dot column. Kept fixed so the rows line up with each other. */
|
||||
private val DotColumn = 12.dp
|
||||
private val DotColumn = 24.dp
|
||||
|
||||
/** Width of the time column. Wide enough for "11:59 PM" and "all-day". */
|
||||
private val TimeColumn = 64.dp
|
||||
@@ -100,17 +98,12 @@ class CalendarPartProvider : PartProvider, KoinComponent {
|
||||
private val searchSettings: CalendarSearchSettings by inject()
|
||||
|
||||
private val agenda = MutableStateFlow<List<CalendarEvent>>(emptyList())
|
||||
private val time = MutableStateFlow(System.currentTimeMillis())
|
||||
|
||||
override fun setTime(time: Long) {
|
||||
this.time.value = time
|
||||
}
|
||||
|
||||
override fun getRanking(context: Context): Flow<Int> = channelFlow {
|
||||
// Load the agenda in the background; the ranking itself only depends on the
|
||||
// already known agenda and the current time, so it can be emitted right away.
|
||||
// already known agenda, so it can be emitted right away.
|
||||
launch { observeAgenda(context) }
|
||||
combine(agenda, time) { events, now -> ranking(events, now) }
|
||||
agenda.map { ranking(it) }
|
||||
.distinctUntilChanged()
|
||||
.collect { send(it) }
|
||||
}
|
||||
@@ -129,6 +122,10 @@ class CalendarPartProvider : PartProvider, KoinComponent {
|
||||
from = now,
|
||||
to = endOfDay(now),
|
||||
excludeCalendars = excludedCalendars.toList(),
|
||||
// No debounce: the ranking stays 0 until the first result arrives, and
|
||||
// the query is only re-run on a calendar change or every 15 minutes, so
|
||||
// holding the result back would only delay the part's appearance.
|
||||
debounceMillis = 0,
|
||||
).first()
|
||||
.filter { !it.isTask }
|
||||
.sortedBy { it.startTime ?: it.endTime }
|
||||
@@ -167,13 +164,8 @@ class CalendarPartProvider : PartProvider, KoinComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private fun ranking(events: List<CalendarEvent>, now: Long): Int {
|
||||
if (events.isEmpty()) return 0
|
||||
val soon = events.any { event ->
|
||||
val start = event.startTime
|
||||
!event.allDay && start != null && start <= now + SoonThreshold && event.endTime > now
|
||||
}
|
||||
return if (soon) RankingSoon else RankingToday
|
||||
private fun ranking(events: List<CalendarEvent>): Int {
|
||||
return if (events.isEmpty()) 0 else Ranking
|
||||
}
|
||||
|
||||
private fun endOfDay(now: Long): Long {
|
||||
|
||||
@@ -24,7 +24,8 @@ import java.util.*
|
||||
|
||||
class DatePartProvider : PartProvider {
|
||||
override fun getRanking(context: Context): Flow<Int> = flow {
|
||||
emit(1)
|
||||
// Second highest, so the date follows the alarm below the clock.
|
||||
emit(90)
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -22,20 +22,28 @@ import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.combineTransform
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.transform
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.joinAll
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlin.time.Duration.Companion.days
|
||||
|
||||
interface CalendarRepository : SearchableRepository<CalendarEvent> {
|
||||
/**
|
||||
* @param debounceMillis how long each result is held back, so bursts of upstream changes
|
||||
* (plugin or permission updates) cause a single query. [debounce] delays the first result
|
||||
* too, so callers that must show something immediately pass 0.
|
||||
*/
|
||||
fun findMany(
|
||||
from: Long = System.currentTimeMillis(),
|
||||
to: Long = from + 14 * 24 * 60 * 60 * 1000L,
|
||||
excludeCalendars: List<String> = emptyList(),
|
||||
excludeAllDayEvents: Boolean = false,
|
||||
debounceMillis: Long = 500,
|
||||
): Flow<ImmutableList<CalendarEvent>>
|
||||
|
||||
fun getCalendars(providerId: String? = null): Flow<List<CalendarList>>
|
||||
@@ -94,6 +102,7 @@ internal class CalendarRepositoryImpl(
|
||||
to: Long,
|
||||
excludeCalendars: List<String>,
|
||||
excludeAllDayEvents: Boolean,
|
||||
debounceMillis: Long,
|
||||
): Flow<ImmutableList<CalendarEvent>> {
|
||||
val hasCalendarPermission = permissionsManager.hasPermission(PermissionGroup.Calendar)
|
||||
val hasTasksPermission = permissionsManager.hasPermission(PermissionGroup.Tasks)
|
||||
@@ -112,17 +121,17 @@ internal class CalendarRepositoryImpl(
|
||||
)
|
||||
}
|
||||
|
||||
emitAll(
|
||||
queryCalendarEvents(
|
||||
query = null,
|
||||
intervalStart = from,
|
||||
intervalEnd = to,
|
||||
excludeAllDayEvents = excludeAllDayEvents,
|
||||
excludeCalendars = excludeCalendars,
|
||||
providers = providers,
|
||||
allowNetwork = false,
|
||||
).debounce(500)
|
||||
val events = queryCalendarEvents(
|
||||
query = null,
|
||||
intervalStart = from,
|
||||
intervalEnd = to,
|
||||
excludeAllDayEvents = excludeAllDayEvents,
|
||||
excludeCalendars = excludeCalendars,
|
||||
providers = providers,
|
||||
awaitAllProviders = true,
|
||||
allowNetwork = false,
|
||||
)
|
||||
emitAll(if (debounceMillis > 0L) events.debounce(debounceMillis) else events)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,12 +142,17 @@ internal class CalendarRepositoryImpl(
|
||||
excludeAllDayEvents: Boolean = false,
|
||||
excludeCalendars: List<String> = emptyList(),
|
||||
allowNetwork: Boolean = false,
|
||||
awaitAllProviders: Boolean = false,
|
||||
providers: List<CalendarProvider>,
|
||||
): Flow<ImmutableList<CalendarEvent>> = flow {
|
||||
supervisorScope {
|
||||
val result = MutableStateFlow(persistentListOf<CalendarEvent>())
|
||||
// Null until the first provider has reported. Emitting the (empty) accumulator right
|
||||
// away would hand collectors an empty list before any query had run, which callers
|
||||
// that take the first result - like the agenda in the clock widget - cannot tell
|
||||
// apart from "no events".
|
||||
val result = MutableStateFlow<ImmutableList<CalendarEvent>?>(null)
|
||||
|
||||
for (provider in providers) {
|
||||
val jobs = providers.map { provider ->
|
||||
launch {
|
||||
val r = provider.search(
|
||||
query,
|
||||
@@ -152,11 +166,19 @@ internal class CalendarRepositoryImpl(
|
||||
allowNetwork = allowNetwork,
|
||||
)
|
||||
result.update {
|
||||
(it + r).toPersistentList()
|
||||
((it ?: persistentListOf()) + r).toPersistentList()
|
||||
}
|
||||
}
|
||||
}
|
||||
emitAll(result)
|
||||
|
||||
if (awaitAllProviders) {
|
||||
// One-shot callers must not receive a partial list, so wait until every
|
||||
// provider has reported; if there is none, report an empty result.
|
||||
jobs.joinAll()
|
||||
emit(result.value ?: persistentListOf())
|
||||
} else {
|
||||
emitAll(result.filterNotNull())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user