Refactor: fix deprecations

This commit is contained in:
Koitharu
2020-05-03 18:04:26 +03:00
parent 8378b3dd90
commit 1b7c8355ec
15 changed files with 83 additions and 59 deletions

View File

@@ -4,7 +4,8 @@ import android.app.Application
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.room.Room import androidx.room.Room
import coil.Coil import coil.Coil
import coil.ImageLoader import coil.ComponentRegistry
import coil.ImageLoaderBuilder
import coil.util.CoilUtils import coil.util.CoilUtils
import com.chuckerteam.chucker.api.ChuckerCollector import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor import com.chuckerteam.chucker.api.ChuckerInterceptor
@@ -85,16 +86,19 @@ class KotatsuApp : Application() {
} }
private fun initCoil() { private fun initCoil() {
Coil.setDefaultImageLoader(ImageLoader(applicationContext) { Coil.setImageLoader(
okHttpClient { ImageLoaderBuilder(applicationContext)
okHttp() .okHttpClient(
.cache(CoilUtils.createDefaultCache(applicationContext)) okHttp()
.build() .cache(CoilUtils.createDefaultCache(applicationContext))
} .build()
componentRegistry { ).componentRegistry(
add(CbzFetcher()) ComponentRegistry.Builder()
} .add(CbzFetcher())
}) .build()
)
.build()
)
} }
private fun initErrorHandler() { private fun initErrorHandler() {

View File

@@ -70,7 +70,6 @@ abstract class BaseActivity : MvpAppCompatActivity(), KoinComponent {
} }
if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
throw StackOverflowError("test") throw StackOverflowError("test")
return true
} }
return super.onKeyDown(keyCode, event) return super.onKeyDown(keyCode, event)
} }

View File

