Skip to content

Commit

Permalink
Reorder downloads + Fixes to extensions autochecker
Browse files Browse the repository at this point in the history
  • Loading branch information
Jays2Kings committed Dec 22, 2019
1 parent 65ca7ab commit 53f7b3a
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import com.hippo.unifile.UniFile
import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.download.model.DownloadQueue
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.util.launchNow
import eu.kanade.tachiyomi.util.launchUI
import kotlinx.coroutines.delay
import rx.Observable
import uy.kohesive.injekt.injectLazy

Expand Down Expand Up @@ -95,6 +93,19 @@ class DownloadManager(context: Context) {
downloader.clearQueue(isNotification)
}

/**
* Reorders the download queue.
*
* @param downloads value to set the download queue to
*/
fun reorderQueue(downloads: List<Download>) {
downloader.pause()
downloader.queue.clear()
downloader.queue.addAll(downloads)
downloader.start()
}


/**
* Tells the downloader to enqueue the given list of chapters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList
import eu.kanade.tachiyomi.util.*
import eu.kanade.tachiyomi.util.ImageUtil
import eu.kanade.tachiyomi.util.RetryWithDelay
import eu.kanade.tachiyomi.util.launchNow
import eu.kanade.tachiyomi.util.launchUI
import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.util.saveTo
import kotlinx.coroutines.async
import okhttp3.Response
import rx.Observable
Expand Down Expand Up @@ -94,7 +99,7 @@ class Downloader(
fun start(): Boolean {
if (isRunning || queue.isEmpty())
return false

notifier.paused = false
if (!subscriptions.hasSubscriptions())
initializeSubscriptions()

Expand Down Expand Up @@ -184,7 +189,6 @@ class Downloader(
if (isRunning) return
isRunning = true
runningRelay.call(true)

subscriptions.clear()

subscriptions += downloadsRelay.concatMapIterable { it }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.notification
import rx.Observable
import rx.Subscription
import rx.schedulers.Schedulers
import timber.log.Timber
import uy.kohesive.injekt.Injekt
Expand All @@ -19,47 +20,16 @@ import java.util.concurrent.TimeUnit

class ExtensionUpdateJob : Job() {

var subscription:Subscription? = null

override fun onRunJob(params: Params): Result {
val extensionManager: ExtensionManager = Injekt.get()
extensionManager.findAvailableExtensions()
/*return extensionManager.getInstalledExtensionsObservable()
.map { list ->
val pendingUpdates = list.filter { it.hasUpdate }
if (pendingUpdates.isNotEmpty()) {
val names = pendingUpdates.map { it.name }

NotificationManagerCompat.from(context).apply {
notify(Notifications.ID_UPDATES_TO_EXTS,
context.notification(Notifications.CHANNEL_UPDATES_TO_EXTS) {
setContentTitle(
context.getString(
R.string.update_check_notification_ext_updates, names.size
)
)
val extNames = if (names.size > 5) {
"${names.take(4).joinToString(", ")}, " + context.getString(
R.string.notification_and_n_more, (names.size - 4)
)
} else names.joinToString(", ")
setContentText(extNames)
setSmallIcon(R.drawable.ic_extension_update)
color = ContextCompat.getColor(context, R.color.colorAccentLight)
setContentIntent(
NotificationReceiver.openExtensionsPendingActivity(
context
)
)
setAutoCancel(true)
})
}
}
Result.SUCCESS
}
.onErrorReturn { Result.FAILURE }
// Sadly, the task needs to be synchronous.
.toBlocking()
.single()*/
Observable.defer {
subscription?.unsubscribe()

// Update favorite manga. Destroy service when completed or in case of an error.
subscription = Observable.defer {
extensionManager.getInstalledExtensionsObservable().map { list ->
val pendingUpdates = list.filter { it.hasUpdate }
if (pendingUpdates.isNotEmpty()) {
Expand Down Expand Up @@ -89,8 +59,9 @@ class ExtensionUpdateJob : Job() {
})
}
}
subscription?.unsubscribe()
Result.SUCCESS
}.onErrorReturn { Result.FAILURE }
}
}.subscribeOn(Schedulers.io())
.subscribe({
}, {
Expand All @@ -100,51 +71,6 @@ class ExtensionUpdateJob : Job() {
return Result.SUCCESS
}

/*fun runStuff(context: Context) {
val extensionManager: ExtensionManager = Injekt.get()
extensionManager.findAvailableExtensions()
Observable.defer {
extensionManager.getInstalledExtensionsObservable().map { list ->
val pendingUpdates = list.filter { it.hasUpdate }
if (pendingUpdates.isNotEmpty()) {
val names = pendingUpdates.map { it.name }
NotificationManagerCompat.from(context).apply {
notify(Notifications.ID_UPDATES_TO_EXTS,
context.notification(Notifications.CHANNEL_UPDATES_TO_EXTS) {
setContentTitle(
context.getString(
R.string.update_check_notification_ext_updates, names.size
)
)
val extNames = if (names.size > 5) {
"${names.take(4).joinToString(", ")}, " + context.getString(
R.string.notification_and_n_more, (names.size - 4)
)
} else names.joinToString(", ")
setContentText(extNames)
setSmallIcon(R.drawable.ic_extension_update)
color = ContextCompat.getColor(context, R.color.colorAccentLight)
setContentIntent(
NotificationReceiver.openExtensionsPendingActivity(
context
)
)
setAutoCancel(true)
})
}
}
Result.SUCCESS
}.onErrorReturn { Result.FAILURE }
}.subscribeOn(Schedulers.io())
.subscribe({
}, {
Timber.e(it)
}, {
})
}*/



companion object {
const val TAG = "ExtensionUpdate"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,24 @@
package eu.kanade.tachiyomi.ui.download

import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.util.inflate
import eu.davidea.flexibleadapter.FlexibleAdapter

/**
* Adapter storing a list of downloads.
*
* @param context the context of the fragment containing this adapter.
*/
class DownloadAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<DownloadHolder>() {

private var items = emptyList<Download>()

init {
setHasStableIds(true)
}

/**
* Sets a list of downloads in the adapter.
*
* @param downloads the list to set.
*/
fun setItems(downloads: List<Download>) {
items = downloads
notifyDataSetChanged()
}
class DownloadAdapter(controller: DownloadController) : FlexibleAdapter<DownloadItem>(null, controller,
true) {

/**
* Returns the number of downloads in the adapter
* Listener called when an item of the list is released.
*/
override fun getItemCount(): Int {
return items.size
}
val onItemReleaseListener: OnItemReleaseListener = controller

/**
* Returns the identifier for a download.
*
* @param position the position in the adapter.
* @return an identifier for the item.
*/
override fun getItemId(position: Int): Long {
return items[position].chapter.id!!
interface OnItemReleaseListener {
/**
* Called when an item of the list is released.
*/
fun onItemReleased(position: Int)
}

/**
* Creates a new view holder.
*
* @param parent the parent view.
* @param viewType the type of the holder.
* @return a new view holder for a manga.
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadHolder {
val view = parent.inflate(R.layout.download_item)
return DownloadHolder(view)
}

/**
* Binds a holder with a new position.
*
* @param holder the holder to bind.
* @param position the position to bind.
*/
override fun onBindViewHolder(holder: DownloadHolder, position: Int) {
val download = items[position]
holder.onSetValues(download)
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package eu.kanade.tachiyomi.ui.download

import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.*
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.DownloadService
import eu.kanade.tachiyomi.data.download.model.Download
Expand All @@ -12,14 +17,15 @@ import kotlinx.android.synthetic.main.download_controller.*
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import java.util.*
import java.util.HashMap
import java.util.concurrent.TimeUnit

/**
* Controller that shows the currently active downloads.
* Uses R.layout.fragment_download_queue.
*/
class DownloadController : NucleusController<DownloadPresenter>() {
class DownloadController : NucleusController<DownloadPresenter>(),
DownloadAdapter.OnItemReleaseListener {

/**
* Adapter containing the active downloads.
Expand Down Expand Up @@ -59,11 +65,12 @@ class DownloadController : NucleusController<DownloadPresenter>() {
setInformationView()

// Initialize adapter.
adapter = DownloadAdapter()
adapter = DownloadAdapter(this@DownloadController)
recycler.adapter = adapter
adapter?.isHandleDragEnabled = true

// Set the layout manager for the recycler and fixed size.
recycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context)
recycler.layoutManager = LinearLayoutManager(view.context)
recycler.setHasFixedSize(true)
recycler.setOnApplyWindowInsetsListener(RecyclerWindowInsetsListener)

Expand Down Expand Up @@ -170,7 +177,7 @@ class DownloadController : NucleusController<DownloadPresenter>() {
// Avoid leaking subscriptions
progressSubscriptions.remove(download)?.unsubscribe()

progressSubscriptions.put(download, subscription)
progressSubscriptions[download] = subscription
}

/**
Expand Down Expand Up @@ -200,10 +207,10 @@ class DownloadController : NucleusController<DownloadPresenter>() {
*
* @param downloads the downloads from the queue.
*/
fun onNextDownloads(downloads: List<Download>) {
fun onNextDownloads(downloads: List<DownloadItem>) {
activity?.invalidateOptionsMenu()
setInformationView()
adapter?.setItems(downloads)
adapter?.updateDataSet(downloads)
}

/**
Expand Down Expand Up @@ -246,4 +253,15 @@ class DownloadController : NucleusController<DownloadPresenter>() {
}
}

/**
* Called when an item is released from a drag.
*
* @param position The position of the released item.
*/
override fun onItemReleased(position: Int) {
val adapter = adapter ?: return
val downloads = (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download }
presenter.reorder(downloads)
}

}
Loading

0 comments on commit 53f7b3a

Please sign in to comment.