Skip to content

Commit 53f7b3a

Browse files
committed
Reorder downloads + Fixes to extensions autochecker
1 parent 65ca7ab commit 53f7b3a

9 files changed

Lines changed: 180 additions & 178 deletions

File tree

app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import com.hippo.unifile.UniFile
55
import com.jakewharton.rxrelay.BehaviorRelay
66
import eu.kanade.tachiyomi.data.database.models.Chapter
77
import eu.kanade.tachiyomi.data.database.models.Manga
8+
import eu.kanade.tachiyomi.data.download.model.Download
89
import eu.kanade.tachiyomi.data.download.model.DownloadQueue
910
import eu.kanade.tachiyomi.source.Source
1011
import eu.kanade.tachiyomi.source.SourceManager
1112
import eu.kanade.tachiyomi.source.model.Page
12-
import eu.kanade.tachiyomi.util.launchNow
13-
import eu.kanade.tachiyomi.util.launchUI
14-
import kotlinx.coroutines.delay
1513
import rx.Observable
1614
import uy.kohesive.injekt.injectLazy
1715

@@ -95,6 +93,19 @@ class DownloadManager(context: Context) {
9593
downloader.clearQueue(isNotification)
9694
}
9795

96+
/**
97+
* Reorders the download queue.
98+
*
99+
* @param downloads value to set the download queue to
100+
*/
101+
fun reorderQueue(downloads: List<Download>) {
102+
downloader.pause()
103+
downloader.queue.clear()
104+
downloader.queue.addAll(downloads)
105+
downloader.start()
106+
}
107+
108+
98109
/**
99110
* Tells the downloader to enqueue the given list of chapters.
100111
*

app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import eu.kanade.tachiyomi.source.SourceManager
1313
import eu.kanade.tachiyomi.source.model.Page
1414
import eu.kanade.tachiyomi.source.online.HttpSource
1515
import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList
16-
import eu.kanade.tachiyomi.util.*
16+
import eu.kanade.tachiyomi.util.ImageUtil
17+
import eu.kanade.tachiyomi.util.RetryWithDelay
18+
import eu.kanade.tachiyomi.util.launchNow
19+
import eu.kanade.tachiyomi.util.launchUI
20+
import eu.kanade.tachiyomi.util.plusAssign
21+
import eu.kanade.tachiyomi.util.saveTo
1722
import kotlinx.coroutines.async
1823
import okhttp3.Response
1924
import rx.Observable
@@ -94,7 +99,7 @@ class Downloader(
9499
fun start(): Boolean {
95100
if (isRunning || queue.isEmpty())
96101
return false
97-
102+
notifier.paused = false
98103
if (!subscriptions.hasSubscriptions())
99104
initializeSubscriptions()
100105

@@ -184,7 +189,6 @@ class Downloader(
184189
if (isRunning) return
185190
isRunning = true
186191
runningRelay.call(true)
187-
188192
subscriptions.clear()
189193

190194
subscriptions += downloadsRelay.concatMapIterable { it }

app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionUpdateJob.kt

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.data.notification.NotificationReceiver
1111
import eu.kanade.tachiyomi.data.notification.Notifications
1212
import eu.kanade.tachiyomi.util.notification
1313
import rx.Observable
14+
import rx.Subscription
1415
import rx.schedulers.Schedulers
1516
import timber.log.Timber
1617
import uy.kohesive.injekt.Injekt
@@ -19,47 +20,16 @@ import java.util.concurrent.TimeUnit
1920

2021
class ExtensionUpdateJob : Job() {
2122

23+
var subscription:Subscription? = null
24+
2225
override fun onRunJob(params: Params): Result {
2326
val extensionManager: ExtensionManager = Injekt.get()
2427
extensionManager.findAvailableExtensions()
25-
/*return extensionManager.getInstalledExtensionsObservable()
26-
.map { list ->
27-
val pendingUpdates = list.filter { it.hasUpdate }
28-
if (pendingUpdates.isNotEmpty()) {
29-
val names = pendingUpdates.map { it.name }
3028

31-
NotificationManagerCompat.from(context).apply {
32-
notify(Notifications.ID_UPDATES_TO_EXTS,
33-
context.notification(Notifications.CHANNEL_UPDATES_TO_EXTS) {
34-
setContentTitle(
35-
context.getString(
36-
R.string.update_check_notification_ext_updates, names.size
37-
)
38-
)
39-
val extNames = if (names.size > 5) {
40-
"${names.take(4).joinToString(", ")}, " + context.getString(
41-
R.string.notification_and_n_more, (names.size - 4)
42-
)
43-
} else names.joinToString(", ")
44-
setContentText(extNames)
45-
setSmallIcon(R.drawable.ic_extension_update)
46-
color = ContextCompat.getColor(context, R.color.colorAccentLight)
47-
setContentIntent(
48-
NotificationReceiver.openExtensionsPendingActivity(
49-
context
50-
)
51-
)
52-
setAutoCancel(true)
53-
})
54-
}
55-
}
56-
Result.SUCCESS
57-
}
58-
.onErrorReturn { Result.FAILURE }
59-
// Sadly, the task needs to be synchronous.
60-
.toBlocking()
61-
.single()*/
62-
Observable.defer {
29+
subscription?.unsubscribe()
30+
31+
// Update favorite manga. Destroy service when completed or in case of an error.
32+
subscription = Observable.defer {
6333
extensionManager.getInstalledExtensionsObservable().map { list ->
6434
val pendingUpdates = list.filter { it.hasUpdate }
6535
if (pendingUpdates.isNotEmpty()) {
@@ -89,8 +59,9 @@ class ExtensionUpdateJob : Job() {
8959
})
9060
}
9161
}
62+
subscription?.unsubscribe()
9263
Result.SUCCESS
93-
}.onErrorReturn { Result.FAILURE }
64+
}
9465
}.subscribeOn(Schedulers.io())
9566
.subscribe({
9667
}, {
@@ -100,51 +71,6 @@ class ExtensionUpdateJob : Job() {
10071
return Result.SUCCESS
10172
}
10273

103-
/*fun runStuff(context: Context) {
104-
val extensionManager: ExtensionManager = Injekt.get()
105-
extensionManager.findAvailableExtensions()
106-
Observable.defer {
107-
extensionManager.getInstalledExtensionsObservable().map { list ->
108-
val pendingUpdates = list.filter { it.hasUpdate }
109-
if (pendingUpdates.isNotEmpty()) {
110-
val names = pendingUpdates.map { it.name }
111-
NotificationManagerCompat.from(context).apply {
112-
notify(Notifications.ID_UPDATES_TO_EXTS,
113-
context.notification(Notifications.CHANNEL_UPDATES_TO_EXTS) {
114-
setContentTitle(
115-
context.getString(
116-
R.string.update_check_notification_ext_updates, names.size
117-
)
118-
)
119-
val extNames = if (names.size > 5) {
120-
"${names.take(4).joinToString(", ")}, " + context.getString(
121-
R.string.notification_and_n_more, (names.size - 4)
122-
)
123-
} else names.joinToString(", ")
124-
setContentText(extNames)
125-
setSmallIcon(R.drawable.ic_extension_update)
126-
color = ContextCompat.getColor(context, R.color.colorAccentLight)
127-
setContentIntent(
128-
NotificationReceiver.openExtensionsPendingActivity(
129-
context
130-
)
131-
)
132-
setAutoCancel(true)
133-
})
134-
}
135-
}
136-
Result.SUCCESS
137-
}.onErrorReturn { Result.FAILURE }
138-
}.subscribeOn(Schedulers.io())
139-
.subscribe({
140-
}, {
141-
Timber.e(it)
142-
}, {
143-
})
144-
}*/
145-
146-
147-
14874
companion object {
14975
const val TAG = "ExtensionUpdate"
15076

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,24 @@
11
package eu.kanade.tachiyomi.ui.download
22

3-
import androidx.recyclerview.widget.RecyclerView
4-
import android.view.ViewGroup
5-
import eu.kanade.tachiyomi.R
6-
import eu.kanade.tachiyomi.data.download.model.Download
7-
import eu.kanade.tachiyomi.util.inflate
3+
import eu.davidea.flexibleadapter.FlexibleAdapter
84

95
/**
106
* Adapter storing a list of downloads.
117
*
128
* @param context the context of the fragment containing this adapter.
139
*/
14-
class DownloadAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<DownloadHolder>() {
15-
16-
private var items = emptyList<Download>()
17-
18-
init {
19-
setHasStableIds(true)
20-
}
21-
22-
/**
23-
* Sets a list of downloads in the adapter.
24-
*
25-
* @param downloads the list to set.
26-
*/
27-
fun setItems(downloads: List<Download>) {
28-
items = downloads
29-
notifyDataSetChanged()
30-
}
10+
class DownloadAdapter(controller: DownloadController) : FlexibleAdapter<DownloadItem>(null, controller,
11+
true) {
3112

3213
/**
33-
* Returns the number of downloads in the adapter
14+
* Listener called when an item of the list is released.
3415
*/
35-
override fun getItemCount(): Int {
36-
return items.size
37-
}
16+
val onItemReleaseListener: OnItemReleaseListener = controller
3817

39-
/**
40-
* Returns the identifier for a download.
41-
*
42-
* @param position the position in the adapter.
43-
* @return an identifier for the item.
44-
*/
45-
override fun getItemId(position: Int): Long {
46-
return items[position].chapter.id!!
18+
interface OnItemReleaseListener {
19+
/**
20+
* Called when an item of the list is released.
21+
*/
22+
fun onItemReleased(position: Int)
4723
}
48-
49-
/**
50-
* Creates a new view holder.
51-
*
52-
* @param parent the parent view.
53-
* @param viewType the type of the holder.
54-
* @return a new view holder for a manga.
55-
*/
56-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadHolder {
57-
val view = parent.inflate(R.layout.download_item)
58-
return DownloadHolder(view)
59-
}
60-
61-
/**
62-
* Binds a holder with a new position.
63-
*
64-
* @param holder the holder to bind.
65-
* @param position the position to bind.
66-
*/
67-
override fun onBindViewHolder(holder: DownloadHolder, position: Int) {
68-
val download = items[position]
69-
holder.onSetValues(download)
70-
}
71-
7224
}

app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package eu.kanade.tachiyomi.ui.download
22

3+
import android.view.LayoutInflater
4+
import android.view.Menu
5+
import android.view.MenuInflater
6+
import android.view.MenuItem
7+
import android.view.View
8+
import android.view.ViewGroup
39
import androidx.recyclerview.widget.LinearLayoutManager
4-
import android.view.*
510
import eu.kanade.tachiyomi.R
611
import eu.kanade.tachiyomi.data.download.DownloadService
712
import eu.kanade.tachiyomi.data.download.model.Download
@@ -12,14 +17,15 @@ import kotlinx.android.synthetic.main.download_controller.*
1217
import rx.Observable
1318
import rx.Subscription
1419
import rx.android.schedulers.AndroidSchedulers
15-
import java.util.*
20+
import java.util.HashMap
1621
import java.util.concurrent.TimeUnit
1722

1823
/**
1924
* Controller that shows the currently active downloads.
2025
* Uses R.layout.fragment_download_queue.
2126
*/
22-
class DownloadController : NucleusController<DownloadPresenter>() {
27+
class DownloadController : NucleusController<DownloadPresenter>(),
28+
DownloadAdapter.OnItemReleaseListener {
2329

2430
/**
2531
* Adapter containing the active downloads.
@@ -59,11 +65,12 @@ class DownloadController : NucleusController<DownloadPresenter>() {
5965
setInformationView()
6066

6167
// Initialize adapter.
62-
adapter = DownloadAdapter()
68+
adapter = DownloadAdapter(this@DownloadController)
6369
recycler.adapter = adapter
70+
adapter?.isHandleDragEnabled = true
6471

6572
// Set the layout manager for the recycler and fixed size.
66-
recycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(view.context)
73+
recycler.layoutManager = LinearLayoutManager(view.context)
6774
recycler.setHasFixedSize(true)
6875
recycler.setOnApplyWindowInsetsListener(RecyclerWindowInsetsListener)
6976

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

173-
progressSubscriptions.put(download, subscription)
180+
progressSubscriptions[download] = subscription
174181
}
175182

176183
/**
@@ -200,10 +207,10 @@ class DownloadController : NucleusController<DownloadPresenter>() {
200207
*
201208
* @param downloads the downloads from the queue.
202209
*/
203-
fun onNextDownloads(downloads: List<Download>) {
210+
fun onNextDownloads(downloads: List<DownloadItem>) {
204211
activity?.invalidateOptionsMenu()
205212
setInformationView()
206-
adapter?.setItems(downloads)
213+
adapter?.updateDataSet(downloads)
207214
}
208215

209216
/**
@@ -246,4 +253,15 @@ class DownloadController : NucleusController<DownloadPresenter>() {
246253
}
247254
}
248255

256+
/**
257+
* Called when an item is released from a drag.
258+
*
259+
* @param position The position of the released item.
260+
*/
261+
override fun onItemReleased(position: Int) {
262+
val adapter = adapter ?: return
263+
val downloads = (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download }
264+
presenter.reorder(downloads)
265+
}
266+
249267
}

0 commit comments

Comments
 (0)