Skip to content

Commit

Permalink
Refactored DayDetailsBottomSheet and removed extra arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyPouya committed Nov 16, 2024
1 parent 2c5fcd7 commit cbe1b77
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 73 deletions.
22 changes: 3 additions & 19 deletions app/src/main/java/com/pouyaheydari/calendar/ui/CalendarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import com.pouyaheydari.calendar.ui.components.WeekDaysComponent
import java.util.GregorianCalendar

// We have to add this to prevent misplacement of dates when combining persian text with numbers
private const val RLM = '\u200F'
internal const val RLM = '\u200F'
private const val CALENDAR_INTENT_TYPE = "vnd.android.cursor.item/event"

@Composable
Expand All @@ -69,23 +69,7 @@ fun CalendarScreen(
shouldShowBottomSheet = state.shouldShowBottomSheet,
onBottomSheetDismissed = { viewModel.onIntent(CalendarUserIntents.OnBottomSheetDismissed) },
setReminderText = stringResource(R.string.set_a_work_to_do),
events = state.selectedDayEvents,
dayOfWeek = state.selectedDay.weekDay.getName(context),
iranianDate = RLM + stringResource(
id = R.string.persian_day_month,
state.selectedDay.shamsiDay.toPersianNumber(),
state.selectedDay.shamsiMonth.getName(context),
state.selectedDay.shamsiYear.toPersianNumber()
),
gregorianDate = RLM + stringResource(
id = R.string.gregorian_full_date,
state.selectedDay.gregorianDay.toPersianNumber(),
state.selectedDay.gregorianMonth.getName(context),
state.selectedDay.gregorianYear.toPersianNumber()
),
onSetReminderClicked = {
handleIntentToDefaultCalendarApp(state, context, view)
},
onSetReminderClicked = { handleIntentToDefaultCalendarApp(state, context, view) },
isHoliday = state.selectedDay.isShamsiHoliday,
selectedDay = state.selectedDay
)
Expand Down Expand Up @@ -120,7 +104,7 @@ private fun handleIntentToDefaultCalendarApp(
}

