0

we are developing a water tracking app in Kotlin / Android Studio. Unfortunately, the achievements (reaching a daily water intake goal for x days in a row) are not being crossed off, I think? Could you help me? Thanks a lot!

Respective functions from AchievementsFragment.kt

    override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentAchievementsBinding.inflate(inflater, container, false)

    // SharedPreferences
    sharedPreferences = requireActivity().getSharedPreferences("app_preferences", Context.MODE_PRIVATE)

    return binding.root
}

// Setting up database operations and listeners in onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val database = AppDatabase.getDatabase(requireContext())
    val waterDao = database.waterDao()

    // Getting the daily water goal
    val dailyWaterGoal = sharedPreferences.getInt("dailyGoal", 2000)

    binding.infoButton.setOnClickListener {
        showInfoDialog()
    }

    // Fetching goal days and updating achievements
    viewLifecycleOwner.lifecycleScope.launch {
        val lastDayIntake = waterDao.getLastDayData()
        var streak = sharedPreferences.getInt("currentStreak", 0)

        if (lastDayIntake?.amount ?: 0 >= dailyWaterGoal) {
            streak++
            sharedPreferences.edit().putInt("currentStreak", streak).apply()
        } else {
            streak = 0
            sharedPreferences.edit().putInt("currentStreak", 0).apply()
        }
        updateAchievements(streak)
    }
}

// Method to update achievements based on number of days
private fun updateAchievements(numDays: Int) {
    updateAchievement(numDays, 3, binding.textAchievement1)
    updateAchievement(numDays, 5, binding.textAchievement2)
    updateAchievement(numDays, 10, binding.textAchievement3)
    updateAchievement(numDays, 30, binding.textAchievement4)
    updateAchievement(numDays, 180, binding.textAchievement5)
}

// Method to update achievements
private fun updateAchievement(numDays: Int, requiredDays: Int, textView: TextView) {
    if (numDays >= requiredDays) {
        textView.paintFlags = textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
    } else {
        textView.paintFlags = textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
    }
}

WaterDao.kt

interface WaterDao {

@Query("SELECT * FROM WaterIntake ORDER BY day, month, year DESC LIMIT 1")
suspend fun getLastDayData(): WaterIntake?

}

data class AmountSum( val date: Int, val totalAmount: Int )

  • Are you able to figure out where exactly the logic is breaking? E.g., does the execution reach the `textView.paintFlags = textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG` line, and hence, this is the line of code that's not working? – Egor Aug 30 '23 at 16:41
  • if you share the entire your code hard to read and understand. share the specific code which is not working – Ranjithkumar Aug 30 '23 at 17:30
  • @Egor thats my problem... i am not sure if the logic to increase streak, cross off accordingly and break in case daily intake goal is not met works. – user15991497 Aug 30 '23 at 20:37
  • From the code posted, I also do not see anything that strikes me as an error logic wise. A suspicious part may be the DB `Order by` clause, take for example day 1 of month 02 will be lower than day 30 of month 01 which breaks a little the intent of it being "last day", so I would prefer to sort it as `YEAR DESC, MONTH DESC, DAY DESC`. The query will look into "known registered dates" ignoring gaps between dates, so that might become a possible bug (not related to the original question though) – Jhilton Aug 30 '23 at 23:30
  • @Jhilton Yeah that makes sense. Right now, however, only the toal number of consecutive days is counter. for example august 05-08 the daily goal was reached. yet the achievement is still crossed off today. additionally, when the goal is reach on august 14-15 too, the second achievement is now also crossed off. how do i implement that it must be consecutive days with no break inbetween? – user15991497 Aug 31 '23 at 06:44
  • `i am not sure if the logic to increase streak, cross off accordingly and break in case daily intake goal is not met works` - can you set breakpoints and use the debugger to trace the code and observe the results of executing every line? You have the benefit of having that code in your IDE where you can execute it, people here can only eyeball it and make guesses about where the issue is. – Egor Aug 31 '23 at 14:26

0 Answers0