|
| 1 | +package com.example.senpos.data.repositories |
| 2 | + |
| 3 | +import com.example.senpos.data.models.Drug |
| 4 | +import com.example.senpos.data.models.IntakeStatus |
| 5 | +import com.example.senpos.data.models.MedicationIntake |
| 6 | +import com.example.senpos.data.models.MedicationPlan |
| 7 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 8 | +import kotlinx.coroutines.flow.StateFlow |
| 9 | +import kotlinx.coroutines.flow.asStateFlow |
| 10 | +import java.util.Calendar |
| 11 | + |
| 12 | +interface MedicationRepository { |
| 13 | + val intakes: StateFlow<List<MedicationIntake>> |
| 14 | + val drugs: StateFlow<List<Drug>> |
| 15 | + val plans: StateFlow<List<MedicationPlan>> |
| 16 | + |
| 17 | + suspend fun updateIntakeStatus(intakeId: String, status: IntakeStatus) |
| 18 | +} |
| 19 | + |
| 20 | +class OfflineMedicationRepository : MedicationRepository { |
| 21 | + |
| 22 | + private val _drugs = MutableStateFlow(listOf( |
| 23 | + Drug(id = "drug_1", name = "Doliprane", dosage = "1000mg", form = "Tablet"), |
| 24 | + Drug(id = "drug_2", name = "Metformine", dosage = "500mg", form = "Tablet"), |
| 25 | + Drug(id = "drug_3", name = "Amlodipine", dosage = "5mg", form = "Tablet"), |
| 26 | + Drug(id = "drug_4", name = "Omeprazole", dosage = "20mg", form = "Capsule") |
| 27 | + )) |
| 28 | + override val drugs: StateFlow<List<Drug>> = _drugs.asStateFlow() |
| 29 | + |
| 30 | + private val _plans = MutableStateFlow(listOf( |
| 31 | + MedicationPlan(id = "plan_1", startDate = daysAgoAt(10, 8), endDate = daysAgoAt(-20, 8), frequency = 28_800_000L, quantity = 1, userId = "user_1"), |
| 32 | + MedicationPlan(id = "plan_2", startDate = daysAgoAt(5, 8), endDate = daysAgoAt(-25, 8), frequency = 43_200_000L, quantity = 1, userId = "user_1"), |
| 33 | + MedicationPlan(id = "plan_3", startDate = daysAgoAt(3, 9), endDate = daysAgoAt(-27, 9), frequency = 86_400_000L, quantity = 2, userId = "user_1"), |
| 34 | + MedicationPlan(id = "plan_4", startDate = daysAgoAt(7, 7), endDate = daysAgoAt(-7, 7), frequency = 86_400_000L, quantity = 1, userId = "user_1") |
| 35 | + )) |
| 36 | + override val plans: StateFlow<List<MedicationPlan>> = _plans.asStateFlow() |
| 37 | + |
| 38 | + private val _intakes = MutableStateFlow(listOf( |
| 39 | + MedicationIntake(id = "t1", realIntakeTime = todayAt(7), status = IntakeStatus.PENDING, drugId = "drug_1", planId = "plan_1"), |
| 40 | + MedicationIntake(id = "t2", realIntakeTime = todayAt(15), status = IntakeStatus.PENDING, drugId = "drug_1", planId = "plan_1"), |
| 41 | + MedicationIntake(id = "t3", realIntakeTime = todayAt(23), status = IntakeStatus.PENDING, drugId = "drug_1", planId = "plan_1"), |
| 42 | + MedicationIntake(id = "t4", realIntakeTime = todayAt(8), status = IntakeStatus.PENDING, drugId = "drug_2", planId = "plan_2"), |
| 43 | + MedicationIntake(id = "t5", realIntakeTime = todayAt(20), status = IntakeStatus.PENDING, drugId = "drug_2", planId = "plan_2"), |
| 44 | + MedicationIntake(id = "t6", realIntakeTime = todayAt(9), status = IntakeStatus.PENDING, drugId = "drug_3", planId = "plan_3"), |
| 45 | + MedicationIntake(id = "t7", realIntakeTime = todayAt(7, 30), status = IntakeStatus.PENDING, drugId = "drug_4", planId = "plan_4"), |
| 46 | + MedicationIntake(id = "o1", realIntakeTime = daysAgoAt(1, 7), status = IntakeStatus.PENDING, drugId = "drug_1", planId = "plan_1"), |
| 47 | + MedicationIntake(id = "o2", realIntakeTime = daysAgoAt(1, 15), status = IntakeStatus.PENDING, drugId = "drug_1", planId = "plan_1"), |
| 48 | + MedicationIntake(id = "o3", realIntakeTime = daysAgoAt(1, 8), status = IntakeStatus.PENDING, drugId = "drug_2", planId = "plan_2"), |
| 49 | + MedicationIntake(id = "o4", realIntakeTime = daysAgoAt(2, 9), status = IntakeStatus.PENDING, drugId = "drug_3", planId = "plan_3") |
| 50 | + )) |
| 51 | + override val intakes: StateFlow<List<MedicationIntake>> = _intakes.asStateFlow() |
| 52 | + |
| 53 | + override suspend fun updateIntakeStatus(intakeId: String, status: IntakeStatus) { |
| 54 | + _intakes.value = _intakes.value.map { |
| 55 | + if (it.id == intakeId) it.copy(status = status) else it |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fun todayAt(hour: Int, minute: Int = 0): Long = |
| 60 | + Calendar.getInstance().apply { |
| 61 | + set(Calendar.HOUR_OF_DAY, hour) |
| 62 | + set(Calendar.MINUTE, minute) |
| 63 | + set(Calendar.SECOND, 0) |
| 64 | + set(Calendar.MILLISECOND, 0) |
| 65 | + }.timeInMillis |
| 66 | + |
| 67 | + private fun daysAgoAt(days: Int, hour: Int): Long = |
| 68 | + todayAt(hour) - days * 24 * 60 * 60 * 1000L |
| 69 | +} |
0 commit comments