diff --git a/app/src/main/kotlin/com/aliucord/manager/di/ServiceModule.kt b/app/src/main/kotlin/com/aliucord/manager/di/ServiceModule.kt index b6454ef..909eaa9 100644 --- a/app/src/main/kotlin/com/aliucord/manager/di/ServiceModule.kt +++ b/app/src/main/kotlin/com/aliucord/manager/di/ServiceModule.kt @@ -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) } diff --git a/app/src/main/kotlin/com/aliucord/manager/domain/manager/DownloadManager.kt b/app/src/main/kotlin/com/aliucord/manager/domain/manager/DownloadManager.kt index 4813c31..d26b16a 100644 --- a/app/src/main/kotlin/com/aliucord/manager/domain/manager/DownloadManager.kt +++ b/app/src/main/kotlin/com/aliucord/manager/domain/manager/DownloadManager.kt @@ -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 -> diff --git a/app/src/main/kotlin/com/aliucord/manager/domain/repository/GithubRepository.kt b/app/src/main/kotlin/com/aliucord/manager/domain/repository/GithubRepository.kt index ef1d1bb..0986270 100644 --- a/app/src/main/kotlin/com/aliucord/manager/domain/repository/GithubRepository.kt +++ b/app/src/main/kotlin/com/aliucord/manager/domain/repository/GithubRepository.kt @@ -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() } diff --git a/app/src/main/kotlin/com/aliucord/manager/network/service/AliucordGithubService.kt b/app/src/main/kotlin/com/aliucord/manager/network/service/AliucordGithubService.kt new file mode 100644 index 0000000..460aab6 --- /dev/null +++ b/app/src/main/kotlin/com/aliucord/manager/network/service/AliucordGithubService.kt @@ -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 { + 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" + } +} diff --git a/app/src/main/kotlin/com/aliucord/manager/network/service/GithubService.kt b/app/src/main/kotlin/com/aliucord/manager/network/service/GithubService.kt index 4a003a5..8d1a4cb 100644 --- a/app/src/main/kotlin/com/aliucord/manager/network/service/GithubService.kt +++ b/app/src/main/kotlin/com/aliucord/manager/network/service/GithubService.kt @@ -9,60 +9,36 @@ import kotlinx.coroutines.withContext class GithubService( private val http: HttpService, ) { - suspend fun getCommits(page: Int): ApiResponse> = withContext(Dispatchers.IO) { - http.request { - url(COMMITS_URL) - parameter("page", page) + suspend fun getCommits(owner: String, repo: String, page: Int = 0): ApiResponse> { + return withContext(Dispatchers.IO) { + http.request { + url("https://api.github.com/repos/$owner/$repo/commits") + parameter("page", page) + } } } - suspend fun getVersion(): ApiResponse = withContext(Dispatchers.IO) { - http.request { - url(DATA_URL) + suspend fun getContributors(owner: String, repo: String): ApiResponse> { + return withContext(Dispatchers.IO) { + http.request { + url("https://api.github.com/repos/$owner/$repo/contributors") + } } } - suspend fun getContributors(): ApiResponse> = withContext(Dispatchers.IO) { - http.request { - url(CONTRIBUTORS_URL) + suspend fun getReleases(owner: String, repo: String): ApiResponse> { + return withContext(Dispatchers.IO) { + http.request { + url("https://api.github.com/repos/$owner/$repo/releases") + } } } - suspend fun getHermesReleases(): ApiResponse> = withContext(Dispatchers.IO) { - http.request { - url(HERMES_RELEASES_URL) + suspend fun getLatestRelease(owner: String, repo: String): ApiResponse { + return withContext(Dispatchers.IO) { + http.request { + url("https://api.github.com/repos/$owner/$repo/releases/latest") + } } } - - suspend fun getAliucordNativeReleases(): ApiResponse> = withContext(Dispatchers.IO) { - http.request { - url(ALIUCORD_NATIVE_RELEASES_URL) - } - } - - suspend fun getManagerReleases(): ApiResponse> = 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" - } } diff --git a/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/HomeViewModel.kt b/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/HomeViewModel.kt index 01b5e29..c72b4e8 100644 --- a/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/HomeViewModel.kt +++ b/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/HomeViewModel.kt @@ -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): LoadResult { 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]) { diff --git a/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/InstallViewModel.kt b/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/InstallViewModel.kt index 5ce1e77..0c256cc 100644 --- a/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/InstallViewModel.kt +++ b/app/src/main/kotlin/com/aliucord/manager/ui/viewmodel/InstallViewModel.kt @@ -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()