refactor: separate aliu-specific from GithubService (#38)

This commit is contained in:
rushii 2022-11-03 21:18:27 -07:00 • committed by GitHub
parent cecd7ab8ab
commit da2f9490e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 75 deletions

View file

@ -1,11 +1,11 @@
package com.aliucord.manager.di
import com.aliucord.manager.network.service.GithubService
import com.aliucord.manager.network.service.MavenService
import com.aliucord.manager.network.service.*
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module
val serviceModule = module {
singleOf(::GithubService)
singleOf(::MavenService)
singleOf(::AliucordGithubService)
}

View file

@ -7,7 +7,7 @@ import androidx.core.content.getSystemService
import androidx.core.net.toUri
import com.aliucord.manager.BuildConfig
import com.aliucord.manager.domain.repository.AliucordMavenRepository
import com.aliucord.manager.network.service.GithubService
import com.aliucord.manager.network.service.AliucordGithubService
import java.io.File
import kotlin.coroutines.*
@ -25,13 +25,13 @@ class DownloadManager(
// Aliucord Kotlin downloads
suspend fun downloadKtInjector(out: File): File =
download(GithubService.KT_INJECTOR_URL, out)
download(AliucordGithubService.KT_INJECTOR_URL, out)
suspend fun downloadAliuhook(version: String, out: File): File =
download(AliucordMavenRepository.getAliuhookUrl(version), out)
suspend fun downloadKotlinDex(out: File): File =
download(GithubService.KOTLIN_DEX_URL, out)
download(AliucordGithubService.KOTLIN_DEX_URL, out)
suspend fun download(url: String, out: File): File {
return suspendCoroutine { continuation ->

View file

@ -1,20 +1,19 @@
package com.aliucord.manager.domain.repository
import com.aliucord.manager.network.dto.GithubUser
import com.aliucord.manager.network.service.GithubService
import com.aliucord.manager.network.service.AliucordGithubService
import com.aliucord.manager.network.utils.transform
class GithubRepository(
private val service: GithubService
private val service: AliucordGithubService
) {
suspend fun getCommits(page: Int) = service.getCommits(page)
suspend fun getCommits(page: Int = 0) = service.getCommits(page)
suspend fun getContributors() = service.getContributors()
.transform { it.sortedByDescending(GithubUser::contributions) }
suspend fun getDiscordKtVersion() = service.getVersion()
suspend fun getDataJson() = service.getDataJson()
suspend fun getHermesReleases() = service.getHermesReleases()
suspend fun getAliucordNativeReleases() = service.getAliucordNativeReleases()
suspend fun getHermesRelease() = service.getLatestHermesRelease()
suspend fun getAliucordNativeRelease() = service.getLatestAliucordNativeRelease()
suspend fun getManagerReleases() = service.getManagerReleases()
}

View file

@ -0,0 +1,37 @@
package com.aliucord.manager.network.service
import com.aliucord.manager.network.dto.Version
import com.aliucord.manager.network.utils.ApiResponse
import io.ktor.client.request.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class AliucordGithubService(
val github: GithubService,
val http: HttpService
) {
suspend fun getDataJson(): ApiResponse<Version> {
return withContext(Dispatchers.IO) {
http.request {
url("https://raw.githubusercontent.com/$ORG/$MAIN_REPO/builds/data.json")
}
}
}
suspend fun getCommits(page: Int = 0) = github.getCommits(ORG, MAIN_REPO, page)
suspend fun getLatestHermesRelease() = github.getLatestRelease(ORG, HERMES_REPO)
suspend fun getLatestAliucordNativeRelease() = github.getLatestRelease(ORG, ALIUNATIVE_REPO)
suspend fun getManagerReleases() = github.getReleases(ORG, MANAGER_REPO)
suspend fun getContributors() = github.getContributors(ORG, MAIN_REPO)
companion object {
private const val ORG = "Aliucord"
private const val MAIN_REPO = "Aliucord"
private const val HERMES_REPO = "Hermes"
private const val ALIUNATIVE_REPO = "AliucordNative"
private const val MANAGER_REPO = "AliucordManager"
const val KT_INJECTOR_URL = "https://raw.githubusercontent.com/$ORG/$MAIN_REPO/builds/Injector.dex"
const val KOTLIN_DEX_URL = "https://raw.githubusercontent.com/$ORG/$MAIN_REPO/main/installer/android/app/src/main/assets/kotlin/classes.dex"
}
}

View file

@ -9,60 +9,36 @@ import kotlinx.coroutines.withContext
class GithubService(
private val http: HttpService,
) {
suspend fun getCommits(page: Int): ApiResponse<List<Commit>> = withContext(Dispatchers.IO) {
http.request {
url(COMMITS_URL)
parameter("page", page)
suspend fun getCommits(owner: String, repo: String, page: Int = 0): ApiResponse<List<Commit>> {
return withContext(Dispatchers.IO) {
http.request {
url("https://api.github.com/repos/$owner/$repo/commits")
parameter("page", page)
}
}
}
suspend fun getVersion(): ApiResponse<Version> = withContext(Dispatchers.IO) {
http.request {
url(DATA_URL)
suspend fun getContributors(owner: String, repo: String): ApiResponse<List<GithubUser>> {
return withContext(Dispatchers.IO) {
http.request {
url("https://api.github.com/repos/$owner/$repo/contributors")
}
}
}
suspend fun getContributors(): ApiResponse<List<GithubUser>> = withContext(Dispatchers.IO) {
http.request {
url(CONTRIBUTORS_URL)
suspend fun getReleases(owner: String, repo: String): ApiResponse<List<GithubRelease>> {
return withContext(Dispatchers.IO) {
http.request {
url("https://api.github.com/repos/$owner/$repo/releases")
}
}
}
suspend fun getHermesReleases(): ApiResponse<List<GithubRelease>> = withContext(Dispatchers.IO) {
http.request {
url(HERMES_RELEASES_URL)
suspend fun getLatestRelease(owner: String, repo: String): ApiResponse<GithubRelease> {
return withContext(Dispatchers.IO) {
http.request {
url("https://api.github.com/repos/$owner/$repo/releases/latest")
}
}
}
suspend fun getAliucordNativeReleases(): ApiResponse<List<GithubRelease>> = withContext(Dispatchers.IO) {
http.request {
url(ALIUCORD_NATIVE_RELEASES_URL)
}
}
suspend fun getManagerReleases(): ApiResponse<List<GithubRelease>> = withContext(Dispatchers.IO) {
http.request {
url(MANAGER_RELEASES_URL)
}
}
companion object {
private const val ORG = "Aliucord"
private const val REPO = "Aliucord"
private const val HERMES_REPO = "Hermes"
private const val NATIVE_REPO = "AliucordNative"
private const val MANAGER_REPO = "AliucordManager"
private const val COMMITS_URL = "https://api.github.com/repos/$ORG/$REPO/commits"
private const val CONTRIBUTORS_URL = "https://api.github.com/repos/$ORG/$REPO/contributors"
private const val DATA_URL = "https://raw.githubusercontent.com/$ORG/$REPO/builds/data.json"
const val KT_INJECTOR_URL = "https://raw.githubusercontent.com/$ORG/$REPO/builds/Injector.dex"
const val KOTLIN_DEX_URL = "https://raw.githubusercontent.com/$ORG/$REPO/main/installer/android/app/src/main/assets/kotlin/classes.dex"
private const val HERMES_RELEASES_URL = "https://api.github.com/repos/$ORG/$HERMES_REPO/releases"
private const val ALIUCORD_NATIVE_RELEASES_URL = "https://api.github.com/repos/$ORG/$NATIVE_REPO/releases"
const val MANAGER_RELEASES_URL = "https://api.github.com/repos/$ORG/$MANAGER_REPO/releases"
}
}

View file

@ -19,7 +19,7 @@ import kotlinx.coroutines.launch
class HomeViewModel(
private val application: Application,
private val githubRepository: GithubRepository,
private val github: GithubRepository,
val preferences: PreferencesManager
) : ViewModel() {
private val packageManager = application.packageManager
@ -38,7 +38,7 @@ class HomeViewModel(
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Commit> {
val page = params.key ?: 0
return githubRepository.getCommits(page).fold(
return github.getCommits(page).fold(
success = { commits ->
val prevKey = if (page > 0) page - 1 else null
val nextKey = if (commits.isNotEmpty()) page + 1 else null
@ -74,9 +74,7 @@ class HomeViewModel(
}
private suspend fun _fetchSupportedVersion() {
val version = githubRepository.getDiscordKtVersion()
version.fold(
github.getDataJson().fold(
success = {
supportedVersion = it.versionName
supportedVersionType = when (it.versionCode[3]) {

View file

@ -215,12 +215,7 @@ class InstallViewModel(
// Download hermes & c++ runtime lib
val (hermesLibrary, cppRuntimeLibrary) = step(InstallStep.DL_HERMES) {
// Fetch gh releases for Aliucord/Hermes
val latestHermesRelease = githubRepository.getHermesReleases().fold(
success = { releases ->
releases.maxBy { DateFormat.getDateTimeInstance().parse(it.createdAt) }
},
fail = { throw it }
)
val latestHermesRelease = githubRepository.getHermesRelease().getOrThrow()
// Download the hermes-release.aar file to replace in the apk
val hermes = externalCacheDir.resolve("hermes-release-${latestHermesRelease.tagName}.aar").also { file ->
@ -246,12 +241,7 @@ class InstallViewModel(
// Download Aliucord Native lib
val aliucordDexFile = step(InstallStep.DL_ALIUNATIVE) {
// Fetch the gh releases for Aliucord/AliucordNative
val latestAliucordNativeRelease = githubRepository.getAliucordNativeReleases().fold(
success = { releases ->
releases.maxBy { DateFormat.getDateTimeInstance().parse(it.createdAt) }
},
fail = { throw it }
)
val latestAliucordNativeRelease = githubRepository.getAliucordNativeRelease().getOrThrow()
// Download the Aliucord classes.dex file to add to the apk
externalCacheDir.resolve("aliucord-${latestAliucordNativeRelease.tagName}.dex").also { file ->
@ -389,7 +379,7 @@ class InstallViewModel(
}
val dataJson = step(InstallStep.FETCH_KT_VERSION) {
githubRepository.getDiscordKtVersion().getOrThrow()
githubRepository.getDataJson().getOrThrow()
}
val arch = Build.SUPPORTED_ABIS.first()