Skip to content

Commit

Permalink
Added events of month to bottom of the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyPouya committed Oct 6, 2024
1 parent 5cb3f8f commit 099b5a3
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.pouyaheydari.calendar.domain

import com.pouyaheydari.calendar.core.pojo.DayType
import com.pouyaheydari.calendar.core.pojo.Event
import com.pouyaheydari.calendar.core.pojo.GregorianDate
import com.pouyaheydari.calendar.core.pojo.GregorianMonths
import com.pouyaheydari.calendar.core.pojo.ShamsiDate
import com.pouyaheydari.calendar.core.pojo.ShamsiMonths
import com.pouyaheydari.calendar.core.pojo.WeekDay
import com.pouyaheydari.calendar.core.utils.CalendarTool
Expand All @@ -11,6 +13,7 @@ import javax.inject.Inject

class GenerateMonthUseCase @Inject constructor(
private val calculateDaysInMonthUseCase: CalculateDaysInMonthUseCase,
private val getEventsByDayUseCase: GetEventsByDayUseCase,
private val calendarTool: CalendarTool,
private val resourceUtils: ResourceUtils,
private val today: DayType.Day,
Expand Down Expand Up @@ -52,6 +55,10 @@ class GenerateMonthUseCase @Inject constructor(
shamsiMonth,
shamsiDay,
gregorianDay.weekDay
),
events = generateEvents(
calendarTool.getIranianDate(),
calendarTool.getGregorianDate()
)
)
)
Expand All @@ -66,6 +73,9 @@ class GenerateMonthUseCase @Inject constructor(
)
}

private fun generateEvents(iranianDate: ShamsiDate, gregorianDate: GregorianDate): List<Event> =
getEventsByDayUseCase(iranianDate, gregorianDate)

