Skip to content

Commit

Permalink
Fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
waseefakhtar committed Jul 1, 2024
2 parents d2d57c0 + bf83250 commit 5ae4d3a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.room.Query
import androidx.room.Update
import com.waseefakhtar.doseapp.data.entity.MedicationEntity
import kotlinx.coroutines.flow.Flow
import java.util.Date

@Dao
interface MedicationDao {
Expand All @@ -34,8 +33,9 @@ interface MedicationDao {
"""
SELECT *
FROM medicationentity
WHERE endDate > :date
WHERE strftime('%Y-%m-%d', medicationTime / 1000, 'unixepoch', 'localtime') = :date
ORDER BY medicationTime ASC
"""
)
fun getMedicationsForDate(date: Date): Flow<List<MedicationEntity>>
fun getMedicationsForDate(date: String): Flow<List<MedicationEntity>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.waseefakhtar.doseapp.domain.model.Medication
import com.waseefakhtar.doseapp.domain.repository.MedicationRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import java.util.Date

class MedicationRepositoryImpl(
private val dao: MedicationDao
Expand All @@ -33,7 +32,7 @@ class MedicationRepositoryImpl(
}
}

override fun getMedicationsForDate(date: Date): Flow<List<Medication>> {
override fun getMedicationsForDate(date: String): Flow<List<Medication>> {
return dao.getMedicationsForDate(
date = date
).map { entities ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.waseefakhtar.doseapp.domain.repository

import com.waseefakhtar.doseapp.domain.model.Medication
import kotlinx.coroutines.flow.Flow
import java.util.Date

interface MedicationRepository {

Expand All @@ -14,5 +13,5 @@ interface MedicationRepository {

fun getAllMedications(): Flow<List<Medication>>

fun getMedicationsForDate(date: Date): Flow<List<Medication>>
fun getMedicationsForDate(date: String): Flow<List<Medication>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ fun Date.toFormattedMonthDateString(): String {
return sdf.format(this)
}

fun Date.toFormattedYearMonthDateString(): String {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
return sdf.format(this)
}

fun String.toDate(): Date? {
return try {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
sdf.parse(this)
} catch (e: Exception) {
e.printStackTrace()
null
}
}

fun Date.toFormattedDateShortString(): String {
val sdf = SimpleDateFormat("dd", Locale.getDefault())
return sdf.format(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ fun HomeRoute(
navController = navController,
state = state,
navigateToMedicationDetail = navigateToMedicationDetail,
onDateSelected = viewModel::selectDate,
onSelectedDate = { viewModel.updateSelectedDate(it) },
logEvent = viewModel::logEvent,
onSelectedDate = {viewModel.updateSelectedDate(it)}
)
}

Expand All @@ -100,8 +101,9 @@ fun HomeScreen(
navController: NavController,
state: HomeState,
navigateToMedicationDetail: (Medication) -> Unit,
logEvent: (String) -> Unit,
onDateSelected: (CalendarModel.DateModel) -> Unit,
onSelectedDate: (Date) -> Unit,
logEvent: (String) -> Unit
) {
Column(
modifier = modifier,
Expand All @@ -111,10 +113,11 @@ fun HomeScreen(
navController = navController,
state = state,
navigateToMedicationDetail = navigateToMedicationDetail,
onSelectedDate = onSelectedDate ,
onSelectedDate = onSelectedDate,
onDateSelected = onDateSelected,
logEvent = {
logEvent.invoke(it)
}
},
)
}
}
Expand Down Expand Up @@ -198,7 +201,6 @@ fun DailyOverviewCard(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun EmptyCard(
navController: NavController,
Expand Down Expand Up @@ -263,11 +265,12 @@ fun DailyMedications(
state: HomeState,
navigateToMedicationDetail: (Medication) -> Unit,
onSelectedDate: (Date) -> Unit,
onDateSelected: (CalendarModel.DateModel) -> Unit,
logEvent: (String) -> Unit
) {


DatesHeader(
lastSelectedDate = state.lastSelectedDate,
logEvent = {
logEvent.invoke(it)
},
Expand Down Expand Up @@ -305,11 +308,16 @@ fun DailyMedications(

@Composable
fun DatesHeader(
logEvent: (String) -> Unit,
onDateSelected: (CalendarModel.DateModel) -> Unit // Callback to pass the selected date
lastSelectedDate: String,
onDateSelected: (CalendarModel.DateModel) -> Unit, // Callback to pass the selected date){}
logEvent: (String) -> Unit
) {
val dataSource = CalendarDataSource()
var calendarModel by remember { mutableStateOf(dataSource.getData(lastSelectedDate = dataSource.today)) }
var calendarModel by remember {
mutableStateOf(
dataSource.getData(lastSelectedDate = dataSource.getLastSelectedDate(lastSelectedDate))
)
}
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.waseefakhtar.doseapp.feature.home.data

import com.waseefakhtar.doseapp.extension.toDate
import com.waseefakhtar.doseapp.extension.toFormattedDateString
import com.waseefakhtar.doseapp.feature.home.model.CalendarModel
import java.util.Calendar
Expand All @@ -12,6 +13,9 @@ class CalendarDataSource {
return Date()
}

fun getLastSelectedDate(dateString: String): Date {
return dateString.toDate() ?: today
}
fun getData(startDate: Date = today, lastSelectedDate: Date): CalendarModel {
val calendar = Calendar.getInstance()
calendar.time = startDate
Expand Down Expand Up @@ -45,7 +49,10 @@ class CalendarDataSource {
return CalendarModel(
selectedDate = toItemModel(lastSelectedDate, true),
visibleDates = dateList.map {
toItemModel(it, it == lastSelectedDate)
toItemModel(
date = it,
isSelectedDate = it.toFormattedDateString() == lastSelectedDate.toFormattedDateString()
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class GetMedicationsUseCase @Inject constructor(
private val repository: MedicationRepository
) {

fun getMedications(): Flow<List<Medication>> {
return repository.getAllMedications()
fun getMedications(date: String? = null): Flow<List<Medication>> {
return if (date != null) {
repository.getMedicationsForDate(date)
} else repository.getAllMedications()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import com.waseefakhtar.doseapp.domain.model.Medication
data class HomeState(
val greeting: String = "",
val userName: String = "",
val lastSelectedDate: String = "",
val medications: List<Medication> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package com.waseefakhtar.doseapp.feature.home.viewmodel
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.waseefakhtar.doseapp.analytics.AnalyticsHelper
import com.waseefakhtar.doseapp.domain.model.Medication
import com.waseefakhtar.doseapp.extension.toFormattedDateString
import com.waseefakhtar.doseapp.extension.toFormattedYearMonthDateString
import com.waseefakhtar.doseapp.feature.home.model.CalendarModel
import com.waseefakhtar.doseapp.feature.home.usecase.GetMedicationsUseCase
import com.waseefakhtar.doseapp.feature.home.usecase.UpdateMedicationUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
Expand All @@ -25,35 +30,39 @@ import javax.inject.Inject
class HomeViewModel @Inject constructor(
private val getMedicationsUseCase: GetMedicationsUseCase,
private val updateMedicationUseCase: UpdateMedicationUseCase,
private val savedStateHandle: SavedStateHandle,
private val analyticsHelper: AnalyticsHelper

) : ViewModel() {

var state by mutableStateOf(HomeState())
private set

private var dateFilter = savedStateHandle.getStateFlow(
DATE_FILTER_KEY,
Date().toFormattedYearMonthDateString()
).onEach {
state = state.copy(
lastSelectedDate = it
)
}

private val _selectedDate = MutableStateFlow(Date())
private val _medications = getMedicationsUseCase.getMedications()



val homeUiState = combine(_selectedDate,_medications) { selectedDate , medications ->
val homeUiState = combine(_selectedDate, _medications) { selectedDate, medications ->
val filteredMedications = medications.filter {
it.medicationTime.toFormattedDateString() == selectedDate.toFormattedDateString()
}.sortedBy { it.medicationTime }

HomeState(
medications = filteredMedications
)

}.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000),
HomeState()
)



fun updateSelectedDate(date: Date) {
_selectedDate.value = date
}
Expand All @@ -72,16 +81,23 @@ class HomeViewModel @Inject constructor(
// TODO: Get greeting by checking system time
}

@OptIn(ExperimentalCoroutinesApi::class)
fun loadMedications() {
viewModelScope.launch {
getMedicationsUseCase.getMedications().onEach { medicationList ->
state = state.copy(
medications = medicationList
)
dateFilter.flatMapLatest { selectedDate ->
getMedicationsUseCase.getMedications(selectedDate).onEach { medicationList ->
state = state.copy(
medications = medicationList
)
}
}.launchIn(viewModelScope)
}
}

fun selectDate(selectedDate: CalendarModel.DateModel) {
savedStateHandle[DATE_FILTER_KEY] = selectedDate.date.toFormattedYearMonthDateString()
}

fun takeMedication(medication: Medication) {
viewModelScope.launch {
updateMedicationUseCase.updateMedication(medication)
Expand All @@ -95,4 +111,7 @@ class HomeViewModel @Inject constructor(
fun logEvent(eventName: String) {
analyticsHelper.logEvent(eventName = eventName)
}
companion object {
const val DATE_FILTER_KEY = "medication_date_filter"
}
}

0 comments on commit 5ae4d3a

Please sign in to comment.