From 17c59657c30fe2c7975d59f1e4919d25e3c6c253 Mon Sep 17 00:00:00 2001 From: len Date: Mon, 18 Apr 2016 20:14:50 +0200 Subject: [PATCH] Allow to unbind manga, closes #258. Fix some network calls leaking --- .../tachiyomi/data/database/DatabaseHelper.kt | 8 +++++ .../tachiyomi/data/network/NetworkHelper.kt | 10 ++++--- .../myanimelist/MyAnimeListDialogFragment.kt | 4 +-- .../manga/myanimelist/MyAnimeListFragment.kt | 4 +++ .../manga/myanimelist/MyAnimeListPresenter.kt | 30 +++++++++++-------- .../res/layout/card_myanimelist_personal.xml | 2 +- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt index 2172c6eb1..18f922d93 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.kt @@ -260,6 +260,14 @@ open class DatabaseHelper(context: Context) { fun deleteMangaSync(manga: MangaSync) = db.delete().`object`(manga).prepare() + fun deleteMangaSyncForManga(manga: Manga) = db.delete() + .byQuery(DeleteQuery.builder() + .table(MangaSyncTable.TABLE) + .where("${MangaSyncTable.COLUMN_MANGA_ID} = ?") + .whereArgs(manga.id) + .build()) + .prepare() + // Categories related queries fun getCategories() = db.get() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt index 05225aad5..baeb10464 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt @@ -44,10 +44,11 @@ class NetworkHelper(context: Context) { @JvmOverloads fun request(request: Request, forceCache: Boolean = false): Observable { + var response: Response? = null return Observable.fromCallable { val c = if (forceCache) forceCacheClient else client - c.newCall(request).execute() - } + c.newCall(request).execute().apply { response = this } + }.doOnUnsubscribe { response?.body()?.close() } } @JvmOverloads @@ -57,6 +58,7 @@ class NetworkHelper(context: Context) { } fun requestBodyProgress(request: Request, listener: ProgressListener): Observable { + var response: Response? = null return Observable.fromCallable { val progressClient = client.newBuilder() .cache(null) @@ -68,8 +70,8 @@ class NetworkHelper(context: Context) { } .build() - progressClient.newCall(request).execute() - }.retry(1) + progressClient.newCall(request).execute().apply { response = this } + }.doOnUnsubscribe { response?.body()?.close() }.retry(1) } val cookies: CookieStore diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListDialogFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListDialogFragment.kt index 5110043b7..1f4fec6f1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListDialogFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListDialogFragment.kt @@ -91,9 +91,7 @@ class MyAnimeListDialogFragment : DialogFragment() { } private fun onPositiveButtonClick() { - selectedItem?.let { - presenter.registerManga(it) - } + presenter.registerManga(selectedItem) } private fun search(query: String) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListFragment.kt index f3f5cab70..170a8ffb9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListFragment.kt @@ -56,6 +56,10 @@ class MyAnimeListFragment : BaseRxFragment() { myanimelist_status.text = presenter.myAnimeList.getStatus(it.status) } ?: run { myanimelist_title.setTextAppearance(context, R.style.TextAppearance_Medium_Button) + myanimelist_title.setText(R.string.action_edit) + myanimelist_chapters.text = "" + myanimelist_score.text = "" + myanimelist_status.text = "" } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.kt index c8d0c54c4..ac28dbe38 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.kt @@ -123,20 +123,24 @@ class MyAnimeListPresenter : BasePresenter() { stop(GET_SEARCH_RESULTS) } - fun registerManga(sync: MangaSync) { - sync.manga_id = manga.id - add(myAnimeList.bind(sync) - .flatMap { response -> - if (response.isSuccessful) { - db.insertMangaSync(sync).asRxObservable() - } else { - Observable.error(Exception("Could not bind manga")) + fun registerManga(sync: MangaSync?) { + if (sync != null) { + sync.manga_id = manga.id + add(myAnimeList.bind(sync) + .flatMap { response -> + if (response.isSuccessful) { + db.insertMangaSync(sync).asRxObservable() + } else { + Observable.error(Exception("Could not bind manga")) + } } - } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe({ }, - { error -> context.toast(error.message) })) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ }, + { error -> context.toast(error.message) })) + } else { + db.deleteMangaSyncForManga(manga).executeAsBlocking() + } } fun getAllStatus(): List { diff --git a/app/src/main/res/layout/card_myanimelist_personal.xml b/app/src/main/res/layout/card_myanimelist_personal.xml index 74202c178..d071cde75 100644 --- a/app/src/main/res/layout/card_myanimelist_personal.xml +++ b/app/src/main/res/layout/card_myanimelist_personal.xml @@ -27,7 +27,7 @@ android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" - android:text="Edit" + android:text="@string/action_edit" style="@style/TextAppearance.Medium.Button"/>