Skip to content

Commit

Permalink
Fixed calculate date range for Statistics screen
Browse files Browse the repository at this point in the history
  • Loading branch information
feelsoftware committed Nov 4, 2022
1 parent 7374906 commit 8dee3c8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
2 changes: 2 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/build
release.jks
*/*.aab
*/*.apk
*/output-metadata.json
*.pepk
google-services.json
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId "com.feelsoftware.feelfine"
minSdkVersion 23
targetSdkVersion 33
versionCode 106
versionName "1.0.2"
versionCode 107
versionName "1.0.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.feelsoftware.feelfine.utils.ActivityEngine
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInStatusCodes
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.common.api.CommonStatusCodes
import com.jakewharton.rxrelay3.PublishRelay
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
Expand Down Expand Up @@ -110,8 +111,16 @@ class GoogleFitPermissionManager(
Timber.e(error, "Failed to update profile")
})
} catch (error: ApiException) {
Timber.e(error, "onPermissionResult error")
if (error.statusCode != GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {
val ignoredErrorCodes = listOf(
GoogleSignInStatusCodes.SIGN_IN_CANCELLED,
CommonStatusCodes.CANCELED,
)
if (!ignoredErrorCodes.contains(error.statusCode)) {
Timber.e(
error,
"Failed to sign in, ${GoogleSignInStatusCodes.getStatusCodeString(error.statusCode)}"
)

activityEngine.activity?.apply {
showErrorDialog(
title = getString(R.string.sign_in),
Expand All @@ -122,6 +131,7 @@ class GoogleFitPermissionManager(
)
}
}

hasPermissionRelay.accept(false)
permissionRequest.accept(false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class GoogleFitRepository(
endTime: Date,
request: AggregateRequest
): Single<AggregateResponse> = Single.create { emitter ->
if (startTime.after(endTime)) {
emitter.onError(IllegalArgumentException("Invalid date range, [$startTime, $endTime]"))
return@create
}

val activity = activityEngine.activity ?: run {
if (emitter.isDisposed) return@create
emitter.onError(IllegalStateException("Activity is null"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class MockFitRepository : FitRepository {
endTime: Date,
generator: (date: Date) -> T
): Single<List<T>> {
if (startTime.after(endTime)) {
return Single.error(IllegalArgumentException("Invalid date range, [$startTime, $endTime]"))
}

val hours = TimeUnit.HOURS.convert(endTime.time - startTime.time, TimeUnit.MILLISECONDS)
val days = (hours / 24 + 1).toInt()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,15 @@ class StatisticViewModel(

isWeekStyleData.map { isWeekStyle ->
if (isWeekStyle) {
val minDow = calendar.getActualMinimum(Calendar.DAY_OF_WEEK)
val maxDow = calendar.getActualMaximum(Calendar.DAY_OF_WEEK)
val (startTime, endTime) = getWeekDates(date)

calendar.set(Calendar.DAY_OF_WEEK, minDow)
calendar.time = startTime
val from = calendar.get(Calendar.DAY_OF_MONTH)
val fromMonth = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.getDefault()
)

calendar.set(Calendar.DAY_OF_WEEK, maxDow)
calendar.time = endTime
val to = calendar.get(Calendar.DAY_OF_MONTH)
val toMonth = calendar.getDisplayName(
Calendar.MONTH, Calendar.SHORT, Locale.getDefault()
Expand All @@ -152,7 +151,7 @@ class StatisticViewModel(
"$from $fromMonth - $to $toMonth"
}
} else {
calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())
SimpleDateFormat("LLLL", Locale.getDefault()).format(calendar.time)
}
}
}
Expand Down Expand Up @@ -257,17 +256,20 @@ class StatisticViewModel(
private fun getWeekDates(date: Date): Pair<Date, Date> {
val startDate = Calendar.getInstance().apply {
time = date
set(Calendar.DAY_OF_WEEK, getMinimum(Calendar.DAY_OF_WEEK))
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.DAY_OF_WEEK, firstDayOfWeek)
set(Calendar.HOUR_OF_DAY, getActualMinimum(Calendar.HOUR_OF_DAY))
set(Calendar.MINUTE, getActualMinimum(Calendar.MINUTE))
set(Calendar.SECOND, getActualMinimum(Calendar.SECOND))
set(Calendar.MILLISECOND, getActualMinimum(Calendar.MILLISECOND))
}.time
val endDate = Calendar.getInstance().apply {
time = date
set(Calendar.DAY_OF_WEEK, getMaximum(Calendar.DAY_OF_WEEK))
set(Calendar.HOUR_OF_DAY, 23)
set(Calendar.MINUTE, 59)
set(Calendar.SECOND, 59)
time = startDate
add(Calendar.WEEK_OF_YEAR, 1)
add(Calendar.DAY_OF_WEEK, -1)
set(Calendar.HOUR_OF_DAY, getActualMaximum(Calendar.HOUR_OF_DAY))
set(Calendar.MINUTE, getActualMaximum(Calendar.MINUTE))
set(Calendar.SECOND, getActualMaximum(Calendar.SECOND))
set(Calendar.MILLISECOND, getActualMaximum(Calendar.MILLISECOND))
}.time
return startDate to endDate
}
Expand All @@ -276,16 +278,18 @@ class StatisticViewModel(
val startDate = Calendar.getInstance().apply {
time = date
set(Calendar.DAY_OF_MONTH, getMinimum(Calendar.DAY_OF_MONTH))
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.HOUR_OF_DAY, getActualMinimum(Calendar.HOUR_OF_DAY))
set(Calendar.MINUTE, getActualMinimum(Calendar.MINUTE))
set(Calendar.SECOND, getActualMinimum(Calendar.SECOND))
set(Calendar.MILLISECOND, getActualMinimum(Calendar.MILLISECOND))
}.time
val endDate = Calendar.getInstance().apply {
time = date
set(Calendar.DAY_OF_MONTH, getMaximum(Calendar.DAY_OF_MONTH))
set(Calendar.HOUR_OF_DAY, 23)
set(Calendar.MINUTE, 59)
set(Calendar.SECOND, 59)
set(Calendar.HOUR_OF_DAY, getActualMaximum(Calendar.HOUR_OF_DAY))
set(Calendar.MINUTE, getActualMaximum(Calendar.MINUTE))
set(Calendar.SECOND, getActualMaximum(Calendar.SECOND))
set(Calendar.MILLISECOND, getActualMaximum(Calendar.MILLISECOND))
}.time
return startDate to endDate
}
Expand Down

0 comments on commit 8dee3c8

Please sign in to comment.