Skip to content

Commit

Permalink
Convert common from android library to jvm library
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Aug 15, 2024
1 parent 90f5fec commit 1f38ca4
Show file tree
Hide file tree
Showing 36 changed files with 148 additions and 204 deletions.
10 changes: 4 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp")
id("com.google.dagger.hilt.android")
kotlin("plugin.serialization") version "2.0.10"
}

android {
Expand All @@ -20,11 +21,11 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = JavaVersion.VERSION_17.toString()
}
viewBinding {
enable = true
Expand Down Expand Up @@ -79,7 +80,4 @@ dependencies {

testImplementation("org.jetbrains.kotlin:kotlin-reflect:1.9.22")
testImplementation("junit:junit:4.13.2")

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DownloadReceiver : BroadcastReceiver() {
withContext(Dispatchers.IO) { downloadDao.getDownload(downloadId) }
} ?: return
val track =
context.getFromCache(download.itemId, Track.creator, "downloads") ?: return
context.getFromCache<Track>(download.itemId, "downloads") ?: return

val downloadManager =
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Downloader(
client as TrackClient
val info = extension.info
val track =
context.getFromCache(download.itemId, Track.creator, "downloads")
context.getFromCache<Track>(download.itemId, "downloads")
?: return@withContext
context.enqueueDownload(info, client, track)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package dev.brahmkshatriya.echo.offline

import androidx.core.net.toUri
import androidx.media3.common.MediaItem
import dev.brahmkshatriya.echo.common.helpers.PagedData
import dev.brahmkshatriya.echo.common.models.Album
import dev.brahmkshatriya.echo.common.models.Artist
import dev.brahmkshatriya.echo.common.models.EchoMediaItem.Companion.toMediaItem
import dev.brahmkshatriya.echo.common.models.ImageHolder.Companion.toImageHolder
import dev.brahmkshatriya.echo.common.models.ImageHolder.Companion.toUriImageHolder
import dev.brahmkshatriya.echo.common.models.MediaItemsContainer
import dev.brahmkshatriya.echo.common.models.Playlist
import dev.brahmkshatriya.echo.common.models.Streamable
Expand All @@ -25,7 +24,7 @@ fun MediaItem.toTrack() = mediaMetadata.run {
title.toString(),
artists,
album,
artworkUri.toString().toUri().toImageHolder(),
artworkUri.toString().toUriImageHolder(),
extras?.getLong("Duration"),
null,
releaseYear.toString(),
Expand All @@ -39,7 +38,7 @@ fun MediaItem.toTrack() = mediaMetadata.run {
fun MediaStoreUtils.MAlbum.toAlbum() = Album(
id.toString(),
title ?: "Unknown",
cover.toString().toUri().toImageHolder(),
cover.toString().toUriImageHolder(),
artists.map { it.toArtist() },
songList.size,
albumYear?.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,17 @@ class OfflineExtension(val context: Context) : ExtensionClient, HomeFeedClient,
override suspend fun deleteSearchHistory(query: QuickSearchItem.SearchQueryItem) {
val history = getHistory().toMutableList()
history.remove(query.query)
context.saveToCache("search_history", "offline") { parcel ->
parcel.writeStringList(history)
}
context.saveToCache("search_history", history, "offline")
}

private fun getHistory() = context.getFromCache("search_history", "offline") {
it.createStringArrayList()?.distinct()?.take(5)
} ?: emptyList()
private fun getHistory() = context.getFromCache<List<String>>("search_history", "offline")
?.distinct()?.take(5)
?: emptyList()

private fun saveInHistory(query: String) {
val history = getHistory().toMutableList()
history.add(0, query)
context.saveToCache("search_history", "offline") { parcel ->
parcel.writeStringList(history)
}
context.saveToCache("search_history", history, "offline")
}

override suspend fun searchTabs(query: String?) =
Expand Down Expand Up @@ -342,7 +338,7 @@ class OfflineExtension(val context: Context) : ExtensionClient, HomeFeedClient,
).map { Tab(it, it) }

override fun getLibraryFeed(tab: Tab?): PagedData<MediaItemsContainer> {
if(refreshLibrary) library = MediaStoreUtils.getAllSongs(context)
if (refreshLibrary) library = MediaStoreUtils.getAllSongs(context)
return when (tab?.id) {
"Folders" -> library.folderStructure.folderList.entries.first().value.toContainer(null).more!!
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import androidx.media3.common.ThumbRating
import dev.brahmkshatriya.echo.common.models.EchoMediaItem
import dev.brahmkshatriya.echo.common.models.Track
import dev.brahmkshatriya.echo.ui.settings.AudioFragment.AudioPreference.Companion.selectStreamIndex
import dev.brahmkshatriya.echo.utils.getParcel
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.toJson

object MediaItemUtils {

Expand Down Expand Up @@ -49,9 +50,9 @@ object MediaItemUtils {
.setIsBrowsable(false)
.setExtras(
bundleOf(
"track" to this,
"track" to this.toJson(),
"clientId" to clientId,
"context" to context,
"context" to context.toJson(),
"loaded" to loaded,
"audioStream" to selectStream(settings, loaded, audioStreamIndex)
)
Expand All @@ -71,9 +72,9 @@ object MediaItemUtils {
}

val MediaMetadata.isLoaded get() = extras?.getBoolean("loaded") ?: false
val MediaMetadata.track get() = requireNotNull(extras?.getParcel<Track>("track"))
val MediaMetadata.track get() = requireNotNull(extras?.getSerialized<Track>("track"))
val MediaMetadata.clientId get() = requireNotNull(extras?.getString("clientId"))
val MediaMetadata.context get() = extras?.getParcel<EchoMediaItem?>("context")
val MediaMetadata.context get() = extras?.getSerialized<EchoMediaItem?>("context")
val MediaMetadata.audioStreamIndex get() = extras?.getInt("audioStream") ?: -1
val MediaMetadata.isLiked get() = (userRating as? ThumbRating)?.isThumbsUp == true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import dev.brahmkshatriya.echo.playback.MediaItemUtils.clientId
import dev.brahmkshatriya.echo.playback.MediaItemUtils.track
import dev.brahmkshatriya.echo.plugger.MusicExtension
import dev.brahmkshatriya.echo.plugger.getExtension
import dev.brahmkshatriya.echo.utils.getParcel
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.noClient
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.searchNotSupported
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.trackNotSupported
Expand Down Expand Up @@ -91,7 +91,7 @@ class PlayerSessionCallback(
private fun radio(player: Player, args: Bundle) = scope.future {
val error = SessionResult(SessionResult.RESULT_ERROR_UNKNOWN)
val clientId = args.getString("clientId") ?: return@future error
val item = args.getParcel<EchoMediaItem>("item") ?: return@future error
val item = args.getSerialized<EchoMediaItem>("item") ?: return@future error
radioFlow.value = Radio.State.Loading
val loaded = Radio.start(
context, messageFlow, throwableFlow, extensionList, clientId, item, 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import dev.brahmkshatriya.echo.playback.MediaItemUtils.clientId
import dev.brahmkshatriya.echo.playback.MediaItemUtils.context
import dev.brahmkshatriya.echo.playback.MediaItemUtils.track
import dev.brahmkshatriya.echo.utils.getFromCache
import dev.brahmkshatriya.echo.utils.getListFromCache
import dev.brahmkshatriya.echo.utils.saveToCache

object ResumptionUtils {
Expand All @@ -22,31 +21,19 @@ object ResumptionUtils {
val contexts = list.map { it.context }
context.saveToCache("queue_tracks", tracks, "queue")
context.saveToCache("queue_contexts", contexts, "queue")
context.saveToCache("queue_clients", "queue") { parcel ->
parcel.writeStringList(clients)
}
context.saveToCache("queue_index", "queue") {
it.writeInt(currentIndex)
}
context.saveToCache("queue_clients", clients, "queue")
context.saveToCache("queue_index", currentIndex, "queue")
}

fun saveCurrentPos(context: Context, position: Long) {
context.saveToCache("position", "queue") {
it.writeLong(position)
}
context.saveToCache("position", position, "queue")
}

private fun recoverQueue(context: Context): List<MediaItem>? {
val settings = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
val tracks = context.getListFromCache<Track>(
"queue_tracks", Track.creator, "queue"
)
val clientIds = context.getFromCache("queue_clients", "queue") {
it.createStringArrayList()
}
val contexts = context.getListFromCache<EchoMediaItem>(
"queue_contexts", EchoMediaItem.creator, "queue"
)
val tracks = context.getFromCache<List<Track>>("queue_tracks", "queue")
val clientIds = context.getFromCache<List<String>>("queue_clients", "queue")
val contexts = context.getFromCache<List<EchoMediaItem>>("queue_contexts", "queue")
return tracks?.mapIndexedNotNull { index, track ->
val clientId = clientIds?.getOrNull(index) ?: return@mapIndexedNotNull null
val item = contexts?.getOrNull(index)
Expand All @@ -55,10 +42,10 @@ object ResumptionUtils {
}

private fun recoverIndex(context: Context) =
context.getFromCache("queue_index", "queue") { it.readInt() }
context.getFromCache<Int>("queue_index", "queue")

private fun recoverPosition(context: Context) =
context.getFromCache("position", "queue") { it.readLong() }
context.getFromCache<Long>("position", "queue")

@OptIn(UnstableApi::class)
fun recoverPlaylist(context: Context): MediaSession.MediaItemsWithStartPosition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class TrackResolver(
}

private fun getTrackFromCache(id: String): Track? {
val track = context.getFromCache(id, Track.creator) ?: return null
val track = context.getFromCache<Track>(id) ?: return null
return if (!track.isExpired()) track else null
}

Expand Down
23 changes: 11 additions & 12 deletions app/src/main/java/dev/brahmkshatriya/echo/plugger/Injector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.SharedPreferences
import androidx.core.content.edit
import dev.brahmkshatriya.echo.common.clients.ExtensionClient
import dev.brahmkshatriya.echo.common.models.ExtensionType
import dev.brahmkshatriya.echo.common.providers.ContextProvider
import dev.brahmkshatriya.echo.common.settings.Settings
import dev.brahmkshatriya.echo.utils.mapState
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -26,17 +25,17 @@ inline fun <reified T : ExtensionClient> StateFlow<List<Result<Pair<ExtensionMet
}
}

inline fun <reified T : ExtensionClient> StateFlow<List<Result<Pair<ExtensionMetadata, T>>>>.injectContext(
context: Context
) = mapState { list ->
list.map {
runCatching {
it.getOrThrow().apply {
(second as? ContextProvider)?.setContext(context)
}
}
}
}
//inline fun <reified T : ExtensionClient> StateFlow<List<Result<Pair<ExtensionMetadata, T>>>>.injectContext(
// context: Context
//) = mapState { list ->
// list.map {
// runCatching {
// it.getOrThrow().apply {
// (second as? ContextProvider)?.setContext(context)
// }
// }
// }
//}

fun toSettings(prefs: SharedPreferences) = object : Settings {
override fun getString(key: String) = prefs.getString(key, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class LyricsExtensionRepo(
) : PluginRepo<ExtensionMetadata, LyricsClient> {
override fun getAllPlugins() = pluginRepo.getAllPlugins()
.injectSettings(ExtensionType.LYRICS, context)
.injectContext(context)
// .injectContext(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class MusicExtensionRepo(

override fun getAllPlugins() = pluginRepo.getAllPlugins()
.injectSettings(ExtensionType.MUSIC, context)
.injectContext(context)
// .injectContext(context)
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class TrackerExtensionRepo(
) : PluginRepo<ExtensionMetadata, TrackerClient> {
override fun getAllPlugins() = pluginRepo.getAllPlugins()
.injectSettings(ExtensionType.TRACKER, context)
.injectContext(context)
// .injectContext(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sealed class DownloadItem {
context: Context, extensionList: MutableStateFlow<List<MusicExtension>?>
): DownloadItem? {
val extension = extensionList.getExtension(clientId) ?: return null
val item = context.getFromCache(itemId, Track.creator, "downloads")?.toMediaItem()
val item = context.getFromCache<Track>(itemId, "downloads")?.toMediaItem()
?: return null
return Single(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import dev.brahmkshatriya.echo.databinding.DialogAddToPlaylistBinding
import dev.brahmkshatriya.echo.ui.adapter.MediaItemSelectableAdapter
import dev.brahmkshatriya.echo.ui.adapter.MediaItemSelectableAdapter.Companion.mediaItemSpanCount
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.getParcel
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.utils.putSerialized

@AndroidEntryPoint
class AddToPlaylistBottomSheet : BottomSheetDialogFragment() {
Expand All @@ -26,14 +27,14 @@ class AddToPlaylistBottomSheet : BottomSheetDialogFragment() {
fun newInstance(clientId: String, item: EchoMediaItem) = AddToPlaylistBottomSheet().apply {
arguments = Bundle().apply {
putString("clientId", clientId)
putParcelable("item", item)
putSerialized("item", item)
}
}
}

private val args by lazy { requireArguments() }
private val clientId by lazy { args.getString("clientId")!! }
private val item: EchoMediaItem by lazy { args.getParcel("item")!! }
private val item: EchoMediaItem by lazy { args.getSerialized("item")!! }

var binding by autoCleared<DialogAddToPlaylistBinding>()
val viewModel by viewModels<AddToPlaylistViewModel>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import dev.brahmkshatriya.echo.ui.editplaylist.EditPlaylistViewModel.ListAction.
import dev.brahmkshatriya.echo.ui.player.PlaylistAdapter
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.dpToPx
import dev.brahmkshatriya.echo.utils.getParcel
import dev.brahmkshatriya.echo.utils.getParcelArray
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.utils.onAppBarChangeListener
import dev.brahmkshatriya.echo.utils.putSerialized
import dev.brahmkshatriya.echo.utils.setupTransition
import dev.brahmkshatriya.echo.viewmodels.SnackBar.Companion.createSnack
import dev.brahmkshatriya.echo.viewmodels.UiViewModel.Companion.applyBackPressCallback
Expand All @@ -48,15 +48,15 @@ class EditPlaylistFragment : Fragment() {
return EditPlaylistFragment().apply {
arguments = Bundle().apply {
putString("clientId", client)
putParcelable("playlist", playlist)
putSerialized("playlist", playlist)
}
}
}
}

private val args by lazy { requireArguments() }
private val clientId by lazy { args.getString("clientId")!! }
private val playlist by lazy { args.getParcel<Playlist>("playlist")!! }
private val playlist by lazy { args.getSerialized<Playlist>("playlist")!! }
var binding by autoCleared<FragmentEditPlaylistBinding>()
val viewModel by activityViewModels<EditPlaylistViewModel>()

Expand Down Expand Up @@ -144,7 +144,7 @@ class EditPlaylistFragment : Fragment() {
"searchedTracks",
viewLifecycleOwner
) { _, bundle ->
val tracks = bundle.getParcelArray<Track>("tracks")!!.toMutableList()
val tracks = bundle.getSerialized<List<Track>>("tracks")!!.toMutableList()
viewModel.edit(Add(viewModel.currentTracks.value?.size ?: 0, tracks))
backCallback.isEnabled = true
}
Expand Down
Loading

0 comments on commit 1f38ca4

Please sign in to comment.