@@ -8,7 +8,7 @@ import android.webkit.MimeTypeMap
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@@ -30,6 +30,7 @@ import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.sub import org.koitharu.kotatsu.utils.ext.sub
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.collections.set
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
class DownloadService : BaseService() { class DownloadService : BaseService() {
@@ -87,7 +88,9 @@ class DownloadService : BaseService() {
try { try {
val repo = MangaProviderFactory.create(manga.source) val repo = MangaProviderFactory.create(manga.source)
val cover = safe { val cover = safe {
Coil.loader().get(manga.coverUrl) Coil.execute(GetRequestBuilder(this@DownloadService)
.data(manga.coverUrl)
.build()).drawable
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
notification.setLargeIcon(cover) notification.setLargeIcon(cover)

View File

@@ -107,7 +107,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
fun addToCategory(manga: Manga, categoryId: Long) { fun addToCategory(manga: Manga, categoryId: Long) {
presenterScope.launch { presenterScope.launch {
try { try {
val categories = withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
repository.addToCategory(manga,categoryId) repository.addToCategory(manga,categoryId)
} }
} catch (e: Exception) { } catch (e: Exception) {
@@ -122,7 +122,7 @@ class FavouriteCategoriesPresenter : BasePresenter<FavouriteCategoriesView>() {
fun removeFromCategory(manga: Manga, categoryId: Long) { fun removeFromCategory(manga: Manga, categoryId: Long) {
presenterScope.launch { presenterScope.launch {
try { try {
val categories = withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
repository.removeFromCategory(manga, categoryId) repository.removeFromCategory(manga, categoryId)
} }
} catch (e: Exception) { } catch (e: Exception) {

View File

@@ -3,7 +3,7 @@ package org.koitharu.kotatsu.ui.reader.thumbnails
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.net.toUri import androidx.core.net.toUri
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import coil.size.PixelSize import coil.size.PixelSize
import coil.size.Size import coil.size.Size
import kotlinx.android.synthetic.main.item_page_thumb.* import kotlinx.android.synthetic.main.item_page_thumb.*
@@ -38,9 +38,10 @@ class PageThumbnailHolder(parent: ViewGroup, private val scope: CoroutineScope)
val pageUrl = MangaProviderFactory.create(data.source).getPageFullUrl(data) val pageUrl = MangaProviderFactory.create(data.source).getPageFullUrl(data)
extra[pageUrl]?.toUri()?.toString() ?: pageUrl extra[pageUrl]?.toUri()?.toString() ?: pageUrl
} }
val drawable = Coil.get(url) { val drawable = Coil.execute(GetRequestBuilder(context)
size(thumbSize) .data(url)
} .size(thumbSize)
.build()).drawable
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
imageView_thumb.setImageDrawable(drawable) imageView_thumb.setImageDrawable(drawable)
} }

View File

@@ -47,11 +47,11 @@ class SourcesAdapter(private val onItemClickListener: OnRecyclerItemClickListene
settings.hiddenSources = hiddenItems.map { x -> x.name }.toSet() settings.hiddenSources = hiddenItems.map { x -> x.name }.toSet()
} }
holder.imageView_config.setOnClickListener { v -> holder.imageView_config.setOnClickListener { v ->
onItemClickListener.onItemClick(holder.requireData(), holder.adapterPosition, v) onItemClickListener.onItemClick(holder.requireData(), holder.bindingAdapterPosition, v)
} }
holder.imageView_handle.setOnTouchListener { v, event -> holder.imageView_handle.setOnTouchListener { v, event ->
if (event.actionMasked == MotionEvent.ACTION_DOWN) { if (event.actionMasked == MotionEvent.ACTION_DOWN) {
onItemClickListener.onItemLongClick(holder.requireData(), holder.adapterPosition, v) onItemClickListener.onItemLongClick(holder.requireData(), holder.bindingAdapterPosition, v)
} else { } else {
false false
} }

View File

@@ -11,8 +11,8 @@ class SourcesReorderCallback : ItemTouchHelper.SimpleCallback(ItemTouchHelper.DO
target: RecyclerView.ViewHolder target: RecyclerView.ViewHolder
): Boolean { ): Boolean {
val adapter = recyclerView.adapter as? SourcesAdapter ?: return false val adapter = recyclerView.adapter as? SourcesAdapter ?: return false
val oldPos = viewHolder.adapterPosition val oldPos = viewHolder.bindingAdapterPosition
val newPos = target.adapterPosition val newPos = target.bindingAdapterPosition
adapter.moveItem(oldPos, newPos) adapter.moveItem(oldPos, newPos)
return true return true
} }

View File

@@ -8,10 +8,9 @@ import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.work.* import androidx.work.*
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.core.KoinComponent import org.koin.core.KoinComponent
@@ -24,6 +23,7 @@ import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.domain.tracking.TrackingRepository import org.koitharu.kotatsu.domain.tracking.TrackingRepository
import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.ui.details.MangaDetailsActivity
import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.toBitmapOrNull
import org.koitharu.kotatsu.utils.ext.toUriOrNull import org.koitharu.kotatsu.utils.ext.toUriOrNull
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -136,9 +136,9 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
setContentText(summary) setContentText(summary)
setContentText(manga.title) setContentText(manga.title)
setNumber(newChapters.size) setNumber(newChapters.size)
setLargeIcon(safe { setLargeIcon(Coil.execute(GetRequestBuilder(applicationContext)
Coil.loader().get(manga.coverUrl).toBitmap() .data(manga.coverUrl)
}) .build()).toBitmapOrNull())
setSmallIcon(R.drawable.ic_stat_book_plus) setSmallIcon(R.drawable.ic_stat_book_plus)
val style = NotificationCompat.InboxStyle(this) val style = NotificationCompat.InboxStyle(this)
for (chapter in newChapters) { for (chapter in newChapters) {

View File

@@ -4,19 +4,17 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.widget.RemoteViews import android.widget.RemoteViews
import android.widget.RemoteViewsService import android.widget.RemoteViewsService
import androidx.core.graphics.drawable.toBitmap
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okio.IOException
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.history.HistoryRepository import org.koitharu.kotatsu.domain.history.HistoryRepository
import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.ui.details.MangaDetailsActivity
import org.koitharu.kotatsu.utils.ext.requireBitmap
import java.io.IOException
class RecentListFactory(context: Context, private val intent: Intent) : RemoteViewsService.RemoteViewsFactory { class RecentListFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
private val packageName = context.packageName
private val dataSet = ArrayList<Manga>() private val dataSet = ArrayList<Manga>()
@@ -36,11 +34,13 @@ class RecentListFactory(context: Context, private val intent: Intent) : RemoteVi
override fun hasStableIds() = true override fun hasStableIds() = true
override fun getViewAt(position: Int): RemoteViews { override fun getViewAt(position: Int): RemoteViews {
val views = RemoteViews(packageName, R.layout.item_recent) val views = RemoteViews(context.packageName, R.layout.item_recent)
val item = dataSet[position] val item = dataSet[position]
try { try {
val cover = runBlocking { val cover = runBlocking {
Coil.loader().get(item.coverUrl).toBitmap() Coil.execute(GetRequestBuilder(context)
.data(item.coverUrl)
.build()).requireBitmap()
} }
views.setImageViewBitmap(R.id.imageView_cover, cover) views.setImageViewBitmap(R.id.imageView_cover, cover)
} catch (e: IOException) { } catch (e: IOException) {

View File

@@ -6,6 +6,6 @@ import android.widget.RemoteViewsService
class RecentWidgetService : RemoteViewsService() { class RecentWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return RecentListFactory(this, intent) return RecentListFactory(this)
} }
} }

View File

@@ -4,19 +4,17 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.widget.RemoteViews import android.widget.RemoteViews
import android.widget.RemoteViewsService import android.widget.RemoteViewsService
import androidx.core.graphics.drawable.toBitmap
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okio.IOException
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.ui.details.MangaDetailsActivity
import org.koitharu.kotatsu.utils.ext.requireBitmap
import java.io.IOException
class ShelfListFactory(context: Context, private val intent: Intent) : RemoteViewsService.RemoteViewsFactory { class ShelfListFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
private val packageName = context.packageName
private val dataSet = ArrayList<Manga>() private val dataSet = ArrayList<Manga>()
@@ -36,12 +34,14 @@ class ShelfListFactory(context: Context, private val intent: Intent) : RemoteVie
override fun hasStableIds() = true override fun hasStableIds() = true
override fun getViewAt(position: Int): RemoteViews { override fun getViewAt(position: Int): RemoteViews {
val views = RemoteViews(packageName, R.layout.item_shelf) val views = RemoteViews(context.packageName, R.layout.item_shelf)
val item = dataSet[position] val item = dataSet[position]
views.setTextViewText(R.id.textView_title, item.title) views.setTextViewText(R.id.textView_title, item.title)
try { try {
val cover = runBlocking { val cover = runBlocking {
Coil.loader().get(item.coverUrl).toBitmap() Coil.execute(GetRequestBuilder(context)
.data(item.coverUrl)
.build()).requireBitmap()
} }
views.setImageViewBitmap(R.id.imageView_cover, cover) views.setImageViewBitmap(R.id.imageView_cover, cover)
} catch (e: IOException) { } catch (e: IOException) {
@@ -57,6 +57,5 @@ class ShelfListFactory(context: Context, private val intent: Intent) : RemoteVie
override fun getViewTypeCount() = 1 override fun getViewTypeCount() = 1
override fun onDestroy() { override fun onDestroy() = Unit
}
} }

View File

@@ -6,6 +6,6 @@ import android.widget.RemoteViewsService
class ShelfWidgetService : RemoteViewsService() { class ShelfWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
return ShelfListFactory(this, intent) return ShelfListFactory(this)
} }
} }

View File

@@ -16,7 +16,7 @@ class AlphanumComparator : Comparator<String> {
val thatChunk = getChunk(s2, s2Length, thatMarker) val thatChunk = getChunk(s2, s2Length, thatMarker)
thatMarker += thatChunk.length thatMarker += thatChunk.length
// If both chunks contain numeric characters, sort them numerically // If both chunks contain numeric characters, sort them numerically
var result = 0 var result: Int
if (thisChunk[0].isDigit() && thatChunk[0].isDigit()) { // Simple chunk comparison by length. if (thisChunk[0].isDigit() && thatChunk[0].isDigit()) { // Simple chunk comparison by length.
val thisChunkLength = thisChunk.length val thisChunkLength = thisChunk.length
result = thisChunkLength - thatChunk.length result = thisChunkLength - thatChunk.length
@@ -37,8 +37,8 @@ class AlphanumComparator : Comparator<String> {
return s1Length - s2Length return s1Length - s2Length
} }
private fun getChunk(s: String, slength: Int, marker: Int): String { private fun getChunk(s: String, slength: Int, cmarker: Int): String {
var marker = marker var marker = cmarker
val chunk = StringBuilder() val chunk = StringBuilder()
var c = s[marker] var c = s[marker]
chunk.append(c) chunk.append(c)

View File

@@ -9,17 +9,16 @@ import androidx.annotation.RequiresApi
import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import androidx.core.graphics.drawable.toBitmap
import coil.Coil import coil.Coil
import coil.api.get import coil.request.GetRequestBuilder
import coil.size.PixelSize import coil.size.PixelSize
import coil.size.Scale
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.MangaDataRepository import org.koitharu.kotatsu.domain.MangaDataRepository
import org.koitharu.kotatsu.ui.details.MangaDetailsActivity import org.koitharu.kotatsu.ui.details.MangaDetailsActivity
import org.koitharu.kotatsu.utils.ext.requireBitmap
import org.koitharu.kotatsu.utils.ext.safe import org.koitharu.kotatsu.utils.ext.safe
class MangaShortcut(private val manga: Manga) { class MangaShortcut(private val manga: Manga) {
@@ -67,10 +66,9 @@ class MangaShortcut(private val manga: Manga) {
val icon = safe { val icon = safe {
val size = getIconSize(context) val size = getIconSize(context)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val bmp = Coil.loader().get(manga.coverUrl) { val bmp = Coil.execute(GetRequestBuilder(context)
size(size) .data(manga.coverUrl)
scale(Scale.FILL) .build()).requireBitmap()
}.toBitmap()
ThumbnailUtils.extractThumbnail(bmp, size.width, size.height, 0) ThumbnailUtils.extractThumbnail(bmp, size.width, size.height, 0)
} }
} }

View File

@@ -0,0 +1,20 @@
package org.koitharu.kotatsu.utils.ext
import androidx.core.graphics.drawable.toBitmap
import coil.request.ErrorResult
import coil.request.RequestResult
import coil.request.SuccessResult
fun RequestResult.requireBitmap() = when(this) {
is SuccessResult -> drawable.toBitmap()
is ErrorResult -> throw throwable
}
fun RequestResult.toBitmapOrNull() = when(this) {
is SuccessResult -> try {
drawable.toBitmap()
} catch (_: Throwable) {
null
}
is ErrorResult -> null
}