101 lines
3.7 KiB
Kotlin
101 lines
3.7 KiB
Kotlin
|
|
package ui
|
||
|
|
|
||
|
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||
|
|
import androidx.compose.foundation.background
|
||
|
|
import androidx.compose.foundation.clickable
|
||
|
|
import androidx.compose.foundation.combinedClickable
|
||
|
|
import androidx.compose.foundation.layout.padding
|
||
|
|
import androidx.compose.foundation.layout.width
|
||
|
|
import androidx.compose.foundation.shape.CircleShape
|
||
|
|
import androidx.compose.material.MaterialTheme
|
||
|
|
import androidx.compose.material.Text
|
||
|
|
import androidx.compose.runtime.Composable
|
||
|
|
import androidx.compose.runtime.getValue
|
||
|
|
import androidx.compose.runtime.mutableStateOf
|
||
|
|
import androidx.compose.runtime.remember
|
||
|
|
import androidx.compose.runtime.setValue
|
||
|
|
import androidx.compose.ui.Modifier
|
||
|
|
import androidx.compose.ui.text.style.TextAlign
|
||
|
|
import androidx.compose.ui.unit.dp
|
||
|
|
import cafe.adriel.voyager.navigator.LocalNavigator
|
||
|
|
import cafe.adriel.voyager.navigator.currentOrThrow
|
||
|
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||
|
|
import kotlinx.serialization.SerialName
|
||
|
|
import kotlinx.serialization.json.Json
|
||
|
|
import kotlinx.serialization.json.decodeFromStream
|
||
|
|
import kotlinx.serialization.json.encodeToStream
|
||
|
|
import ui.Capture.Companion.getCaptureDuration
|
||
|
|
import ui.screens.ProfileDetails
|
||
|
|
import ui.screens.ProfilesManager
|
||
|
|
import ui.screens.Settings
|
||
|
|
import java.io.FileInputStream
|
||
|
|
import java.io.FileOutputStream
|
||
|
|
import java.nio.file.Path
|
||
|
|
import java.nio.file.Paths
|
||
|
|
import kotlin.io.path.absolutePathString
|
||
|
|
|
||
|
|
class Profiles {
|
||
|
|
companion object {
|
||
|
|
fun getCurrentProfile() = load(Settings.userSettings.getProperty("current_profile", null))
|
||
|
|
|
||
|
|
@OptIn(ExperimentalFoundationApi::class)
|
||
|
|
@Composable
|
||
|
|
fun currentProfile(modifier: Modifier) { //TODO: store profile in mutableState so component updates when currentProfile
|
||
|
|
var profile by remember { mutableStateOf(getCurrentProfile()) }
|
||
|
|
|
||
|
|
val navigator = LocalNavigator.currentOrThrow
|
||
|
|
Text(
|
||
|
|
color = MaterialTheme.colors.onPrimary,
|
||
|
|
text = if (profile != null) profile!!.name else "Not Selected",
|
||
|
|
textAlign = TextAlign.Center,
|
||
|
|
modifier = modifier
|
||
|
|
.background(MaterialTheme.colors.primary, CircleShape)
|
||
|
|
.padding(5.dp)
|
||
|
|
.width(150.dp)
|
||
|
|
.combinedClickable(
|
||
|
|
onClick = { if (profile != null) navigator.push(ProfileDetails(profile!!)) },
|
||
|
|
onDoubleClick = { navigator.push(ProfilesManager()) }
|
||
|
|
)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
data class Profile(
|
||
|
|
val name: String,
|
||
|
|
@SerialName("ai_name")
|
||
|
|
val aiName: String,
|
||
|
|
@SerialName("program_name")
|
||
|
|
val programName: Path,
|
||
|
|
val commands: List<Command>
|
||
|
|
)
|
||
|
|
|
||
|
|
data class Command(
|
||
|
|
@SerialName("wording_Variants")
|
||
|
|
val wordingVariants: List<String>,
|
||
|
|
val triggers: List<String>
|
||
|
|
)
|
||
|
|
|
||
|
|
@OptIn(ExperimentalSerializationApi::class)
|
||
|
|
fun load(profile: String?): Profile? {
|
||
|
|
if (profile != null) {
|
||
|
|
val stream = FileInputStream(getProfileLocation(profile))
|
||
|
|
val file = Json.decodeFromStream<Profile>(stream)
|
||
|
|
stream.close()
|
||
|
|
return file
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
//TODO: Add logging messages
|
||
|
|
}
|
||
|
|
|
||
|
|
@OptIn(ExperimentalSerializationApi::class)
|
||
|
|
fun save(profile: Profile) {
|
||
|
|
val stream = FileOutputStream(getProfileLocation(profile.name))
|
||
|
|
Json.encodeToStream(profile, stream)
|
||
|
|
stream.flush()
|
||
|
|
stream.close()
|
||
|
|
}
|
||
|
|
|
||
|
|
fun getProfileLocation(profile: String) = Paths.get("profiles/$profile.profile").absolutePathString()
|
||
|
|
}
|
||
|
|
}
|