Refactor: fix deprecations

pull/26/head
Koitharu 6 years ago
parent 8378b3dd90
commit 1b7c8355ec

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

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

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

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

@ -3,7 +3,7 @@ package org.koitharu.kotatsu.ui.reader.thumbnails
import android.view.ViewGroup
import androidx.core.net.toUri
import coil.Coil
import coil.api.get
import coil.request.GetRequestBuilder
import coil.size.PixelSize
import coil.size.Size
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)
extra[pageUrl]?.toUri()?.toString() ?: pageUrl
}
val drawable = Coil.get(url) {
size(thumbSize)
}
val drawable = Coil.execute(GetRequestBuilder(context)
.data(url)
.size(thumbSize)
.build()).drawable
withContext(Dispatchers.Main) {
imageView_thumb.setImageDrawable(drawable)
}

@ -47,11 +47,11 @@ class SourcesAdapter(private val onItemClickListener: OnRecyclerItemClickListene
settings.hiddenSources = hiddenItems.map { x -> x.name }.toSet()
}
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 ->
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
onItemClickListener.onItemLongClick(holder.requireData(), holder.adapterPosition, v)
onItemClickListener.onItemLongClick(holder.requireData(), holder.bindingAdapterPosition, v)
} else {
false
}

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

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

@ -4,19 +4,17 @@ import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import android.widget.RemoteViewsService
import androidx.core.graphics.drawable.toBitmap
import coil.Coil
import coil.api.get
import coil.request.GetRequestBuilder
import kotlinx.coroutines.runBlocking
import okio.IOException
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.history.HistoryRepository
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 {
private val packageName = context.packageName
class RecentListFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
private val dataSet = ArrayList<Manga>()
@ -36,11 +34,13 @@ class RecentListFactory(context: Context, private val intent: Intent) : RemoteVi
override fun hasStableIds() = true
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]
try {
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)
} catch (e: IOException) {

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

@ -4,19 +4,17 @@ import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import android.widget.RemoteViewsService
import androidx.core.graphics.drawable.toBitmap
import coil.Coil
import coil.api.get
import coil.request.GetRequestBuilder
import kotlinx.coroutines.runBlocking
import okio.IOException
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.domain.favourites.FavouritesRepository
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 {
private val packageName = context.packageName
class ShelfListFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
private val dataSet = ArrayList<Manga>()
@ -36,12 +34,14 @@ class ShelfListFactory(context: Context, private val intent: Intent) : RemoteVie
override fun hasStableIds() = true
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]
views.setTextViewText(R.id.textView_title, item.title)
try {
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)
} catch (e: IOException) {
@ -57,6 +57,5 @@ class ShelfListFactory(context: Context, private val intent: Intent) : RemoteVie
override fun getViewTypeCount() = 1
override fun onDestroy() {
}
override fun onDestroy() = Unit
}

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

@ -16,7 +16,7 @@ class AlphanumComparator : Comparator<String> {
val thatChunk = getChunk(s2, s2Length, thatMarker)
thatMarker += thatChunk.length
// 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.
val thisChunkLength = thisChunk.length
result = thisChunkLength - thatChunk.length
@ -37,8 +37,8 @@ class AlphanumComparator : Comparator<String> {
return s1Length - s2Length
}
private fun getChunk(s: String, slength: Int, marker: Int): String {
var marker = marker
private fun getChunk(s: String, slength: Int, cmarker: Int): String {
var marker = cmarker
val chunk = StringBuilder()
var c = s[marker]
chunk.append(c)

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

@ -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
}
Loading…
Cancel
Save