Skip to content

Commit

Permalink
Merge branch 'features/xef-mobiles' of https://github.com/xebia-funct…
Browse files Browse the repository at this point in the history
…ional/xef into features/xef-mobiles

# Conflicts:
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/MainLayout.kt
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/XefAndroidApp.kt
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/ui/composable/FilePickerDialog.kt
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/ui/screens/menu/AssistantScreen.kt
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/ui/screens/menu/CreateAssistantScreen.kt
#	server/xefMobile/composeApp/src/androidMain/kotlin/com/xef/xefMobile/ui/viewmodels/AuthViewModel.kt
  • Loading branch information
JoseP3r32 committed May 27, 2024
2 parents 8576e72 + 1b23c0d commit bc3ecfd
Show file tree
Hide file tree
Showing 28 changed files with 1,053 additions and 706 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun Routing.assistantRoutes(logger: KLogger) {
fun Routing.assistantRoutes(logger: KLogger) {
authenticate("auth-bearer") {
post("/v1/settings/assistants") {
try {
Expand All @@ -23,7 +23,7 @@ fun Routing.assistantRoutes(logger: KLogger) {
val request = call.receive<CreateAssistantRequest>()
val assistant = Assistant(request)
val response = assistant.get()
logger.info{"Created assistant: ${response.name} with id: ${response.id}"}
logger.info { "Created assistant: ${response.name} with id: ${response.id}" }
call.respond(status = HttpStatusCode.Created, response)
} else {
call.respond(
Expand All @@ -42,13 +42,12 @@ fun Routing.assistantRoutes(logger: KLogger) {
val token = call.getToken()
val openAI = OpenAI(Config(token = token.value), logRequests = true)
val assistantsApi = openAI.assistants
val response = assistantsApi.listAssistants(configure = {
header("OpenAI-Beta", "assistants=v2")
})
val response =
assistantsApi.listAssistants(configure = { header("OpenAI-Beta", "assistants=v2") })
call.respond(HttpStatusCode.OK, response)
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error{"Error creating assistant: $trace"}
logger.error { "Error creating assistant: $trace" }
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}
Expand All @@ -65,7 +64,7 @@ fun Routing.assistantRoutes(logger: KLogger) {
}
val assistant = Assistant(id)
val response = assistant.modify(request).get()
logger.info{"Modified assistant: ${response.name} with id: ${response.id}"}
logger.info { "Modified assistant: ${response.name} with id: ${response.id}" }
call.respond(HttpStatusCode.OK, response)
} else {
call.respond(
Expand All @@ -75,7 +74,7 @@ fun Routing.assistantRoutes(logger: KLogger) {
}
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error{"Error modifying assistant: $trace"}
logger.error { "Error modifying assistant: $trace" }
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}
Expand All @@ -91,16 +90,15 @@ fun Routing.assistantRoutes(logger: KLogger) {
val openAI = OpenAI(Config(token = token.value), logRequests = true)
val assistantsApi = openAI.assistants
val assistant = assistantsApi.getAssistant(id)
val response = assistantsApi.deleteAssistant(id, configure = {
header("OpenAI-Beta", "assistants=v2")
})
logger.info{"Deleted assistant: ${assistant.name} with id: ${response.id}"}
val response =
assistantsApi.deleteAssistant(id, configure = { header("OpenAI-Beta", "assistants=v2") })
logger.info { "Deleted assistant: ${assistant.name} with id: ${response.id}" }
call.respond(status = HttpStatusCode.NoContent, response)
} catch (e: Exception) {
val trace = e.stackTraceToString()
logger.error{"Error deleting assistant: $trace"}
logger.error { "Error deleting assistant: $trace" }
call.respond(HttpStatusCode.BadRequest, "Invalid request: $trace")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import com.server.movile.xef.android.ui.viewmodels.AuthViewModel
import com.xef.xefMobile.services.ApiService

class MainActivity : ComponentActivity() {
private lateinit var authViewModel: AuthViewModel
private lateinit var authViewModel: AuthViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
authViewModel = AuthViewModel(this, ApiService())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
authViewModel = AuthViewModel(this, ApiService())

setContent {
XefAndroidApp(authViewModel = authViewModel)
}
}
setContent { XefAndroidApp(authViewModel = authViewModel) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.server.movile.xef.android.ui.viewmodels.IAuthViewModel
import com.xef.xefMobile.ui.screens.Screens
import kotlinx.coroutines.launch
import org.xef.xefMobile.R
import com.xef.xefMobile.ui.screens.Screens

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainLayout(
navController: NavController,
authViewModel: IAuthViewModel,
userName: String,
content: @Composable () -> Unit
navController: NavController,
authViewModel: IAuthViewModel,
userName: String,
content: @Composable () -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val CustomLightBlue = Color(0xFFADD8E6)
val CustomTextBlue = Color(0xFF0199D7)
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val CustomLightBlue = Color(0xFFADD8E6)
val CustomTextBlue = Color(0xFF0199D7)
val context = LocalContext.current

val authToken by authViewModel.authToken.observeAsState()

Expand Down Expand Up @@ -276,5 +276,181 @@ fun MainLayout(
content()
}
}
HorizontalDivider()

NavigationDrawerItem(
label = { Text(text = "Home", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.home_24px),
contentDescription = "home",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Home.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Organizations", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.source_environment_24px),
contentDescription = "organizations",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Organizations.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Assistants", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.smart_toy_24px),
contentDescription = "assistants",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Assistants.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Projects", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.school_24px),
contentDescription = "projects",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Projects.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Chat", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.chat_24px),
contentDescription = "chat",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Chat.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Generic question", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.support_agent_24px),
contentDescription = "generic question",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.GenericQuestion.screen) { popUpTo(0) }
}
)
NavigationDrawerItem(
label = { Text(text = "Settings", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.settings_24px),
contentDescription = "settings",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
navController.navigate(Screens.Settings.screen) { popUpTo(0) }
}
)

Spacer(modifier = Modifier.weight(1f))
NavigationDrawerItem(
label = { Text(text = "Logout", color = CustomTextBlue) },
selected = false,
icon = {
Image(
painter = painterResource(id = R.drawable.logout_24dp_fill0_wght400_grad0_opsz24),
contentDescription = "logout",
modifier = Modifier.size(24.dp),
colorFilter = ColorFilter.tint(CustomTextBlue)
)
},
onClick = {
coroutineScope.launch { drawerState.close() }
authViewModel.logout()
Toast.makeText(context, "Logged out", Toast.LENGTH_SHORT).show()
navController.navigate(Screens.Login.screen) { popUpTo(0) { inclusive = true } }
}
)
}
},
) {
Scaffold(
topBar = {
TopAppBar(
title = {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth()
) {
Image(
painter = painterResource(id = R.drawable.xef_brand_name_white),
contentDescription = "Logo",
modifier = Modifier.size(60.dp)
)
Spacer(modifier = Modifier.weight(1f))
Text(
text = userName,
color = Color.White,
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.padding(end = 16.dp)
)
}
},
colors =
TopAppBarDefaults.topAppBarColors(
containerColor = CustomLightBlue,
titleContentColor = Color.White,
navigationIconContentColor = Color.White
),
navigationIcon = {
IconButton(onClick = { coroutineScope.launch { drawerState.open() } }) {
Icon(Icons.Rounded.Menu, contentDescription = "MenuButton")
}
},
)
}
) {
Box(modifier = Modifier.padding(it)) { content() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ import com.xef.xefMobile.ui.screens.SettingsScreen
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun XefAndroidApp(authViewModel: IAuthViewModel) {
val navigationController = rememberNavController()
val userName by authViewModel.userName.observeAsState("")
val navigationController = rememberNavController()
val userName by authViewModel.userName.observeAsState("")

NavHost(
NavHost(
navController = navigationController,
startDestination = Screens.Login.screen,
modifier = Modifier.padding(top = 16.dp)
) {
composable(Screens.Login.screen) { LoginScreen(authViewModel, navigationController) }
composable(Screens.Register.screen) { RegisterScreen(authViewModel, navigationController) }
composable(Screens.Home.screen) {
MainLayout(
navController = navigationController,
startDestination = Screens.Login.screen,
modifier = Modifier.padding(top = 16.dp)
Expand Down Expand Up @@ -59,5 +67,24 @@ fun XefAndroidApp(authViewModel: IAuthViewModel) {
}
// ... other composable screens ...
}
composable(Screens.Assistants.screen) {
MainLayout(
navController = navigationController,
authViewModel = authViewModel,
userName = userName.orEmpty()
) {
AssistantScreen(navigationController, authViewModel)
}
}
composable(Screens.CreateAssistant.screen) {
MainLayout(
navController = navigationController,
authViewModel = authViewModel,
userName = userName.orEmpty()
) {
CreateAssistantScreen(navigationController)
}
}
// ... other composable screens ...
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ package com.xef.xefMobile.model

import kotlinx.serialization.Serializable

@Serializable
data class Assistant(
val id: String,
val name: String
)
@Serializable data class Assistant(val id: String, val name: String)

@Serializable
data class AssistantsResponse(
val data: List<Assistant>
)
@Serializable data class AssistantsResponse(val data: List<Assistant>)
Loading

0 comments on commit bc3ecfd

Please sign in to comment.