From 12273bdd1713b481bbe9b896e7249f0993434d3a Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 4 Aug 2024 11:05:43 +0200 Subject: [PATCH] calculate remaining time and move opening termux to another class --- .../activity/dashboard/DashboardViewModel.kt | 58 +++++++++---------- .../activity/dashboard/screen/Dashboard.kt | 6 +- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/eu/m724/vastapp/activity/dashboard/DashboardViewModel.kt b/app/src/main/java/eu/m724/vastapp/activity/dashboard/DashboardViewModel.kt index 1701f1f..21b65de 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/dashboard/DashboardViewModel.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/dashboard/DashboardViewModel.kt @@ -1,17 +1,13 @@ package eu.m724.vastapp.activity.dashboard import android.annotation.SuppressLint -import android.app.PendingIntent import android.content.Context -import android.content.Intent -import android.widget.Toast import androidx.activity.ComponentActivity import androidx.lifecycle.Lifecycle import androidx.lifecycle.ViewModel import eu.m724.vastapp.R import eu.m724.vastapp.activity.Opener import eu.m724.vastapp.activity.PermissionChecker -import eu.m724.vastapp.activity.termux.TermuxSshActivity import eu.m724.vastapp.vastai.ApiRoute import eu.m724.vastapp.vastai.VastApi import eu.m724.vastapp.vastai.api.InstancesUrlRequestCallback @@ -25,7 +21,7 @@ import kotlinx.coroutines.flow.update class DashboardViewModel( - private val initialUser: User, + initialUser: User, private val vastApi: VastApi ) : ViewModel() { // TODO do something with the user @@ -40,6 +36,9 @@ class DashboardViewModel( private val _user: MutableStateFlow = MutableStateFlow(initialUser) val user: StateFlow = _user.asStateFlow() + private val _remainingTime: MutableStateFlow = MutableStateFlow(-1) + var remainingTime: StateFlow = _remainingTime.asStateFlow() + private val _refreshError: MutableStateFlow> = MutableStateFlow(emptyList()) val refreshError: StateFlow> = _refreshError.asStateFlow() @@ -72,6 +71,22 @@ class DashboardViewModel( _uiState.update { it.copy(refreshing = it.refreshing - 1) } + + if (instances.isEmpty()) { // TODO move this + _remainingTime.value = -1 + } else { + var totalDph = 0.0 + + instances.forEach { + if (it.status == "running") + totalDph += it.instance.pricing.dphTotal!! + else + totalDph += it.instance.pricing.dphTotal!! - it.instance.pricing.dphBase + // TODO make this ideal + } + + _remainingTime.value = (user.value.credit / totalDph * 3600).toInt() + } }, { apiFailure -> _refreshError.update { it + apiFailure.errorMessage!! } _uiState.update { @@ -99,6 +114,7 @@ class DashboardViewModel( // TODO I don't like this function especially the last line } + @SuppressLint("SdCardPath") fun sshButtonClick(activity: ComponentActivity, rentedInstance: RentedInstance) { val sshCommand = "ssh -p ${rentedInstance.sshProxyPort} root@${rentedInstance.sshProxyHost}" val context = activity.applicationContext @@ -110,10 +126,11 @@ class DashboardViewModel( ) { granted, asked -> if (granted) { val arguments = arrayOf( + "/data/data/com.termux/files/usr/bin/ssh", "-p", rentedInstance.sshProxyPort.toString(), "root@" + rentedInstance.sshProxyHost ) - startTermux(context, arguments) + Opener.startTermux(context, arguments) Thread.sleep(100) println(activity.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) } else { @@ -127,33 +144,12 @@ class DashboardViewModel( } private fun copyToClipboard(context: Context, text: String) { - Toast.makeText( - context, + Opener.copyToClipboard( + text, + "ssh command", context.getString(R.string.copied_to_clipboard), - Toast.LENGTH_SHORT - ).show() // TODO hide on a12 - - Opener.copyToClipboard(text, "ssh command", context) - } - - @SuppressLint("SdCardPath") - private fun startTermux(context: Context, arguments: Array) { - val noSshIntent = Intent(context, TermuxSshActivity::class.java) - val pendingIntent = PendingIntent.getActivity( - context, - 0, - noSshIntent, - PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE + context ) - - val intent = Intent() - intent.setClassName("com.termux", "com.termux.app.RunCommandService") - intent.setAction("com.termux.RUN_COMMAND") - intent.putExtra("com.termux.RUN_COMMAND_PATH", "/data/data/com.termux/files/usr/bin/ssh") - intent.putExtra("com.termux.RUN_COMMAND_ARGUMENTS", arguments) - intent.putExtra("com.termux.RUN_COMMAND_PENDING_INTENT", pendingIntent) - - context.startForegroundService(intent) } } \ No newline at end of file diff --git a/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt b/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt index 3089dcf..fadbaed 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/dashboard/screen/Dashboard.kt @@ -26,9 +26,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -48,7 +46,7 @@ fun DashboardScreen(dashboardViewModel: DashboardViewModel) { val user by dashboardViewModel.user.collectAsState() val rentedInstances by dashboardViewModel.rentedInstances.collectAsState() - val remainingTime by rememberSaveable { mutableIntStateOf(6000000) } + val remainingTime by dashboardViewModel.remainingTime.collectAsState() val isRefreshing by remember(uiState) { derivedStateOf { uiState.refreshing > 0 } } val scrollState = rememberScrollState() @@ -170,7 +168,7 @@ fun balanceColor(balance: Double, warningThreshold: Double): Color { @Composable fun formatTime(seconds: Int): String { if (seconds <= 0) - return stringResource(id = R.string.time_minutes_short, 0.0) + return "―" val minutes: Double = seconds / 60.0 if (minutes < 60)