Skip to content

Commit

Permalink
feat: implementation of update time in playlists closes #131
Browse files Browse the repository at this point in the history
  • Loading branch information
Malopieds committed Jun 26, 2024
1 parent 471b63d commit b1c2ca6
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ enum class AlbumSortType {
}

enum class PlaylistSortType {
CREATE_DATE, NAME, SONG_COUNT
CREATE_DATE, NAME, SONG_COUNT, LAST_UPDATED
}

enum class MixSortType {
CREATE_DATE, NAME
CREATE_DATE, NAME, LAST_UPDATED
}

enum class MyTopFilter {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/zionhuang/music/db/DatabaseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ interface DatabaseDao {
@Query("SELECT *, (SELECT COUNT(*) FROM playlist_song_map WHERE playlistId = playlist.id) AS songCount FROM playlist ORDER BY rowId")
fun playlistsByCreateDateAsc(): Flow<List<Playlist>>

@Transaction
@Query("SELECT *, (SELECT COUNT(*) FROM playlist_song_map WHERE playlistId = playlist.id) AS songCount FROM playlist ORDER BY lastUpdateTime")
fun playlistsByUpdatedDateAsc(): Flow<List<Playlist>>

@Transaction
@Query("SELECT *, (SELECT COUNT(*) FROM playlist_song_map WHERE playlistId = playlist.id) AS songCount FROM playlist ORDER BY name")
fun playlistsByNameAsc(): Flow<List<Playlist>>
Expand All @@ -569,6 +573,7 @@ interface DatabaseDao {
PlaylistSortType.CREATE_DATE -> playlistsByCreateDateAsc()
PlaylistSortType.NAME -> playlistsByNameAsc()
PlaylistSortType.SONG_COUNT -> playlistsBySongCountAsc()
PlaylistSortType.LAST_UPDATED -> playlistsByUpdatedDateAsc()
}.map { it.reversed(descending) }

@Transaction
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/zionhuang/music/db/MusicDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zionhuang.music.db

import android.annotation.SuppressLint
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import androidx.core.content.contentValuesOf
Expand Down Expand Up @@ -59,7 +60,7 @@ class MusicDatabase(
SortedSongAlbumMap::class,
PlaylistSongMapPreview::class
],
version = 13,
version = 14,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 2, to = 3),
Expand All @@ -72,7 +73,8 @@ class MusicDatabase(
AutoMigration(from = 9, to = 10, spec = Migration9To10::class),
AutoMigration(from = 10, to = 11, spec = Migration10To11::class),
AutoMigration(from = 11, to = 12, spec = Migration11To12::class),
AutoMigration(from = 12, to = 13, spec = Migration12To13::class)
AutoMigration(from = 12, to = 13, spec = Migration12To13::class),
AutoMigration(from = 13, to = 14, spec = Migration13To14::class),
]
)
@TypeConverters(Converters::class)
Expand Down Expand Up @@ -352,3 +354,11 @@ class Migration12To13 : AutoMigrationSpec {
db.execSQL("UPDATE song SET liked = 1 WHERE inLibrary IS NOT NULL")
}
}

