Small cleanup
This commit is contained in:
parent
5a67d8169d
commit
079ca1d0b3
@ -7,11 +7,12 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.bluelinelabs.conductor.ControllerChangeHandler
|
import com.bluelinelabs.conductor.ControllerChangeHandler
|
||||||
import com.bluelinelabs.conductor.changehandler.AnimatorChangeHandler
|
import com.bluelinelabs.conductor.changehandler.AnimatorChangeHandler
|
||||||
|
import com.bluelinelabs.conductor.changehandler.FadeChangeHandler
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An [AnimatorChangeHandler] that will cross fade two views
|
* An [AnimatorChangeHandler] that will cross fade two views
|
||||||
*/
|
*/
|
||||||
class OneWayFadeChangeHandler : AnimatorChangeHandler {
|
class OneWayFadeChangeHandler : FadeChangeHandler {
|
||||||
constructor()
|
constructor()
|
||||||
constructor(removesFromViewOnPush: Boolean) : super(removesFromViewOnPush)
|
constructor(removesFromViewOnPush: Boolean) : super(removesFromViewOnPush)
|
||||||
constructor(duration: Long) : super(duration)
|
constructor(duration: Long) : super(duration)
|
||||||
@ -33,10 +34,6 @@ class OneWayFadeChangeHandler : AnimatorChangeHandler {
|
|||||||
return animator
|
return animator
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun resetFromView(from: View) {
|
|
||||||
from.alpha = 1f
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun copy(): ControllerChangeHandler {
|
override fun copy(): ControllerChangeHandler {
|
||||||
return OneWayFadeChangeHandler(animationDuration, removesFromViewOnPush())
|
return OneWayFadeChangeHandler(animationDuration, removesFromViewOnPush())
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import android.app.Service
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
|
import android.os.IBinder
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
@ -19,6 +20,7 @@ import eu.kanade.tachiyomi.network.GET
|
|||||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||||
import eu.kanade.tachiyomi.network.await
|
import eu.kanade.tachiyomi.network.await
|
||||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||||
|
import eu.kanade.tachiyomi.util.system.acquireWakeLock
|
||||||
import eu.kanade.tachiyomi.util.system.isServiceRunning
|
import eu.kanade.tachiyomi.util.system.isServiceRunning
|
||||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||||
import exh.md.similar.sql.models.MangaSimilarImpl
|
import exh.md.similar.sql.models.MangaSimilarImpl
|
||||||
@ -35,7 +37,6 @@ import okio.source
|
|||||||
import uy.kohesive.injekt.Injekt
|
import uy.kohesive.injekt.Injekt
|
||||||
import uy.kohesive.injekt.api.get
|
import uy.kohesive.injekt.api.get
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
class SimilarUpdateService(
|
class SimilarUpdateService(
|
||||||
val db: DatabaseHelper = Injekt.get()
|
val db: DatabaseHelper = Injekt.get()
|
||||||
@ -59,7 +60,7 @@ class SimilarUpdateService(
|
|||||||
*/
|
*/
|
||||||
private lateinit var wakeLock: PowerManager.WakeLock
|
private lateinit var wakeLock: PowerManager.WakeLock
|
||||||
|
|
||||||
var similarServiceScope = CoroutineScope(Dispatchers.IO + Job())
|
private val similarServiceScope = CoroutineScope(Dispatchers.IO + Job())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscription where the update is done.
|
* Subscription where the update is done.
|
||||||
@ -93,31 +94,32 @@ class SimilarUpdateService(
|
|||||||
*/
|
*/
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
wakeLock = acquireWakeLock("SimilarUpdateService")
|
||||||
startForeground(Notifications.ID_SIMILAR_PROGRESS, progressNotification.build())
|
startForeground(Notifications.ID_SIMILAR_PROGRESS, progressNotification.build())
|
||||||
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
|
|
||||||
PowerManager.PARTIAL_WAKE_LOCK,
|
|
||||||
"SimilarUpdateService:WakeLock"
|
|
||||||
)
|
|
||||||
wakeLock.acquire(TimeUnit.MINUTES.toMillis(30))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
override fun stopService(name: Intent?): Boolean {
|
||||||
* Method called when the service is destroyed. It destroys subscriptions and releases the wake
|
destroyJob()
|
||||||
* lock.
|
return super.stopService(name)
|
||||||
*/
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
destroyJob()
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun destroyJob() {
|
||||||
job?.cancel()
|
job?.cancel()
|
||||||
similarServiceScope.cancel()
|
if (similarServiceScope.isActive) similarServiceScope.cancel()
|
||||||
if (wakeLock.isHeld) {
|
if (wakeLock.isHeld) {
|
||||||
wakeLock.release()
|
wakeLock.release()
|
||||||
}
|
}
|
||||||
super.onDestroy()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method needs to be implemented, but it's not used/needed.
|
* This method needs to be implemented, but it's not used/needed.
|
||||||
*/
|
*/
|
||||||
override fun onBind(intent: Intent) = null
|
override fun onBind(intent: Intent): IBinder? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called when the service receives an intent.
|
* Method called when the service receives an intent.
|
||||||
@ -128,7 +130,7 @@ class SimilarUpdateService(
|
|||||||
* @return the start value of the command.
|
* @return the start value of the command.
|
||||||
*/
|
*/
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (intent == null) return Service.START_NOT_STICKY
|
if (intent == null) return START_NOT_STICKY
|
||||||
|
|
||||||
// Unsubscribe from any previous subscription if needed.
|
// Unsubscribe from any previous subscription if needed.
|
||||||
job?.cancel()
|
job?.cancel()
|
||||||
@ -182,12 +184,12 @@ class SimilarUpdateService(
|
|||||||
return@mapIndexed null
|
return@mapIndexed null
|
||||||
}
|
}
|
||||||
|
|
||||||
val similar = MangaSimilarImpl()
|
MangaSimilarImpl().apply {
|
||||||
similar.id = index.toLong()
|
id = index.toLong()
|
||||||
similar.manga_id = similarFromJson.id.toLong()
|
manga_id = similarFromJson.id.toLong()
|
||||||
similar.matched_ids = similarFromJson.similarIds.joinToString(MangaSimilarImpl.DELIMITER)
|
matched_ids = similarFromJson.similarIds.joinToString(MangaSimilarImpl.DELIMITER)
|
||||||
similar.matched_titles = similarFromJson.similarTitles.joinToString(MangaSimilarImpl.DELIMITER)
|
matched_titles = similarFromJson.similarTitles.joinToString(MangaSimilarImpl.DELIMITER)
|
||||||
return@mapIndexed similar
|
}
|
||||||
}.filterNotNull()
|
}.filterNotNull()
|
||||||
|
|
||||||
showProgressNotification(dataToInsert.size, totalManga)
|
showProgressNotification(dataToInsert.size, totalManga)
|
||||||
@ -211,39 +213,46 @@ class SimilarUpdateService(
|
|||||||
val similars = mutableListOf<SimilarFromJson>()
|
val similars = mutableListOf<SimilarFromJson>()
|
||||||
|
|
||||||
while (reader.peek() != JsonReader.Token.END_DOCUMENT) {
|
while (reader.peek() != JsonReader.Token.END_DOCUMENT) {
|
||||||
val nextToken = reader.peek()
|
when (reader.peek()) {
|
||||||
|
JsonReader.Token.BEGIN_OBJECT -> {
|
||||||
if (JsonReader.Token.BEGIN_OBJECT == nextToken) {
|
reader.beginObject()
|
||||||
reader.beginObject()
|
|
||||||
} else if (JsonReader.Token.NAME == nextToken) {
|
|
||||||
val name = reader.nextName()
|
|
||||||
if (!processingManga && name.isDigitsOnly()) {
|
|
||||||
processingManga = true
|
|
||||||
// similar add id
|
|
||||||
mangaId = name
|
|
||||||
} else if (name == "m_titles") {
|
|
||||||
processingTitles = true
|
|
||||||
}
|
}
|
||||||
} else if (JsonReader.Token.BEGIN_ARRAY == nextToken) {
|
JsonReader.Token.NAME -> {
|
||||||
reader.beginArray()
|
val name = reader.nextName()
|
||||||
} else if (JsonReader.Token.END_ARRAY == nextToken) {
|
if (!processingManga && name.isDigitsOnly()) {
|
||||||
reader.endArray()
|
processingManga = true
|
||||||
if (processingTitles) {
|
// similar add id
|
||||||
processingManga = false
|
mangaId = name
|
||||||
processingTitles = false
|
} else if (name == "m_titles") {
|
||||||
similars.add(SimilarFromJson(mangaId!!, similarIds.toList(), similarTitles.toList()))
|
processingTitles = true
|
||||||
mangaId = null
|
}
|
||||||
similarIds = mutableListOf()
|
|
||||||
similarTitles = mutableListOf()
|
|
||||||
}
|
}
|
||||||
} else if (JsonReader.Token.NUMBER == nextToken) {
|
JsonReader.Token.BEGIN_ARRAY -> {
|
||||||
similarIds.add(reader.nextInt().toString())
|
reader.beginArray()
|
||||||
} else if (JsonReader.Token.STRING == nextToken) {
|
|
||||||
if (processingTitles) {
|
|
||||||
similarTitles.add(reader.nextString())
|
|
||||||
}
|
}
|
||||||
} else if (JsonReader.Token.END_OBJECT == nextToken) {
|
JsonReader.Token.END_ARRAY -> {
|
||||||
reader.endObject()
|
reader.endArray()
|
||||||
|
if (processingTitles) {
|
||||||
|
processingManga = false
|
||||||
|
processingTitles = false
|
||||||
|
similars.add(SimilarFromJson(mangaId!!, similarIds.toList(), similarTitles.toList()))
|
||||||
|
mangaId = null
|
||||||
|
similarIds = mutableListOf()
|
||||||
|
similarTitles = mutableListOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonReader.Token.NUMBER -> {
|
||||||
|
similarIds.add(reader.nextInt().toString())
|
||||||
|
}
|
||||||
|
JsonReader.Token.STRING -> {
|
||||||
|
if (processingTitles) {
|
||||||
|
similarTitles.add(reader.nextString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonReader.Token.END_OBJECT -> {
|
||||||
|
reader.endObject()
|
||||||
|
}
|
||||||
|
else -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,6 @@ fun Float.floor(): Int = floor(this).toInt()
|
|||||||
|
|
||||||
fun Double.floor(): Int = floor(this).toInt()
|
fun Double.floor(): Int = floor(this).toInt()
|
||||||
|
|
||||||
fun Int.nullIfZero() = if (this == 0) null else this
|
fun Int.nullIfZero() = takeUnless { it == 0 }
|
||||||
|
|
||||||
fun Long.nullIfZero() = if (this == 0L) null else this
|
fun Long.nullIfZero() = takeUnless { it == 0L }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user