Refactored refreshing album method to separate service call
This commit is contained in:
1 parent
f4bf2786de
commit
d8fc106a89
10 files changed
+204
-206
No files matched your search
@@ -19,7 +19,7 @@ import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
import com.google.android.renderscript.Toolkit
|
||||
import com.lazygeniouz.dfc.file.DocumentFileCompat
|
||||
|
||||
enum class Type { HOME, LOCK, BOTH }
|
||||
enum class Type { HOME, LOCK, BOTH, REFRESH }
|
||||
|
||||
/**
|
||||
* Get the dimensions of the image from the uri
|
||||
@@ -150,14 +150,15 @@ fun blurBitmap(source: Bitmap, percent: Int): Bitmap {
|
||||
* Retrieve wallpaper URIs from a folder directory URI
|
||||
*/
|
||||
fun getWallpaperFromFolder(folderUri: String, context: Context): List<String> {
|
||||
try {
|
||||
/*try {
|
||||
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
|
||||
return listFilesRecursive(folderDocumentFile, context)
|
||||
} catch (e: Exception) {
|
||||
val folderDocumentFile = DocumentFile.fromTreeUri(context, folderUri.toUri())
|
||||
return listFilesRecursive(folderDocumentFile, context)
|
||||
}
|
||||
|
||||
}*/
|
||||
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
|
||||
return listFilesRecursive(folderDocumentFile, context)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,14 +200,14 @@ fun listFilesRecursive(parent: DocumentFile?, context: Context): List<String> {
|
||||
*/
|
||||
fun findFirstValidUri(context: Context, wallpapers: List<Wallpaper>, folders: List<Folder>): String? {
|
||||
wallpapers.forEach { wallpaper ->
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
|
||||
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
|
||||
if (file?.exists() == true) {
|
||||
return wallpaper.wallpaperUri
|
||||
}
|
||||
}
|
||||
folders.forEach { folder ->
|
||||
folder.wallpapers.forEach { wallpaper ->
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.toUri())
|
||||
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.toUri())
|
||||
if (file?.exists() == true) {
|
||||
return wallpaper
|
||||
}
|
||||
@@ -219,11 +220,12 @@ fun findFirstValidUri(context: Context, wallpapers: List<Wallpaper>, folders: Li
|
||||
* Get the folder name from the folder URI
|
||||
*/
|
||||
fun getFolderNameFromUri(folderUri: String, context: Context): String? {
|
||||
return try {
|
||||
/*return try {
|
||||
DocumentFileCompat.fromTreeUri(context, folderUri.toUri())?.name
|
||||
} catch (e: Exception) {
|
||||
DocumentFile.fromTreeUri(context, folderUri.toUri())?.name
|
||||
}
|
||||
}*/
|
||||
return DocumentFileCompat.fromTreeUri(context, folderUri.toUri())?.name
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+50
-3
@@ -258,6 +258,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -272,6 +273,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -287,6 +289,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.updateWallpaperAlarm(it)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,6 +306,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.updateWallpaperAlarm(it)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,7 +335,21 @@ fun PaperizeApp(
|
||||
firstSet = settingsState.value.firstSet,
|
||||
onToggleChanger = { enableWallpaperChanger ->
|
||||
settingsViewModel.onEvent(SettingsEvent.SetChangerToggle(enableWallpaperChanger))
|
||||
if (selectedState.value.selectedAlbum!= null && enableWallpaperChanger && (settingsState.value.setHomeWallpaper || settingsState.value.setLockWallpaper)) {
|
||||
if (selectedState.value.selectedAlbum != null && enableWallpaperChanger && (settingsState.value.setHomeWallpaper || settingsState.value.setLockWallpaper)) {
|
||||
wallpaperScreenViewModel.onEvent(
|
||||
WallpaperEvent.UpdateSelectedAlbum(
|
||||
selectedState.value.selectedAlbum!!.copy(
|
||||
album = selectedState.value.selectedAlbum!!.album.copy(
|
||||
currentHomeWallpaper = if (settingsState.value.setHomeWallpaper) selectedState.value.selectedAlbum!!.album.homeWallpapersInQueue.firstOrNull() else null,
|
||||
currentLockWallpaper = if (settingsState.value.scheduleSeparately && settingsState.value.setLockWallpaper) selectedState.value.selectedAlbum!!.album.lockWallpapersInQueue.firstOrNull() else if (settingsState.value.setLockWallpaper) selectedState.value.selectedAlbum!!.album.homeWallpapersInQueue.firstOrNull() else null
|
||||
)
|
||||
),
|
||||
null,
|
||||
settingsState.value.scheduleSeparately,
|
||||
settingsState.value.setHomeWallpaper,
|
||||
settingsState.value.setLockWallpaper
|
||||
)
|
||||
)
|
||||
job?.cancel()
|
||||
job = scope.launch {
|
||||
delay(2000)
|
||||
@@ -341,13 +359,22 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
else { scheduler.cancelWallpaperAlarm() }
|
||||
},
|
||||
onSelectAlbum = {album ->
|
||||
settingsViewModel.onEvent(SettingsEvent.SetChangerToggle(true))
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(null, album, settingsState.value.scheduleSeparately))
|
||||
wallpaperScreenViewModel.onEvent(
|
||||
WallpaperEvent.UpdateSelectedAlbum(
|
||||
null,
|
||||
album,
|
||||
settingsState.value.scheduleSeparately,
|
||||
settingsState.value.setHomeWallpaper,
|
||||
settingsState.value.setLockWallpaper
|
||||
)
|
||||
)
|
||||
if (settingsState.value.firstSet) {
|
||||
settingsViewModel.onEvent(SettingsEvent.SetFirstSet)
|
||||
}
|
||||
@@ -359,6 +386,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = settingsState.value.scheduleSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
},
|
||||
onDarkenPercentage = {
|
||||
@@ -414,6 +442,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = false
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
else if (selectedState.value.selectedAlbum != null && settingsState.value.enableChanger) {
|
||||
@@ -443,7 +472,9 @@ fun PaperizeApp(
|
||||
timeInMinutes2 = settingsState.value.lockInterval,
|
||||
scheduleSeparately = false
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}}
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
else if (selectedState.value.selectedAlbum != null && settingsState.value.enableChanger) {
|
||||
job?.cancel()
|
||||
@@ -456,6 +487,21 @@ fun PaperizeApp(
|
||||
onScheduleSeparatelyChange = { changeSeparately ->
|
||||
settingsViewModel.onEvent(SettingsEvent.SetScheduleSeparately(changeSeparately))
|
||||
if (selectedState.value.selectedAlbum!= null && settingsState.value.enableChanger) {
|
||||
wallpaperScreenViewModel.onEvent(
|
||||
WallpaperEvent.UpdateSelectedAlbum(
|
||||
selectedState.value.selectedAlbum!!.copy(
|
||||
album = selectedState.value.selectedAlbum!!.album.copy(
|
||||
currentHomeWallpaper = if (settingsState.value.setHomeWallpaper) selectedState.value.selectedAlbum!!.album.homeWallpapersInQueue.firstOrNull() else null,
|
||||
currentLockWallpaper = if (changeSeparately && settingsState.value.setLockWallpaper) selectedState.value.selectedAlbum!!.album.lockWallpapersInQueue.firstOrNull() else if (settingsState.value.setLockWallpaper) selectedState.value.selectedAlbum!!.album.homeWallpapersInQueue.firstOrNull() else null
|
||||
)
|
||||
|
||||
),
|
||||
null,
|
||||
settingsState.value.scheduleSeparately,
|
||||
settingsState.value.setHomeWallpaper,
|
||||
settingsState.value.setLockWallpaper
|
||||
)
|
||||
)
|
||||
job?.cancel()
|
||||
job = scope.launch {
|
||||
delay(2000)
|
||||
@@ -465,6 +511,7 @@ fun PaperizeApp(
|
||||
scheduleSeparately = changeSeparately
|
||||
)
|
||||
alarmItem.let{scheduler.scheduleWallpaperAlarm(it, null, true, true)}
|
||||
scheduler.scheduleRefresh()
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
+3
-3
@@ -101,7 +101,7 @@ class AlbumsViewModel @Inject constructor (
|
||||
repository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
@@ -112,7 +112,7 @@ class AlbumsViewModel @Inject constructor (
|
||||
repository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class AlbumsViewModel @Inject constructor (
|
||||
}
|
||||
else {
|
||||
// Update album cover uri if null or invalid
|
||||
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFile.fromSingleUri(context, it) }
|
||||
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFileCompat.fromSingleUri(context, it) }
|
||||
if (albumCoverFile == null || !albumCoverFile.exists()) {
|
||||
val newCoverUri = findFirstValidUri(context, albumWithWallpaper.wallpapers, albumWithWallpaper.folders)
|
||||
repository.updateAlbum(albumWithWallpaper.album.copy(coverUri = newCoverUri))
|
||||
|
||||
+5
-3
@@ -7,8 +7,10 @@ sealed class WallpaperEvent {
|
||||
data class UpdateSelectedAlbum(
|
||||
val selectedAlbum: SelectedAlbum?,
|
||||
val album: AlbumWithWallpaperAndFolder?,
|
||||
val scheduleSeparately: Boolean = false
|
||||
val scheduleSeparately: Boolean = false,
|
||||
val setHome: Boolean = false,
|
||||
val setLock: Boolean = false
|
||||
): WallpaperEvent()
|
||||
object Reset : WallpaperEvent()
|
||||
object Refresh : WallpaperEvent()
|
||||
data object Reset : WallpaperEvent()
|
||||
data object Refresh : WallpaperEvent()
|
||||
}
|
||||
+3
-2
@@ -43,6 +43,7 @@ class WallpaperScreenViewModel @Inject constructor (
|
||||
when (event) {
|
||||
is WallpaperEvent.UpdateSelectedAlbum -> {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
if (!event.setHome && !event.setLock) return@launch
|
||||
if (event.selectedAlbum != null) {
|
||||
_state.update {
|
||||
it.copy(
|
||||
@@ -67,8 +68,8 @@ class WallpaperScreenViewModel @Inject constructor (
|
||||
album = event.album.album.copy(
|
||||
homeWallpapersInQueue = shuffledWallpapers1,
|
||||
lockWallpapersInQueue = shuffledWallpapers2,
|
||||
currentHomeWallpaper = shuffledWallpapers1.firstOrNull(),
|
||||
currentLockWallpaper = if (event.scheduleSeparately) shuffledWallpapers2.firstOrNull() else shuffledWallpapers1.firstOrNull(),
|
||||
currentHomeWallpaper = if (event.setHome) shuffledWallpapers1.firstOrNull() else null,
|
||||
currentLockWallpaper = if (event.scheduleSeparately && event.setLock) shuffledWallpapers2.firstOrNull() else if (event.setLock) shuffledWallpapers1.firstOrNull() else null,
|
||||
),
|
||||
wallpapers = wallpapers
|
||||
)
|
||||
|
||||
+38
-29
@@ -3,7 +3,6 @@ package com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.anthonyla.paperize.core.SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT
|
||||
import com.anthonyla.paperize.core.Type
|
||||
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService1
|
||||
@@ -17,37 +16,47 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
class WallpaperReceiver: BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (context != null) {
|
||||
val timeInMinutes1 = intent?.getIntExtra("timeInMinutes1", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
|
||||
val timeInMinutes2 = intent?.getIntExtra("timeInMinutes2", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
|
||||
val scheduleSeparately = intent?.getBooleanExtra("scheduleSeparately", false) ?: false
|
||||
val type = intent?.getIntExtra("type", Type.BOTH.ordinal) ?: Type.BOTH.ordinal
|
||||
val serviceIntent = Intent().apply {
|
||||
putExtra("timeInMinutes1", timeInMinutes1)
|
||||
putExtra("timeInMinutes2", timeInMinutes2)
|
||||
putExtra("scheduleSeparately", scheduleSeparately)
|
||||
putExtra("type", type)
|
||||
}
|
||||
if (type == Type.BOTH.ordinal || type == Type.HOME.ordinal) {
|
||||
serviceIntent.setClass(context, WallpaperService1::class.java).apply {
|
||||
action = WallpaperService1.Actions.START.toString()
|
||||
}
|
||||
} else {
|
||||
serviceIntent.setClass(context, WallpaperService2::class.java).apply {
|
||||
action = WallpaperService2.Actions.START.toString()
|
||||
val refresh = intent?.getBooleanExtra("refresh", false) ?: false
|
||||
if (refresh) {
|
||||
val serviceIntent = Intent(context, WallpaperService1::class.java).apply {
|
||||
action = WallpaperService1.Actions.REFRESH.toString()
|
||||
}
|
||||
context.startService(serviceIntent)
|
||||
WallpaperScheduler(context).scheduleRefresh()
|
||||
}
|
||||
context.startService(serviceIntent)
|
||||
else {
|
||||
val timeInMinutes1 = intent?.getIntExtra("timeInMinutes1", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
|
||||
val timeInMinutes2 = intent?.getIntExtra("timeInMinutes2", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
|
||||
val scheduleSeparately = intent?.getBooleanExtra("scheduleSeparately", false) ?: false
|
||||
val type = intent?.getIntExtra("type", Type.BOTH.ordinal) ?: Type.BOTH.ordinal
|
||||
val serviceIntent = Intent().apply {
|
||||
putExtra("timeInMinutes1", timeInMinutes1)
|
||||
putExtra("timeInMinutes2", timeInMinutes2)
|
||||
putExtra("scheduleSeparately", scheduleSeparately)
|
||||
putExtra("type", type)
|
||||
}
|
||||
if (type == Type.BOTH.ordinal || type == Type.HOME.ordinal) {
|
||||
serviceIntent.setClass(context, WallpaperService1::class.java).apply {
|
||||
action = WallpaperService1.Actions.START.toString()
|
||||
}
|
||||
} else {
|
||||
serviceIntent.setClass(context, WallpaperService2::class.java).apply {
|
||||
action = WallpaperService2.Actions.START.toString()
|
||||
}
|
||||
}
|
||||
context.startService(serviceIntent)
|
||||
|
||||
// Schedule next alarm for next wallpaper change
|
||||
val origin = intent?.getIntExtra("origin", -1)?.takeIf { it != -1 }
|
||||
WallpaperScheduler(context).scheduleWallpaperAlarm(
|
||||
WallpaperAlarmItem(
|
||||
timeInMinutes1 = timeInMinutes1,
|
||||
timeInMinutes2 = timeInMinutes2,
|
||||
scheduleSeparately = scheduleSeparately
|
||||
),
|
||||
origin
|
||||
)
|
||||
// Schedule next alarm for next wallpaper change
|
||||
val origin = intent?.getIntExtra("origin", -1)?.takeIf { it != -1 }
|
||||
WallpaperScheduler(context).scheduleWallpaperAlarm(
|
||||
WallpaperAlarmItem(
|
||||
timeInMinutes1 = timeInMinutes1,
|
||||
timeInMinutes2 = timeInMinutes2,
|
||||
scheduleSeparately = scheduleSeparately
|
||||
),
|
||||
origin
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
-8
@@ -22,6 +22,9 @@ class WallpaperScheduler (
|
||||
): WallpaperAlarmScheduler {
|
||||
private val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
|
||||
/**
|
||||
* Schedules the wallpaper alarm based on the origin and changeImmediate
|
||||
*/
|
||||
override fun scheduleWallpaperAlarm(wallpaperAlarmItem: WallpaperAlarmItem, origin: Int?, changeImmediate: Boolean, cancelImmediate: Boolean) {
|
||||
if (cancelImmediate) cancelWallpaperAlarm()
|
||||
else {
|
||||
@@ -37,21 +40,21 @@ class WallpaperScheduler (
|
||||
}
|
||||
if (wallpaperAlarmItem.scheduleSeparately) {
|
||||
when (origin) {
|
||||
Type.HOME.ordinal -> {
|
||||
if (changeImmediate) changeWallpaperImmediate(wallpaperAlarmItem, Type.HOME)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, origin)
|
||||
}
|
||||
Type.LOCK.ordinal -> {
|
||||
if (changeImmediate) changeWallpaperImmediate(wallpaperAlarmItem, Type.LOCK)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.LOCK, origin)
|
||||
}
|
||||
Type.HOME.ordinal -> {
|
||||
if (changeImmediate) changeWallpaperImmediate(wallpaperAlarmItem, Type.HOME)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, origin)
|
||||
}
|
||||
null -> {
|
||||
if (changeImmediate) {
|
||||
changeWallpaperImmediate(wallpaperAlarmItem, Type.HOME)
|
||||
changeWallpaperImmediate(wallpaperAlarmItem, Type.LOCK)
|
||||
changeWallpaperImmediate(wallpaperAlarmItem, Type.HOME)
|
||||
}
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, Type.HOME.ordinal)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.LOCK, Type.LOCK.ordinal)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, Type.HOME.ordinal)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,31 +64,41 @@ class WallpaperScheduler (
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the alarms
|
||||
*/
|
||||
override fun updateWallpaperAlarm(wallpaperAlarmItem: WallpaperAlarmItem) {
|
||||
cancelWallpaperAlarm()
|
||||
if (wallpaperAlarmItem.scheduleSeparately) {
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, null, true)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.LOCK, null, true)
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.HOME, null, true)
|
||||
}
|
||||
else {
|
||||
scheduleWallpaper(wallpaperAlarmItem, Type.BOTH, null, true)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the wallpaper without scheduling alarms
|
||||
*/
|
||||
override fun updateWallpaper(scheduleSeparately: Boolean) {
|
||||
if (scheduleSeparately) {
|
||||
updateWallpaper(Type.HOME)
|
||||
updateWallpaper(Type.LOCK)
|
||||
updateWallpaper(Type.HOME)
|
||||
}
|
||||
else {
|
||||
updateWallpaper(Type.BOTH)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels all active alarms
|
||||
*/
|
||||
override fun cancelWallpaperAlarm() {
|
||||
cancelAlarm(Type.HOME)
|
||||
cancelAlarm(Type.LOCK)
|
||||
cancelAlarm(Type.BOTH)
|
||||
cancelAlarm(Type.REFRESH)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,6 +108,7 @@ class WallpaperScheduler (
|
||||
val nextTime = when (type) {
|
||||
Type.HOME, Type.BOTH -> LocalDateTime.now().plusMinutes(wallpaperAlarmItem.timeInMinutes1.toLong())
|
||||
Type.LOCK -> LocalDateTime.now().plusMinutes(wallpaperAlarmItem.timeInMinutes2.toLong())
|
||||
else -> LocalDateTime.now()
|
||||
}
|
||||
val intent = Intent(context, WallpaperReceiver::class.java).apply {
|
||||
putExtra("timeInMinutes1", wallpaperAlarmItem.timeInMinutes1)
|
||||
@@ -156,6 +170,27 @@ class WallpaperScheduler (
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a refresh alarm every 24hrs
|
||||
*/
|
||||
fun scheduleRefresh() {
|
||||
cancelAlarm(Type.REFRESH)
|
||||
val intent = Intent(context, WallpaperReceiver::class.java).apply {
|
||||
putExtra("refresh", true)
|
||||
}
|
||||
val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0)
|
||||
alarmManager.setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
Type.REFRESH.ordinal,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the service to change the wallpaper
|
||||
*/
|
||||
|
||||
+33
-23
@@ -72,7 +72,8 @@ class WallpaperService1: Service() {
|
||||
enum class Actions {
|
||||
START,
|
||||
REQUEUE,
|
||||
UPDATE
|
||||
UPDATE,
|
||||
REFRESH
|
||||
}
|
||||
|
||||
override fun onBind(p0: Intent?): IBinder? {
|
||||
@@ -110,6 +111,9 @@ class WallpaperService1: Service() {
|
||||
Actions.UPDATE.toString() -> {
|
||||
workerTaskUpdate()
|
||||
}
|
||||
Actions.REFRESH.toString() -> {
|
||||
workerTaskRefresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
return START_NOT_STICKY
|
||||
@@ -124,8 +128,7 @@ class WallpaperService1: Service() {
|
||||
private fun workerTaskStart(setHomeOrLock: Boolean? = null) {
|
||||
workerHandler.post {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
refreshAlbum(this@WallpaperService1)
|
||||
delay(2000)
|
||||
if (scheduleSeparately) delay(5000)
|
||||
changeWallpaper(this@WallpaperService1, setHomeOrLock)
|
||||
}
|
||||
stopSelf()
|
||||
@@ -155,6 +158,15 @@ class WallpaperService1: Service() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun workerTaskRefresh() {
|
||||
workerHandler.post {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
refreshAlbum(this@WallpaperService1)
|
||||
}
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a notification for the wallpaper service
|
||||
*/
|
||||
@@ -252,9 +264,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = if (setHomeOrLock) it.album.homeWallpapersInQueue.drop(1) else it.album.homeWallpapersInQueue,
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) it.album.lockWallpapersInQueue.drop(1) else it.album.lockWallpapersInQueue,
|
||||
currentHomeWallpaper = if (setHomeOrLock) wallpaper else it.album.currentHomeWallpaper,
|
||||
currentLockWallpaper = if (!setHomeOrLock) wallpaper else it.album.currentLockWallpaper
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) it.album.lockWallpapersInQueue.drop(1) else it.album.lockWallpapersInQueue
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -299,9 +309,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = if (setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.homeWallpapersInQueue,
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.lockWallpapersInQueue,
|
||||
currentHomeWallpaper = if (setHomeOrLock) wallpaper else it.album.currentHomeWallpaper,
|
||||
currentLockWallpaper = if (!setHomeOrLock) wallpaper else it.album.currentLockWallpaper
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.lockWallpapersInQueue
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 }
|
||||
)
|
||||
@@ -344,9 +352,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.drop(1),
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper },
|
||||
currentHomeWallpaper = null,
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -391,9 +397,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.drop(1),
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper },
|
||||
currentHomeWallpaper = null,
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 }
|
||||
)
|
||||
@@ -468,8 +472,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.filter { it != wallpaper1 },
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 },
|
||||
currentHomeWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -498,8 +501,7 @@ class WallpaperService1: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.filter { it != wallpaper1 },
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 },
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -527,8 +529,16 @@ class WallpaperService1: Service() {
|
||||
setHome: Boolean, setLock: Boolean,
|
||||
setLockOrHome: Boolean? = null,
|
||||
blur: Boolean = false,
|
||||
blurPercent: Int
|
||||
blurPercent: Int,
|
||||
): Boolean {
|
||||
when {
|
||||
setLockOrHome == true -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
|
||||
setLockOrHome == false -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
|
||||
setHome && setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
|
||||
setHome -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
|
||||
setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
|
||||
else -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
|
||||
}
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
try {
|
||||
val imageSize = wallpaper.getImageDimensions(context) ?: return false
|
||||
@@ -631,7 +641,7 @@ class WallpaperService1: Service() {
|
||||
albumWithWallpapers.forEach { albumWithWallpaper ->
|
||||
// Delete wallpaper if the URI is invalid
|
||||
val invalidWallpapers = albumWithWallpaper.wallpapers.filterNot { wallpaper ->
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
|
||||
val file = DocumentFileCompat.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
|
||||
file?.exists() == true
|
||||
}
|
||||
if (invalidWallpapers.isNotEmpty()) {
|
||||
@@ -646,7 +656,7 @@ class WallpaperService1: Service() {
|
||||
albumRepository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
@@ -657,7 +667,7 @@ class WallpaperService1: Service() {
|
||||
albumRepository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFileCompat.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
|
||||
+17
-125
@@ -19,7 +19,6 @@ import android.util.DisplayMetrics
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import com.anthonyla.paperize.R
|
||||
import com.anthonyla.paperize.core.ScalingConstants
|
||||
import com.anthonyla.paperize.core.SettingsConstants
|
||||
@@ -30,19 +29,14 @@ import com.anthonyla.paperize.core.darkenBitmap
|
||||
import com.anthonyla.paperize.core.fillBitmap
|
||||
import com.anthonyla.paperize.core.fitBitmap
|
||||
import com.anthonyla.paperize.core.getImageDimensions
|
||||
import com.anthonyla.paperize.core.getWallpaperFromFolder
|
||||
import com.anthonyla.paperize.core.stretchBitmap
|
||||
import com.anthonyla.paperize.data.settings.SettingsDataStore
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.AlbumRepository
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.MainActivity
|
||||
import com.lazygeniouz.dfc.file.DocumentFileCompat
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.IOException
|
||||
@@ -124,9 +118,6 @@ class WallpaperService2: Service() {
|
||||
private fun workerTaskStart(setHomeOrLock: Boolean? = null) {
|
||||
workerHandler.post {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
delay(3000)
|
||||
refreshAlbum(this@WallpaperService2)
|
||||
delay(2000)
|
||||
changeWallpaper(this@WallpaperService2, setHomeOrLock)
|
||||
}
|
||||
stopSelf()
|
||||
@@ -247,7 +238,7 @@ class WallpaperService2: Service() {
|
||||
setLock = setLock,
|
||||
setLockOrHome = setHomeOrLock,
|
||||
blur = blur,
|
||||
blurPercent = blurPercentage
|
||||
blurPercent = blurPercentage,
|
||||
)) {
|
||||
selectedAlbum.wallpapers.firstOrNull { it.wallpaperUri == wallpaper }
|
||||
?.let { it1 ->
|
||||
@@ -257,8 +248,6 @@ class WallpaperService2: Service() {
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = if (setHomeOrLock) it.album.homeWallpapersInQueue.drop(1) else it.album.homeWallpapersInQueue,
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) it.album.lockWallpapersInQueue.drop(1) else it.album.lockWallpapersInQueue,
|
||||
currentHomeWallpaper = if (setHomeOrLock) wallpaper else it.album.currentHomeWallpaper,
|
||||
currentLockWallpaper = if (!setHomeOrLock) wallpaper else it.album.currentLockWallpaper
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -303,9 +292,7 @@ class WallpaperService2: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = if (setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.homeWallpapersInQueue,
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.lockWallpapersInQueue,
|
||||
currentHomeWallpaper = if (setHomeOrLock) wallpaper else it.album.currentHomeWallpaper,
|
||||
currentLockWallpaper = if (!setHomeOrLock) wallpaper else it.album.currentLockWallpaper
|
||||
lockWallpapersInQueue = if (!setHomeOrLock) newWallpaperInQueue.drop(1) else it.album.lockWallpapersInQueue
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 }
|
||||
)
|
||||
@@ -339,7 +326,7 @@ class WallpaperService2: Service() {
|
||||
setHome = setHome,
|
||||
setLock = setLock,
|
||||
blur = blur,
|
||||
blurPercent = blurPercentage
|
||||
blurPercent = blurPercentage,
|
||||
)) {
|
||||
selectedAlbum.wallpapers.firstOrNull { it.wallpaperUri == wallpaper }
|
||||
?.let { it1 ->
|
||||
@@ -348,9 +335,7 @@ class WallpaperService2: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.drop(1),
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper },
|
||||
currentHomeWallpaper = null,
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -364,7 +349,7 @@ class WallpaperService2: Service() {
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.drop(1),
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper },
|
||||
currentHomeWallpaper = wallpaper,
|
||||
currentLockWallpaper = wallpaper
|
||||
currentLockWallpaper = wallpaper,
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -385,7 +370,7 @@ class WallpaperService2: Service() {
|
||||
setLock = setLock,
|
||||
setLockOrHome = setHomeOrLock,
|
||||
blur = blur,
|
||||
blurPercent = blurPercentage
|
||||
blurPercent = blurPercentage,
|
||||
)
|
||||
) {
|
||||
selectedAlbum.wallpapers.firstOrNull { it.wallpaperUri == wallpaper }
|
||||
@@ -395,9 +380,7 @@ class WallpaperService2: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.drop(1),
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper },
|
||||
currentHomeWallpaper = null,
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 }
|
||||
)
|
||||
@@ -472,8 +455,7 @@ class WallpaperService2: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.filter { it != wallpaper1 },
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 },
|
||||
currentHomeWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -502,8 +484,7 @@ class WallpaperService2: Service() {
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
homeWallpapersInQueue = it.album.homeWallpapersInQueue.filter { it != wallpaper1 },
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 },
|
||||
currentLockWallpaper = null
|
||||
lockWallpapersInQueue = it.album.lockWallpapersInQueue.filter { it != wallpaper1 }
|
||||
),
|
||||
wallpapers = it.wallpapers.filter { it == it1 },
|
||||
)
|
||||
@@ -533,6 +514,14 @@ class WallpaperService2: Service() {
|
||||
blur: Boolean = false,
|
||||
blurPercent: Int
|
||||
): Boolean {
|
||||
when {
|
||||
setLockOrHome == true -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
|
||||
setLockOrHome == false -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
|
||||
setHome && setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
|
||||
setHome -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for home")
|
||||
setLock -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for lock")
|
||||
else -> Log.d("PaperizeWallpaperChanger", "Setting wallpaper for both")
|
||||
}
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
try {
|
||||
val imageSize = wallpaper.getImageDimensions(context) ?: return false
|
||||
@@ -627,101 +616,4 @@ class WallpaperService2: Service() {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshAlbum(context: Context) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
var albumWithWallpapers = albumRepository.getAlbumsWithWallpaperAndFolder().first()
|
||||
albumWithWallpapers.forEach { albumWithWallpaper ->
|
||||
// Delete wallpaper if the URI is invalid
|
||||
val invalidWallpapers = albumWithWallpaper.wallpapers.filterNot { wallpaper ->
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.wallpaperUri.toUri())
|
||||
file?.exists() == true
|
||||
}
|
||||
if (invalidWallpapers.isNotEmpty()) {
|
||||
albumRepository.deleteWallpaperList(invalidWallpapers)
|
||||
}
|
||||
|
||||
// Update folder cover uri and wallpapers uri
|
||||
albumWithWallpaper.folders.forEach { folder ->
|
||||
try {
|
||||
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
|
||||
if (!folderDirectory.isDirectory()) {
|
||||
albumRepository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
DocumentFile.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
|
||||
if (!folderDirectory.isDirectory) {
|
||||
albumRepository.deleteFolder(folder)
|
||||
} else {
|
||||
val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
|
||||
val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
|
||||
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
|
||||
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete empty albums
|
||||
if (albumWithWallpaper.wallpapers.isEmpty() && albumWithWallpaper.folders.all { it.wallpapers.isEmpty() }) {
|
||||
albumRepository.deleteAlbum(albumWithWallpaper.album)
|
||||
}
|
||||
}
|
||||
|
||||
// Update selected album
|
||||
albumWithWallpapers = albumRepository.getAlbumsWithWallpaperAndFolder().first()
|
||||
val selectedAlbum = selectedRepository.getSelectedAlbum().first().firstOrNull()
|
||||
if (selectedAlbum != null) {
|
||||
albumWithWallpapers.find { it.album.initialAlbumName == selectedAlbum.album.initialAlbumName }
|
||||
?.let { foundAlbum ->
|
||||
val albumNameHashCode = foundAlbum.album.initialAlbumName.hashCode()
|
||||
val wallpapers: List<Wallpaper> =
|
||||
foundAlbum.wallpapers + foundAlbum.folders.flatMap { folder ->
|
||||
folder.wallpapers.map { wallpaper ->
|
||||
Wallpaper(
|
||||
initialAlbumName = foundAlbum.album.initialAlbumName,
|
||||
wallpaperUri = wallpaper,
|
||||
key = wallpaper.hashCode() + albumNameHashCode,
|
||||
)
|
||||
}
|
||||
}
|
||||
val wallpapersUri = wallpapers.map { it.wallpaperUri }.toSet()
|
||||
if (wallpapersUri.isEmpty()) {
|
||||
selectedRepository.deleteAll()
|
||||
onDestroy()
|
||||
}
|
||||
else {
|
||||
val newSelectedAlbum = SelectedAlbum(
|
||||
album = foundAlbum.album.copy(
|
||||
homeWallpapersInQueue = if (selectedAlbum.album.homeWallpapersInQueue.firstOrNull() in wallpapersUri) {
|
||||
val firstElement = selectedAlbum.album.homeWallpapersInQueue.first()
|
||||
val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
|
||||
listOf(firstElement) + modifiedWallpapersUri.shuffled()
|
||||
} else { wallpapersUri.shuffled() },
|
||||
lockWallpapersInQueue = if (selectedAlbum.album.lockWallpapersInQueue.firstOrNull() in wallpapersUri) {
|
||||
val firstElement = selectedAlbum.album.lockWallpapersInQueue.first()
|
||||
val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
|
||||
listOf(firstElement) + modifiedWallpapersUri.shuffled()
|
||||
} else { wallpapersUri.shuffled() },
|
||||
currentHomeWallpaper = if (selectedAlbum.album.currentHomeWallpaper in wallpapersUri) selectedAlbum.album.currentHomeWallpaper else selectedAlbum.album.homeWallpapersInQueue.firstOrNull { it in wallpapersUri },
|
||||
currentLockWallpaper = if (selectedAlbum.album.currentLockWallpaper in wallpapersUri) selectedAlbum.album.currentLockWallpaper else selectedAlbum.album.lockWallpapersInQueue.firstOrNull { it in wallpapersUri }
|
||||
),
|
||||
wallpapers = wallpapers
|
||||
)
|
||||
selectedRepository.upsertSelectedAlbum(newSelectedAlbum)
|
||||
}
|
||||
} ?: run { onDestroy() }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("PaperizeWallpaperChanger", "Error refreshing album", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user