Skip to content

Commit

Permalink
Different color for each statistic type
Browse files Browse the repository at this point in the history
  • Loading branch information
feelsoftware committed Mar 7, 2023
1 parent d846904 commit b3913dd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ val presentationModule = module {
)
}
viewModel {
StatisticViewModel(androidContext(), get<GetFitDataUseCase>(), get<UserRepository>())
StatisticViewModel(
get<ActivityEngine>(),
androidContext(),
get<GetFitDataUseCase>(),
get<UserRepository>(),
)
}
viewModel {
ProfileViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ package com.feelsoftware.feelfine.ui.statistic

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.lifecycle.*
Expand All @@ -23,6 +27,7 @@ import com.feelsoftware.feelfine.fit.usecase.GetFitDataUseCase
import com.feelsoftware.feelfine.ui.base.BaseFragment
import com.feelsoftware.feelfine.ui.base.BaseViewModel
import com.feelsoftware.feelfine.ui.profile.DemoProfileBadge
import com.feelsoftware.feelfine.utils.ActivityEngine
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis.XAxisPosition
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition
Expand Down Expand Up @@ -112,13 +117,17 @@ class StatisticFragment : BaseFragment<StatisticViewModel>(R.layout.fragment_sta
chart.axisLeft.valueFormatter = chartData.yFormatter
chart.axisRight.isEnabled = false

chart.data = BarData(BarDataSet(chartData.data, ""))
chart.data = BarData(BarDataSet(chartData.data, "")
.apply {
color = chartData.color
})
chart.data.setValueFormatter(chartData.yFormatter)
chart.invalidate()
}
}

