mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 19:27:25 +01:00
Allow to cancel update. #192. Needs testing
This commit is contained in:
parent
c2a65c71e1
commit
e4ee03cb61
@ -68,6 +68,10 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</receiver>
|
</receiver>
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name=".data.library.LibraryUpdateService$CancelUpdateReceiver">
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".data.library.LibraryUpdateAlarm">
|
android:name=".data.library.LibraryUpdateAlarm">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
|
@ -33,7 +33,7 @@ import javax.inject.Inject
|
|||||||
* @param context the application context.
|
* @param context the application context.
|
||||||
* @return the intent of the service.
|
* @return the intent of the service.
|
||||||
*/
|
*/
|
||||||
fun getStartIntent(context: Context): Intent {
|
fun getIntent(context: Context): Intent {
|
||||||
return Intent(context, LibraryUpdateService::class.java)
|
return Intent(context, LibraryUpdateService::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,10 +78,14 @@ class LibraryUpdateService : Service() {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun start(context: Context) {
|
fun start(context: Context) {
|
||||||
if (!isRunning(context)) {
|
if (!isRunning(context)) {
|
||||||
context.startService(getStartIntent(context))
|
context.startService(getIntent(context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun stop(context: Context) {
|
||||||
|
context.stopService(getIntent(context))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -160,6 +164,9 @@ class LibraryUpdateService : Service() {
|
|||||||
val newUpdates = ArrayList<Manga>()
|
val newUpdates = ArrayList<Manga>()
|
||||||
val failedUpdates = ArrayList<Manga>()
|
val failedUpdates = ArrayList<Manga>()
|
||||||
|
|
||||||
|
val cancelIntent = PendingIntent.getBroadcast(this, 0,
|
||||||
|
Intent(this, CancelUpdateReceiver::class.java), 0)
|
||||||
|
|
||||||
// Get the manga list that is going to be updated.
|
// Get the manga list that is going to be updated.
|
||||||
val allLibraryMangas = db.favoriteMangas.executeAsBlocking()
|
val allLibraryMangas = db.favoriteMangas.executeAsBlocking()
|
||||||
val toUpdate = if (!preferences.updateOnlyNonCompleted())
|
val toUpdate = if (!preferences.updateOnlyNonCompleted())
|
||||||
@ -170,7 +177,7 @@ class LibraryUpdateService : Service() {
|
|||||||
// Emit each manga and update it sequentially.
|
// Emit each manga and update it sequentially.
|
||||||
return Observable.from(toUpdate)
|
return Observable.from(toUpdate)
|
||||||
// Notify manga that will update.
|
// Notify manga that will update.
|
||||||
.doOnNext { showProgressNotification(it, count.andIncrement, toUpdate.size) }
|
.doOnNext { showProgressNotification(it, count.andIncrement, toUpdate.size, cancelIntent) }
|
||||||
// Update the chapters of the manga.
|
// Update the chapters of the manga.
|
||||||
.concatMap { manga -> updateManga(manga)
|
.concatMap { manga -> updateManga(manga)
|
||||||
// If there's any error, return empty update and continue.
|
// If there's any error, return empty update and continue.
|
||||||
@ -262,7 +269,7 @@ class LibraryUpdateService : Service() {
|
|||||||
*/
|
*/
|
||||||
private fun showNotification(title: String, body: String) {
|
private fun showNotification(title: String, body: String) {
|
||||||
val n = notification() {
|
val n = notification() {
|
||||||
setSmallIcon(R.drawable.ic_action_refresh)
|
setSmallIcon(R.drawable.ic_refresh_white_24dp)
|
||||||
setContentTitle(title)
|
setContentTitle(title)
|
||||||
setContentText(body)
|
setContentText(body)
|
||||||
}
|
}
|
||||||
@ -275,12 +282,13 @@ class LibraryUpdateService : Service() {
|
|||||||
* @param current the current progress.
|
* @param current the current progress.
|
||||||
* @param total the total progress.
|
* @param total the total progress.
|
||||||
*/
|
*/
|
||||||
private fun showProgressNotification(manga: Manga, current: Int, total: Int) {
|
private fun showProgressNotification(manga: Manga, current: Int, total: Int, cancelIntent: PendingIntent) {
|
||||||
val n = notification() {
|
val n = notification() {
|
||||||
setSmallIcon(R.drawable.ic_action_refresh)
|
setSmallIcon(R.drawable.ic_refresh_white_24dp)
|
||||||
setContentTitle(manga.title)
|
setContentTitle(manga.title)
|
||||||
setProgress(total, current, false)
|
setProgress(total, current, false)
|
||||||
setOngoing(true)
|
setOngoing(true)
|
||||||
|
addAction(R.drawable.ic_clear, getString(R.string.action_cancel), cancelIntent)
|
||||||
}
|
}
|
||||||
notificationManager.notify(UPDATE_NOTIFICATION_ID, n)
|
notificationManager.notify(UPDATE_NOTIFICATION_ID, n)
|
||||||
}
|
}
|
||||||
@ -295,7 +303,7 @@ class LibraryUpdateService : Service() {
|
|||||||
val body = getUpdatedMangasBody(updates, failed)
|
val body = getUpdatedMangasBody(updates, failed)
|
||||||
|
|
||||||
val n = notification() {
|
val n = notification() {
|
||||||
setSmallIcon(R.drawable.ic_action_refresh)
|
setSmallIcon(R.drawable.ic_refresh_white_24dp)
|
||||||
setContentTitle(title)
|
setContentTitle(title)
|
||||||
setStyle(NotificationCompat.BigTextStyle().bigText(body))
|
setStyle(NotificationCompat.BigTextStyle().bigText(body))
|
||||||
setContentIntent(notificationIntent)
|
setContentIntent(notificationIntent)
|
||||||
@ -335,9 +343,22 @@ class LibraryUpdateService : Service() {
|
|||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (NetworkUtil.isNetworkConnected(context)) {
|
if (NetworkUtil.isNetworkConnected(context)) {
|
||||||
AndroidComponentUtil.toggleComponent(context, this.javaClass, false)
|
AndroidComponentUtil.toggleComponent(context, this.javaClass, false)
|
||||||
context.startService(getStartIntent(context))
|
context.startService(getIntent(context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CancelUpdateReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method called when user stops the update.
|
||||||
|
* @param context the application context.
|
||||||
|
* @param intent the intent received.
|
||||||
|
*/
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
LibraryUpdateService.stop(context)
|
||||||
|
context.notificationManager.cancel(UPDATE_NOTIFICATION_ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,7 @@
|
|||||||
<string name="action_retry">Retry</string>
|
<string name="action_retry">Retry</string>
|
||||||
<string name="action_open_in_browser">Open in browser</string>
|
<string name="action_open_in_browser">Open in browser</string>
|
||||||
<string name="action_display_mode">Change display mode</string>
|
<string name="action_display_mode">Change display mode</string>
|
||||||
|
<string name="action_cancel">Cancel</string>
|
||||||
|
|
||||||
<!-- Buttons -->
|
<!-- Buttons -->
|
||||||
<string name="button_ok">OK</string>
|
<string name="button_ok">OK</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user