Skip to content

Commit

Permalink
package repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Smithy-x committed Mar 10, 2024
1 parent 57a949e commit d82ae3b
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import android.view.ViewGroup
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -55,7 +58,7 @@ class ConsoleFragmentView: Fragment() {
@Composable
fun ConsolesView() {
val vm = hiltViewModel<ConsoleViewModel>()
val consoleState by remember { vm.consoles }
val consoleState by vm.consoles.collectAsState()
ConsoleViewState(
state = consoleState,
onConsoleClick = { console: Console ->
Expand All @@ -77,19 +80,21 @@ fun ConsoleViewState(
onConsoleClick: (Console) -> Unit,
onConsolePin: (Console, Boolean) -> Unit
) {
Column() {
Column {
when (state) {
is ConsoleState.Error -> {

}

is ConsoleState.Success -> {
state.repos.forEach {
ConsoleView(
console = it,
onConsoleClick = onConsoleClick,
onConsolePin = onConsolePin
)
LazyColumn {
items(state.repos) {
ConsoleView(
console = it,
onConsoleClick = onConsoleClick,
onConsolePin = onConsolePin
)
}
}
}

Expand All @@ -101,10 +106,9 @@ fun ConsoleViewState(
}


@Preview
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewConsoleView() {
//ConsolesView()
val consoles = arrayOf(
Console(
ip = "192.168.1.184",
Expand Down Expand Up @@ -191,7 +195,6 @@ fun ConsoleView(
contentDescription = null // decorative element
)


Column(
modifier = Modifier
.padding(16.dp, 0.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class ConsoleViewModel @Inject constructor(
@SharedPreferenceStorage val manager: SharedPreferenceManager
) : ViewModel() {

private var _consoles: MutableState<ConsoleState> = mutableStateOf(ConsoleState.Loading)
private var _consoles: MutableStateFlow<ConsoleState> = MutableStateFlow(ConsoleState.Loading)

val consoles: State<ConsoleState> get() = _consoles
val consoles: StateFlow<ConsoleState> get() = _consoles

init {
getConsoles()
Expand All @@ -39,16 +39,21 @@ class ConsoleViewModel @Inject constructor(
consoleUseCase()
.flowOn(Dispatchers.IO)
.onEach { consoles ->
_consoles.value = ConsoleState.Success(consoles)
_consoles.update {
ConsoleState.Success(consoles)
}
}.onCompletion { error ->
if (error != null) {
"Error ${error.message}".e("ERROR", error)
_consoles.value =
_consoles.update {
ConsoleState.Error("Unable to fetch consoles: ${error.message}")
}
}
}.catch { error ->
"Error ${error.message}".e("ERROR", error)
_consoles.value = ConsoleState.Error("Unable to fetch consoles: ${error.message}")
_consoles.update {
ConsoleState.Error("Unable to fetch consoles: ${error.message}")
}
}.launchIn(viewModelScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private fun FileView(file: FTPFile, onFileClick: (FTPFile) -> Unit) {
}
}

@Preview
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewFtpStateView() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ inline fun ArticleList(crossinline content: @Composable () -> Unit) {
}


@Preview
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewHomeView() {
HomeView()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.vonley.mi.ui.screens.packages.data.repository

import io.vonley.mi.common.Resource
import io.vonley.mi.models.Payload
import io.vonley.mi.ui.screens.packages.data.local.PackageRepositoryDao
import io.vonley.mi.ui.screens.packages.data.local.entity.Package
import io.vonley.mi.ui.screens.packages.data.local.entity.PackageType
import io.vonley.mi.ui.screens.packages.domain.remote.RepoService
import io.vonley.mi.ui.screens.packages.data.local.entity.Repo
import io.vonley.mi.ui.screens.packages.domain.repository.PackageRepository
Expand All @@ -10,7 +13,7 @@ import javax.inject.Inject

class PackageRepositoryImpl @Inject constructor(
private val dao: PackageRepositoryDao,
private val repo: RepoService
private val repo: RepoService,
) : PackageRepository {

private suspend fun fetch(link: String, save: Boolean = true): Repo? {
Expand Down Expand Up @@ -78,6 +81,18 @@ class PackageRepositoryImpl @Inject constructor(
return fetch("https://raw.githubusercontent.com/mitai-app/versioning/main/packages.json") != null
}

override suspend fun fromPackage(pkg: Package, key: String): Payload? {
val url = pkg.dl[key] ?: return null
val body = repo.downloadFile(url).body() ?: return null

val name = pkg.name + when (pkg.type) {
PackageType.PLUGIN -> ".bin"
PackageType.APP -> ".pkg"
else -> ""
}
return Payload(name, body.byteStream())
}

override suspend fun exists(link: String): Boolean {
return dao.exists(link)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.vonley.mi.ui.screens.packages.domain.remote

import io.vonley.mi.ui.screens.packages.data.local.entity.Repo
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Url

interface RepoService {
@GET
suspend fun search(@Url url: String): Response<Repo>

@GET
suspend fun downloadFile(@Url url: String): Response<ResponseBody>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vonley.mi.ui.screens.packages.domain.repository

import io.vonley.mi.common.Resource
import io.vonley.mi.models.Payload
import io.vonley.mi.ui.screens.packages.data.local.entity.Package
import io.vonley.mi.ui.screens.packages.data.local.entity.Repo

interface PackageRepository {
Expand All @@ -13,4 +15,5 @@ interface PackageRepository {
suspend fun delete(link: String)
suspend fun count(): Int
suspend fun getDefaultRepo(): Boolean
suspend fun fromPackage(pkg: Package, key: String): Payload?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.vonley.mi.ui.screens.packages.domain.usecase

import io.vonley.mi.common.Resource
import io.vonley.mi.di.network.MiServer
import io.vonley.mi.di.network.PSXService
import io.vonley.mi.di.network.callbacks.PayloadCallback
import io.vonley.mi.models.Payload
import io.vonley.mi.models.enums.PlatformType
import io.vonley.mi.ui.screens.consoles.domain.model.Console
import io.vonley.mi.ui.screens.packages.data.local.entity.Package
import io.vonley.mi.ui.screens.packages.data.local.entity.PackageType
import io.vonley.mi.ui.screens.packages.domain.repository.PackageRepository
import io.vonley.mi.utils.SharedPreferenceManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class SendPackageUseCase @Inject constructor(
val repo: PackageRepository,
val service: PSXService,
val miServer: MiServer,
val manager: SharedPreferenceManager
) {
operator fun invoke(pkg: Package, onPayloadCallback: PayloadCallback): Flow<Resource<String>> = flow {
when (pkg.type) {
PackageType.APP -> {

}

PackageType.TOOL -> {


}

PackageType.PLUGIN -> {
val console = (service.sync.target as? Console)

if (console == null) {
this@flow.emit(
Resource.Error(
"An Error Occurred",
"Something went wrong with sending the payload"
)
)
return@flow
}

if (console.type != PlatformType.PS4) {
this@flow.emit(
Resource.Error(
"Unsupported Console",
"Console is not supported"
)
)
return@flow
}

val version = manager.targetVersion?.replace(".", "")
if(version.isNullOrEmpty()) {
this@flow.emit(
Resource.Error(
"Target is not connected",
"Unknown console version... usually we get the target by connecting to the phone server"
)
)
return@flow
}

if(!pkg.dl.containsKey(version)) {
this@flow.emit(
Resource.Error(
"Package not found",
"There are no legitimate versions for your console associated with this package."
)
)
return@flow
}

val payload: Payload? = repo.fromPackage(pkg, version)

if(payload == null) {
this@flow.emit(
Resource.Error(
"Download Failed",
"Could not download the file"
)
)
return@flow
}

service.uploadBin(miServer, arrayListOf(payload), onPayloadCallback)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,15 @@ fun LoadingRepo() {

@Composable
fun RepoListView(repos: List<Repo>) {
val vm = hiltViewModel<PackageViewModel>()
repos.forEach {
val expanded = remember { mutableStateOf(false) }
RepoCard(repo = it) {
expanded.value = !expanded.value
}
if (expanded.value) {
ListRepoPackages(it.packages) { pkg ->

vm.send(pkg)
}
}
}
Expand Down
Loading

0 comments on commit d82ae3b

Please sign in to comment.