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

This commit is contained in:
len 2016-04-18 20:14:50 +02:00
parent 81bce8ef76
commit 17c59657c3
6 changed files with 37 additions and 21 deletions

View File

@ -260,6 +260,14 @@ open class DatabaseHelper(context: Context) {
fun deleteMangaSync(manga: MangaSync) = db.delete().`object`(manga).prepare() 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 // Categories related queries
fun getCategories() = db.get() fun getCategories() = db.get()

View File

@ -44,10 +44,11 @@ class NetworkHelper(context: Context) {
@JvmOverloads @JvmOverloads
fun request(request: Request, forceCache: Boolean = false): Observable<Response> { fun request(request: Request, forceCache: Boolean = false): Observable<Response> {
var response: Response? = null
return Observable.fromCallable { return Observable.fromCallable {
val c = if (forceCache) forceCacheClient else client val c = if (forceCache) forceCacheClient else client
c.newCall(request).execute() c.newCall(request).execute().apply { response = this }
} }.doOnUnsubscribe { response?.body()?.close() }
} }
@JvmOverloads @JvmOverloads
@ -57,6 +58,7 @@ class NetworkHelper(context: Context) {
} }
fun requestBodyProgress(request: Request, listener: ProgressListener): Observable<Response> { fun requestBodyProgress(request: Request, listener: ProgressListener): Observable<Response> {
var response: Response? = null
return Observable.fromCallable { return Observable.fromCallable {
val progressClient = client.newBuilder() val progressClient = client.newBuilder()
.cache(null) .cache(null)
@ -68,8 +70,8 @@ class NetworkHelper(context: Context) {
} }
.build() .build()
progressClient.newCall(request).execute() progressClient.newCall(request).execute().apply { response = this }
}.retry(1) }.doOnUnsubscribe { response?.body()?.close() }.retry(1)
} }
val cookies: CookieStore val cookies: CookieStore

View File

@ -91,9 +91,7 @@ class MyAnimeListDialogFragment : DialogFragment() {
} }
private fun onPositiveButtonClick() { private fun onPositiveButtonClick() {
selectedItem?.let { presenter.registerManga(selectedItem)
presenter.registerManga(it)
}
} }
private fun search(query: String) { private fun search(query: String) {

View File

@ -56,6 +56,10 @@ class MyAnimeListFragment : BaseRxFragment<MyAnimeListPresenter>() {
myanimelist_status.text = presenter.myAnimeList.getStatus(it.status) myanimelist_status.text = presenter.myAnimeList.getStatus(it.status)
} ?: run { } ?: run {
myanimelist_title.setTextAppearance(context, R.style.TextAppearance_Medium_Button) 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 = ""
} }
} }

View File

@ -123,20 +123,24 @@ class MyAnimeListPresenter : BasePresenter<MyAnimeListFragment>() {
stop(GET_SEARCH_RESULTS) stop(GET_SEARCH_RESULTS)
} }
fun registerManga(sync: MangaSync) { fun registerManga(sync: MangaSync?) {
sync.manga_id = manga.id if (sync != null) {
add(myAnimeList.bind(sync) sync.manga_id = manga.id
.flatMap { response -> add(myAnimeList.bind(sync)
if (response.isSuccessful) { .flatMap { response ->
db.insertMangaSync(sync).asRxObservable() if (response.isSuccessful) {
} else { db.insertMangaSync(sync).asRxObservable()
Observable.error(Exception("Could not bind manga")) } else {
Observable.error(Exception("Could not bind manga"))
}
} }
} .subscribeOn(Schedulers.io())
.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread()) .subscribe({ },
.subscribe({ }, { error -> context.toast(error.message) }))
{ error -> context.toast(error.message) })) } else {
db.deleteMangaSyncForManga(manga).executeAsBlocking()
}
} }
fun getAllStatus(): List<String> { fun getAllStatus(): List<String> {

View File

@ -27,7 +27,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:text="Edit" android:text="@string/action_edit"
style="@style/TextAppearance.Medium.Button"/> style="@style/TextAppearance.Medium.Button"/>
</RelativeLayout> </RelativeLayout>