Moved shuffled album functionality into the album model to reduce complexity
This commit is contained in:
1 parent
eb99329521
commit
37858a3e5c
18 files changed
+166
-159
No files matched your search
@@ -6,6 +6,7 @@
|
||||
android:name="android.software.paperize"
|
||||
android:required="true" />
|
||||
|
||||
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
||||
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
|
||||
|
||||
<application
|
||||
|
||||
+65
-22
@@ -6,6 +6,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageDecoder
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.net.toUri
|
||||
@@ -14,6 +15,8 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
@@ -21,40 +24,80 @@ import javax.inject.Inject
|
||||
@AndroidEntryPoint
|
||||
class WallpaperReceiver: BroadcastReceiver() {
|
||||
@Inject lateinit var repository: SelectedAlbumRepository
|
||||
@Inject lateinit var wallpaperScheduler: WallpaperScheduler
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
val defaultInterval = 15L
|
||||
val timeInMinutes = intent?.getLongExtra("timeInMinutes", defaultInterval) ?: defaultInterval
|
||||
if (context != null) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
Log.d("WallpaperReceiver", "Changing wallpaper. Available wallpaper: " + repository.countWallpapersInRotation())
|
||||
Log.d("WallpaperReceiver", "Changing wallpaper")
|
||||
changeWallpaper(context)
|
||||
wallpaperScheduler.scheduleWallpaperChanger(timeInMinutes, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the wallpaper to the next wallpaper in the queue of the selected album
|
||||
* If none left, reshuffle the wallpapers and pick the first one
|
||||
*/
|
||||
private suspend fun changeWallpaper(context: Context) {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
val wallpaper = repository.getRandomWallpaperInRotation()
|
||||
if (wallpaper != null) {
|
||||
try {
|
||||
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
val source = ImageDecoder.createSource(
|
||||
context.contentResolver,
|
||||
wallpaper.wallpaperUri.toUri()
|
||||
val album = repository.getSelectedAlbum().first().firstOrNull()
|
||||
album?.let {
|
||||
var wallpaper = it.album.wallpapersInQueue.firstOrNull()
|
||||
if (wallpaper != null) {
|
||||
setWallpaper(context, wallpaper.toUri())
|
||||
Log.d("WallpaperReceiver", "Count: ${it.album.wallpapersInQueue.size}")
|
||||
/*
|
||||
repository.upsertSelectedAlbum(
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
wallpapersInQueue = it.album.wallpapersInQueue.drop(1)
|
||||
)
|
||||
)
|
||||
ImageDecoder.decodeBitmap(source)
|
||||
} else {
|
||||
context.contentResolver.openInputStream(wallpaper.wallpaperUri.toUri())
|
||||
?.use { inputStream ->
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
}
|
||||
bitmap?.let { image -> wallpaperManager.setBitmap(image) }
|
||||
repository.upsertWallpaper(wallpaper.copy(isInRotation = false))
|
||||
} catch (e: IOException) {
|
||||
Log.e("WallpaperReceiver", "Error changing wallpaper", e)
|
||||
)
|
||||
*/
|
||||
}
|
||||
/*
|
||||
else {
|
||||
val newWallpaperInQueue = it.wallpapers.map { it.wallpaperUri }.shuffled()
|
||||
wallpaper = newWallpaperInQueue.firstOrNull()
|
||||
if (wallpaper != null) {
|
||||
setWallpaper(context, wallpaper.toUri())
|
||||
Log.d("WallpaperReceiver", "Count before: ${it.album.wallpapersInQueue.size}")
|
||||
repository.upsertSelectedAlbum(
|
||||
it.copy(
|
||||
album = it.album.copy(
|
||||
wallpapersInQueue = newWallpaperInQueue.drop(1)
|
||||
)
|
||||
)
|
||||
)
|
||||
Log.d("WallpaperReceiver", "Count after: ${it.album.wallpapersInQueue.size}")
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
else {
|
||||
repository.setAllWallpapersInRotation()
|
||||
}
|
||||
|
||||
private fun setWallpaper(context: Context, wallpaper: Uri) {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
try {
|
||||
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
val source = ImageDecoder.createSource(
|
||||
context.contentResolver,
|
||||
wallpaper
|
||||
)
|
||||
ImageDecoder.decodeBitmap(source)
|
||||
} else {
|
||||
context.contentResolver.openInputStream(wallpaper)
|
||||
?.use { inputStream ->
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
}
|
||||
bitmap?.let { image -> wallpaperManager.setBitmap(image) }
|
||||
} catch (e: IOException) {
|
||||
Log.e("WallpaperReceiver", "Error changing wallpaper", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
-14
@@ -4,35 +4,64 @@ import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
class WallpaperScheduler (
|
||||
class WallpaperScheduler @Inject constructor (
|
||||
private val context: Context
|
||||
) {
|
||||
private val alarmManager = context.getSystemService(AlarmManager::class.java)
|
||||
private val requestCode = "Paperize".hashCode()
|
||||
|
||||
fun scheduleWallpaperChanger(timeInMinutes: Long) {
|
||||
fun scheduleWallpaperChanger(timeInMinutes: Long, activateImmediately: Boolean = true) {
|
||||
val intervalInMillis = TimeUnit.MINUTES.toMillis(timeInMinutes)
|
||||
alarmManager.setRepeating(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
System.currentTimeMillis(),
|
||||
intervalInMillis,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
0,
|
||||
Intent(context, WallpaperReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
val intent = Intent(context, WallpaperReceiver::class.java).apply {
|
||||
putExtra("timeInMinutes", timeInMinutes)
|
||||
}
|
||||
|
||||
if (activateImmediately) {
|
||||
context.sendBroadcast(intent)
|
||||
}
|
||||
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
if (alarmManager.canScheduleExactAlarms()) {
|
||||
alarmManager.setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
System.currentTimeMillis() + intervalInMillis,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
Log.d("WallpaperReceiver", "Scheduled wallpaper changer in ${TimeUnit.MILLISECONDS.toMinutes(intervalInMillis)} minutes")
|
||||
} else {
|
||||
// Handle the case where the app cannot schedule exact alarms
|
||||
Log.e("WallpaperReceiver", "Cannot schedule exact alarms")
|
||||
cancelWallpaperChanger()
|
||||
}
|
||||
} else {
|
||||
alarmManager.setExact(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
System.currentTimeMillis() + intervalInMillis,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
)
|
||||
Log.d("WallpaperReceiver", "Scheduled wallpaper changer every $timeInMinutes minutes")
|
||||
Log.d("WallpaperReceiver", "Scheduled wallpaper changer in ${TimeUnit.MILLISECONDS.toMinutes(intervalInMillis)} minutes")
|
||||
}
|
||||
}
|
||||
|
||||
fun cancelWallpaperChanger() {
|
||||
alarmManager.cancel(
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
0,
|
||||
requestCode,
|
||||
Intent(context, WallpaperReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
-14
@@ -21,18 +21,4 @@ class Converters {
|
||||
*/
|
||||
@TypeConverter
|
||||
fun jsonStringToList(value: String) = gson.fromJson(value, Array<String>::class.java).toList()
|
||||
|
||||
@TypeConverter
|
||||
fun fromPairList(value: List<Pair<String, Boolean>>): String {
|
||||
val gson = Gson()
|
||||
val type = object : TypeToken<List<Pair<String, Boolean>>>() {}.type
|
||||
return gson.toJson(value, type)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
fun toPairList(value: String): List<Pair<String, Boolean>> {
|
||||
val gson = Gson()
|
||||
val type = object : TypeToken<List<Pair<String, Boolean>>>() {}.type
|
||||
return gson.fromJson(value, type)
|
||||
}
|
||||
}
|
||||
-12
@@ -22,18 +22,6 @@ interface SelectedAlbumDao {
|
||||
@Query("SELECT * FROM album")
|
||||
fun getSelectedAlbum(): Flow<List<SelectedAlbum>>
|
||||
|
||||
@Query("SELECT * FROM wallpaper WHERE isInRotation = 1")
|
||||
fun getWallpapersInRotation(): Flow<List<Wallpaper>>
|
||||
|
||||
@Query("SELECT * FROM wallpaper ORDER BY RANDOM() LIMIT 1")
|
||||
fun getRandomWallpaperInRotation(): Wallpaper?
|
||||
|
||||
@Query("SELECT COUNT(*) FROM wallpaper WHERE isInRotation = 1")
|
||||
fun countWallpapersInRotation(): Int
|
||||
|
||||
@Query("UPDATE wallpaper SET isInRotation = 1")
|
||||
suspend fun setAllWallpapersInRotation()
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun upsertAlbum(album: Album)
|
||||
|
||||
|
||||
-16
@@ -16,22 +16,6 @@ class SelectedAlbumRepositoryImpl(
|
||||
return dao.getSelectedAlbum()
|
||||
}
|
||||
|
||||
override fun getWallpapersInRotation(): Flow<List<Wallpaper>> {
|
||||
return dao.getWallpapersInRotation()
|
||||
}
|
||||
|
||||
override fun getRandomWallpaperInRotation(): Wallpaper? {
|
||||
return dao.getRandomWallpaperInRotation()
|
||||
}
|
||||
|
||||
override fun countWallpapersInRotation(): Int {
|
||||
return dao.countWallpapersInRotation()
|
||||
}
|
||||
|
||||
override suspend fun setAllWallpapersInRotation() {
|
||||
dao.setAllWallpapersInRotation()
|
||||
}
|
||||
|
||||
override suspend fun upsertSelectedAlbum(selectedAlbum: SelectedAlbum) {
|
||||
dao.upsertAlbum(selectedAlbum.album)
|
||||
dao.upsertWallpaperList(selectedAlbum.wallpapers)
|
||||
|
||||
@@ -9,11 +9,14 @@ import androidx.room.PrimaryKey
|
||||
* @param initialAlbumName The initial album name -- should not be changed as it is used as the key for database queries
|
||||
* @param displayedAlbumName The displayed album name
|
||||
* @param coverUri The cover uri of the album
|
||||
* @param initialized Whether the album has been initialized
|
||||
* @param wallpapersInQueue The wallpapers in the queue to be displayed
|
||||
*/
|
||||
@Entity
|
||||
data class Album(
|
||||
@PrimaryKey(autoGenerate = false) val initialAlbumName: String,
|
||||
val displayedAlbumName: String,
|
||||
val coverUri: String?,
|
||||
val initialized: Boolean = false
|
||||
val initialized: Boolean = false,
|
||||
val wallpapersInQueue: List<String> = emptyList()
|
||||
)
|
||||
@@ -9,7 +9,7 @@ import androidx.room.PrimaryKey
|
||||
* @param initialAlbumName The initial album name.
|
||||
* @param folderName The folder name.
|
||||
* @param coverUri The cover URI.
|
||||
* @param wallpapers The list of wallpaper URI strings paired with inRotation boolean as a <Pair>
|
||||
* @param wallpapers The list of wallpaper URI strings in the folder.
|
||||
* @param folderUri The folder directory URI.
|
||||
*/
|
||||
@Entity
|
||||
@@ -17,7 +17,7 @@ data class Folder(
|
||||
val initialAlbumName: String,
|
||||
val folderName: String?,
|
||||
val coverUri: String?,
|
||||
val wallpapers: List<Pair<String, Boolean>> = emptyList(),
|
||||
val wallpapers: List<String> = emptyList(),
|
||||
val folderUri: String,
|
||||
@PrimaryKey(autoGenerate = false) val key: Int
|
||||
)
|
||||
@@ -8,12 +8,10 @@ import androidx.room.PrimaryKey
|
||||
*
|
||||
* @param initialAlbumName The name of the album the wallpaper belongs to.
|
||||
* @param wallpaperUri The URI of the wallpaper.
|
||||
* @param isInRotation Whether the wallpaper is in rotation.
|
||||
*/
|
||||
@Entity
|
||||
data class Wallpaper(
|
||||
val initialAlbumName: String,
|
||||
val wallpaperUri: String,
|
||||
val isInRotation: Boolean,
|
||||
@PrimaryKey(autoGenerate = false) val key: Int
|
||||
)
|
||||
-20
@@ -11,26 +11,6 @@ interface SelectedAlbumRepository {
|
||||
*/
|
||||
fun getSelectedAlbum(): Flow<List<SelectedAlbum>>
|
||||
|
||||
/**
|
||||
* Get wallpapers in rotation from database
|
||||
*/
|
||||
fun getWallpapersInRotation(): Flow<List<Wallpaper>>
|
||||
|
||||
/**
|
||||
* Get random wallpaper in rotation
|
||||
*/
|
||||
fun getRandomWallpaperInRotation(): Wallpaper?
|
||||
|
||||
/**
|
||||
* Get count of wallpapers in rotation
|
||||
*/
|
||||
fun countWallpapersInRotation(): Int
|
||||
|
||||
/**
|
||||
* Add all wallpapers back into rotation
|
||||
*/
|
||||
suspend fun setAllWallpapersInRotation()
|
||||
|
||||
/**
|
||||
* Upsert or insert selectedAlbum to database with its wallpapers
|
||||
*/
|
||||
|
||||
+22
-19
@@ -19,6 +19,8 @@ import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import com.anthonyla.paperize.data.Contact
|
||||
import com.anthonyla.paperize.feature.wallpaper.alarmmanager.WallpaperScheduler
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen.AddAlbumScreen
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
|
||||
@@ -66,26 +68,26 @@ fun PaperizeApp(
|
||||
|
||||
selectedState.value.selectedAlbum?.let { selectedAlbum ->
|
||||
albumState.value.albumsWithWallpapers.find { it.album.initialAlbumName == selectedAlbum.album.initialAlbumName }
|
||||
?.let { foundAlbum -> wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(
|
||||
foundAlbum.copy(
|
||||
album = foundAlbum.album,
|
||||
folders = foundAlbum.folders.map { folder ->
|
||||
folder.copy(
|
||||
wallpapers = folder.wallpapers.map { wallpaper ->
|
||||
Pair(
|
||||
wallpaper.first,
|
||||
selectedAlbum.wallpapers.find { selectedWallpaper -> selectedWallpaper.wallpaperUri == wallpaper.first }?.isInRotation ?: true
|
||||
)
|
||||
}
|
||||
)
|
||||
},
|
||||
wallpapers = foundAlbum.wallpapers.map {
|
||||
it.copy(
|
||||
isInRotation = selectedAlbum.wallpapers.find { wallpaper -> wallpaper.wallpaperUri == it.wallpaperUri }?.isInRotation ?: true
|
||||
?.let { foundAlbum ->
|
||||
val wallpapers: List<Wallpaper> = foundAlbum.wallpapers + foundAlbum.folders.flatMap { folder ->
|
||||
folder.wallpapers.map { wallpaper ->
|
||||
Wallpaper(
|
||||
initialAlbumName = foundAlbum.album.initialAlbumName,
|
||||
wallpaperUri = wallpaper,
|
||||
key = wallpaper.hashCode() + foundAlbum.album.initialAlbumName.hashCode(),
|
||||
)
|
||||
}
|
||||
}
|
||||
val wallpaperUriSet = wallpapers.map { it.wallpaperUri }.toSet()
|
||||
val newSelectedAlbum = SelectedAlbum(
|
||||
album = foundAlbum.album.copy(
|
||||
wallpapersInQueue = selectedAlbum.album.wallpapersInQueue.filter { it in wallpaperUriSet }
|
||||
),
|
||||
wallpapers = wallpapers
|
||||
)
|
||||
)) } ?: run {
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(newSelectedAlbum))
|
||||
}
|
||||
?: run {
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.Reset)
|
||||
wallpaperScheduler.cancelWallpaperChanger()
|
||||
}
|
||||
@@ -118,7 +120,8 @@ fun PaperizeApp(
|
||||
navController.navigate("${NavScreens.AlbumView.route}/$it")
|
||||
},
|
||||
onScheduleWallpaperChanger = { timeInMinutes ->
|
||||
wallpaperScheduler.scheduleWallpaperChanger(timeInMinutes)
|
||||
wallpaperScheduler.cancelWallpaperChanger()
|
||||
wallpaperScheduler.scheduleWallpaperChanger(timeInMinutes, true)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -209,7 +212,7 @@ fun PaperizeApp(
|
||||
},
|
||||
onShowFolderView = { folder, folderName, wallpapers ->
|
||||
val encodedFolder = runBlocking { encodeUri(uri = folder) }
|
||||
val encodedWallpapers = runBlocking { encodeUri(uri = Gson().toJson(wallpapers.map { it.first })) }
|
||||
val encodedWallpapers = runBlocking { encodeUri(uri = Gson().toJson(wallpapers)) }
|
||||
navController.navigate("${NavScreens.FolderView.route}/$encodedFolder/$folderName/$encodedWallpapers")
|
||||
},
|
||||
onDeleteAlbum = {
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ fun AddAlbumScreen(
|
||||
onBackClick: () -> Unit,
|
||||
onConfirmation: () -> Unit,
|
||||
onShowWallpaperView: (String) -> Unit,
|
||||
onShowFolderView: (String, String?, List<Pair<String, Boolean>>) -> Unit
|
||||
onShowFolderView: (String, String?, List<String>) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val lazyListState = rememberLazyStaggeredGridState()
|
||||
|
||||
+1
-2
@@ -122,7 +122,6 @@ class AddAlbumViewModel @Inject constructor(
|
||||
initialAlbumName = it.initialAlbumName,
|
||||
wallpaperUri = event.wallpaperUri,
|
||||
key = event.wallpaperUri.hashCode() + it.initialAlbumName.hashCode(),
|
||||
isInRotation = true
|
||||
)
|
||||
),
|
||||
) }
|
||||
@@ -142,7 +141,7 @@ class AddAlbumViewModel @Inject constructor(
|
||||
folderName = folderName,
|
||||
folderUri = event.directoryUri,
|
||||
coverUri = wallpapers.randomOrNull(),
|
||||
wallpapers = wallpapers.map { it to true },
|
||||
wallpapers = wallpapers,
|
||||
key = event.directoryUri.hashCode() + state.initialAlbumName.hashCode()
|
||||
)
|
||||
),
|
||||
|
||||
+3
-3
@@ -105,7 +105,7 @@ class AlbumsViewModel @Inject constructor (
|
||||
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()
|
||||
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers.map { it to true }))
|
||||
repository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,9 +175,9 @@ class AlbumsViewModel @Inject constructor (
|
||||
}
|
||||
folders.forEach { folder ->
|
||||
folder.wallpapers.forEach { wallpaper ->
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.first.toUri())
|
||||
val file = DocumentFile.fromSingleUri(context, wallpaper.toUri())
|
||||
if (file?.exists() == true) {
|
||||
return wallpaper.first
|
||||
return wallpaper
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ fun AlbumViewScreen(
|
||||
album: AlbumWithWallpaperAndFolder,
|
||||
onBackClick: () -> Unit,
|
||||
onShowWallpaperView: (String) -> Unit,
|
||||
onShowFolderView: (String, String?, List<Pair<String, Boolean>>) -> Unit,
|
||||
onShowFolderView: (String, String?, List<String>) -> Unit,
|
||||
onDeleteAlbum: () -> Unit,
|
||||
onTitleChange: (String, AlbumWithWallpaperAndFolder) -> Unit,
|
||||
onSelectionDeleted: () -> Unit
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen
|
||||
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.AlbumWithWallpaperAndFolder
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
|
||||
sealed class WallpaperEvent {
|
||||
data class UpdateSelectedAlbum(val albumWithWallpaperAndFolder: AlbumWithWallpaperAndFolder): WallpaperEvent()
|
||||
data class ExitRotation(val wallpaper: Wallpaper): WallpaperEvent()
|
||||
data class UpdateSelectedAlbum(val selectedAlbum: SelectedAlbum): WallpaperEvent()
|
||||
object Reset : WallpaperEvent()
|
||||
object Refresh : WallpaperEvent()
|
||||
}
|
||||
+19
-5
@@ -20,6 +20,8 @@ import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.AlbumBottomSheet
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.CurrentSelectedAlbum
|
||||
@@ -37,7 +39,7 @@ fun WallpaperScreen(
|
||||
|
||||
Scaffold(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
content = { it
|
||||
content = { padding ->
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
@@ -59,11 +61,23 @@ fun WallpaperScreen(
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.SET_WALLPAPER) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.SET_WALLPAPER), 0)
|
||||
}
|
||||
|
||||
if ((selectedState.value.selectedAlbum?.album?.initialAlbumName ?: "") != album.album.initialAlbumName) {
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(album))
|
||||
val wallpapers: List<Wallpaper> = album.wallpapers + album.folders.flatMap { folder ->
|
||||
folder.wallpapers.map { wallpaper ->
|
||||
Wallpaper(
|
||||
initialAlbumName = album.album.initialAlbumName,
|
||||
wallpaperUri = wallpaper,
|
||||
key = wallpaper.hashCode() + album.album.initialAlbumName.hashCode(),
|
||||
)
|
||||
}
|
||||
}
|
||||
onScheduleWallpaperChanger(1)
|
||||
val newSelectedAlbum = SelectedAlbum(
|
||||
album = album.album.copy(
|
||||
wallpapersInQueue = wallpapers.map { it.wallpaperUri }.shuffled()
|
||||
),
|
||||
wallpapers = wallpapers
|
||||
)
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(newSelectedAlbum))
|
||||
onScheduleWallpaperChanger(15)
|
||||
openBottomSheet = false
|
||||
}
|
||||
)
|
||||
|
||||
+2
-23
@@ -43,29 +43,8 @@ class WallpaperScreenViewModel @Inject constructor (
|
||||
when (event) {
|
||||
is WallpaperEvent.UpdateSelectedAlbum -> {
|
||||
viewModelScope.launch {
|
||||
repository.getSelectedAlbum().firstOrNull()?.let { repository.deleteAll() }
|
||||
val wallpapers: MutableList<Wallpaper> = event.albumWithWallpaperAndFolder.wallpapers.toMutableList()
|
||||
val wallpapersFromFolder = event.albumWithWallpaperAndFolder.folders.flatMap { folder ->
|
||||
folder.wallpapers.map { wallpaper ->
|
||||
Wallpaper(
|
||||
initialAlbumName = event.albumWithWallpaperAndFolder.album.initialAlbumName,
|
||||
wallpaperUri = wallpaper.first,
|
||||
key = wallpaper.hashCode() + event.albumWithWallpaperAndFolder.album.initialAlbumName.hashCode(),
|
||||
isInRotation = wallpaper.second
|
||||
)
|
||||
}
|
||||
}
|
||||
wallpapers.addAll(wallpapersFromFolder)
|
||||
val newSelectedAlbum = SelectedAlbum(
|
||||
album = event.albumWithWallpaperAndFolder.album,
|
||||
wallpapers = wallpapers
|
||||
)
|
||||
repository.upsertSelectedAlbum(newSelectedAlbum)
|
||||
}
|
||||
}
|
||||
is WallpaperEvent.ExitRotation -> {
|
||||
viewModelScope.launch {
|
||||
repository.upsertWallpaper(event.wallpaper.copy(isInRotation = false))
|
||||
repository.deleteAll()
|
||||
repository.upsertSelectedAlbum(event.selectedAlbum)
|
||||
}
|
||||
}
|
||||
is WallpaperEvent.Refresh -> {
|
||||
|
||||
Reference in new issue
Block a user