Skip to content

Commit

Permalink
Migrate to Coil3 (#24)
Browse files Browse the repository at this point in the history
* build.gradle.ks: Add coil dependency

* build.gradle.kts: Add coil network dependency

* GameFile: Implement coil image loading

* GameFile: Use appContext

* build.gradle: Update kotlin to 1.9.0

* GameFile: fix some errors

* Update GameFile.kt

* GameFile: Use GlobalScope

* Update GameFile.kt

* GameFile: fix another error

* EditorActivity: fix errors due updated kotlin version
  • Loading branch information
Ishan09811 authored Jan 6, 2025
1 parent 3f60cce commit 455ce33
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
9 changes: 5 additions & 4 deletions Source/Android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ 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 = "17"
}

lint {
Expand Down Expand Up @@ -102,7 +102,8 @@ dependencies {
implementation("androidx.core:core-ktx:1.15.0")
implementation("androidx.recyclerview:recyclerview:1.3.2")
implementation("com.google.android.material:material:1.12.0")
implementation("com.squareup.picasso:picasso:2.71828")
implementation("io.coil-kt.coil3:coil:3.0.4")
implementation("io.coil-kt.coil3:coil-network-okhttp:3.0.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class EditorActivity : AppCompatActivity() {
var result = ""
try {
response = downloader.newCall(request).execute()
if (response != null && response.code() == 200 && response.body() != null) {
result = response.body()!!.string()
if (response != null && response.code == 200 && response.body != null) {
result = response.body!!.string()
}
} catch (e: IOException) {
// ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.widget.ImageView
import androidx.annotation.Keep
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import coil3.imageLoader
import coil3.Image
import coil3.toBitmap
import coil3.request.ImageRequest
import coil3.request.SuccessResult
import org.dolphinemu.dolphinemu.NativeLibrary
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.utils.CoverHelper
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization
import org.dolphinemu.dolphinemu.DolphinApplication
import java.io.File
import java.io.FileOutputStream
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Keep
class GameFile private constructor(private val pointer: Long) {
Expand Down Expand Up @@ -141,36 +149,35 @@ class GameFile private constructor(private val pointer: Long) {
}

private fun loadFromNetwork(imageView: ImageView, callback: Callback) {
Picasso.get()
.load(CoverHelper.buildGameTDBUrl(this, null))
.placeholder(R.drawable.no_banner)
.error(R.drawable.no_banner)
.into(imageView, object : Callback {
override fun onSuccess() {
callback.onSuccess()
}

override fun onError(e: Exception) {
val id = getGameTdbId()
var region: String? = null
region = if (id.length < 3) {
callback.onError(e)
return
} else if (id[3] != 'E') {
"US"
} else if (id[3] != 'J') {
"JA"
} else {
callback.onError(e)
return
GlobalScope.launch(Dispatchers.Main) {
val request = ImageRequest.Builder(DolphinApplication.getAppContext())
.data(CoverHelper.buildGameTDBUrl(this@GameFile, null))
.build()

val result = withContext(Dispatchers.IO) { DolphinApplication.getAppContext().imageLoader.execute(request) }

if (result is SuccessResult) {
imageView.setImageBitmap((result.image as Image).toBitmap())
callback.onSuccess()
} else {
val id = getGameTdbId()
var region: String? = null
if (id.length < 3) {
callback.onError(Exception("failed to load game banner"))
return@launch // Use return@launch to exit from the coroutine
} else {
region = when (id[3]) {
'E' -> "US"
'J' -> "JA"
else -> {
callback.onError(Exception("failed to load game banner"))
return@launch // Return here as well to exit the coroutine
}
}
Picasso.get()
.load(CoverHelper.buildGameTDBUrl(this@GameFile, region))
.placeholder(R.drawable.no_banner)
.error(R.drawable.no_banner)
.into(imageView, callback)
}
})
// TODO(Ishan09811): try to load with region once
}
}
}

private fun loadFromISO(imageView: ImageView): Boolean {
Expand Down Expand Up @@ -201,4 +208,9 @@ class GameFile private constructor(private val pointer: Long) {
@JvmStatic
external fun parse(path: String): GameFile?
}

interface Callback {
fun onSuccess()
fun onError(e: Exception)
}
}
2 changes: 1 addition & 1 deletion Source/Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
plugins {
id 'com.android.application' version '8.7.3' apply false
id 'com.android.library' version '8.7.3' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
}

0 comments on commit 455ce33

Please sign in to comment.