From 8856164dd0229b8dc05fbb73ffb67d42bc78be62 Mon Sep 17 00:00:00 2001 From: rumboalla Date: Sun, 23 Jul 2023 14:05:23 +0200 Subject: [PATCH] Start implementing search for F-Droid --- .../apkupdater/data/fdroid/FdroidUpdate.kt | 5 ++++- .../main/java/com/apkupdater/di/MainModule.kt | 6 +++--- .../apkupdater/repository/FdroidRepository.kt | 21 ++++++++++++++++++- .../apkupdater/ui/component/UiComponents.kt | 1 + 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/apkupdater/data/fdroid/FdroidUpdate.kt b/app/src/main/java/com/apkupdater/data/fdroid/FdroidUpdate.kt index 67bdeace..26fdf3ba 100644 --- a/app/src/main/java/com/apkupdater/data/fdroid/FdroidUpdate.kt +++ b/app/src/main/java/com/apkupdater/data/fdroid/FdroidUpdate.kt @@ -15,6 +15,9 @@ fun FdroidUpdate.toAppUpdate() = AppUpdate( apk.versionName, apk.versionCode, FdroidSource, - app.icon.toUri(), + if(app.icon.isEmpty()) + "https://f-droid.org/assets/ic_repo_app_default.png".toUri() + else + "https://f-droid.org/repo/icons-640/${app.icon}".toUri(), "https://f-droid.org/repo/${apk.apkName}" ) diff --git a/app/src/main/java/com/apkupdater/di/MainModule.kt b/app/src/main/java/com/apkupdater/di/MainModule.kt index 805d2748..60162eb0 100644 --- a/app/src/main/java/com/apkupdater/di/MainModule.kt +++ b/app/src/main/java/com/apkupdater/di/MainModule.kt @@ -3,8 +3,6 @@ package com.apkupdater.di import android.content.Context import androidx.work.WorkManager import com.apkupdater.BuildConfig -import com.google.gson.GsonBuilder -import com.kryptoprefs.preferences.KryptoBuilder import com.apkupdater.R import com.apkupdater.prefs.Prefs import com.apkupdater.repository.ApkMirrorRepository @@ -21,6 +19,8 @@ import com.apkupdater.viewmodel.MainViewModel import com.apkupdater.viewmodel.SearchViewModel import com.apkupdater.viewmodel.SettingsViewModel import com.apkupdater.viewmodel.UpdatesViewModel +import com.google.gson.GsonBuilder +import com.kryptoprefs.preferences.KryptoBuilder import okhttp3.Cache import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor @@ -113,6 +113,6 @@ val mainModule = module { viewModel { SettingsViewModel(get(), get(), WorkManager.getInstance(get())) } - viewModel { parameters -> SearchViewModel(parameters.get(), get()) } + viewModel { parameters -> SearchViewModel(parameters.get(), get(), get()) } } diff --git a/app/src/main/java/com/apkupdater/repository/FdroidRepository.kt b/app/src/main/java/com/apkupdater/repository/FdroidRepository.kt index 963a864e..8b4329fd 100644 --- a/app/src/main/java/com/apkupdater/repository/FdroidRepository.kt +++ b/app/src/main/java/com/apkupdater/repository/FdroidRepository.kt @@ -41,7 +41,26 @@ class FdroidRepository( emit(updates) }.catch { emit(emptyList()) - Log.e("FdroidRepository", "Error", it) + Log.e("FdroidRepository", "Error looking for updates.", it) + } + + suspend fun search(text: String) = flow { + val response = service.getJar() + val data = jarToJson(response.byteStream()) + val updates = data.apps + .asSequence() + .filter { it.name.contains(text) } + .map { FdroidUpdate(data.packages[it.packageName]!![0], it) } + .filter { it.apk.minSdkVersion <= api } + .filter { filterArch(it) } + .filter { filterAlpha(it) } + .filter { filterBeta(it) } + .map { it.toAppUpdate() } + .toList() + emit(Result.success(updates)) + }.catch { + emit(Result.failure(it)) + Log.e("FdroidRepository", "Error searching.", it) } private fun filterAlpha(update: FdroidUpdate) = when { diff --git a/app/src/main/java/com/apkupdater/ui/component/UiComponents.kt b/app/src/main/java/com/apkupdater/ui/component/UiComponents.kt index d762f35c..64938438 100644 --- a/app/src/main/java/com/apkupdater/ui/component/UiComponents.kt +++ b/app/src/main/java/com/apkupdater/ui/component/UiComponents.kt @@ -266,6 +266,7 @@ fun SearchImage(app: AppUpdate, onInstall: (String) -> Unit = {}) = Box { { onInstall(app.link) }, Modifier.align(Alignment.TopEnd).padding(4.dp) ) + SourceIcon(app.source, Modifier.align(Alignment.TopStart).padding(4.dp)) } @Composable