Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: BP Revamp in LB-Android Part 1 (Completed Overview Screens) #382

Merged
merged 18 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.listenbrainz.android.data.dao

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
Expand All @@ -24,6 +26,32 @@ import org.listenbrainz.android.model.dao.PlaylistDao
import org.listenbrainz.android.model.dao.SongDao
import org.listenbrainz.sharedtest.utils.CoroutineTestRule


object Migrations {
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
val cursor = db.query("PRAGMA table_info('SONGS')")
var columnExists = false
val columnNameIndex = cursor.getColumnIndex("name")
if (columnNameIndex != -1) {
while (cursor.moveToNext()) {
val columnName = cursor.getString(columnNameIndex)
if (columnName == "lastListenedTo") {
columnExists = true
break
}
}
}
cursor.close()

if (!columnExists) {
db.execSQL(
"ALTER TABLE 'SONGS' ADD COLUMN 'lastListenedTo' INTEGER NOT NULL DEFAULT 0"
)
}
}
}
}
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
@SmallTest
Expand Down Expand Up @@ -61,7 +89,8 @@ class DaoTest {
song.toLong(),
song.toLong(),
song.toLong(),
song.toLong()
song.toLong(),
0
)
}

Expand All @@ -83,6 +112,7 @@ class DaoTest {
song.toLong(),
song.toLong(),
song.toLong(),
song.toLong(),
song.toLong()
)
}
Expand All @@ -107,6 +137,7 @@ class DaoTest {
song.toLong(),
song.toLong(),
song.toLong(),
song.toLong(),
song.toLong()
)

Expand All @@ -127,6 +158,7 @@ class DaoTest {
val context = ApplicationProvider.getApplicationContext<Context>()
brainzPlayerDatabase = Room
.inMemoryDatabaseBuilder(context, BrainzPlayerDatabase::class.java)
.addMigrations(Migrations.MIGRATION_1_2)
.build()
albumDao = brainzPlayerDatabase.albumDao()
artistDao = brainzPlayerDatabase.artistDao()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.listenbrainz.android.util.TypeConverter
ArtistEntity::class,
PlaylistEntity::class
],
version = 1,
version = 2,
exportSchema = false
)
@TypeConverters(TypeConverter::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,42 @@ package org.listenbrainz.android.di.brainzplayer

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

object Migrations {
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
val cursor = db.query("PRAGMA table_info('SONGS')")
var columnExists = false
val columnNameIndex = cursor.getColumnIndex("name")
if (columnNameIndex != -1) {
while (cursor.moveToNext()) {
val columnName = cursor.getString(columnNameIndex)
if (columnName == "lastListenedTo") {
columnExists = true
break
}
}
}
cursor.close()

if (!columnExists) {
db.execSQL(
"ALTER TABLE 'SONGS' ADD COLUMN 'lastListenedTo' INTEGER NOT NULL DEFAULT 0"
)
}
}
}
}


