This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6afc322
commit fd2d0c2
Showing
2 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 74 additions & 3 deletions
77
...main/java/app/shosetsu/android/datasource/remote/impl/update/FDroidAppUpdateDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,89 @@ | ||
package app.shosetsu.android.datasource.remote.impl.update | ||
|
||
import app.shosetsu.android.common.EmptyResponseBodyException | ||
import app.shosetsu.android.common.ext.quickie | ||
import app.shosetsu.android.datasource.remote.base.IRemoteAppUpdateDataSource | ||
import app.shosetsu.android.domain.model.local.AppUpdateEntity | ||
import app.shosetsu.lib.exceptions.HTTPException | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import okhttp3.OkHttpClient | ||
import java.io.IOException | ||
import java.io.InputStream | ||
|
||
class FDroidAppUpdateDataSource : IRemoteAppUpdateDataSource, | ||
/** | ||
* Load app updates from F-Droid | ||
*/ | ||
class FDroidAppUpdateDataSource( | ||
private val okHttpClient: OkHttpClient | ||
) : IRemoteAppUpdateDataSource, | ||
IRemoteAppUpdateDataSource.Downloadable { | ||
companion object { | ||
private const val FDROID_UPDATE_URL = | ||
"https://f-droid.org/api/v1/packages/app.shosetsu.android" | ||
|
||
private const val FDROID_DOWNLOAD_URL = "https://f-droid.org/repo/app.shosetsu.android_" | ||
|
||
private val json = Json { | ||
encodeDefaults = true | ||
} | ||
} | ||
|
||
@Serializable | ||
data class PackagesInfo( | ||
val packageName: String = "", | ||
val suggestedVersionCode: Int = -1, | ||
val packages: List<PackageData> = emptyList(), | ||
val error: String? = null | ||
) | ||
|
||
@Serializable | ||
data class PackageData( | ||
val versionName: String, | ||
val versionCode: Int | ||
) | ||
|
||
@Throws(HTTPException::class, EmptyResponseBodyException::class, IOException::class) | ||
@OptIn(ExperimentalSerializationApi::class) | ||
override suspend fun loadAppUpdate(): AppUpdateEntity { | ||
TODO("Add F-DROID update source") | ||
okHttpClient.quickie(FDROID_UPDATE_URL).use { response -> | ||
if (response.isSuccessful) { | ||
return response.body?.use { responseBody -> | ||
responseBody.byteStream().use { responseStream -> | ||
val info = json.decodeFromStream<PackagesInfo>(responseStream) | ||
if (info.error != null) | ||
throw EmptyResponseBodyException(info.error) | ||
|
||
var packageData = info.packages.firstOrNull { | ||
it.versionCode == info.suggestedVersionCode | ||
} | ||
|
||
if (packageData == null) | ||
packageData = info.packages.first() | ||
|
||
AppUpdateEntity( | ||
packageData.versionName, | ||
packageData.versionCode, | ||
url = FDROID_DOWNLOAD_URL + "${packageData.versionCode}.apk", | ||
notes = emptyList() | ||
) | ||
} | ||
} ?: throw EmptyResponseBodyException(FDROID_UPDATE_URL) | ||
} | ||
throw HTTPException(response.code) | ||
} | ||
} | ||
|
||
@Throws(EmptyResponseBodyException::class, HTTPException::class, IOException::class) | ||
override suspend fun downloadAppUpdate(update: AppUpdateEntity): InputStream { | ||
TODO("Add F-DROID update source") | ||
okHttpClient.quickie(update.url).let { response -> | ||
if (response.isSuccessful) { | ||
return response.body?.byteStream() | ||
?: throw EmptyResponseBodyException(update.url) | ||
} else throw HTTPException(response.code) | ||
} | ||
} | ||
|
||
} |