@Composable
fun CalendarComponent(
private fun CalendarComponent(
modifier: Modifier = Modifier,
paddingValues: PaddingValues,
today: DayType.Day,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.pouyaheydari.calendar.ui

import com.pouyaheydari.calendar.core.pojo.DayType
import com.pouyaheydari.calendar.core.pojo.Event
import com.pouyaheydari.calendar.domain.Month

data class CalendarScreenState(
val today: DayType.Day,
val displayMonth: Month = Month(),
val shouldShowBottomSheet: Boolean = false,
val selectedDay: DayType.Day,
val selectedDayEvents: List<Event> = emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,20 @@ class CalendarViewModel @Inject constructor(
today.shamsiYear to today.shamsiMonth

private val _screenState = MutableStateFlow(
CalendarScreenState(
today = today,
selectedDay = today
)
CalendarScreenState(today = today, selectedDay = today)
)
val screenState: StateFlow<CalendarScreenState> = _screenState
.onStart { updateDisplayedDate() }
.stateIn(
viewModelScope, SharingStarted.WhileSubscribed(5000), CalendarScreenState(
today = today,
selectedDay = today
)
viewModelScope,
SharingStarted.WhileSubscribed(5000),
CalendarScreenState(today = today, selectedDay = today)
)

fun onIntent(intent: CalendarUserIntents) = when (intent) {
is CalendarUserIntents.OnDayClicked -> {
_screenState.update {
it.copy(
selectedDay = intent.day,
selectedDayEvents = intent.day.events,
shouldShowBottomSheet = true
)
it.copy(selectedDay = intent.day, shouldShowBottomSheet = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
Expand All @@ -14,6 +15,7 @@ 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.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewFontScale
import androidx.compose.ui.tooling.preview.PreviewLightDark
Expand All @@ -25,21 +27,20 @@ import com.pouyaheydari.calendar.core.pojo.Event
import com.pouyaheydari.calendar.core.pojo.GregorianMonths
import com.pouyaheydari.calendar.core.pojo.ShamsiMonths
import com.pouyaheydari.calendar.core.pojo.WeekDay
import com.pouyaheydari.calendar.core.utils.toPersianNumber
import com.pouyaheydari.calendar.ui.RLM

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DayDetailsBottomSheet(
dayOfWeek: String,
iranianDate: String,
gregorianDate: String,
events: List<Event>,
internal fun DayDetailsBottomSheet(
selectedDay: DayType.Day,
isHoliday: Boolean = false,
setReminderText: String = "",
onSetReminderClicked: () -> Unit = {},
onBottomSheetDismissed: () -> Unit = {},
shouldShowBottomSheet: Boolean = false,
isHoliday: Boolean = false,
selectedDay: DayType.Day,
) {
val context = LocalContext.current
AnimatedVisibility(visible = shouldShowBottomSheet) {
ModalBottomSheet(
onDismissRequest = onBottomSheetDismissed,
Expand All @@ -50,9 +51,19 @@ fun DayDetailsBottomSheet(
modifier = Modifier
.padding(8.dp)
.fillMaxWidth(),
dayOfWeek = dayOfWeek,
iranianDate = iranianDate,
gregorianDate = gregorianDate,
dayOfWeek = selectedDay.weekDay.getName(context),
iranianDate = RLM + stringResource(
id = R.string.persian_day_month,
selectedDay.shamsiDay.toPersianNumber(),
selectedDay.shamsiMonth.getName(context),
selectedDay.shamsiYear.toPersianNumber()
),
gregorianDate = RLM + stringResource(
id = R.string.gregorian_full_date,
selectedDay.gregorianDay.toPersianNumber(),
selectedDay.gregorianMonth.getName(context),
selectedDay.gregorianYear.toPersianNumber()
),
elevation = CardDefaults.cardElevation(),
headerColor = headerColor
)
Expand All @@ -62,53 +73,58 @@ fun DayDetailsBottomSheet(
.padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
if (events.isEmpty()) {
item {
Text(
modifier = Modifier.padding(16.dp),
text = stringResource(R.string.no_events)
)
}
if (selectedDay.events.isEmpty()) {
emptyStateComponent()
} else {
item {
EventComponent(day = selectedDay)
}
}
item {
OutlinedButton(
modifier = Modifier.padding(16.dp),
onClick = onSetReminderClicked
) {
Text(text = setReminderText)
}
}
setReminderInCalendarButtonComponent(onSetReminderClicked, setReminderText)
}
}
}
}

private fun LazyListScope.emptyStateComponent() {
item {
Text(
modifier = Modifier.padding(16.dp),
text = stringResource(R.string.no_events)
)
}
}

private fun LazyListScope.setReminderInCalendarButtonComponent(
onSetReminderClicked: () -> Unit,
setReminderText: String
) {
item {
OutlinedButton(modifier = Modifier.padding(16.dp), onClick = onSetReminderClicked) {
Text(text = setReminderText)
}
}
}

@PreviewScreenSizes
@PreviewFontScale
@PreviewLightDark
@Composable
private fun Preview() {
DayDetailsBottomSheet(
dayOfWeek = "شنبه",
iranianDate = "۱۰ آذر ۱۴۰۲",
gregorianDate = "۲۲ سپتامبر ۲۰۲۴",
shouldShowBottomSheet = true,
setReminderText = "کاری برای انجام در این روز تنظیم کنید",
events = listOf(Event("روز جهانی درخت کاری", true)),
selectedDay = DayType.Day(
10,
ShamsiMonths.Farwarding,
1402,
WeekDay.Saturday,
22,
GregorianMonths.Jun,
2024,
false,
false,
listOf(Event("روز جهانی درخت کاری", true))
shamsiDay = 10,
shamsiMonth = ShamsiMonths.Farwarding,
shamsiYear = 1402,
weekDay = WeekDay.Saturday,
gregorianDay = 22,
gregorianMonth = GregorianMonths.Jun,
gregorianYear = 2024,
isShamsiHoliday = false,
today = false,
events = listOf(Event("روز جهانی درخت کاری", true))
),
)
}

0 comments on commit cbe1b77

Please sign in to comment.