@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
Expand All @@ -21,6 +50,7 @@ object DatabaseModule {
BrainzPlayerDatabase::class.java,
"brainzplayer_database"
)
.addMigrations(Migrations.MIGRATION_1_2)
.build()

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ data class ArtistEntity(
@PrimaryKey(autoGenerate = true)
val artistID: Long = 0,
val name: String,
val songs: List<SongEntity>,
val albums: List<AlbumEntity>
var songs: List<SongEntity>,
var albums: List<AlbumEntity>
)
9 changes: 6 additions & 3 deletions app/src/main/java/org/listenbrainz/android/model/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ data class Song (
val albumID: Long=0L,
val album: String="",
val albumArt: String="",
val discNumber : Long = 0L
val discNumber : Long = 0L,
var lastListenedTo : Long = 0L
) {
companion object {
val emptySong = Song(
Expand All @@ -29,7 +30,8 @@ data class Song (
albumID = 0L,
album = "",
albumArt = "",
discNumber = 0L
discNumber = 0L,
lastListenedTo = 0L
)

fun preview(): Song =
Expand All @@ -46,7 +48,8 @@ data class Song (
albumID = 0L,
album = "Album",
albumArt = "",
discNumber = 0L
discNumber = 0L,
lastListenedTo = 0L
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ data class SongEntity(
val duration : Long,
val dateModified : Long,
val artistId : Long,
val discNumber : Long
val discNumber : Long,
val lastListenedTo : Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ArtistDao {
@Query(value = "SELECT * FROM ARTISTS ORDER BY `name`")
fun getArtistEntitiesAsList() : List<ArtistEntity>

@Insert(onConflict = OnConflictStrategy.NONE)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addArtists(artistEntities: List<ArtistEntity>)//: List<Long>

/*@Query(value = "DELETE FROM ARTISTS WHERE name = :artistName")
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/listenbrainz/android/model/dao/SongDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import org.listenbrainz.android.model.SongEntity

Expand All @@ -20,8 +21,21 @@ interface SongDao {
@Query(value = "SELECT * FROM SONGS ORDER BY `title`")
fun getSongEntitiesAsList() : List<SongEntity>

@Query(value = "SELECT * FROM SONGS ORDER BY `lastListenedTo` DESC")
fun getRecentlyPlayedSongs() : Flow<List<SongEntity>>
@Query(value = "SELECT * FROM SONGS WHERE (:currentTime - lastListenedTo) < 86400000 ORDER BY (:currentTime - lastListenedTo) ASC")
fun getSongsPlayedToday(
currentTime : Long = System.currentTimeMillis()
) : Flow<List<SongEntity>>
@Query(value = "SELECT * FROM SONGS WHERE (:currentTime - lastListenedTo) > 86400000 AND (:currentTime - lastListenedTo) < 604800000 ORDER BY (:currentTime - lastListenedTo) ASC")
fun getSongsPlayedThisWeek(
currentTime : Long = System.currentTimeMillis()
) : Flow<List<SongEntity>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addSongs(songEntities: List<SongEntity>)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun updateSong(songEntity: SongEntity)

@Delete
suspend fun deleteSong(songEntity: SongEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,23 @@ class ArtistRepositoryImpl @Inject constructor(
// Both jobs are being executed simultaneously.
songsJob = async {
for (artist in artists) {
val mutableSongs = artist.songs.toMutableList()
// Here, if userRequestedRefresh is true, it will refresh songs cache which is what we expect from refreshing
artist.songs.toMutableList().addAll(addAllSongsOfArtist(artist.toArtist(), userRequestedRefresh).map {
mutableSongs.addAll(addAllSongsOfArtist(artist.toArtist(), userRequestedRefresh).map {
it.toSongEntity()
})

artist.songs = mutableSongs.toList()
}
}
albumsJob = async {
for (artist in artists) {
val mutableAlbums = artist.albums.toMutableList()
// We do not need to refresh cache (songsListCache) here as it already got refreshed above when we created list of albums.
artist.albums.toMutableList().addAll(addAllAlbumsOfArtist(artist.toArtist()).map {
mutableAlbums.addAll(addAllAlbumsOfArtist(artist.toArtist()).map {
it.toAlbumEntity()
})
artist.albums = mutableAlbums.toList()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package org.listenbrainz.android.repository.brainzplayer

import kotlinx.coroutines.flow.Flow
import org.listenbrainz.android.model.Song
import org.listenbrainz.android.model.SongEntity

interface SongRepository {
fun getSongsStream() : Flow<List<Song>>
fun getRecentlyPlayedSongs() : Flow<List<Song>>
fun getSongsPlayedToday() : Flow<List<Song>>
fun getSongsPlayedThisWeek() : Flow<List<Song>>
suspend fun addSongs(userRequestedRefresh: Boolean = false): Boolean
suspend fun updateSong(song : Song)

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.listenbrainz.android.repository.brainzplayer

import android.util.Log
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.listenbrainz.android.model.Song
import org.listenbrainz.android.model.SongEntity
import org.listenbrainz.android.model.dao.SongDao
import org.listenbrainz.android.util.SongsData
import org.listenbrainz.android.util.Transformer.toSong
Expand Down Expand Up @@ -38,4 +40,34 @@ class SongRepositoryImpl @Inject constructor(

return songs.isNotEmpty()
}

override suspend fun updateSong(song : Song) {
songDao.updateSong(song.toSongEntity())
Log.v("pranav" , "Song updated!")
}

override fun getRecentlyPlayedSongs(): Flow<List<Song>> =
songDao.getRecentlyPlayedSongs()
.map { it ->
it.map{
it.toSong()
}
}

override fun getSongsPlayedToday(): Flow<List<Song>> =
songDao.getSongsPlayedToday()
.map { it ->
it.map {
it.toSong()
}
}

override fun getSongsPlayedThisWeek(): Flow<List<Song>> =
songDao.getSongsPlayedThisWeek()
.map {
it ->
it.map {
it.toSong()
}
}
}
Loading
Loading