Skip to content

Commit

Permalink
Twelve: Activity backend
Browse files Browse the repository at this point in the history
Change-Id: I2932d5110fa5fbb63c984b288ed4fe66801d8d9c
  • Loading branch information
SebaUbuntu committed Nov 21, 2024
1 parent 183e45e commit 86e6c4d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.lineageos.twelve.database.TwelveDatabase
import org.lineageos.twelve.database.entities.Item
import org.lineageos.twelve.ext.mapEachRow
import org.lineageos.twelve.ext.queryFlow
import org.lineageos.twelve.models.ActivityTab
import org.lineageos.twelve.models.Album
import org.lineageos.twelve.models.Artist
import org.lineageos.twelve.models.ArtistWorks
Expand Down Expand Up @@ -199,6 +200,10 @@ class LocalDataSource(context: Context, private val database: TwelveDatabase) :
} ?: RequestStatus.Error(MediaError.NOT_FOUND)
}

override fun activity() = flowOf(
RequestStatus.Success<_, MediaError>(listOf<ActivityTab>())
)

override fun albums(sortingRule: SortingRule) = contentResolver.queryFlow(
albumsUri,
albumsProjection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.lineageos.twelve.datasources

import android.net.Uri
import kotlinx.coroutines.flow.Flow
import org.lineageos.twelve.models.ActivityTab
import org.lineageos.twelve.models.Album
import org.lineageos.twelve.models.Artist
import org.lineageos.twelve.models.ArtistWorks
Expand Down Expand Up @@ -41,6 +42,11 @@ interface MediaDataSource {
*/
suspend fun mediaTypeOf(mediaItemUri: Uri): MediaRequestStatus<MediaType>

/**
* Home page content.
*/
fun activity(): Flow<MediaRequestStatus<List<ActivityTab>>>

/**
* Get all the albums. All albums must have at least one audio associated with them.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import org.lineageos.twelve.datasources.subsonic.models.AlbumID3
import org.lineageos.twelve.datasources.subsonic.models.ArtistID3
import org.lineageos.twelve.datasources.subsonic.models.Child
import org.lineageos.twelve.datasources.subsonic.models.Error
import org.lineageos.twelve.models.ActivityTab
import org.lineageos.twelve.models.Album
import org.lineageos.twelve.models.Artist
import org.lineageos.twelve.models.ArtistWorks
import org.lineageos.twelve.models.Audio
import org.lineageos.twelve.models.Genre
import org.lineageos.twelve.models.GenreContent
import org.lineageos.twelve.models.LocalizedString
import org.lineageos.twelve.models.MediaType
import org.lineageos.twelve.models.Playlist
import org.lineageos.twelve.models.ProviderArgument
Expand Down Expand Up @@ -87,6 +89,59 @@ class SubsonicDataSource(arguments: Bundle) : MediaDataSource {
} ?: RequestStatus.Error(MediaError.NOT_FOUND)
}

override fun activity() = suspend {
val mostPlayedAlbums = subsonicClient.getAlbumList2(
"frequent",
10
).toRequestStatus {
ActivityTab(
"most_played_albums",
LocalizedString(
"Most played albums",
R.string.activity_most_played_albums,
),
album.sortedByDescending { it.playCount }.map { it.toMediaItem() }
)
}

val randomAlbums = subsonicClient.getAlbumList2(
"random",
10
).toRequestStatus {
ActivityTab(
"random_albums",
LocalizedString(
"Random albums",
R.string.activity_random_albums,
),
album.map { it.toMediaItem() }
)
}

val randomSongs = subsonicClient.getRandomSongs(20).toRequestStatus {
ActivityTab(
"random_songs",
LocalizedString(
"Random songs",
R.string.activity_random_songs,
),
song.map { it.toMediaItem() }
)
}

RequestStatus.Success<_, MediaError>(
listOf(
mostPlayedAlbums,
randomAlbums,
randomSongs,
).mapNotNull {
(it as? RequestStatus.Success)?.data?.takeIf { activityTab ->
activityTab.items.isNotEmpty()
}
}
)
}.asFlow()

override fun albums(sortingRule: SortingRule) = suspend {
subsonicClient.getAlbumList2(
"alphabeticalByName",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data class SubsonicResponse(
val chatMessages: TODO = null,
val albumList: AlbumList? = null,
val albumList2: AlbumList2? = null,
val randomSongs: TODO = null,
val randomSongs: Songs? = null,
val songsByGenre: Songs? = null,
val lyrics: TODO = null,
val podcasts: TODO = null,
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/org/lineageos/twelve/models/ActivityTab.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.twelve.models

data class ActivityTab(
val id: String,
val title: LocalizedString,
val items: List<MediaItem<*>>,
) : UniqueItem<ActivityTab> {
override fun areItemsTheSame(other: ActivityTab) = id == other.id

override fun areContentsTheSame(other: ActivityTab) =
title == other.title && items.toTypedArray().contentEquals(other.items.toTypedArray())
}
23 changes: 23 additions & 0 deletions app/src/main/java/org/lineageos/twelve/models/LocalizedString.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.twelve.models

import android.content.Context
import androidx.annotation.StringRes

data class LocalizedString(
val value: String,
@StringRes val stringResId: Int? = null,
val stringResIdArgs: List<Any>? = null,
) {
override fun toString() = value

fun getString(context: Context) = stringResId?.let { stringResId ->
stringResIdArgs?.let { stringResIdArgs ->
context.getString(stringResId, *stringResIdArgs.toTypedArray())
} ?: context.getString(stringResId)
} ?: value
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ class MediaRepository(
mediaTypeOf(mediaItemUri)
}

/**
* @see MediaDataSource.activity
*/
fun activity() = navigationDataSource.flatMapLatest { it.activity() }

/**
* @see MediaDataSource.albums
*/
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@
<string name="sort_by_release_date">Release date</string>
<string name="sort_by_title">Title</string>
<string name="sort_by_unknown">Unknown</string>

<!-- Activities -->
<string name="activity_most_played_albums">Most played albums</string>
<string name="activity_random_albums">Random albums</string>
<string name="activity_random_songs">Random songs</string>
</resources>

0 comments on commit 86e6c4d

Please sign in to comment.