-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract logic for calculating neighbouring periods
- Loading branch information
1 parent
8fac94a
commit d3d8364
Showing
10 changed files
with
185 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
myExpenses/src/main/java/org/totschnig/myexpenses/util/GroupingNavigator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.totschnig.myexpenses.util | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import org.totschnig.myexpenses.model.Grouping | ||
import org.totschnig.myexpenses.model.Grouping.DAY | ||
import org.totschnig.myexpenses.model.Grouping.MONTH | ||
import org.totschnig.myexpenses.model.Grouping.NONE | ||
import org.totschnig.myexpenses.model.Grouping.WEEK | ||
import org.totschnig.myexpenses.viewmodel.data.DateInfo | ||
import org.totschnig.myexpenses.viewmodel.data.DateInfoExtra | ||
|
||
@Parcelize | ||
data class GroupingInfo( | ||
val grouping: Grouping = NONE, | ||
val year: Int = -1, | ||
val second: Int = -1 | ||
) : Parcelable | ||
|
||
object GroupingNavigator { | ||
fun current(grouping: Grouping, dateInfo: DateInfo) = with(dateInfo) { | ||
GroupingInfo( | ||
grouping = grouping, | ||
year = when (grouping) { | ||
WEEK -> yearOfWeekStart | ||
MONTH -> yearOfMonthStart | ||
else -> year | ||
}, | ||
second = when (grouping) { | ||
DAY -> day | ||
WEEK -> week | ||
MONTH -> month | ||
else -> 0 | ||
} | ||
) | ||
} | ||
|
||
fun nextYear(groupingInfo: GroupingInfo) = groupingInfo.copy(year = groupingInfo.year + 1) | ||
|
||
fun next(groupingInfo: GroupingInfo, dateInfo: DateInfoExtra): GroupingInfo { | ||
val nextSecond = groupingInfo.second + 1 | ||
val overflow = nextSecond > dateInfo.maxValue | ||
return groupingInfo.copy( | ||
year = if (overflow) groupingInfo.year + 1 else groupingInfo.year, | ||
second = if (overflow) groupingInfo.grouping.minValue else nextSecond | ||
) | ||
} | ||
|
||
fun previousYear(groupingInfo: GroupingInfo) = groupingInfo.copy(year = groupingInfo.year - 1) | ||
|
||
fun previous(groupingInfo: GroupingInfo, dateInfo: DateInfoExtra): GroupingInfo { | ||
val nextSecond = groupingInfo.second - 1 | ||
val underflow = nextSecond < groupingInfo.grouping.minValue | ||
return groupingInfo.copy( | ||
year = if (underflow) groupingInfo.year - 1 else groupingInfo.year, | ||
second = if (underflow) dateInfo.maxValue else nextSecond | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
myExpenses/src/main/java/org/totschnig/myexpenses/viewmodel/data/BudgetProgress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.