Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
manto89 committed Jul 8, 2021
1 parent 0ad7358 commit 1262f85
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down
48 changes: 32 additions & 16 deletions app/src/main/java/io/github/z3r0c00l_2k/aquadroid/StatsActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand All @@ -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<Entry>()
var entries = ArrayList<Entry>()
val dateArray = ArrayList<String>()

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()
}
Expand Down Expand Up @@ -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"
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -246,7 +248,9 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() {

sqliteHelper.updateTotalIntake(
AppUtils.getCurrentDate()!!,
df.format(totalIntake).toInt()
df.format(totalIntake).toInt(),
wakeupTime,
sleepingTime
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -83,15 +145,41 @@ class SqliteHelper(val context: Context) : SQLiteOpenHelper(
return 0
}

fun getStatsInRange(start: Date, end: Date, wakeUpTime: Long, sleepingTime: Long): ArrayList<Entry>{

// 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<Entry>()
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
return db.rawQuery(selectQuery, null)

}

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.z3r0c00l_2k.aquadroid.utils

import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.util.*


Expand All @@ -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"
Expand Down

0 comments on commit 1262f85

Please sign in to comment.