Fixed next set time retrieval selecting the wrong time

Updated machine translations
This commit is contained in:
Anthonyy232 committed 2024-05-24 15:13:25 -07:00
1 parent bca711a50d
commit 25717d83fd
43 files changed
+5826 -42

No files matched your search

@@ -26,6 +26,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.data.settings.SettingsDataStore
import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.SettingsEvent
import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.SettingsViewModel
import com.anthonyla.paperize.feature.wallpaper.presentation.themes.PaperizeTheme
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.WallpaperScreenViewModel
@@ -87,6 +88,7 @@ class MainActivity : ComponentActivity() {
}
if (selectedState.value.selectedAlbum != null && settingsState.value.enableChanger) {
if (!isAlreadyRunning) {
settingsViewModel.onEvent(SettingsEvent.RefreshNextSetTime)
val intent = Intent(context, WallpaperService::class.java).apply {
action = WallpaperService.Actions.START.toString()
putExtra("timeInMinutes1", settingsState.value.homeInterval)
@@ -130,11 +130,6 @@ fun PaperizeApp(
settingsState.value.scheduleSeparately
)
)
settingsViewModel.onEvent(
SettingsEvent.SetHomeWallpaperInterval(
settingsState.value.homeInterval
)
)
} ?: run {
wallpaperScreenViewModel.onEvent(WallpaperEvent.Reset)
Intent(context, WallpaperService::class.java).also {
@@ -284,16 +279,12 @@ fun PaperizeApp(
if (settingsState.value.enableChanger) {
job?.cancel()
job = scope.launch {
delay(2000)
delay(3000)
val intent = Intent(context, WallpaperService::class.java).apply {
action = WallpaperService.Actions.REQUEUE.toString()
putExtra("timeInMinutes1", timeInMinutes)
putExtra("timeInMinutes2", settingsState.value.lockInterval)
putExtra("scheduleSeparately", settingsState.value.scheduleSeparately)
selectedState.value.selectedAlbum?.album?.homeWallpapersInQueue.let { it1 ->
if (it1 != null) { putExtra("currentSize", it1.size) }
}
selectedState.value.selectedAlbum?.wallpapers?.let { it1 -> putExtra("totalSize", it1.size) }
}
context.startForegroundService(intent)
}
@@ -305,16 +296,12 @@ fun PaperizeApp(
if (settingsState.value.enableChanger) {
job?.cancel()
job = scope.launch {
delay(2000)
delay(3000)
val intent = Intent(context, WallpaperService::class.java).apply {
action = WallpaperService.Actions.REQUEUE.toString()
putExtra("timeInMinutes1", settingsState.value.homeInterval)
putExtra("timeInMinutes2", timeInMinutes)
putExtra("scheduleSeparately", settingsState.value.scheduleSeparately)
selectedState.value.selectedAlbum?.album?.homeWallpapersInQueue.let { it1 ->
if (it1 != null) { putExtra("currentSize", it1.size) }
}
selectedState.value.selectedAlbum?.wallpapers?.let { it1 -> putExtra("totalSize", it1.size) }
}
context.startForegroundService(intent)
}
@@ -7,6 +7,7 @@ sealed class SettingsEvent {
data object Refresh: SettingsEvent()
data object Reset: SettingsEvent()
data object SetFirstLaunch: SettingsEvent()
data object RefreshNextSetTime: SettingsEvent()
data class SetDarkMode(val darkMode: Boolean?): SettingsEvent()
data class SetDynamicTheming(val dynamicTheming: Boolean): SettingsEvent()
data class SetAnimate(val animate: Boolean): SettingsEvent()
@@ -1,11 +1,13 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen
import android.icu.util.UniversalTimeScale.toLong
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.anthonyla.paperize.core.ScalingConstants
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.data.settings.SettingsDataStore
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
@@ -181,18 +183,27 @@ class SettingsViewModel @Inject constructor (
is SettingsEvent.SetHomeWallpaperInterval -> {
viewModelScope.launch(Dispatchers.IO) {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val time = LocalDateTime.now()
val formattedLastSetTime = time.format(formatter)
val formattedNextSetTime = time.plusMinutes(event.interval.toLong()).format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, formattedLastSetTime)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, formattedNextSetTime)
settingsDataStoreImpl.putInt(SettingsConstants.HOME_WALLPAPER_CHANGE_INTERVAL, event.interval)
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val currentTime = LocalDateTime.now()
val nextSetTime: String?
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, currentTime.format(formatter))
if (_state.value.scheduleSeparately) {
val nextSetTime1 = currentTime.plusMinutes(event.interval.toLong())
val nextSetTime2 = currentTime.plusMinutes(_state.value.lockInterval.toLong())
nextSetTime = (if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2)!!.format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime)
}
else {
nextSetTime = currentTime.plusMinutes(event.interval.toLong()).format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime)
}
_state.update {
it.copy(
homeInterval = event.interval,
lastSetTime = formattedLastSetTime,
nextSetTime = formattedNextSetTime
lastSetTime = currentTime.format(formatter),
nextSetTime = nextSetTime
)
}
}
@@ -200,18 +211,49 @@ class SettingsViewModel @Inject constructor (
is SettingsEvent.SetLockWallpaperInterval -> {
viewModelScope.launch(Dispatchers.IO) {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val time = LocalDateTime.now()
val formattedLastSetTime = time.format(formatter)
val formattedNextSetTime = time.plusMinutes(event.interval.toLong()).format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, formattedLastSetTime)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, formattedNextSetTime)
settingsDataStoreImpl.putInt(SettingsConstants.LOCK_WALLPAPER_CHANGE_INTERVAL, event.interval)
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val currentTime = LocalDateTime.now()
val nextSetTime: String?
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, currentTime.format(formatter))
if (_state.value.scheduleSeparately) {
val nextSetTime1 = currentTime.plusMinutes(_state.value.homeInterval.toLong())
val nextSetTime2 = currentTime.plusMinutes(event.interval.toLong())
nextSetTime = (if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2)!!.format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime)
}
else {
nextSetTime = currentTime.plusMinutes(event.interval.toLong()).format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime)
}
_state.update {
it.copy(
lockInterval = event.interval,
lastSetTime = formattedLastSetTime,
nextSetTime = formattedNextSetTime
lastSetTime = currentTime.format(formatter),
nextSetTime = nextSetTime
)
}
}
}
is SettingsEvent.RefreshNextSetTime -> {
viewModelScope.launch {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val currentTime = LocalDateTime.now()
val nextSetTime: String?
if (_state.value.scheduleSeparately) {
val nextSetTime1 = currentTime.plusMinutes(_state.value.homeInterval.toLong())
val nextSetTime2 = currentTime.plusMinutes(_state.value.lockInterval.toLong())
nextSetTime = (if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2)!!.format(formatter)
}
else {
nextSetTime = currentTime.plusMinutes(_state.value.homeInterval.toLong()).format(formatter)
}
_state.update {
it.copy(
lastSetTime = currentTime.format(formatter),
nextSetTime = nextSetTime
)
}
}
@@ -57,6 +57,8 @@ class WallpaperService: Service() {
private lateinit var runnableCode2: Runnable
private var timeInMinutes1: Int = SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT
private var timeInMinutes2: Int = SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT
private var nextSetTime1: LocalDateTime? = null
private var nextSetTime2: LocalDateTime? = null
private var scheduleSeparately: Boolean = false
enum class Actions {
@@ -175,6 +177,25 @@ class WallpaperService: Service() {
timeInMinutes2 = intent.getIntExtra("timeInMinutes2", SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT)
scheduleSeparately = intent.getBooleanExtra("scheduleSeparately", false)
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val currentTime = LocalDateTime.now()
CoroutineScope(Dispatchers.IO).launch {
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, currentTime.format(formatter))
if (scheduleSeparately) {
nextSetTime1 = currentTime.plusMinutes(timeInMinutes1.toLong())
nextSetTime2 = currentTime.plusMinutes(timeInMinutes2.toLong())
val earliestTime = (if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2)!!.format(formatter)
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, earliestTime)
}
else {
nextSetTime1 = currentTime.plusMinutes(timeInMinutes1.toLong())
nextSetTime2 = nextSetTime1
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, currentTime.plusMinutes(timeInMinutes1.toLong()).format(formatter))
}
}
val notification = createNotification()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
@@ -267,11 +288,18 @@ class WallpaperService: Service() {
* Creates a notification for the wallpaper service
*/
private fun createNotification(): Notification {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val earliestTime = when {
nextSetTime1 != null && nextSetTime2 != null -> (if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2)!!.format(formatter)
nextSetTime1 != null -> nextSetTime1!!.format(formatter)
nextSetTime2 != null -> nextSetTime2!!.format(formatter)
else -> null
}
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
return NotificationCompat.Builder(this, "wallpaper_service_channel")
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.paperize_is_running))
.setContentText(getString(R.string.next_wallpaper_change, earliestTime ?: R.string.app_name))
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build()
@@ -282,6 +310,7 @@ class WallpaperService: Service() {
* If none left, reshuffle the wallpapers and pick the first one
*/
private suspend fun changeWallpaper(context: Context, setHomeOrLock: Boolean? = null) {
Log.d("PaperizeWallpaperChanger", "Changing wallpaper set: {$setHomeOrLock}")
try {
val selectedAlbum = selectedRepository.getSelectedAlbum().first().firstOrNull()
if (selectedAlbum == null) {
@@ -289,10 +318,6 @@ class WallpaperService: Service() {
return
}
else {
val notification = createNotification()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
val toggled = settingsDataStoreImpl.getBoolean(SettingsConstants.ENABLE_CHANGER) ?: false
val setHome = settingsDataStoreImpl.getBoolean(SettingsConstants.HOME_WALLPAPER) ?: false
val setLock = settingsDataStoreImpl.getBoolean(SettingsConstants.LOCK_WALLPAPER) ?: false
@@ -301,12 +326,33 @@ class WallpaperService: Service() {
return
}
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
val time = LocalDateTime.now()
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, time.format(formatter))
settingsDataStoreImpl.putString(
SettingsConstants.NEXT_SET_TIME,
time.plusMinutes(timeInMinutes1.toLong()).format(formatter)
)
val currentTime = LocalDateTime.now()
settingsDataStoreImpl.putString(SettingsConstants.LAST_SET_TIME, currentTime.format(formatter))
if (setHomeOrLock == null) {
nextSetTime1 = currentTime.plusMinutes(timeInMinutes1.toLong())
nextSetTime2 = nextSetTime1
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, currentTime.plusMinutes(timeInMinutes1.toLong()).format(formatter))
}
else {
if (setHomeOrLock) { nextSetTime1 = currentTime.plusMinutes(timeInMinutes1.toLong()) }
else { nextSetTime2 = currentTime.plusMinutes(timeInMinutes2.toLong()) }
if (nextSetTime1 == null && nextSetTime2 != null) {
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime2!!.format(formatter))
}
else if (nextSetTime1 != null && nextSetTime2 == null) {
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, nextSetTime1!!.format(formatter))
}
else {
val earliestTime = if (nextSetTime1!!.isBefore(nextSetTime2)) nextSetTime1 else nextSetTime2
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, earliestTime!!.format(formatter))
}
}
val notification = createNotification()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
val scaling = settingsDataStoreImpl.getString(SettingsConstants.WALLPAPER_SCALING)?.let { ScalingConstants.valueOf(it) } ?: ScalingConstants.FILL
val darken = settingsDataStoreImpl.getBoolean(SettingsConstants.DARKEN) ?: false
val darkenPercentage = settingsDataStoreImpl.getInt(SettingsConstants.DARKEN_PERCENTAGE) ?: 100