Skip to content

Commit

Permalink
Refactored packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyPouya committed Sep 8, 2024
1 parent 58fef20 commit 9a5ad84
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 183 deletions.
25 changes: 5 additions & 20 deletions app/src/main/java/com/pouyaheydari/calendar/di/DateModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import com.pouyaheydari.calendar.core.pojo.CalendarModel
import com.pouyaheydari.calendar.core.pojo.Day
import com.pouyaheydari.calendar.core.utils.CalendarTool
import com.pouyaheydari.calendar.core.utils.ResourceUtils
import com.pouyaheydari.calendar.repository.MonthGeneratorClass
import java.util.*
import javax.inject.Singleton

/**
* This object contains hilt modules needed for injection
*/
@Module
@InstallIn(SingletonComponent::class)
object DateModule {

/**
* Provides current date details
*/
@Provides
@Singleton
fun todayProvider(calendar: GregorianCalendar): CalendarModel {
fun todayProvider(calendar: GregorianCalendar): Day {
val calendarTool = CalendarTool(calendar)
return with(calendarTool) {
CalendarModel(
Day(
iranianDay,
iranianMonth,
iranianYear,
Expand All @@ -40,32 +34,23 @@ object DateModule {
}
}

/**
* Provides Gregorian calendar instance
*/
@Provides
@Singleton
fun gregorianCalendarProvider() = GregorianCalendar()

/**
* Provides CalendarTool instance to convert dates from Gregorian to Shamsi
*/
@Provides
@Singleton
fun calendarToolProvider(calendar: GregorianCalendar) =
CalendarTool(calendar)

/**
* Provides local repository
*/
@Provides
@Singleton
fun monthGeneratorProvider(
app: Application,
calendarModel: CalendarModel,
day: Day,
calendarTool: CalendarTool
): MonthGeneratorClass {
ResourceUtils(app)
return MonthGeneratorClass(calendarTool, calendarModel)
return MonthGeneratorClass(calendarTool, day)
}
}
3 changes: 0 additions & 3 deletions app/src/main/java/com/pouyaheydari/calendar/pojo/DateModel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package com.pouyaheydari.calendar.pojo

/**
* Contains details of a day
*/
data class DateModel(val year: Int, val month: Int, val dayNumber: Int)
3 changes: 0 additions & 3 deletions app/src/main/java/com/pouyaheydari/calendar/pojo/MonthType.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package com.pouyaheydari.calendar.pojo

/**
* An Enum to request next or previous month
*/
enum class MonthType { NEXT_MONTH, PREVIOUS_MONTH }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pouyaheydari.calendar.repository

import com.pouyaheydari.calendar.core.pojo.CalendarModel
import com.pouyaheydari.calendar.core.pojo.Day
import com.pouyaheydari.calendar.core.utils.CalendarTool
import com.pouyaheydari.calendar.core.utils.EMPTY_DATE
import com.pouyaheydari.calendar.core.utils.ResourceUtils
Expand All @@ -23,19 +23,19 @@ private const val SUNDAY = 6
*/
class MonthGeneratorClass @Inject constructor(
private var calendar: CalendarTool,
private val currentDate: CalendarModel
private val currentDate: Day
) {

fun getMonthList(monthType: MonthType): List<CalendarModel> {
val list = arrayListOf<CalendarModel>()
fun getMonthList(monthType: MonthType): List<Day> {
val list = arrayListOf<Day>()
val month =
if (monthType == MonthType.NEXT_MONTH) getNextMonthDate() else getPreviousMonthDate()
list.addAll(addEmptyDays(calendar.dayOfWeek))
for (i in 1..month.dayNumber) {
with(calendar) {
setIranianDate(month.year, month.month, i)
list.add(
CalendarModel(
Day(
iranianDay,
iranianMonth,
iranianYear,
Expand All @@ -52,20 +52,20 @@ class MonthGeneratorClass @Inject constructor(
return list
}

private fun checkIsToday(calendarModel: CalendarModel) {
calendarModel.today =
calendarModel.iranianDay != -1 &&
currentDate.iranianYear == calendarModel.iranianYear &&
currentDate.iranianMonth == calendarModel.iranianMonth &&
currentDate.iranianDay == calendarModel.iranianDay
private fun checkIsToday(day: Day) {
day.today =
day.shamsiDay != -1 &&
currentDate.shamsiYear == day.shamsiYear &&
currentDate.shamsiMonth == day.shamsiMonth &&
currentDate.shamsiDay == day.shamsiDay
}

private fun checkIsHoliday(calendarModel: CalendarModel) = with(calendarModel) {
if (dayOfWeek == FRIDAY || (iranianYear == currentDate.iranianYear && ResourceUtils.vacationP.containsKey(
iranianMonth * 100 + iranianDay
private fun checkIsHoliday(day: Day) = with(day) {
if (dayOfWeek == FRIDAY || (shamsiYear == currentDate.shamsiYear && ResourceUtils.vacationP.containsKey(
shamsiMonth * 100 + shamsiDay
))
)
isHoliday = true
isShamsiHoliday = true
}

private fun getNextMonthDate(): DateModel {
Expand Down Expand Up @@ -102,7 +102,7 @@ class MonthGeneratorClass @Inject constructor(
return DateModel(year, month, dayNumber)
}

private fun addEmptyDays(dayOfWeek: Int): ArrayList<CalendarModel> = when (dayOfWeek) {
private fun addEmptyDays(dayOfWeek: Int): ArrayList<Day> = when (dayOfWeek) {
MONDAY -> emptyDayMaker(2)
TUESDAY -> emptyDayMaker(3)
WEDNESDAY -> emptyDayMaker(4)
Expand All @@ -113,11 +113,11 @@ class MonthGeneratorClass @Inject constructor(
else -> throw IllegalArgumentException("Day is not defined in the week!")
}

private fun emptyDayMaker(dayOfWeek: Int): ArrayList<CalendarModel> {
val list = arrayListOf<CalendarModel>()
private fun emptyDayMaker(dayOfWeek: Int): ArrayList<Day> {
val list = arrayListOf<Day>()
for (i in 1..dayOfWeek) {
list.add(
CalendarModel(
Day(
EMPTY_DATE,
EMPTY_DATE,
EMPTY_DATE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pouyaheydari.calendar.features.calendar.main
package com.pouyaheydari.calendar.ui

import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -8,17 +8,17 @@ import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.pouyaheydari.calendar.R
import com.pouyaheydari.calendar.core.pojo.CalendarModel
import com.pouyaheydari.calendar.core.pojo.Day
import com.pouyaheydari.calendar.core.utils.SELECTED_DAY_DETAILS
import com.pouyaheydari.calendar.features.calendar.main.theme.PersianCalendarTheme
import com.pouyaheydari.calendar.ui.theme.PersianCalendarTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class CalendarFragment : Fragment() {

@Inject
lateinit var today: CalendarModel
lateinit var today: Day
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pouyaheydari.calendar.features.calendar.main
package com.pouyaheydari.calendar.ui

import android.content.Context
import androidx.compose.foundation.layout.Column
Expand All @@ -13,25 +13,24 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.pouyaheydari.calendar.R
import com.pouyaheydari.calendar.core.pojo.CalendarModel
import com.pouyaheydari.calendar.core.pojo.Day
import com.pouyaheydari.calendar.core.utils.extensions.toEnglishMonth
import com.pouyaheydari.calendar.core.utils.extensions.toPersianMonth
import com.pouyaheydari.calendar.core.utils.extensions.toPersianNumber
import com.pouyaheydari.calendar.core.utils.extensions.toPersianWeekDay
import com.pouyaheydari.calendar.features.calendar.main.components.HeaderComponent
import com.pouyaheydari.calendar.features.calendar.main.components.MonthComponent
import com.pouyaheydari.calendar.features.calendar.main.components.MonthYearTitleComponent
import com.pouyaheydari.calendar.features.calendar.main.components.WeekDaysComponent
import com.pouyaheydari.calendar.ui.components.HeaderComponent
import com.pouyaheydari.calendar.ui.components.MonthComponent
import com.pouyaheydari.calendar.ui.components.MonthYearTitleComponent
import com.pouyaheydari.calendar.ui.components.WeekDaysComponent

@Composable
fun CalendarScreen(
modifier: Modifier = Modifier,
today: CalendarModel,
today: Day,
viewModel: CalendarViewModel = viewModel(),
onDaySelected: (CalendarModel) -> Unit
onDaySelected: (Day) -> Unit
) {
val date =
viewModel.getMonthLiveData().collectAsStateWithLifecycle().value
val date = viewModel.screenState.collectAsStateWithLifecycle().value
val context = LocalContext.current
CalendarComponent(
modifier,
Expand All @@ -46,10 +45,10 @@ fun CalendarScreen(
@Composable
fun CalendarComponent(
modifier: Modifier = Modifier,
today: CalendarModel,
date: List<CalendarModel>?,
today: Day,
date: List<Day>?,
context: Context,
onDaySelected: (CalendarModel) -> Unit,
onDaySelected: (Day) -> Unit,
onNextMonthClicked: () -> Unit = {},
onPreviousMonthClicked: () -> Unit = {}
) {
Expand All @@ -65,21 +64,21 @@ fun CalendarComponent(
),
iranianDate = rlm + stringResource(
id = R.string.persian_day_month,
today.iranianDay.toPersianNumber(),
today.iranianMonth.toPersianMonth(context),
today.iranianYear.toPersianNumber()
today.shamsiDay.toPersianNumber(),
today.shamsiMonth.toPersianMonth(context),
today.shamsiYear.toPersianNumber()
),
gregorianDate = rlm + stringResource(
id = R.string.gregorian_full_date,
today.gDay.toPersianNumber(),
today.gMonth.toEnglishMonth(context),
today.gYear.toPersianNumber()
today.gregorianDay.toPersianNumber(),
today.gregorianMonth.toEnglishMonth(context),
today.gregorianYear.toPersianNumber()
)
)
Spacer(modifier = Modifier.padding(all = 8.dp))
MonthYearTitleComponent(
monthName = date?.lastOrNull()?.iranianMonth?.toPersianMonth(context).orEmpty(),
year = date?.lastOrNull()?.iranianYear?.toPersianNumber().orEmpty(),
monthName = date?.lastOrNull()?.shamsiMonth?.toPersianMonth(context).orEmpty(),
year = date?.lastOrNull()?.shamsiYear?.toPersianNumber().orEmpty(),
onNextMonthClicked = onNextMonthClicked,
onPreviousMonthClicked = onPreviousMonthClicked
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pouyaheydari.calendar.features.calendar.main
package com.pouyaheydari.calendar.ui

import androidx.lifecycle.ViewModel
import com.pouyaheydari.calendar.core.pojo.CalendarModel
import com.pouyaheydari.calendar.core.pojo.Day
import com.pouyaheydari.calendar.pojo.MonthType.NEXT_MONTH
import com.pouyaheydari.calendar.pojo.MonthType.PREVIOUS_MONTH
import com.pouyaheydari.calendar.repository.MonthGeneratorClass
Expand All @@ -11,36 +11,29 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

/**
* ViewModel of CalendarFragment
*/
@HiltViewModel
class CalendarViewModel @Inject constructor(private val monthGenerator: MonthGeneratorClass) :
ViewModel() {

private val monthLiveData = MutableStateFlow<List<CalendarModel>>(emptyList())
private val _screenState = MutableStateFlow<List<Day>>(emptyList())
val screenState: StateFlow<List<Day>> = _screenState

init {
monthGenerator.getMonthList(PREVIOUS_MONTH)
monthLiveData.update { monthGenerator.getMonthList(NEXT_MONTH) }
_screenState.update { monthGenerator.getMonthList(NEXT_MONTH) }
}

/**
* Fetches and publishes the next month (from the month that currently user is watching) as a list of days
*/
fun getNextMonth() {
monthLiveData.update { monthGenerator.getMonthList(NEXT_MONTH) }
_screenState.update { monthGenerator.getMonthList(NEXT_MONTH) }
}

/**
* Fetches and publishes the previous month (from the month that currently user is watching) as a list of days
*/
fun getPreviousMonth() {
monthLiveData.update { monthGenerator.getMonthList(PREVIOUS_MONTH) }
_screenState.update { monthGenerator.getMonthList(PREVIOUS_MONTH) }
}

/**
* Returns live data of [monthLiveData]
*/
fun getMonthLiveData(): StateFlow<List<CalendarModel>> = monthLiveData
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pouyaheydari.calendar.features.calendar.main.components
package com.pouyaheydari.calendar.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand All @@ -12,10 +12,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewFontScale
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.dp
import com.pouyaheydari.calendar.features.calendar.main.theme.DarkBlue80
import com.pouyaheydari.calendar.features.calendar.main.theme.TodayShape
import com.pouyaheydari.calendar.ui.theme.DarkBlue80
import com.pouyaheydari.calendar.ui.theme.TodayShape

@Composable
fun DayItemComponent(
Expand Down Expand Up @@ -50,7 +52,9 @@ fun DayItemComponent(
}
}

@Preview
@PreviewScreenSizes
@PreviewFontScale
@PreviewLightDark
@Composable
private fun Preview() {
DayItemComponent(persianDay = "۳۰", gregorianDay = "9", isToday = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pouyaheydari.calendar.features.calendar.main.components
package com.pouyaheydari.calendar.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -17,8 +17,8 @@ import androidx.compose.ui.tooling.preview.PreviewFontScale
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
import androidx.compose.ui.unit.dp
import com.pouyaheydari.calendar.features.calendar.main.theme.HeaderShape
import com.pouyaheydari.calendar.features.calendar.main.theme.PersianCalendarTheme
import com.pouyaheydari.calendar.ui.theme.HeaderShape
import com.pouyaheydari.calendar.ui.theme.PersianCalendarTheme

@Composable
fun HeaderComponent(
Expand Down
Loading

0 comments on commit 9a5ad84

Please sign in to comment.