Skip to content

Commit

Permalink
Migrate History Page To Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithTamim committed Jan 7, 2025
1 parent aab0b51 commit 3ab8e55
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.about.libraries)
alias(libs.plugins.compose.compiler)
}

android {
Expand Down Expand Up @@ -38,6 +39,10 @@ android {
buildFeatures {
viewBinding = true
buildConfig = true
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.3.2"
}
}

Expand All @@ -64,4 +69,16 @@ dependencies {
implementation(libs.aboutlibraries.core)
implementation(libs.aboutlibraries)

val composeBom = platform("androidx.compose:compose-bom:2024.12.01")
implementation(composeBom)
androidTestImplementation(composeBom)

implementation ("androidx.compose.runtime:runtime")
implementation ("androidx.compose.ui:ui")
implementation ("androidx.compose.foundation:foundation")
implementation ("androidx.compose.foundation:foundation-layout")
implementation ("androidx.compose.material3:material3")
implementation ("androidx.compose.runtime:runtime-livedata")
implementation ("androidx.compose.ui:ui-tooling")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.nasahacker.steelmind.compose.ui.component

import androidx.compose.foundation.layout.RowScope
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBar(
title: String,
onNavigationClick: () -> Unit,
actions: @Composable RowScope.() -> Unit = {}
) {
TopAppBar(
title = { Text(text = title, style = MaterialTheme.typography.titleLarge) },
navigationIcon = {
IconButton(onClick = onNavigationClick) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "Back"
)
}
},
actions = actions,
colors = TopAppBarDefaults.topAppBarColors()
)
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewAppBar() {
AppBar(
title = "Sample App Bar",
onNavigationClick = {},
actions = {
IconButton(onClick = {}) {
Icon(
imageVector = Icons.Filled.Delete,
contentDescription = "Action"
)
}
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.nasahacker.steelmind.compose.ui.component

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
fun DeleteDialog(
onDismissRequest: () -> Unit,
onConfirmation: () -> Unit,
dialogTitle: String,
dialogText: String,
icon: ImageVector,
) {
AlertDialog(
icon = {
Icon(icon, contentDescription = null)
},
title = {
Text(text = dialogTitle)
},
text = {
Text(
text = dialogText,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
},
onDismissRequest = {
onDismissRequest()
},
confirmButton = {
TextButton(
onClick = {
onConfirmation()
}
) {
Text("Confirm")
}
},
dismissButton = {
TextButton(
onClick = {
onDismissRequest()
}
) {
Text("Dismiss")
}
}
)
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewDeleteDialog(modifier: Modifier = Modifier) {
DeleteDialog(
onDismissRequest = {},
onConfirmation = { },
dialogTitle = "Delete All?",
dialogText = "This action will delete all the history data, confirm to go ahead.",
icon = Icons.Filled.Delete
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.nasahacker.steelmind.compose.ui.component

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HistoryItem(
action: String,
remarks: String = "No Remarks",
time: String,
onClick: () -> Unit,
onLongPress: () -> Unit,
) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.surfaceVariant)
.padding(4.dp)
.combinedClickable(
onClick = { onClick() },
onLongClick = { onLongPress() }
),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
text = action,
style = MaterialTheme.typography.titleMedium.copy(
color = MaterialTheme.colorScheme.primary,
fontWeight = FontWeight.Bold
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
text = remarks,
style = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurfaceVariant
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
text = time,
style = MaterialTheme.typography.bodySmall.copy(
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
HorizontalDivider(
modifier = Modifier.padding(top = 8.dp),
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f),
thickness = 1.dp
)
}

}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun PreviewHistoryItem() {
MaterialTheme {
HistoryItem(
action = "Started Workout",
remarks = "Completed first set of exercises",
time = "12/10/2025 10:00 PM",
onClick = {},
onLongPress = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nasahacker.steelmind.compose.ui.screen

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun AboutScreen(modifier: Modifier = Modifier) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nasahacker.steelmind.compose.ui.screen

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier


@Composable
fun DebugLogsScreen(modifier: Modifier = Modifier) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.nasahacker.steelmind.compose.ui.screen

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.nasahacker.steelmind.compose.ui.component.AppBar
import com.nasahacker.steelmind.compose.ui.component.DeleteDialog
import com.nasahacker.steelmind.compose.ui.component.HistoryItem
import com.nasahacker.steelmind.dto.History
import com.nasahacker.steelmind.util.MmkvManager

@Composable
fun HistoryScreen(onNavigationClick: () -> Unit) {
// States
var historyList by remember { mutableStateOf(MmkvManager.getHistory()) }
var isDeleteAllDialogVisible by remember { mutableStateOf(false) }
var isDeleteItemDialogVisible by remember { mutableStateOf(false) }
var selectedHistory: History? by remember { mutableStateOf(null) }
val context = LocalContext.current

Column(modifier = Modifier.fillMaxSize()) {
// Top AppBar
AppBar(
title = "History",
onNavigationClick = onNavigationClick,
actions = {
IconButton(onClick = { isDeleteAllDialogVisible = true }) {
Icon(
imageVector = Icons.Outlined.Delete,
contentDescription = "Delete All History"
)
}
}
)

// History List
LazyColumn {
items(historyList) { history ->
HistoryItem(
action = history.action,
remarks = history.remarks,
time = history.time,
onClick = {
// Handle click event if needed
},
onLongPress = {
selectedHistory = history
isDeleteItemDialogVisible = true
}
)
}
}

// Delete All Dialog
if (isDeleteAllDialogVisible) {
DeleteDialog(
onDismissRequest = { isDeleteAllDialogVisible = false },
onConfirmation = {
MmkvManager.clearAllHistory()
historyList = MmkvManager.getHistory()
isDeleteAllDialogVisible = false
Toast.makeText(context, "All history deleted successfully.", Toast.LENGTH_SHORT).show()
},
dialogTitle = "Delete All?",
dialogText = "This action will delete all the history data. Confirm to proceed.",
icon = Icons.Filled.Delete
)
}

// Delete Item Dialog
if (isDeleteItemDialogVisible) {
selectedHistory?.let { history ->
DeleteDialog(
onDismissRequest = { isDeleteItemDialogVisible = false },
onConfirmation = {
MmkvManager.deleteHistory(history)
historyList = MmkvManager.getHistory()
isDeleteItemDialogVisible = false
Toast.makeText(context, "Item deleted successfully.", Toast.LENGTH_SHORT).show()
},
dialogTitle = "Delete Item?",
dialogText = "This action will delete the selected item. Confirm to proceed.",
icon = Icons.Filled.Delete
)
}
}
}
}

@Preview(showSystemUi = true, showBackground = true)
@Composable
fun PreviewHistoryScreen() {
HistoryScreen(onNavigationClick = {})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nasahacker.steelmind.compose.ui.screen

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun MainScreen(modifier: Modifier = Modifier) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nasahacker.steelmind.compose.ui.theme

import androidx.compose.ui.graphics.Color

val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
Loading

0 comments on commit 3ab8e55

Please sign in to comment.