Skip to content

Commit

Permalink
Upload files, settings OpenAiKey, auth fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseP3r32 committed May 27, 2024
1 parent 7314692 commit 8576e72
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/xef_brand_icon"
android:label="xefMobile"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Menu
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -38,6 +41,18 @@ fun MainLayout(
val CustomTextBlue = Color(0xFF0199D7)
val context = LocalContext.current

val authToken by authViewModel.authToken.observeAsState()

LaunchedEffect(authToken) {
if (authToken == null) {
navController.navigate(Screens.Login.screen) {
popUpTo(0) {
inclusive = true
}
}
}
}

ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = true,
Expand Down Expand Up @@ -211,12 +226,6 @@ fun MainLayout(
drawerState.close()
}
authViewModel.logout()
Toast.makeText(context, "Logged out", Toast.LENGTH_SHORT).show()
navController.navigate(Screens.Login.screen) {
popUpTo(0) {
inclusive = true
}
}
})
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.server.movile.xef.android.ui.screens.menu.CreateAssistantScreen
import com.server.movile.xef.android.ui.screens.navigationdrawercompose.HomeScreen
import com.server.movile.xef.android.ui.viewmodels.IAuthViewModel
import com.xef.xefMobile.ui.screens.Screens
import com.xef.xefMobile.ui.screens.SettingsScreen

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
Expand Down Expand Up @@ -51,6 +52,11 @@ fun XefAndroidApp(authViewModel: IAuthViewModel) {
CreateAssistantScreen(navigationController)
}
}
composable(Screens.Settings.screen) {
MainLayout(navController = navigationController, authViewModel = authViewModel, userName = userName.orEmpty()) {
SettingsScreen(navigationController, authViewModel)
}
}
// ... other composable screens ...
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState

import com.server.movile.xef.android.ui.themes.CustomColors
import com.xef.xefMobile.ui.viewmodels.PathViewModel

