-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #337 from cyb3rko/picasso-to-coil
Replace Picasso with Coil
- Loading branch information
Showing
12 changed files
with
132 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.gotify | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.graphics.drawable.BitmapDrawable | ||
import coil.ImageLoader | ||
import coil.annotation.ExperimentalCoilApi | ||
import coil.disk.DiskCache | ||
import coil.executeBlocking | ||
import coil.request.ImageRequest | ||
import com.github.gotify.api.CertUtils | ||
import com.github.gotify.client.model.Application | ||
import java.io.IOException | ||
import okhttp3.OkHttpClient | ||
import org.tinylog.kotlin.Logger | ||
|
||
internal class CoilHandler(private val context: Context, private val settings: Settings) { | ||
private val imageLoader = makeImageLoader() | ||
|
||
private fun makeImageLoader(): ImageLoader { | ||
val builder = OkHttpClient.Builder() | ||
CertUtils.applySslSettings(builder, settings.sslSettings()) | ||
return ImageLoader.Builder(context) | ||
.okHttpClient(builder.build()) | ||
.diskCache { | ||
DiskCache.Builder() | ||
.directory(context.cacheDir.resolve("coil-cache")) | ||
.build() | ||
} | ||
.build() | ||
} | ||
|
||
@Throws(IOException::class) | ||
fun getImageFromUrl(url: String?): Bitmap { | ||
val request = ImageRequest.Builder(context) | ||
.data(url) | ||
.build() | ||
return (imageLoader.executeBlocking(request).drawable as BitmapDrawable).bitmap | ||
} | ||
|
||
fun getIcon(app: Application?): Bitmap { | ||
if (app == null) { | ||
return BitmapFactory.decodeResource(context.resources, R.drawable.gotify) | ||
} | ||
try { | ||
return getImageFromUrl( | ||
Utils.resolveAbsoluteUrl("${settings.url}/", app.image) | ||
) | ||
} catch (e: IOException) { | ||
Logger.error(e, "Could not load image for notification") | ||
} | ||
return BitmapFactory.decodeResource(context.resources, R.drawable.gotify) | ||
} | ||
|
||
fun get() = imageLoader | ||
|
||
@OptIn(ExperimentalCoilApi::class) | ||
fun evict() { | ||
try { | ||
imageLoader.diskCache?.clear() | ||
imageLoader.memoryCache?.clear() | ||
} catch (e: IOException) { | ||
Logger.error(e, "Problem evicting Coil cache") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.