From 1262f85f204dd02a1b3b0e1f533f2ca439961cc4 Mon Sep 17 00:00:00 2001 From: manto89 Date: Thu, 8 Jul 2021 18:41:27 +0200 Subject: [PATCH] z3r0c00l-2k/AquaDroid#60 Added getIntake based off wakeup and sleeping time Changed statistics values based off wakeup and sleeping time Changed statistics to get only the last 7 days --- .../z3r0c00l_2k/aquadroid/MainActivity.kt | 7 +- .../z3r0c00l_2k/aquadroid/StatsActivity.kt | 48 +++++--- .../fragments/BottomSheetFragment.kt | 8 +- .../aquadroid/helpers/NotificationHelper.kt | 2 +- .../aquadroid/helpers/SqliteHelper.kt | 114 ++++++++++++++++-- .../z3r0c00l_2k/aquadroid/utils/AppUtils.kt | 14 +++ 6 files changed, 158 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/MainActivity.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/MainActivity.kt index 5da218f..2ca6ef0 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/MainActivity.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/MainActivity.kt @@ -58,8 +58,9 @@ class MainActivity : AppCompatActivity() { fun updateValues() { totalIntake = sharedPref.getInt(AppUtils.TOTAL_INTAKE, 0) - - inTook = sqliteHelper.getIntook(dateNow) + val wakeUpTime = sharedPref.getLong(AppUtils.WAKEUP_TIME, 800) + val sleepingTime = sharedPref.getLong(AppUtils.SLEEPING_TIME_KEY,2300) + inTook = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!, wakeUpTime, sleepingTime) setWaterLevel(inTook, totalIntake) } @@ -102,7 +103,7 @@ class MainActivity : AppCompatActivity() { fabAdd.setOnClickListener { if (selectedOption != null) { if ((inTook * 100 / totalIntake) <= 140) { - if (sqliteHelper.addIntook(dateNow, selectedOption!!) > 0) { + if (sqliteHelper.addIntook(AppUtils.getCurrentDateTime(), selectedOption!!, totalIntake) > 0) { inTook += selectedOption!! setWaterLevel(inTook, totalIntake) diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/StatsActivity.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/StatsActivity.kt index a23c4e6..46805f8 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/StatsActivity.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/StatsActivity.kt @@ -1,7 +1,6 @@ package io.github.z3r0c00l_2k.aquadroid import android.content.SharedPreferences -import android.database.Cursor import android.graphics.Color import android.os.Bundle import android.widget.Toast @@ -17,6 +16,9 @@ import io.github.z3r0c00l_2k.aquadroid.helpers.SqliteHelper import io.github.z3r0c00l_2k.aquadroid.utils.AppUtils import io.github.z3r0c00l_2k.aquadroid.utils.ChartXValueFormatter import kotlinx.android.synthetic.main.activity_stats.* +import java.text.SimpleDateFormat +import java.util.* +import kotlin.collections.ArrayList import kotlin.math.max @@ -33,27 +35,40 @@ class StatsActivity : AppCompatActivity() { sharedPref = getSharedPreferences(AppUtils.USERS_SHARED_PREF, AppUtils.PRIVATE_MODE) sqliteHelper = SqliteHelper(this) + val wakeUpTime = sharedPref.getLong(AppUtils.WAKEUP_TIME, 0) + val sleepingTime = sharedPref.getLong(AppUtils.SLEEPING_TIME_KEY, 0) btnBack.setOnClickListener { finish() } - val entries = ArrayList() + var entries = ArrayList() val dateArray = ArrayList() - val cursor: Cursor = sqliteHelper.getAllStats() - - if (cursor.moveToFirst()) { - - for (i in 0 until cursor.count) { - dateArray.add(cursor.getString(1)) - val percent = cursor.getInt(2) / cursor.getInt(3).toFloat() * 100 - totalPercentage += percent - totalGlasses += cursor.getInt(2) - entries.add(Entry(i.toFloat(), percent)) - cursor.moveToNext() - } +// val cursor: Cursor = sqliteHelper.getAllStats() + var start = Calendar.getInstance() + var end = Calendar.getInstance() + start.roll(Calendar.DAY_OF_YEAR, -7) + val values = sqliteHelper.getStatsInRange(start.time, end.time, wakeUpTime, sleepingTime) + for (day in start.time.time..end.time.time step 86400000){ + val c = Calendar.getInstance() + c.timeInMillis = day + dateArray.add(SimpleDateFormat("dd-MM").format(c.time)) + } + if (values.size > 0){ + entries = values +// if (cursor.moveToFirst()) { +// +// for (i in 0 until cursor.count) { +// dateArray.add(cursor.getString(1)) +// val percent = cursor.getInt(2) / cursor.getInt(3).toFloat() * 100 +// totalPercentage += percent +// totalGlasses += cursor.getInt(2) +// entries.add(Entry(i.toFloat(), percent)) +// cursor.moveToNext() +// } +// } else { Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show() } @@ -108,10 +123,11 @@ class StatsActivity : AppCompatActivity() { chart.data = lineData chart.invalidate() + val remaining = sharedPref.getInt( AppUtils.TOTAL_INTAKE, 0 - ) - sqliteHelper.getIntook(AppUtils.getCurrentDate()!!) + ) - sqliteHelper.getIntook(AppUtils.getCurrentDate()!!, wakeUpTime, sleepingTime) if (remaining > 0) { remainingIntake.text = "$remaining ml" @@ -125,7 +141,7 @@ class StatsActivity : AppCompatActivity() { ) } ml" - val percentage = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!) * 100 / sharedPref.getInt( + val percentage = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!, wakeUpTime, sleepingTime) * 100 / sharedPref.getInt( AppUtils.TOTAL_INTAKE, 0 ) diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/fragments/BottomSheetFragment.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/fragments/BottomSheetFragment.kt index cbb525c..3913e1d 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/fragments/BottomSheetFragment.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/fragments/BottomSheetFragment.kt @@ -236,7 +236,9 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() { sqliteHelper.updateTotalIntake( AppUtils.getCurrentDate()!!, - customTarget.toInt() + customTarget.toInt(), + wakeupTime, + sleepingTime ) } else { val totalIntake = AppUtils.calculateIntake(weight.toInt(), workTime.toInt()) @@ -246,7 +248,9 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() { sqliteHelper.updateTotalIntake( AppUtils.getCurrentDate()!!, - df.format(totalIntake).toInt() + df.format(totalIntake).toInt(), + wakeupTime, + sleepingTime ) } diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/NotificationHelper.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/NotificationHelper.kt index 098ec44..3943c61 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/NotificationHelper.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/NotificationHelper.kt @@ -99,7 +99,7 @@ class NotificationHelper(val ctx: Context) { if (startTimestamp == 0L || stopTimestamp == 0L || totalIntake == 0) return false - val percent = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!) * 100 / totalIntake + val percent = sqliteHelper.getIntook(AppUtils.getCurrentDate()!!, startTimestamp, stopTimestamp) * 100 / totalIntake val now = Calendar.getInstance().time diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/SqliteHelper.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/SqliteHelper.kt index 3233148..1f98e77 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/SqliteHelper.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/helpers/SqliteHelper.kt @@ -5,6 +5,10 @@ import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import com.github.mikephil.charting.data.Entry +import io.github.z3r0c00l_2k.aquadroid.utils.AppUtils +import java.text.SimpleDateFormat +import java.util.* class SqliteHelper(val context: Context) : SQLiteOpenHelper( context, @@ -36,6 +40,11 @@ class SqliteHelper(val context: Context) : SQLiteOpenHelper( onCreate(db) } + override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { + db!!.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS) + onCreate(db) + } + fun addAll(date: String, intook: Int, totalintake: Int): Long { if (checkExistance(date) == 0) { val values = ContentValues() @@ -50,26 +59,79 @@ class SqliteHelper(val context: Context) : SQLiteOpenHelper( return -1 } - fun getIntook(date: String): Int { - val selectQuery = "SELECT $KEY_INTOOK FROM $TABLE_STATS WHERE $KEY_DATE = ?" + fun getIntook(date: String, wakeUpTime: Long, sleepingTime: Long ): Int { + val requestedDate = SimpleDateFormat("dd-MM-yyyy").parse(date) + val wakeUpTimeString = AppUtils.formatTime(Date(wakeUpTime)) + val wakeUpFormatted = SimpleDateFormat("yyyy-MM-dd").format(requestedDate.time).plus("T").plus(wakeUpTimeString) + val sleepingTimeString = AppUtils.formatTime(Date(sleepingTime)) + var sleepingTimeFormatted = SimpleDateFormat("yyyy-MM-dd").format(requestedDate.time).plus("T").plus(sleepingTimeString) + if (sleepingTime < wakeUpTime){ + var c = Calendar.getInstance() + c.time = requestedDate + c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1) + val sleepingDayFormatted = SimpleDateFormat("yyyy-MM-dd").format(c.time) + sleepingTimeFormatted = sleepingDayFormatted.plus("T").plus(sleepingTimeString) + } + val selectQuery = "SELECT $KEY_INTOOK FROM $TABLE_STATS WHERE $KEY_DATE BETWEEN ? AND ?" val db = this.readableDatabase - db.rawQuery(selectQuery, arrayOf(date)).use { + var ret = 0 + db.rawQuery(selectQuery, arrayOf(wakeUpFormatted, sleepingTimeFormatted)).use { if (it.moveToFirst()) { - return it.getInt(it.getColumnIndex(KEY_INTOOK)) + for (i in 0 until it.count){ + ret += it.getInt(it.getColumnIndex(KEY_INTOOK)) + it.moveToNext() + } } } - return 0 + return ret } - fun addIntook(date: String, selectedOption: Int): Int { - val intook = getIntook(date) - val db = this.writableDatabase - val contentValues = ContentValues() - contentValues.put(KEY_INTOOK, intook + selectedOption) + fun getTotalIntake(date: String, wakeUpTime: Long, sleepingTime: Long): Int{ + + val requestedDate = SimpleDateFormat("dd-MM-yyyy").parse(date) + val wakeUpTimeString = AppUtils.formatTime(Date(wakeUpTime)) + val wakeUpFormatted = SimpleDateFormat("yyyy-MM-dd").format(requestedDate.time).plus("T").plus(wakeUpTimeString) + val sleepingTimeString = AppUtils.formatTime(Date(sleepingTime)) + var sleepingTimeFormatted = SimpleDateFormat("yyyy-MM-dd").format(requestedDate.time).plus("T").plus(sleepingTimeString) + if (sleepingTime < wakeUpTime){ + var c = Calendar.getInstance() + c.time = requestedDate + c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 1) + val sleepingDayFormatted = SimpleDateFormat("yyyy-MM-dd").format(c.time) + sleepingTimeFormatted = sleepingDayFormatted.plus("T").plus(sleepingTimeString) + } + val selectQuery = "SELECT MAX($KEY_TOTAL_INTAKE) FROM $TABLE_STATS WHERE $KEY_DATE BETWEEN ? AND ?" + val db = this.readableDatabase + var ret = 0 + db.rawQuery(selectQuery, arrayOf(wakeUpFormatted, sleepingTimeFormatted)).use { + if (it.moveToFirst()) { + ret += it.getInt(0) +// for (i in 0 until it.count){ +// ret += it.getInt(it.getColumnIndex(KEY_INTOOK)) +// it.moveToNext() +// } + } + } + return ret + } - val response = db.update(TABLE_STATS, contentValues, "$KEY_DATE = ?", arrayOf(date)) + fun addIntook(date: String, selectedOption: Int, totalintake: Int): Long { + val values = ContentValues() + values.put(KEY_DATE, date) + values.put(KEY_INTOOK, selectedOption) + values.put(KEY_TOTAL_INTAKE, totalintake) + val db = this.writableDatabase + val response = db.insert(TABLE_STATS, null, values) db.close() return response +// val intook = getIntook(date) +// val db = this.writableDatabase +// val contentValues = ContentValues() +// contentValues.put(KEY_INTOOK, intook + selectedOption) +// +// val response = db.update(TABLE_STATS, contentValues, "$KEY_DATE = ?", arrayOf(date)) +// db.close() +// return response } fun checkExistance(date: String): Int { @@ -83,6 +145,32 @@ class SqliteHelper(val context: Context) : SQLiteOpenHelper( return 0 } + fun getStatsInRange(start: Date, end: Date, wakeUpTime: Long, sleepingTime: Long): ArrayList{ + +// var s = Calendar.getInstance() +// s.time = start +// val startDay = s.get(Calendar.DAY_OF_YEAR) +// +// var e = Calendar.getInstance() +// e.time = end +// val endDay = e.get(Calendar.Day) + var ret = arrayListOf() + var counter = 0 + for (i in start.time..end.time step 86400000){ + val c = Calendar.getInstance() + c.timeInMillis = i + val intook = getIntook(SimpleDateFormat("dd-MM-yyyy").format(c.time), wakeUpTime, sleepingTime) + val total = getTotalIntake(SimpleDateFormat("dd-MM-yyyy").format(c.time),wakeUpTime, sleepingTime) + var percentage = 0.0f + if(total > 0){ + percentage = (intook/total.toFloat()) * 100 + } + ret.add(Entry(counter.toFloat(), percentage)) + counter++ + } + return ret + } + fun getAllStats(): Cursor { val selectQuery = "SELECT * FROM $TABLE_STATS" val db = this.readableDatabase @@ -90,8 +178,8 @@ class SqliteHelper(val context: Context) : SQLiteOpenHelper( } - fun updateTotalIntake(date: String, totalintake: Int): Int { - val intook = getIntook(date) + fun updateTotalIntake(date: String, totalintake: Int, wakeUpTime: Long, sleepingTime: Long): Int { + val intook = getIntook(date, wakeUpTime, sleepingTime) val db = this.writableDatabase val contentValues = ContentValues() contentValues.put(KEY_TOTAL_INTAKE, totalintake) diff --git a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/utils/AppUtils.kt b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/utils/AppUtils.kt index 7f95afa..311988d 100644 --- a/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/utils/AppUtils.kt +++ b/app/src/main/java/io/github/z3r0c00l_2k/aquadroid/utils/AppUtils.kt @@ -1,6 +1,7 @@ package io.github.z3r0c00l_2k.aquadroid.utils import java.text.SimpleDateFormat +import java.time.LocalDateTime import java.util.* @@ -18,6 +19,19 @@ class AppUtils { return df.format(c) } + fun getCurrentDateTime(): String { + val c = Calendar.getInstance().time + return formatDateIso8601(c) + } + fun formatDateIso8601(datetime: Date): String{ + val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + return df.format(datetime) + } + fun formatTime(datetime: Date) : String{ + val df = SimpleDateFormat("HH:mm") + return df.format(datetime) + } + val USERS_SHARED_PREF = "user_pref" val PRIVATE_MODE = 0 val WEIGHT_KEY = "weight"