Expand All @@ -27,7 +26,7 @@ import com.xef.xefMobile.ui.viewmodels.PathViewModel
fun FilePickerDialog(
onDismissRequest: () -> Unit,
customColors: CustomColors,
onFilesSelected: () -> Unit // Callback for when files are selected
onFilesSelected: () -> Unit
) {
val viewModel: PathViewModel = viewModel()
val state = viewModel.state
Expand Down Expand Up @@ -65,7 +64,7 @@ fun FilePickerDialog(
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp))
HorizontalDivider()
Divider()
}
},
text = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.xef.xefMobile.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.server.movile.xef.android.ui.viewmodels.IAuthViewModel
import kotlinx.coroutines.launch
import org.xef.xefMobile.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(navController: NavController, authViewModel: IAuthViewModel) {
var apiKey by remember { mutableStateOf("") }
val coroutineScope = rememberCoroutineScope()
val customColors = Color(0xFFADD8E6)
val CustomTextBlue = Color(0xFF0199D7)

Box(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
.padding(16.dp)
) {
Column(
modifier = Modifier.align(Alignment.Center),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = "Settings",
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
color = Color.Black
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "These are xef-server settings.",
fontSize = 16.sp,
color = Color.Gray
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = apiKey,
onValueChange = { apiKey = it },
label = { Text("OpenAI API key") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = {
coroutineScope.launch {
// Handle saving the API key
// Example: authViewModel.saveApiKey(apiKey)
}
},
colors = ButtonDefaults.buttonColors(containerColor = customColors),
modifier = Modifier.fillMaxWidth()
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Image(
painter = painterResource(id = R.drawable.save_24dp),
contentDescription = "save settings icon",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
Spacer(modifier = Modifier.width(8.dp))
Text(text = "Save Settings", color = CustomTextBlue)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,19 @@ fun AssistantScreen(navController: NavController, authViewModel: IAuthViewModel)
Spacer(modifier = Modifier.height(8.dp))
}
}
}

Button(
onClick = { navController.navigate(Screens.CreateAssistant.screen) },
colors = ButtonDefaults.buttonColors(
containerColor = customColors.buttonColor,
contentColor = MaterialTheme.colorScheme.onPrimary
),
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(16.dp)
) {
Text(text = "Create New Assistant")
}
Button(
onClick = { navController.navigate(Screens.CreateAssistant.screen) },
colors = ButtonDefaults.buttonColors(
containerColor = customColors.buttonColor,
contentColor = MaterialTheme.colorScheme.onPrimary
),
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(16.dp)
) {
Text(text = "Create New Assistant")
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.server.movile.xef.android.ui.themes.LocalCustomColors

import com.xef.xefMobile.ui.composable.FilePickerDialog

class CreateAssistantActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Pass the NavController to CreateAssistantScreen
val navController = rememberNavController()
CreateAssistantScreen(navController)
}
Expand All @@ -43,6 +42,7 @@ fun CreateAssistantScreen(navController: NavController) {
val list = listOf("gpt-4o", "gpt-4", "gpt-3.5-turbo-16K", "gpt-3.5-turbo-0125", "gpt-3.5-turbo")
var isExpanded by remember { mutableStateOf(false) }
var selectedText by remember { mutableStateOf(list[0]) }
var showFilePicker by remember { mutableStateOf(false) }

val customColors = LocalCustomColors.current

Expand Down Expand Up @@ -121,14 +121,14 @@ fun CreateAssistantScreen(navController: NavController) {
}
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
OutlinedButton(
onClick = { /* handle cancel */ },
TextButton(
onClick = { showFilePicker = true },
colors = ButtonDefaults.outlinedButtonColors(
containerColor = Color.Transparent,
contentColor = customColors.buttonColor
)
) {
Text("File Search")
Text("File Search +")
}
Spacer(modifier = Modifier.weight(1f))
Switch(
Expand All @@ -142,14 +142,14 @@ fun CreateAssistantScreen(navController: NavController) {
}
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
OutlinedButton(
TextButton(
onClick = { /* handle cancel */ },
colors = ButtonDefaults.outlinedButtonColors(
containerColor = Color.Transparent,
contentColor = customColors.buttonColor
)
) {
Text("Code Interpreter")
Text("Code Interpreter +")
}
Spacer(modifier = Modifier.weight(1f))
Switch(
Expand All @@ -163,14 +163,14 @@ fun CreateAssistantScreen(navController: NavController) {
}
Spacer(modifier = Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically) {
OutlinedButton(
TextButton(
onClick = { /* handle cancel */ },
colors = ButtonDefaults.outlinedButtonColors(
containerColor = Color.Transparent,
contentColor = customColors.buttonColor
)
) {
Text("Functions")
Text("Functions +")
}
Spacer(modifier = Modifier.weight(1f))
}
Expand Down Expand Up @@ -214,6 +214,16 @@ fun CreateAssistantScreen(navController: NavController) {
}
}
}
if (showFilePicker) {
FilePickerDialog(
onDismissRequest = { showFilePicker = false },
customColors = customColors,
onFilesSelected = {
// Handle file selection here if needed
showFilePicker = false
}
)
}
}
}

Expand Down Expand Up @@ -264,7 +274,6 @@ fun AssistantFloatField(label: String, value: Float, onValueChange: (Float) -> U
@Preview(showBackground = false)
@Composable
fun CreateAssistantScreenPreview() {
// Create a mock NavController for the preview
val navController = rememberNavController()
CreateAssistantScreen(navController)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class AuthViewModel(
}.firstOrNull()
_authToken.value = token


token?.let {
loadUserName()
}
Expand All @@ -76,7 +75,7 @@ class AuthViewModel(
try {
val loginResponse = apiService.loginUser(loginRequest)
updateAuthToken(loginResponse.authToken)
updateUserName(loginResponse.user.name) // Extract user's name
updateUserName(loginResponse.user.name)
_authToken.value = loginResponse.authToken
_userName.value = loginResponse.user.name
} catch (e: Exception) {
Expand Down Expand Up @@ -110,7 +109,7 @@ class AuthViewModel(
try {
val registerResponse = apiService.registerUser(request)
updateAuthToken(registerResponse.authToken)
updateUserName(name) // Directly use the name provided during registration
updateUserName(name)
_authToken.value = registerResponse.authToken
_userName.value = name
} catch (e: Exception) {
Expand All @@ -135,11 +134,11 @@ class AuthViewModel(
withContext(Dispatchers.IO) {
dataStore.edit { preferences ->
preferences.remove(stringPreferencesKey("authToken"))
preferences.remove(stringPreferencesKey("userName")) // Add this line
preferences.remove(stringPreferencesKey("userName"))
}
}
_authToken.postValue(null)
_userName.postValue(null) // Add this line
_userName.postValue(null)
_errorMessage.postValue("Logged out successfully")
} catch (e: Exception) {
_errorMessage.postValue("Failed to sign out")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M840,280v480q0,33 -23.5,56.5T760,840L200,840q-33,0 -56.5,-23.5T120,760v-560q0,-33 23.5,-56.5T200,120h480l160,160ZM760,314L646,200L200,200v560h560v-446ZM480,720q50,0 85,-35t35,-85q0,-50 -35,-85t-85,-35q-50,0 -85,35t-35,85q0,50 35,85t85,35ZM240,400h360v-160L240,240v160ZM200,314v446,-560 114Z"
android:fillColor="#5f6368"/>
</vector>

0 comments on commit 8576e72

Please sign in to comment.