Skip to content

Commit

Permalink
frontend: update from old repo
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Apr 4, 2024
1 parent 396c24f commit a4bb20a
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dk.scheduling.schedulingfrontend

import android.util.Log
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import dk.scheduling.schedulingfrontend.api.ApiClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

@Composable
fun ApiButton() {
Button(onClick = {
val call = ApiClient.apiService.test()

call.enqueue(
object : Callback<String> {
override fun onResponse(
call: Call<String>,
response: Response<String>,
) {
if (response.isSuccessful) {
// val post = response.body()
Log.i("testAPI", "we got a response")
// Handle the retrieved post data
} else {
Log.w("testAPI", "we did not get a successful response")
// Handle error
}
}

override fun onFailure(
call: Call<String>,
t: Throwable,
) {
// Handle failure
Log.e("testAPI", "could not get a response")
}
},
)
}) {
Text("Test API")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dk.scheduling.schedulingfrontend

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.sp

@Composable
fun HomePage() {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = "This is the HomePage, click the heart to reach the api button!", fontSize = 24.sp)
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
package dk.scheduling.schedulingfrontend

import App
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import dk.scheduling.schedulingfrontend.api.ApiClient
import dk.scheduling.schedulingfrontend.ui.theme.SchedulingFrontendTheme
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SchedulingFrontendTheme {
Button(onClick = {
val call = ApiClient.apiService.test()

call.enqueue(
object : Callback<String> {
override fun onResponse(
call: Call<String>,
response: Response<String>,
) {
if (response.isSuccessful) {
// val post = response.body()
Log.i("testAPI", "we got a response")
// Handle the retrieved post data
} else {
Log.w("testAPI", "we did not get a successful response")
// Handle error
}
}

override fun onFailure(
call: Call<String>,
t: Throwable,
) {
// Handle failure
Log.e("testAPI", "could not get a response")
}
},
)
}) {
Text("Test API")
}
App()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Surface
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import dk.scheduling.schedulingfrontend.HomePage
import dk.scheduling.schedulingfrontend.Page
import dk.scheduling.schedulingfrontend.pagesInfo

@Composable
fun App() {
var currentPage by remember { mutableStateOf(Page.HomePage) }

Box(modifier = Modifier.fillMaxSize()) {
// Content of the current page
Column(modifier = Modifier.fillMaxSize()) {
pagesInfo.find { it.page == currentPage }?.composable?.invoke() ?: HomePage() // Default to HomePage if not found
Spacer(modifier = Modifier.weight(1f))
}

// Bottom navigation bar
BottomNavigationBar(
currentPage = currentPage,
onPageSelected = { page -> currentPage = page },
modifier = Modifier.align(Alignment.BottomCenter),
)
}
}

@Composable
fun BottomNavigationBar(
currentPage: Page,
onPageSelected: (Page) -> Unit,
modifier: Modifier = Modifier,
) {
Surface(color = Color.Gray, modifier = modifier) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly,
verticalAlignment = Alignment.CenterVertically,
) {
pagesInfo.forEach { pageInfo ->
BottomNavigationButton(
icon = pageInfo.icon,
isSelected = currentPage == pageInfo.page,
) { onPageSelected(pageInfo.page) }
}
}
}
}

@Composable
fun BottomNavigationButton(
icon: ImageVector,
isSelected: Boolean,
onClick: () -> Unit,
) {
IconButton(onClick = onClick) {
Icon(
imageVector = icon,
contentDescription = null,
tint = if (isSelected) Color.White else Color.Magenta,
)
}
}

@Preview(showBackground = true, device = "spec:id=reference_phone,shape=Normal,width=411,height=891,unit=dp,dpi=420")
@Composable
fun PreviewApp() {
App()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dk.scheduling.schedulingfrontend

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.sp

@Composable
fun Page3() {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text(text = "Page 3", fontSize = 24.sp)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dk.scheduling.schedulingfrontend

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector

enum class Page {
HomePage,
ApiButtonPage,
PAGE_3,
}

data class PageInfo(
val page: Page,
val icon: ImageVector,
val composable: @Composable () -> Unit,
)

val pagesInfo =
listOf(
PageInfo(Page.HomePage, Icons.Default.Home) { HomePage() },
PageInfo(Page.ApiButtonPage, Icons.Default.Favorite) { ApiButton() },
PageInfo(Page.PAGE_3, Icons.Default.Settings) { Page3() },
)
Loading

0 comments on commit a4bb20a

Please sign in to comment.