Migrate search settings
This commit is contained in:
@@ -2,11 +2,17 @@ package de.mm20.launcher2.wikipedia
|
||||
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.crashreporter.CrashReporter
|
||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||
import de.mm20.launcher2.search.data.Wikipedia
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.channelFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.OkHttpClient
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
@@ -17,7 +23,9 @@ interface WikipediaRepository {
|
||||
|
||||
class WikipediaRepositoryImpl(
|
||||
private val context: Context
|
||||
): WikipediaRepository {
|
||||
) : WikipediaRepository, KoinComponent {
|
||||
|
||||
private val dataStore: LauncherDataStore by inject()
|
||||
|
||||
private val httpClient by lazy {
|
||||
OkHttpClient
|
||||
@@ -36,51 +44,56 @@ class WikipediaRepositoryImpl(
|
||||
.build()
|
||||
}
|
||||
|
||||
val wikipediaService by lazy {
|
||||
private val wikipediaService by lazy {
|
||||
retrofit.create(WikipediaApi::class.java)
|
||||
}
|
||||
|
||||
|
||||
override fun search(query: String): Flow<Wikipedia?> = channelFlow {
|
||||
send(null)
|
||||
httpClient.dispatcher.run {
|
||||
runningCalls().forEach {
|
||||
it.cancel()
|
||||
}
|
||||
queuedCalls().forEach {
|
||||
it.cancel()
|
||||
withContext(Dispatchers.IO) {
|
||||
httpClient.dispatcher.cancelAll()
|
||||
}
|
||||
if (query.isBlank()) return@channelFlow
|
||||
|
||||
dataStore.data.map { it.wikipediaSearch }.collectLatest {
|
||||
if (it.enabled) {
|
||||
send(queryWikipedia(query, it.images))
|
||||
} else {
|
||||
send(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun queryWikipedia(query: String, loadImages: Boolean): Wikipedia? {
|
||||
|
||||
if (query.isBlank()) return@channelFlow
|
||||
|
||||
val result = try {
|
||||
wikipediaService.search(query)
|
||||
} catch (e: Exception) {
|
||||
CrashReporter.logException(e)
|
||||
return@channelFlow
|
||||
return null
|
||||
}
|
||||
|
||||
val page = result.query?.pages?.values?.toList()?.getOrNull(0) ?: return@channelFlow
|
||||
val page = result.query?.pages?.values?.toList()?.getOrNull(0) ?: return null
|
||||
|
||||
val image = if (LauncherPreferences.instance.searchWikipediaPictures) {
|
||||
val image = if (loadImages) {
|
||||
val width = context.resources.displayMetrics.widthPixels / 2
|
||||
val imageResult = try {
|
||||
wikipediaService.getPageImage(page.pageid, width)
|
||||
} catch (e: Exception) {
|
||||
CrashReporter.logException(e)
|
||||
return@channelFlow
|
||||
return null
|
||||
}
|
||||
imageResult.query?.pages?.values?.toList()?.getOrNull(0)?.thumbnail?.source
|
||||
} else null
|
||||
|
||||
val wiki = Wikipedia(
|
||||
return Wikipedia(
|
||||
label = page.title,
|
||||
id = page.pageid,
|
||||
text = page.extract,
|
||||
image = image
|
||||
)
|
||||
send(wiki)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user