Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ allow for collection of multiple trees per day #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.LocalDate
@Dao
interface DayDao {

@Query("SELECT steps / goal as treeAmount FROM day WHERE steps >= goal")
@Query("SELECT SUM(steps/goal) FROM day WHERE steps >= goal")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the WHERE steps >= goal. The SUM will only count collected trees. This is because we're dividing integers here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least I think so. Please confirm it still works as expected.

fun getTreeCount(): Flow<Int>

@Query("SELECT * FROM day ORDER BY date ASC LIMIT 1")
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/pl/bk20/forest/core/domain/model/Day.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ data class Day(
get() = run {
steps * 0.1925 / 1000.0
}

val treesCollected
get() = run {
steps / goal
}
}

fun Day.Companion.of(date: LocalDate, settings: Settings, steps: Int = 0): Day {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class StatsSummary(
}

fun StatsSummary.Companion.of(days: List<Day>): StatsSummary {
val treesCollected = days.sumOf { it.steps / it.goal }
val treesCollected = days.sumOf { it.treesCollected }
val stepsTaken = days.sumOf { it.steps.toLong() }
val calorieBurned = days.sumOf { it.calorieBurned }
val distanceTravelled = days.sumOf { it.distanceTravelled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
Expand Down Expand Up @@ -41,6 +40,9 @@ class StatsDetailsFragment : Fragment() {
val stepsText = resources.getQuantityString(
R.plurals.step_count_format, stepsTaken, stepsTaken
)
val treesText = resources.getQuantityString(
R.plurals.trees_collected_format, treesCollected, treesCollected
)
val calorieText = getString(
R.string.calorie_burned_format, calorieBurned
)
Expand All @@ -52,9 +54,7 @@ class StatsDetailsFragment : Fragment() {
)
binding.apply {
textStepCount.text = stepsText
textTreeCollected.text = resources.getQuantityString(
R.plurals.trees_collected_format, treesCollected, treesCollected
)
textTreesCollected.text = treesText
textCalorieBurned.text = calorieText
textDistanceTravelled.text = distanceText
textCarbonDioxideSaved.text = carbonDioxideText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class StatsDetailsViewModel(
_day.value = day.value.copy(
date = it.date,
stepsTaken = it.steps,
treesCollected = it.steps / it.goal,
treesCollected = it.treesCollected,
calorieBurned = it.calorieBurned.roundToInt(),
distanceTravelled = it.distanceTravelled,
carbonDioxideSaved = it.carbonDioxideSaved
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/fragment_stats_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
app:tint="?attr/colorOnSecondary" />

<TextView
android:id="@+id/text_tree_collected"
android:id="@+id/text_trees_collected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
Expand All @@ -138,8 +138,8 @@
android:text="@string/trees_collected"
android:textAppearance="?attr/textAppearanceBodyMedium"
app:layout_constraintBottom_toBottomOf="@+id/view_tree_background"
app:layout_constraintStart_toStartOf="@+id/text_tree_collected"
app:layout_constraintTop_toBottomOf="@+id/text_tree_collected"
app:layout_constraintStart_toStartOf="@+id/text_trees_collected"
app:layout_constraintTop_toBottomOf="@+id/text_trees_collected"
app:layout_constraintVertical_chainStyle="packed" />

</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down