Pārlūkot izejas kodu

Allow to unbind manga, closes #258. Fix some network calls leaking

len 9 gadi atpakaļ
vecāks
revīzija
17c59657c3

+ 8 - 0
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()

+ 6 - 4
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<Response> {
+        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<Response> {
+        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

+ 1 - 3
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) {

+ 4 - 0
app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListFragment.kt

@@ -56,6 +56,10 @@ class MyAnimeListFragment : BaseRxFragment<MyAnimeListPresenter>() {
             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 = ""
         }
 
     }

+ 17 - 13
app/src/main/java/eu/kanade/tachiyomi/ui/manga/myanimelist/MyAnimeListPresenter.kt

@@ -123,20 +123,24 @@ class MyAnimeListPresenter : BasePresenter<MyAnimeListFragment>() {
         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<String> {

+ 1 - 1
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"/>
 
     </RelativeLayout>