Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Add Medication and Medication Confirm screen #12

Merged
merged 19 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-parcelize'
}

android {
Expand Down Expand Up @@ -50,6 +51,7 @@ android {
dependencies {
def nav_version = "2.4.2"
def hilt_version = "2.42"
def gson_version = "2.9.0"

implementation 'androidx.core:core-ktx:1.8.0'
implementation "androidx.compose.ui:ui:$compose_version"
Expand All @@ -65,6 +67,9 @@ dependencies {
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

// Gson
implementation "com.google.code.gson:gson:$gson_version"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
57 changes: 52 additions & 5 deletions app/src/main/java/com/waseefakhtar/doseapp/DoseApp.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.waseefakhtar.doseapp

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
Expand All @@ -11,7 +15,12 @@ import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.FloatingActionButtonElevation
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
Expand All @@ -20,18 +29,24 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.waseefakhtar.doseapp.feature.addmedication.navigation.AddMedicationDestination
import com.waseefakhtar.doseapp.navigation.DoseNavHost
import com.waseefakhtar.doseapp.navigation.DoseTopLevelNavigation
import com.waseefakhtar.doseapp.navigation.TOP_LEVEL_DESTINATIONS
Expand All @@ -56,15 +71,34 @@ fun DoseApp() {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination

val bottomBarVisibility = rememberSaveable { (mutableStateOf(true)) }
val fabVisibility = rememberSaveable { (mutableStateOf(true)) }

Scaffold(
modifier = Modifier.padding(16.dp),
modifier = Modifier.padding(24.dp),
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.onBackground,
floatingActionButton = {
AnimatedVisibility(
visible = fabVisibility.value,
enter = slideInVertically(initialOffsetY = { it }),
exit = slideOutVertically(targetOffsetY = { it }),
content = {
DoseFAB(navController)
})

},
bottomBar = {
DoseBottomBar(
onNavigateToTopLevelDestination = doseTopLevelNavigation::navigateTo,
currentDestination = currentDestination
)
AnimatedVisibility(
visible = bottomBarVisibility.value,
enter = slideInVertically(initialOffsetY = { it }),
exit = slideOutVertically(targetOffsetY = { it }),
content = {
DoseBottomBar(
onNavigateToTopLevelDestination = doseTopLevelNavigation::navigateTo,
currentDestination = currentDestination
)
})
}
) { padding ->
Row(
Expand All @@ -78,6 +112,8 @@ fun DoseApp() {
) {

DoseNavHost(
bottomBarVisibility = bottomBarVisibility,
fabVisibility = fabVisibility,
navController = navController,
modifier = Modifier
.padding(padding)
Expand Down Expand Up @@ -130,6 +166,17 @@ private fun DoseBottomBar(
}
}

@Composable
fun DoseFAB(navController: NavController) {
ExtendedFloatingActionButton(
text = { Text(text = stringResource(id = R.string.add_medication)) },
icon = { Icon(imageVector = Icons.Default.Add, contentDescription = "Add") },
onClick = {
navController.navigate(AddMedicationDestination.route)
},
elevation = FloatingActionButtonDefaults.elevation(0.dp, 0.dp))
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.waseefakhtar.doseapp.domain.model

import android.os.Bundle
import android.os.Parcelable
import androidx.navigation.NavType
import com.google.gson.Gson
import com.waseefakhtar.doseapp.util.TimesOfDay
import kotlinx.parcelize.Parcelize
import java.util.Date

@Parcelize
data class Medication(
val id: Int,
val name: String,
val dosage: Int,
val recurrence: String,
val endDate: Date,
val timesOfDay: List<TimesOfDay>,
) : Parcelable

class AssetParamType : NavType<Medication>(isNullableAllowed = false) {
override fun get(bundle: Bundle, key: String): Medication? {
return bundle.getParcelable(key)
}

override fun parseValue(value: String): Medication {
return Gson().fromJson(value, Medication::class.java)
}

override fun put(bundle: Bundle, key: String, value: Medication) {
bundle.putParcelable(key, value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.waseefakhtar.doseapp.extension

import java.text.SimpleDateFormat
import java.util.*

fun Date.toFormattedString(): String {
val sdf = SimpleDateFormat("LLLL dd, yyyy", Locale.getDefault())
return sdf.format(this)
}
Loading