Skip to content

Commit

Permalink
[#7] Add unit tests to CalendarViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz800 committed Nov 17, 2023
1 parent 25d91d3 commit 59bfb17
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 141 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ permissions:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
- name: Set up JDK 18
- uses: actions/setup-java@v3
with:
java-version: '11'
java-version: 17
distribution: 'temurin'
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
- uses: gradle/gradle-build-action@v2
with:
arguments: build
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dependencies {
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation 'androidx.activity:activity-compose:1.8.0'
implementation 'androidx.activity:activity-compose:1.8.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
174 changes: 87 additions & 87 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link";;
else
PRG=`dirname "$PRG"`"/$link"
fi
done
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

APP_NAME="Gradle"
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
Expand All @@ -66,111 +66,111 @@ msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar


# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`

JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option

if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=`expr $i + 1`
done
case $i in
0) set -- ;
1) set -- "$args0" ;
2) set -- "$args0" "$args1" ;
3) set -- "$args0" "$args1" "$args2" ;
4) set -- "$args0" "$args1" "$args2" "$args3" ;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;
esac
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option

if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=`expr $i + 1`
done
case $i in
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

# Escape application args
save () {
Expand Down
17 changes: 7 additions & 10 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ android {
}

dependencies {

implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
Expand All @@ -45,15 +44,13 @@ dependencies {
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "com.google.accompanist:accompanist-pager:0.23.1"
implementation "com.google.accompanist:accompanist-flowlayout:0.26.4-beta"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
// lifecycle
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2"


// Compose testing dependencies
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$rootProject.compose_version"
testImplementation "junit:junit:4.13.2"
testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test-jvm:1.6.4"
androidTestImplementation "androidx.test.ext:junit:1.1.5"
androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"
androidTestImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.compose_version"
}
35 changes: 24 additions & 11 deletions library/src/main/java/com/mabn/calendarlibrary/CalendarViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.mabn.calendarlibrary
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mabn.calendarlibrary.core.CalendarIntent
import com.mabn.calendarlibrary.core.DateTimeConstants
import com.mabn.calendarlibrary.core.Period
import com.mabn.calendarlibrary.core.RelativePosition
import com.mabn.calendarlibrary.utils.*
import com.mabn.calendarlibrary.utils.getNextDates
import com.mabn.calendarlibrary.utils.getRemainingDatesInWeek
Expand All @@ -15,6 +17,7 @@ import java.time.LocalDate
import java.time.YearMonth

class CalendarViewModel : ViewModel() {

private val _visibleDates =
MutableStateFlow(
calculateCollapsedCalendarDays(
Expand All @@ -29,12 +32,16 @@ class CalendarViewModel : ViewModel() {
val currentMonth: StateFlow<YearMonth>
get() = calendarExpanded.zip(visibleDates) { isExpanded, dates ->
if (isExpanded) {
dates[1][dates[1].size / 2].yearMonth()
dates[RelativePosition.CURRENT.ordinal][dates[RelativePosition.CURRENT.ordinal].size / 2].yearMonth()
} else {
if (dates[1].count { it.month == dates[1][0].month } > 3)
dates[1][0].yearMonth()
else
dates[1][dates[1].size - 1].yearMonth()
if (dates[RelativePosition.CURRENT.ordinal].count { it.month == dates[RelativePosition.CURRENT.ordinal].first().month } > RelativePosition.values().size
) {
dates[RelativePosition.CURRENT.ordinal].first()
.yearMonth()
} else {
dates[RelativePosition.CURRENT.ordinal].last()
.yearMonth()
}
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, LocalDate.now().yearMonth())

Expand All @@ -47,11 +54,14 @@ class CalendarViewModel : ViewModel() {
when (intent) {
CalendarIntent.ExpandCalendar -> {
calculateCalendarDates(
startDate = currentMonth.value.minusMonths(1).atDay(1),
startDate = currentMonth.value
.minusMonths(1)
.atDay(1),
period = Period.MONTH
)
_calendarExpanded.value = true
}

CalendarIntent.CollapseCalendar -> {
calculateCalendarDates(
startDate = calculateCollapsedCalendarVisibleStartDay()
Expand All @@ -61,9 +71,11 @@ class CalendarViewModel : ViewModel() {
)
_calendarExpanded.value = false
}

is CalendarIntent.LoadNextDates -> {
calculateCalendarDates(intent.startDate, intent.period)
}

is CalendarIntent.SelectDate -> {
viewModelScope.launch {
_selectedDate.emit(intent.date)
Expand All @@ -87,22 +99,23 @@ class CalendarViewModel : ViewModel() {
}

private fun calculateCollapsedCalendarVisibleStartDay(): LocalDate {
val halfOfMonth = visibleDates.value[1][visibleDates.value[1].size / 2]
val halfOfMonth =
visibleDates.value[RelativePosition.CURRENT.ordinal][visibleDates.value[RelativePosition.CURRENT.ordinal].size / 2]
val visibleMonth = YearMonth.of(halfOfMonth.year, halfOfMonth.month)
return if (selectedDate.value.month == visibleMonth.month && selectedDate.value.year == visibleMonth.year)
selectedDate.value
else visibleMonth.atDay(1)
}

private fun calculateCollapsedCalendarDays(startDate: LocalDate): Array<List<LocalDate>> {
val dates = startDate.getNextDates(21)
return Array(3) {
dates.slice(it * 7 until (it + 1) * 7)
val dates = startDate.getNextDates(RelativePosition.values().size * DateTimeConstants.DAYS_IN_WEEK)
return Array(RelativePosition.values().size) {
dates.slice(it * DateTimeConstants.DAYS_IN_WEEK until (it + 1) * DateTimeConstants.DAYS_IN_WEEK)
}
}

private fun calculateExpandedCalendarDays(startDate: LocalDate): Array<List<LocalDate>> {
val array = Array(3) { monthIndex ->
val array = Array(RelativePosition.values().size) { monthIndex ->
val monthFirstDate = startDate.plusMonths(monthIndex.toLong())
val monthLastDate = monthFirstDate.plusMonths(1).minusDays(1)
val weekBeginningDate = monthFirstDate.getWeekStartDate()
Expand Down
Loading

0 comments on commit 59bfb17

Please sign in to comment.