Skip to content

Commit

Permalink
Date time move forward and backward issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuppan committed Jan 11, 2024
1 parent da35b74 commit 5be3f4b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {

dependencies {
implementation(project(":core:model"))
implementation(project(":core:common"))
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.dataStore.preference)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import com.naveenapps.expensemanager.core.common.utils.fromLocalToUTCTimeStamp
import com.naveenapps.expensemanager.core.common.utils.fromUTCToLocalTimeStamp
import com.naveenapps.expensemanager.core.model.DateRangeType
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -25,16 +27,19 @@ class DateRangeDataStore @Inject constructor(private val dataStore: DataStore<Pr
}

suspend fun setDateRanges(startDate: Long, endDate: Long) = dataStore.edit { preferences ->
preferences[KEY_DATE_RANGE_START_TIME_TYPE] = startDate
preferences[KEY_DATE_RANGE_END_TIME_TYPE] = endDate
preferences[KEY_DATE_RANGE_START_TIME_TYPE] = startDate.fromLocalToUTCTimeStamp()
preferences[KEY_DATE_RANGE_END_TIME_TYPE] = endDate.fromLocalToUTCTimeStamp()
}

fun getDateRanges(): Flow<List<Long>?> =
dataStore.data.map { preferences ->
val startDate = preferences[KEY_DATE_RANGE_START_TIME_TYPE]
val endDate = preferences[KEY_DATE_RANGE_END_TIME_TYPE]
if (startDate != null && endDate != null) {
listOf(startDate, endDate)
listOf(
startDate.fromUTCToLocalTimeStamp(),
endDate.fromUTCToLocalTimeStamp()
)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.naveenapps.expensemanager.core.domain.usecase.settings.filter.datera
import com.naveenapps.expensemanager.core.common.utils.toCompleteDate
import com.naveenapps.expensemanager.core.model.DateRangeType
import com.naveenapps.expensemanager.core.model.Resource
import com.naveenapps.expensemanager.core.repository.DateRangeFilterRepository
import kotlinx.datetime.Instant
import javax.inject.Inject

class MoveDateRangeBackwardUseCase @Inject constructor(
private val dateRangeFilterRepository: com.naveenapps.expensemanager.core.repository.DateRangeFilterRepository,
private val dateRangeFilterRepository: DateRangeFilterRepository,
) {

suspend operator fun invoke(type: DateRangeType): Resource<Boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,50 @@ class MoveDateRangeForwardUseCase @Inject constructor(
}
}

fun Instant.addRespectiveFrame(type: DateRangeType): Instant {
fun Instant.addRespectiveFrame(
type: DateRangeType,
timeZone: TimeZone = TimeZone.currentSystemDefault()
): Instant {
return when (type) {
DateRangeType.TODAY -> {
this.plus(1, DateTimeUnit.DAY, TimeZone.UTC)
this.plus(1, DateTimeUnit.DAY, timeZone)
}

DateRangeType.THIS_WEEK -> {
this.plus(1, DateTimeUnit.WEEK, TimeZone.UTC)
this.plus(1, DateTimeUnit.WEEK, timeZone)
}

DateRangeType.THIS_MONTH -> {
this.plus(1, DateTimeUnit.MONTH, TimeZone.UTC)
this.plus(1, DateTimeUnit.MONTH, timeZone)
}

DateRangeType.THIS_YEAR -> {
this.plus(1, DateTimeUnit.YEAR, TimeZone.UTC)
this.plus(1, DateTimeUnit.YEAR, timeZone)
}

else -> this
}
}

fun Instant.minusRespectiveFrame(type: DateRangeType): Instant {
fun Instant.minusRespectiveFrame(
type: DateRangeType,
timeZone: TimeZone = TimeZone.currentSystemDefault()
): Instant {
return when (type) {
DateRangeType.TODAY -> {
this.minus(1, DateTimeUnit.DAY, TimeZone.UTC)
this.minus(1, DateTimeUnit.DAY, timeZone)
}

DateRangeType.THIS_WEEK -> {
this.minus(1, DateTimeUnit.WEEK, TimeZone.UTC)
this.minus(1, DateTimeUnit.WEEK, timeZone)
}

DateRangeType.THIS_MONTH -> {
this.minus(1, DateTimeUnit.MONTH, TimeZone.UTC)
this.minus(1, DateTimeUnit.MONTH, timeZone)
}

DateRangeType.THIS_YEAR -> {
this.minus(1, DateTimeUnit.YEAR, TimeZone.UTC)
this.minus(1, DateTimeUnit.YEAR, timeZone)
}

else -> this
Expand Down

0 comments on commit 5be3f4b

Please sign in to comment.