class Migration13To14 : AutoMigrationSpec {
@SuppressLint("Range")
override fun onPostMigrate(db: SupportSQLiteDatabase) {
db.execSQL("UPDATE playlist SET createdAt = '${Converters().dateToTimestamp(LocalDateTime.now())}'")
db.execSQL("UPDATE playlist SET lastUpdateTime = '${Converters().dateToTimestamp(LocalDateTime.now())}'")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import androidx.compose.runtime.Immutable
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.apache.commons.lang3.RandomStringUtils
import java.time.LocalDateTime

@Immutable
@Entity(tableName = "playlist")
data class PlaylistEntity(
@PrimaryKey val id: String = generatePlaylistId(),
val name: String,
val browseId: String? = null,
val createdAt: LocalDateTime? = LocalDateTime.now(),
val lastUpdateTime: LocalDateTime? = LocalDateTime.now(),
) {
companion object {
const val LIKED_PLAYLIST_ID = "LP_LIKED"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/AlbumMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.zionhuang.music.ui.component.DownloadGridMenu
import com.zionhuang.music.ui.component.GridMenu
import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import java.time.LocalDateTime

@Composable
fun AlbumMenu(
Expand Down Expand Up @@ -127,6 +128,7 @@ fun AlbumMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/PlayerMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import com.zionhuang.music.ui.component.DownloadGridMenu
import com.zionhuang.music.ui.component.GridMenu
import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import java.time.LocalDateTime
import kotlin.math.log2
import kotlin.math.pow
import kotlin.math.round
Expand Down Expand Up @@ -101,6 +102,7 @@ fun PlayerMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
},
onDismiss = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.zionhuang.music.utils.makeTimeString
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.LocalDateTime

@Composable
fun PlaylistMenu(
Expand Down Expand Up @@ -126,7 +127,7 @@ fun PlaylistMenu(
onDone = { name ->
onDismiss()
database.query {
update(playlist.playlist.copy(name = name))
update(playlist.playlist.copy(name = name, lastUpdateTime = LocalDateTime.now()))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.zionhuang.music.ui.component.DefaultDialog
import com.zionhuang.music.ui.component.DownloadGridMenu
import com.zionhuang.music.ui.component.GridMenu
import com.zionhuang.music.ui.component.GridMenuItem
import java.time.LocalDateTime

@Composable
fun SelectionSongMenu(
Expand Down Expand Up @@ -88,6 +89,7 @@ fun SelectionSongMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
},
Expand Down Expand Up @@ -294,6 +296,7 @@ fun SelectionMediaMetadataMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/SongMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import com.zionhuang.music.ui.component.SongListItem
import com.zionhuang.music.ui.component.TextFieldDialog
import java.time.LocalDateTime

@Composable
fun SongMenu(
Expand Down Expand Up @@ -111,6 +112,7 @@ fun SongMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
},
onDismiss = { showChoosePlaylistDialog = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import com.zionhuang.music.ui.component.YouTubeListItem
import com.zionhuang.music.utils.reportException
import java.time.LocalDateTime

@Composable
fun YouTubeAlbumMenu(
Expand Down Expand Up @@ -120,6 +121,7 @@ fun YouTubeAlbumMenu(
position = position++
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDateTime

@Composable
fun YouTubePlaylistMenu(
Expand Down Expand Up @@ -68,6 +69,7 @@ fun YouTubePlaylistMenu(
position = position++
)
)
update(targetPlaylist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import com.zionhuang.music.ui.component.ListDialog
import com.zionhuang.music.ui.component.ListItem
import com.zionhuang.music.utils.joinByBullet
import com.zionhuang.music.utils.makeTimeString
import java.time.LocalDateTime

@Composable
fun YouTubeSongMenu(
Expand Down Expand Up @@ -99,6 +100,7 @@ fun YouTubeSongMenu(
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
},
onDismiss = { showChoosePlaylistDialog = false }
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/zionhuang/music/ui/player/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import com.zionhuang.music.utils.makeTimeString
import com.zionhuang.music.utils.rememberPreference
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import java.time.LocalDateTime
import kotlin.math.roundToInt

@OptIn(ExperimentalFoundationApi::class, ExperimentalAnimationApi::class)
Expand Down Expand Up @@ -232,11 +233,12 @@ fun BottomSheetPlayer(
insert(it)
insert(
PlaylistSongMap(
songId = it.id,
playlistId = playlist.id,
position = playlist.songCount
)
songId = it.id,
playlistId = playlist.id,
position = playlist.songCount
)
)
update(playlist.playlist.copy(lastUpdateTime = LocalDateTime.now()))
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ fun LibraryMixScreen(
MixSortType.CREATE_DATE -> when (item) {
is Album -> item.album.bookmarkedAt
is Artist -> item.artist.bookmarkedAt
is Playlist -> item.playlist.createdAt
else -> LocalDateTime.now()
}

MixSortType.LAST_UPDATED -> when(item) {
is Album -> item.album.lastUpdateTime
is Artist -> item.artist.lastUpdateTime
is Playlist -> item.playlist.lastUpdateTime
else -> LocalDateTime.now()
}

Expand Down Expand Up @@ -141,6 +149,7 @@ fun LibraryMixScreen(
sortTypeText = { sortType ->
when (sortType) {
MixSortType.CREATE_DATE -> R.string.sort_by_create_date
MixSortType.LAST_UPDATED -> R.string.sort_by_last_updated
MixSortType.NAME -> R.string.sort_by_name
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fun LibraryPlaylistsScreen(
PlaylistSortType.CREATE_DATE -> R.string.sort_by_create_date
PlaylistSortType.NAME -> R.string.sort_by_name
PlaylistSortType.SONG_COUNT -> R.string.sort_by_song_count
PlaylistSortType.LAST_UPDATED -> R.string.sort_by_last_updated
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import org.burnoutcrew.reorderable.ReorderableItem
import org.burnoutcrew.reorderable.detectReorder
import org.burnoutcrew.reorderable.rememberReorderableLazyListState
import org.burnoutcrew.reorderable.reorderable
import java.time.LocalDateTime

@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
@Composable
Expand Down Expand Up @@ -189,7 +190,7 @@ fun LocalPlaylistScreen(
initialTextFieldValue = TextFieldValue(playlistEntity.name, TextRange(playlistEntity.name.length)),
onDone = { name ->
database.query {
update(playlistEntity.copy(name = name))
update(playlistEntity.copy(name = name, lastUpdateTime = LocalDateTime.now()))
}
}
)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@

<!-- Sort menu -->
<string name="sort_by_create_date">Date added</string>
<string name="sort_by_last_updated">Date updated</string>
<string name="sort_by_name">Name</string>
<string name="sort_by_artist">Artist</string>
<string name="sort_by_year">Year</string>
Expand Down

0 comments on commit b1c2ca6

Please sign in to comment.