class StatisticViewModel(
private val activityEngine: ActivityEngine,
private val context: Context,
useCase: GetFitDataUseCase,
userRepository: UserRepository,
Expand Down Expand Up @@ -185,27 +194,36 @@ class StatisticViewModel(
when (currentCategory) {
Category.ACTIVITY -> useCase.getActivity(startTime, endTime)
.map {
it.toChartData(valueFormatter = {
Duration(it).toHoursMinutes()
}) { activityInfo ->
it.toChartData(
color = R.color.activity_primary,
valueFormatter = { value ->
Duration(value).toHoursMinutes()
}
) { activityInfo ->
activityInfo.date to activityInfo.total.minutesTotal
}
}
.toLiveData()
Category.STEPS -> useCase.getSteps(startTime, endTime)
.map {
it.toChartData(valueFormatter = {
it.toString()
}) { stepsInfo ->
it.toChartData(
color = R.color.steps_primary,
valueFormatter = { value ->
value.toString()
}
) { stepsInfo ->
stepsInfo.date to stepsInfo.count
}
}
.toLiveData()
Category.SLEEP -> useCase.getSleep(startTime, endTime)
.map {
it.toChartData(valueFormatter = {
Duration(it).toHoursMinutes()
}) { sleepInfo ->
it.toChartData(
color = R.color.sleep_primary,
valueFormatter = { value ->
Duration(value).toHoursMinutes()
}
) { sleepInfo ->
sleepInfo.date to sleepInfo.total.minutesTotal
}
}
Expand Down Expand Up @@ -317,6 +335,7 @@ class StatisticViewModel(
get() = context.getString(titleResId).lowercase(Locale.getDefault())

private fun <T> List<T>.toChartData(
@ColorRes color: Int,
valueFormatter: (Int) -> String,
mapper: (T) -> Pair<Date, Int>
): ChartData {
Expand All @@ -341,7 +360,10 @@ class StatisticViewModel(
override fun getFormattedValue(value: Float): String {
return valueFormatter(value.roundToInt())
}
}
},
color = activityEngine.activity?.let {
ContextCompat.getColor(it, color)
} ?: Color.BLUE,
)
// endregion
}
Expand All @@ -350,7 +372,8 @@ class StatisticViewModel(
data class ChartData(
val data: List<BarEntry>,
val xFormatter: ValueFormatter,
val yFormatter: ValueFormatter
val yFormatter: ValueFormatter,
@ColorInt val color: Int,
)

private enum class Category(@StringRes val titleResId: Int) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_activity_score.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pastelPink">
android:background="@color/activity_secondary">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/centralGL"
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/res/layout/fragment_current_score.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
android:fontFamily="@font/nunito_black"
android:gravity="center"
android:paddingTop="8dp"
android:text="Details"
android:text="@string/score_details"
android:textColor="@color/greyishBrown"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
Expand All @@ -152,7 +152,7 @@
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_score"
android:backgroundTint="@color/lavenderBlue"
android:backgroundTint="@color/steps_primary"
app:layout_constraintBottom_toTopOf="@+id/secondGL"
app:layout_constraintEnd_toStartOf="@+id/centerGL"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -165,7 +165,7 @@
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:fontFamily="@font/nunito_black"
android:text="Steps"
android:text="@string/statistic_category_steps"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/stepsView"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -265,7 +265,7 @@
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_score"
android:backgroundTint="@color/lavender"
android:backgroundTint="@color/sleep_primary"
app:layout_constraintBottom_toTopOf="@+id/secondGL"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/centerGL"
Expand Down Expand Up @@ -328,7 +328,7 @@
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:fontFamily="@font/nunito_black"
android:text="Sleep"
android:text="@string/statistic_category_sleep"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/sleepView"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -342,7 +342,7 @@
android:backgroundTint="@color/white"
android:fontFamily="@font/nunito_black"
android:src="@drawable/ic_arrow"
android:text="Steps"
android:text="@string/statistic_category_steps"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/sleepView"
app:layout_constraintEnd_toEndOf="parent"
Expand Down Expand Up @@ -380,7 +380,7 @@
android:layout_marginEnd="8dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_score"
android:backgroundTint="@color/powerPink"
android:backgroundTint="@color/activity_primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/centerGL"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -392,7 +392,7 @@
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:fontFamily="@font/nunito_black"
android:text="Activity"
android:text="@string/statistic_category_activity"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/activityView"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -492,7 +492,7 @@
android:layout_marginEnd="2dp"
android:layout_marginBottom="12dp"
android:background="@drawable/background_score"
android:backgroundTint="@color/sand"
android:backgroundTint="@color/mood_primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/centerGL"
Expand All @@ -504,7 +504,7 @@
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:fontFamily="@font/nunito_black"
android:text="Mood"
android:text="@string/statistic_category_mood"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/moodView"
app:layout_constraintStart_toStartOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_mood.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/sandTwo">
android:background="@color/mood_secondary">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/centralGL"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_sleep_score.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lavenderTwo">
android:background="@color/sleep_secondary">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/centralGL"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_step_score.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/powderBlue">
android:background="@color/steps_secondary">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/centralGL"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@
<color name="pastelPink">#ffc3d6</color>
<color name="gunmetal">#525a5b</color>
<color name="sandTwo">#dbe48b</color>
<color name="steps_primary">@color/lavenderBlue</color>
<color name="steps_secondary">@color/powderBlue</color>
<color name="sleep_primary">@color/lavender</color>
<color name="sleep_secondary">@color/lavenderTwo</color>
<color name="activity_primary">@color/powerPink</color>
<color name="activity_secondary">@color/pastelPink</color>
<color name="mood_primary">@color/sand</color>
<color name="mood_secondary">@color/sandTwo</color>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
you have every day mood questionnaire.
For updating your goals, please, visit profile settings</string>
<string name="mood_score">Mood score</string>
<string name="score_details">Details</string>
<string-array name="score_values">
<item>Steps</item>
<item>Sleep</item>
Expand All @@ -64,6 +65,7 @@
<string name="statistic_category_activity">Activity</string>
<string name="statistic_category_steps">Steps</string>
<string name="statistic_category_sleep">Sleep</string>
<string name="statistic_category_mood">Mood</string>
<string name="log_out">Log out</string>
<string name="error_alert_button">Close</string>
<string name="demo_profile_badge">Demo</string>
Expand Down

0 comments on commit b3913dd

Please sign in to comment.