Skip to content

Commit

Permalink
Switch to coil and add okhttp client
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Oct 18, 2024
1 parent 8344d0d commit 9b57d45
Show file tree
Hide file tree
Showing 25 changed files with 289 additions and 181 deletions.
7 changes: 3 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ dependencies {

implementation("androidx.fragment:fragment-ktx:1.6.2")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.6")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.8.0")
implementation("androidx.paging:paging-common-ktx:3.3.2")
implementation("androidx.paging:paging-runtime-ktx:3.3.2")
implementation("androidx.preference:preference-ktx:1.2.1")
Expand All @@ -78,9 +77,9 @@ dependencies {
implementation("com.google.dagger:hilt-android:2.48.1")
ksp("com.google.dagger:hilt-android-compiler:2.48.1")

ksp("com.github.bumptech.glide:ksp:4.14.2")
implementation("com.github.bumptech.glide:glide:4.16.0")
implementation("jp.wasabeef:glide-transformations:4.3.0")
implementation("io.coil-kt.coil3:coil:3.0.0-rc01")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("io.coil-kt.coil3:coil-network-okhttp:3.0.0-rc01")

implementation("com.github.madrapps:pikolo:2.0.2")
implementation("com.github.bosphere.android-fadingedgelayout:fadingedgelayout:1.0.0")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<data android:mimeType="application/octet-stream" />
<data android:scheme="file"/>
<data android:pathPattern=".*\\.eapk"/>
<data android:mimeType="*/*"/>
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/dev/brahmkshatriya/echo/EchoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.SingletonImageLoader
import coil3.disk.DiskCache
import coil3.disk.directory
import coil3.memory.MemoryCache
import coil3.request.allowHardware
import coil3.request.crossfade
import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import com.google.android.material.color.ThemeUtils
Expand All @@ -25,7 +33,7 @@ import kotlinx.coroutines.plus
import javax.inject.Inject

@HiltAndroidApp
class EchoApplication : Application() {
class EchoApplication : Application(), SingletonImageLoader.Factory {

@Inject
lateinit var settings: SharedPreferences
Expand Down Expand Up @@ -100,4 +108,22 @@ class EchoApplication : Application() {
.getPackageInfo(packageName, 0)
.versionName!!
}

override fun newImageLoader(context: PlatformContext): ImageLoader {
return ImageLoader.Builder(context)
.memoryCache {
MemoryCache.Builder()
.maxSizePercent(context, 0.25)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizeBytes(1024 * 1024 * 100) // 100MB
.build()
}
.allowHardware(false)
.crossfade(true)
.build()
}
}
31 changes: 13 additions & 18 deletions app/src/main/java/dev/brahmkshatriya/echo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,26 @@ class MainActivity : AppCompatActivity() {

controllerFuture = playerFuture

intent?.onIntent()
addOnNewIntentListener { onIntent(it) }
onIntent(intent)
}

override fun onDestroy() {
super.onDestroy()
controllerFuture?.let { MediaBrowser.releaseFuture(it) }
}

private fun Intent.onIntent() {
val fromNotif = hasExtra("fromNotification")
if (fromNotif) {
uiViewModel.fromNotification.value = true
return
}
val uri = data
println("URI: $uri")
when (uri?.scheme) {
"echo" -> openItemFragmentFromUri(uri)
"file" -> openExtensionInstaller(uri)
private fun onIntent(intent: Intent?) {
this.intent = null
intent ?: return
val fromNotif = intent.hasExtra("fromNotification")
if (fromNotif) uiViewModel.fromNotification.value = true
else {
val uri = intent.data
when (uri?.scheme) {
"echo" -> openItemFragmentFromUri(uri)
"file" -> openExtensionInstaller(uri)
}
}
}

override fun onNewIntent(intent: Intent?) {
intent?.onIntent()
super.onNewIntent(intent)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import dev.brahmkshatriya.echo.playback.MediaItemUtils.clientId
import dev.brahmkshatriya.echo.playback.MediaItemUtils.track
import dev.brahmkshatriya.echo.playback.listeners.Radio
import dev.brahmkshatriya.echo.ui.exception.ExceptionFragment.Companion.toExceptionDetails
import dev.brahmkshatriya.echo.utils.future
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.putSerialized
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel.Companion.noClient
Expand All @@ -48,7 +49,6 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.guava.future
import kotlinx.coroutines.withContext

@UnstableApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import android.net.Uri
import androidx.media3.common.util.BitmapLoader
import androidx.media3.common.util.UnstableApi
import com.google.common.util.concurrent.ListenableFuture
import dev.brahmkshatriya.echo.R
import dev.brahmkshatriya.echo.common.models.ImageHolder
import dev.brahmkshatriya.echo.utils.future
import dev.brahmkshatriya.echo.utils.loadBitmap
import dev.brahmkshatriya.echo.utils.toData
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.guava.future

@UnstableApi
class PlayerBitmapLoader(
Expand All @@ -23,15 +21,12 @@ class PlayerBitmapLoader(

override fun supportsMimeType(mimeType: String) = true

override fun decodeBitmap(data: ByteArray) = scope.future(Dispatchers.IO) {
override fun decodeBitmap(data: ByteArray) = scope.future {
BitmapFactory.decodeByteArray(data, 0, data.size) ?: error("Failed to decode bitmap")
}

private val emptyBitmap
get() = context.loadBitmap(R.drawable.art_music) ?: error("Empty bitmap")

override fun loadBitmap(uri: Uri): ListenableFuture<Bitmap> = scope.future {
val cover = runCatching { uri.toString().toData<ImageHolder>() }.getOrNull()
cover?.loadBitmap(context) ?: emptyBitmap
val cover = uri.toString().toData<ImageHolder>()
cover.loadBitmap(context) ?: error("Failed to load bitmap of $cover")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ sealed class MediaItemViewHolder(
fun ItemListsCoverBinding.bind(item: EchoMediaItem.Lists): (Boolean) -> Unit {
playlist.isVisible = item is EchoMediaItem.Lists.PlaylistItem
val cover = item.cover
cover.loadWith(listImageView) {
cover.loadWith(listImageView, null, item.placeHolder()) {
cover.loadInto(listImageView1)
cover.loadInto(listImageView2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import dev.brahmkshatriya.echo.extensions.isClient
import dev.brahmkshatriya.echo.ui.extension.ExtensionsListBottomSheet
import dev.brahmkshatriya.echo.ui.login.LoginUserBottomSheet
import dev.brahmkshatriya.echo.ui.settings.SettingsFragment
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel
import dev.brahmkshatriya.echo.viewmodels.LoginUserViewModel
Expand All @@ -26,7 +26,7 @@ fun MaterialToolbar.configureMainMenu(fragment: MainFragment) {
extensions.transitionName = "extensions"

fragment.observe(extensionViewModel.extensionFlow) { client ->
client?.metadata?.iconUrl?.toImageHolder().loadWith(extensions, R.drawable.ic_extension) {
client?.metadata?.iconUrl?.toImageHolder().loadAsCircle(extensions, R.drawable.ic_extension) {
menu.findItem(R.id.menu_extensions).icon = it
}
}
Expand All @@ -49,7 +49,7 @@ fun MaterialToolbar.configureMainMenu(fragment: MainFragment) {
val user = u?.toUser()
val isLoginClient = extension?.isClient<LoginClient>() ?: false
if (isLoginClient) {
user?.cover.loadWith(settings, R.drawable.ic_account_circle_48dp) {
user?.cover.loadAsCircle(settings, R.drawable.ic_account_circle_48dp) {
menu.findItem(R.id.menu_settings).icon = it
}
} else menu.findItem(R.id.menu_settings).setIcon(R.drawable.ic_settings_outline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import dev.brahmkshatriya.echo.databinding.ItemDownloadGroupBinding
import dev.brahmkshatriya.echo.ui.adapter.ShelfEmptyAdapter
import dev.brahmkshatriya.echo.ui.adapter.MediaItemViewHolder.Companion.placeHolder
import dev.brahmkshatriya.echo.utils.loadInto
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle

class DownloadingAdapter(
val listener: Listener
Expand Down Expand Up @@ -56,7 +56,7 @@ class DownloadingAdapter(
binding.downloadTitle.text = download.item.title
download.item.cover?.loadInto(binding.itemImageView, download.item.placeHolder())
binding.itemExtension.apply {
download.clientIcon?.toImageHolder().loadWith(this, R.drawable.ic_extension) {
download.clientIcon?.toImageHolder().loadAsCircle(this, R.drawable.ic_extension) {
setImageDrawable(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dev.brahmkshatriya.echo.common.Extension
import dev.brahmkshatriya.echo.common.models.ImageHolder.Companion.toImageHolder
import dev.brahmkshatriya.echo.databinding.ItemExtensionBinding
import dev.brahmkshatriya.echo.ui.adapter.ShelfEmptyAdapter
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle

class
ExtensionAdapter(
Expand Down Expand Up @@ -52,7 +52,7 @@ ExtensionAdapter(
}
binding.extensionVersion.text = "${metadata.version}${metadata.importType.name}"
binding.itemExtension.apply {
metadata.iconUrl?.toImageHolder().loadWith(this, R.drawable.ic_extension) {
metadata.iconUrl?.toImageHolder().loadAsCircle(this, R.drawable.ic_extension) {
setImageDrawable(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import dev.brahmkshatriya.echo.ui.common.openFragment
import dev.brahmkshatriya.echo.ui.login.LoginUserBottomSheet.Companion.bind
import dev.brahmkshatriya.echo.ui.settings.ExtensionFragment
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.utils.onAppBarChangeListener
import dev.brahmkshatriya.echo.utils.setupTransition
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel
Expand Down Expand Up @@ -116,7 +116,7 @@ class ExtensionInfoFragment : Fragment() {
}
}

metadata.iconUrl?.toImageHolder().loadWith(binding.extensionIcon, R.drawable.ic_extension) {
metadata.iconUrl?.toImageHolder().loadAsCircle(binding.extensionIcon, R.drawable.ic_extension) {
binding.extensionIcon.setImageDrawable(it)
}
binding.extensionDetails.text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import dev.brahmkshatriya.echo.extensions.plugger.ApkPluginSource
import dev.brahmkshatriya.echo.extensions.plugger.AppInfo
import dev.brahmkshatriya.echo.utils.ApkLinkParser
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -84,7 +84,7 @@ class ExtensionInstallerBottomSheet : BottomSheetDialogFragment() {
val (extensionType, metadata) = value

binding.extensionTitle.text = metadata.name
metadata.iconUrl?.toImageHolder().loadWith(binding.extensionIcon, R.drawable.ic_extension) {
metadata.iconUrl?.toImageHolder().loadAsCircle(binding.extensionIcon, R.drawable.ic_extension) {
binding.extensionIcon.setImageDrawable(it)
}
binding.extensionDetails.text = metadata.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import dev.brahmkshatriya.echo.ui.common.openFragment
import dev.brahmkshatriya.echo.ui.player.lyrics.LyricsViewModel
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.collect
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.viewmodels.ExtensionViewModel

class ExtensionsListBottomSheet : BottomSheetDialogFragment() {
Expand Down Expand Up @@ -85,7 +85,7 @@ class ExtensionsListBottomSheet : BottomSheetDialogFragment() {
button.text = metadata.name
binding.buttonToggleGroup.addView(button)
button.isChecked = metadata.id == viewModel.currentFlow.value
metadata.iconUrl?.toImageHolder().loadWith(button, R.drawable.ic_extension) {
metadata.iconUrl?.toImageHolder().loadAsCircle(button, R.drawable.ic_extension) {
button.icon = it
}
button.id = index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import dev.brahmkshatriya.echo.ui.editplaylist.AddToPlaylistBottomSheet
import dev.brahmkshatriya.echo.ui.exception.ExceptionFragment.Companion.copyToClipboard
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.utils.putSerialized
import dev.brahmkshatriya.echo.viewmodels.DownloadViewModel
Expand Down Expand Up @@ -345,7 +345,7 @@ class ItemBottomSheet : BottomSheetDialogFragment() {

is ItemAction.Custom -> {
binding.textView.text = action.title
action.image.loadWith(binding.root) {
action.image.loadAsCircle(binding.root) {
if (it == null) {
binding.imageView.imageTintList = colorState
binding.imageView.setImageResource(action.placeholder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import dev.brahmkshatriya.echo.utils.dpToPx
import dev.brahmkshatriya.echo.utils.getSerialized
import dev.brahmkshatriya.echo.utils.load
import dev.brahmkshatriya.echo.utils.loadInto
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadWithThumb
import dev.brahmkshatriya.echo.utils.onAppBarChangeListener
import dev.brahmkshatriya.echo.utils.putSerialized
import dev.brahmkshatriya.echo.utils.setupTransition
Expand Down Expand Up @@ -216,7 +216,7 @@ class ItemFragment : Fragment() {
binding.toolBar.title = it.title.trim()
}

it.cover.loadWith(binding.cover, item.cover, it.placeHolder())
it.cover.loadWithThumb(binding.cover, item.cover, it.placeHolder())
with(viewModel) {
when (it) {
is AlbumItem -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import dev.brahmkshatriya.echo.extensions.isClient
import dev.brahmkshatriya.echo.ui.exception.AppException
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.collect
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.utils.onAppBarChangeListener
import dev.brahmkshatriya.echo.utils.setupTransition
Expand Down Expand Up @@ -128,7 +128,7 @@ class LoginFragment : Fragment() {
return
}

metadata.iconUrl?.toImageHolder().loadWith(binding.extensionIcon, R.drawable.ic_extension) {
metadata.iconUrl?.toImageHolder().loadAsCircle(binding.extensionIcon, R.drawable.ic_extension) {
binding.extensionIcon.setImageDrawable(it)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dev.brahmkshatriya.echo.databinding.DialogLoginUserListBinding
import dev.brahmkshatriya.echo.db.models.UserEntity.Companion.toEntity
import dev.brahmkshatriya.echo.ui.common.openFragment
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.loadWith
import dev.brahmkshatriya.echo.utils.loadAsCircle
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.viewmodels.LoginUserViewModel

Expand Down Expand Up @@ -67,7 +67,7 @@ class LoginUserListBottomSheet : BottomSheetDialogFragment() {
).root
button.text = user.name
binding.accountListToggleGroup.addView(button)
user.cover.loadWith(button, R.drawable.ic_account_circle) { button.icon = it }
user.cover.loadAsCircle(button, R.drawable.ic_account_circle) { button.icon = it }
button.id = index
}
}
Expand Down
Loading

0 comments on commit 9b57d45

Please sign in to comment.