Use coroutine CancellationException
This commit is contained in:
1 parent
9f79e2505c
commit
869da61fd0
3 files changed
+8
-37
No files matched your search
@@ -16,9 +16,9 @@
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import com.hippo.ehviewer.EhApplication.Companion.okHttpClient
|
||||
import com.hippo.ehviewer.client.exception.CancelledException
|
||||
import eu.kanade.tachiyomi.util.lang.launchUI
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -183,9 +183,10 @@ class EhClient {
|
||||
}
|
||||
withUIContext { callback?.onSuccess(result) }
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException)
|
||||
throw e // Don't catch coroutine CancellationException
|
||||
e.printStackTrace()
|
||||
if (e !is CancelledException)
|
||||
withUIContext { callback?.onFailure(e) }
|
||||
withUIContext { callback?.onFailure(e) }
|
||||
}
|
||||
request.task = null
|
||||
request.callback = null
|
||||
@@ -212,15 +213,9 @@ class EhClient {
|
||||
private val mStop
|
||||
get() = !(job?.isActive ?: false)
|
||||
|
||||
// Called in Job thread
|
||||
@Throws(CancelledException::class)
|
||||
fun setCall(call: Call?) {
|
||||
if (mStop) {
|
||||
// Stopped Job thread
|
||||
throw CancelledException()
|
||||
} else {
|
||||
mCall.lazySet(call)
|
||||
}
|
||||
if (mStop) call?.cancel()
|
||||
else mCall.lazySet(call)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
|
||||
@@ -31,7 +31,6 @@ import com.hippo.ehviewer.client.data.GalleryCommentList;
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail;
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo;
|
||||
import com.hippo.ehviewer.client.data.PreviewSet;
|
||||
import com.hippo.ehviewer.client.exception.CancelledException;
|
||||
import com.hippo.ehviewer.client.exception.EhException;
|
||||
import com.hippo.ehviewer.client.exception.ParseException;
|
||||
import com.hippo.ehviewer.client.parser.ArchiveParser;
|
||||
@@ -64,6 +63,7 @@ import org.jsoup.select.Elements;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.FormBody;
|
||||
@@ -94,7 +94,7 @@ public class EhEngine {
|
||||
private static void doThrowException(Call call, int code, @Nullable Headers headers,
|
||||
@Nullable String body, Throwable e) throws Throwable {
|
||||
if (call.isCanceled()) {
|
||||
throw new CancelledException();
|
||||
throw new CancellationException("Call cancelled!");
|
||||
}
|
||||
|
||||
// Check sad panda
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.ehviewer.client.exception;
|
||||
|
||||
public class CancelledException extends Exception {
|
||||
|
||||
public CancelledException() {
|
||||
super("CANCELED");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user