private fun checkIsHoliday(
iranianYear: Int,
shamsiMonth: ShamsiMonths,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.pouyaheydari.calendar.domain

import com.pouyaheydari.calendar.core.pojo.DayType
import com.pouyaheydari.calendar.core.pojo.Event
import com.pouyaheydari.calendar.core.pojo.GregorianDate
import com.pouyaheydari.calendar.core.pojo.ShamsiDate
import com.pouyaheydari.calendar.core.utils.ResourceUtils
import javax.inject.Inject

class GetEventsByDayUseCase @Inject constructor(private val resourceUtils: ResourceUtils) {
operator fun invoke(day: DayType.Day): List<Event> {
val persianTemp = day.shamsiMonth.monthNumber * 100 + day.shamsiDay
val gregorianTemp = day.gregorianMonth.monthNumber * 100 + day.gregorianDay
operator fun invoke(shamsiDate: ShamsiDate, gregorianDate: GregorianDate): List<Event> {
val persianTemp = shamsiDate.month.monthNumber * 100 + shamsiDate.day
val gregorianTemp = gregorianDate.month.monthNumber * 100 + gregorianDate.day
val persianEvents = resourceUtils.eventP[persianTemp].orEmpty()
val gregorianEvents = resourceUtils.eventG[gregorianTemp].orEmpty()

return buildList {
persianEvents.forEach {
add(Event(day = day.shamsiDay, description = it))
add(Event(day = shamsiDate.day, description = it))
}
gregorianEvents.forEach {
add(Event(day = day.gregorianDay, description = it))
add(Event(day = shamsiDate.day, description = it))
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions app/src/main/java/com/pouyaheydari/calendar/ui/CalendarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import android.content.Context
import android.content.Intent
import android.provider.CalendarContract
import android.view.View
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.getString
import androidx.core.content.ContextCompat.startActivity
Expand All @@ -27,6 +33,7 @@ import com.pouyaheydari.calendar.core.pojo.DayType
import com.pouyaheydari.calendar.core.utils.toPersianNumber
import com.pouyaheydari.calendar.domain.Month
import com.pouyaheydari.calendar.ui.components.DayDetailsBottomSheet
import com.pouyaheydari.calendar.ui.components.EventComponent
import com.pouyaheydari.calendar.ui.components.HeaderComponent
import com.pouyaheydari.calendar.ui.components.MonthComponent
import com.pouyaheydari.calendar.ui.components.MonthYearTitleComponent
Expand Down Expand Up @@ -118,9 +125,11 @@ fun CalendarComponent(
onNextMonthClicked: () -> Unit = {},
onPreviousMonthClicked: () -> Unit = {}
) {
Column(modifier = modifier.fillMaxSize()) {
Column(modifier = modifier) {
HeaderComponent(
modifier = Modifier.fillMaxHeight(0.3f),
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
elevation = CardDefaults.cardElevation(defaultElevation = 16.dp),
dayOfWeek = stringResource(
id = R.string.today_is_day_of_week,
Expand Down Expand Up @@ -158,6 +167,27 @@ fun CalendarComponent(
onSwipeToNextMonth = onNextMonthClicked,
onSwipeToPreviousMonth = onPreviousMonthClicked
)
Text(
modifier = Modifier
.fillMaxWidth()
.padding(end = 8.dp, top = 16.dp),
text = stringResource(R.string.events, month.shamsiMonth.getName(context)),
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onSurface,
textAlign = TextAlign.End
)
LazyColumn(contentPadding = PaddingValues(top = 8.dp)) {
items(month.days) { day ->
if (day is DayType.Day) {
day.events.forEach { event ->
EventComponent(
modifier = Modifier.clickable { onDaySelected(day) },
event = event
)
}
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pouyaheydari.calendar.ui

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

data class CalendarScreenState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.pouyaheydari.calendar.core.pojo.ShamsiMonths
import com.pouyaheydari.calendar.domain.CalculateNextMonthUseCase
import com.pouyaheydari.calendar.domain.CalculatePreviousMonthUseCase
import com.pouyaheydari.calendar.domain.GenerateMonthUseCase
import com.pouyaheydari.calendar.domain.GetEventsByDayUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -22,7 +21,6 @@ class CalendarViewModel @Inject constructor(
private val calculateNextMonthUseCase: CalculateNextMonthUseCase,
private val calculatePreviousMonthUseCase: CalculatePreviousMonthUseCase,
private val generateMonthUseCase: GenerateMonthUseCase,
private val getEventsByDayUseCase: GetEventsByDayUseCase,
today: DayType.Day
) : ViewModel() {

Expand All @@ -46,11 +44,10 @@ class CalendarViewModel @Inject constructor(

fun onIntent(intent: CalendarUserIntents) = when (intent) {
is CalendarUserIntents.OnDayClicked -> {
val events = getEventsByDayUseCase(day = intent.day)
_screenState.update {
it.copy(
selectedDay = intent.day,
selectedDayEvents = events,
selectedDayEvents = intent.day.events,
shouldShowBottomSheet = true
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pouyaheydari.calendar.ui.components

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -22,7 +21,7 @@ import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.dp
import com.pouyaheydari.calendar.R
import com.pouyaheydari.calendar.domain.Event
import com.pouyaheydari.calendar.core.pojo.Event

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -46,7 +45,7 @@ fun DayDetailsBottomSheet(
HeaderComponent(
modifier = Modifier
.padding(8.dp)
.fillMaxHeight(0.2f),
.fillMaxWidth(),
dayOfWeek = dayOfWeek,
iranianDate = iranianDate,
gregorianDate = gregorianDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ fun MonthYearTitleComponent(
Column(modifier = Modifier.weight(1f), horizontalAlignment = Alignment.CenterHorizontally) {
Text(
text = shamsiDate,
style = MaterialTheme.typography.titleMedium,
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onSurface,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.padding(4.dp))
Text(
text = gregorianDate,
style = MaterialTheme.typography.titleMedium,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface,
textAlign = TextAlign.Center
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.pouyaheydari.calendar.core.utils.toPersianNumber
import com.pouyaheydari.calendar.domain.Event
import com.pouyaheydari.calendar.core.pojo.Event

@Composable
fun EventComponent(modifier: Modifier = Modifier, event: Event) {
Expand All @@ -28,9 +28,9 @@ fun EventComponent(modifier: Modifier = Modifier, event: Event) {
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = event.day.toPersianNumber(), style = MaterialTheme.typography.titleMedium)
Text(text = event.day.toPersianNumber(), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurface)
Spacer(modifier = Modifier.padding(8.dp))
Text(text = event.description, style = MaterialTheme.typography.titleSmall)
Text(text = event.description, style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurface)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.pouyaheydari.calendar.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CardElevation
Expand All @@ -30,19 +29,33 @@ fun HeaderComponent(
headerColor: Color = MaterialTheme.colorScheme.primary
) {
Card(
modifier = modifier.fillMaxWidth(),
shape = HeaderShape,
colors = CardDefaults.cardColors().copy(containerColor = headerColor),
elevation = elevation
) {
Column(
modifier = Modifier.fillMaxSize(),
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.SpaceAround
) {
Text(dayOfWeek, color = Color.White, style = MaterialTheme.typography.headlineSmall)
Text(iranianDate, color = Color.White, style = MaterialTheme.typography.headlineSmall)
Text(gregorianDate, color = Color.White, style = MaterialTheme.typography.headlineSmall)
Text(
modifier = Modifier.padding(4.dp),
text = dayOfWeek,
color = Color.White,
style = MaterialTheme.typography.titleLarge
)
Text(
modifier = Modifier.padding(4.dp),
text = iranianDate,
color = Color.White,
style = MaterialTheme.typography.titleMedium
)
Text(
modifier = Modifier.padding(4.dp),
text = gregorianDate,
color = Color.White,
style = MaterialTheme.typography.titleSmall
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sealed interface DayType {
val gregorianMonth: GregorianMonths,
val gregorianYear: Int,
val isShamsiHoliday: Boolean = false,
val today: Boolean = false
val today: Boolean = false,
val events: List<Event> = emptyList()
) : DayType
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.pouyaheydari.calendar.domain
package com.pouyaheydari.calendar.core.pojo

data class Event(val day: Int, val description: String)
2 changes: 1 addition & 1 deletion core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<string name="gregorian_full_date">%1s %2s %3s</string>
<string name="today_is_day_of_week">امروز %1s</string>
<string name="month_year">%1$s %2$s</string>
<string name="events_of_this_day">مناسبت های این روز :</string>
<string name="events">: مناسبت های ماه %1s</string>
<string name="set_a_work_to_do">کاری برای انجام در تقویم تنظیم کنید</string>
<string name="no_events">مناسبتی برای این روز وجود ندارد</string>
<string name="google_calendar_is_not_installed">برای استفاده از این ویژگی نیاز است که برنامه تقویم گوگل را نصب نمایید</string>
Expand Down

0 comments on commit 099b5a3

Please sign in to comment.