package org.wikipedia.page

import android.annotation.SuppressLint
import kotlinx.coroutines.*
import org.wikipedia.WikipediaApp
import org.wikipedia.readinglist.database.ReadingListDbHelper
import org.wikipedia.readinglist.database.ReadingListPage
import org.wikipedia.util.log.L

object PageAvailableOfflineHandler {
    interface Callback {
        fun onFinish(available: Boolean)
    }

    fun check(page: ReadingListPage, callback: Callback) {
        callback.onFinish(WikipediaApp.getInstance().isOnline || (page.offline() && !page.saving()))
    }

    @SuppressLint("CheckResult")
    fun check(pageTitle: PageTitle, callback: Callback) {
        if (WikipediaApp.getInstance().isOnline) {
            callback.onFinish(true)
            return
        }
        CoroutineScope(Dispatchers.Main).launch(CoroutineExceptionHandler { _, exception ->
            run {
                callback.onFinish(false)
                L.w(exception)
            }
        }) {
            val readingListPage = withContext(Dispatchers.IO) { ReadingListDbHelper.instance().findPageInAnyList(pageTitle) }
            callback.onFinish(readingListPage!!.offline() && !readingListPage.saving())
        }
    }
}
package org.wikipedia.page

import android.annotation.SuppressLint
import kotlinx.coroutines.*
import org.wikipedia.WikipediaApp
import org.wikipedia.readinglist.database.ReadingListDbHelper
import org.wikipedia.readinglist.database.ReadingListPage
import org.wikipedia.util.log.L

object PageAvailableOfflineHandler {
    interface Callback {
        fun onFinish(available: Boolean)
    }

    fun check(page: ReadingListPage, callback: Callback) {
        callback.onFinish(WikipediaApp.getInstance().isOnline || (page.offline() && !page.saving()))
    }

    @SuppressLint("CheckResult")
    fun check(pageTitle: PageTitle, callback: Callback) {
        if (WikipediaApp.getInstance().isOnline) {
            callback.onFinish(true)
            return
        }
        CoroutineScope(Dispatchers.Main).launch(CoroutineExceptionHandler { _, exception ->
            run {
                callback.onFinish(false)
                L.w(exception)
            }
        }) {
            val readingListPage = withContext(Dispatchers.IO) { ReadingListDbHelper.instance().findPageInAnyList(pageTitle) }
            callback.onFinish(readingListPage != null && readingListPage.offline() && !readingListPage.saving())
        }
    }
}
Fix possible crash if cannot find any reading list
Fix possible crash if cannot find any reading list

dbrant/apps-android-wikipedia,dbrant/apps-android-wikipedia,dbrant/apps-android-wikipedia,wikimedia/apps-android-wikipedia,wikimedia/apps-android-wikipedia,dbrant/apps-android-wikipedia,wikimedia/apps-android-wikipedia,dbrant/apps-android-wikipedia,wikimedia/apps-android-wikipedia