Skip to content

Commit

Permalink
ref: Global refactoring, optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
bulkabuka committed Apr 30, 2024
1 parent 7b19317 commit 9b14939
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 434 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
36 changes: 7 additions & 29 deletions app/src/main/java/org/leftbrained/uptaskapp/AuthActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.leftbrained.uptaskapp
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.SharedPreferences
import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
Expand All @@ -19,10 +20,12 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import org.jetbrains.exposed.sql.transactions.transaction
import org.leftbrained.uptaskapp.classes.Checks.checkAuth
import org.leftbrained.uptaskapp.db.UptaskDb
import org.leftbrained.uptaskapp.db.User
import org.leftbrained.uptaskapp.db.connectToDb
import org.leftbrained.uptaskapp.ui.theme.AppTheme
import org.leftbrained.uptaskapp.viewmodel.UserViewModel

fun Context.findActivity(): Activity {
var context = this
Expand All @@ -35,6 +38,7 @@ fun Context.findActivity(): Activity {

@Composable
fun AuthActivity(navController: NavController) {
val userVm = remember { UserViewModel() }
var login by remember {
mutableStateOf("")
}
Expand Down Expand Up @@ -104,37 +108,11 @@ fun AuthActivity(navController: NavController) {
maxLines = 1
)
Button(onClick = {
if (login == "" || password == "") {
Toast.makeText(
activity,
context.getString(R.string.please_fill_all_fields),
Toast.LENGTH_SHORT
).show()
return@Button
}
if (!checkAuth(login, password, activity, context)) return@Button
connectToDb()
val user = transaction { User.find { UptaskDb.Users.login eq login }.firstOrNull() }
if (user == null) {
Toast.makeText(
activity,
context.getString(R.string.user_with_this_login),
Toast.LENGTH_SHORT
).show()
return@Button
}
if (user.password != password) {
Toast.makeText(
activity,
context.getString(R.string.incorrect_password),
Toast.LENGTH_SHORT
).show()
return@Button
}
val user = userVm.authUser(login, password, activity, context) ?: return@Button
val sharedPref = activity.getPreferences(Context.MODE_PRIVATE)
with(sharedPref.edit()) {
putString("user", user.id.value.toString())
apply()
}
userVm.setCurrentUser(sharedPref, user)
val userId = transaction { user.id.value }
navController.navigate("taskList/$userId")
}) {
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/org/leftbrained/uptaskapp/RegisterActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import org.leftbrained.uptaskapp.db.UptaskDb
import org.leftbrained.uptaskapp.db.User
import org.leftbrained.uptaskapp.db.connectToDb
import org.leftbrained.uptaskapp.ui.theme.AppTheme
import org.leftbrained.uptaskapp.viewmodel.UserViewModel

@Composable
fun RegisterActivity(navController: NavController) {
connectToDb()
val userVm = remember { UserViewModel() }
var login by remember {
mutableStateOf("")
}
Expand Down Expand Up @@ -96,19 +98,10 @@ fun RegisterActivity(navController: NavController) {
{
if (login.isNotEmpty() && password.isNotEmpty()) {
connectToDb()
transaction {
User.new { this.login = login; this.password = password }
}
val userId = transaction {
User.find {
UptaskDb.Users.login eq login
}.first().id.value
}
with(activity.getPreferences(Context.MODE_PRIVATE)?.edit()) {
this?.putString("user", userId.toString())
this?.apply()
}
navController.navigate("taskList/${userId}")
val usr = userVm.newUser(login, password)
val usrId = transaction { usr.id.value }
userVm.setCurrentUser(activity.getPreferences(Context.MODE_PRIVATE), User.findById(usrId)!!)
navController.navigate("taskList/${usrId}")
} else {
Toast.makeText(context, context.getString(R.string.please_fill_all_fields), Toast.LENGTH_SHORT).show()
}
Expand Down
59 changes: 3 additions & 56 deletions app/src/main/java/org/leftbrained/uptaskapp/StatsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ import org.leftbrained.uptaskapp.db.UserTask
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StatsActivity(navController: NavController, userId: Int) {
val sharedPref = (LocalContext.current as Activity).getPreferences(Context.MODE_PRIVATE)
val sharedPref = LocalContext.current.getSharedPreferences("logs", Context.MODE_PRIVATE)
val pickerState = rememberDateRangePickerState()
var currentStats by remember { mutableStateOf(Stats(userId, 0, 0, 0)) }
var currentStats by remember { mutableStateOf(Stats()) }
Scaffold(
topBar = {
TopAppBar(title = { Text("Stats") }, navigationIcon = {
Expand Down Expand Up @@ -107,25 +107,7 @@ fun StatsActivity(navController: NavController, userId: Int) {
.toLocalDateTime(
TimeZone.currentSystemDefault()
)
with(sharedPref.edit()) {
val logs = sharedPref.getStringSet("logs", mutableSetOf())
val totalTasks = logs?.count {
val date = LocalDate.parse(it.split(" - ")[1])
val action = it.split(" - ")[3]
date in startDate.date..endDate.date && action == "Add" && userId == it.split(" - ")[2].toInt()
} ?: 0
val completedTasks = logs?.count {
val date = LocalDate.parse(it.split(" - ")[1])
val action = it.split(" - ")[3]
date in startDate.date..endDate.date && action == "Done" && userId == it.split(" - ")[2].toInt()
} ?: 0
val undoneTasks = logs?.count {
val date = LocalDate.parse(it.split(" - ")[1])
val action = it.split(" - ")[3]
date in startDate.date..endDate.date && action == "Undone" && userId == it.split(" - ")[2].toInt()
} ?: 0
currentStats = Stats(userId, totalTasks, completedTasks, undoneTasks)
}
currentStats = currentStats.checkStats(sharedPref, startDate, endDate, userId)
},
modifier = Modifier.padding(8.dp)
) {
Expand All @@ -151,39 +133,4 @@ fun StatsActivity(navController: NavController, userId: Int) {
)
}
}
}

//fun getStats(userId: Int, startDate: LocalDate, endDate: LocalDate): Stats {
// return transaction {
// val completedTasksCount = UserTask.find {
// (UptaskDb.UserTasks.userId eq userId) and
// (UptaskDb.Logs.date greaterEq startDate) and
// (UptaskDb.Logs.date lessEq endDate) and
// (UptaskDb.Logs.action eq "Done")
// }.count()
//
// val incompleteTasksCount = UserTask.find {
// (UptaskDb.UserTasks.userId eq userId) and
// (UptaskDb.Logs.date greaterEq startDate) and
// (UptaskDb.Logs.date lessEq endDate) and
// (UptaskDb.Logs.action eq "Undone")
// }.count()
//
// val totalTasksCount = UserTask.find {
// (UptaskDb.UserTasks.userId eq userId) and
// (UptaskDb.Logs.date greaterEq startDate) and
// (UptaskDb.Logs.date lessEq endDate) and
// (UptaskDb.Logs.action eq "Add")
// }.count()
//
// val newStats = Stats(userId, totalTasksCount, completedTasksCount, incompleteTasksCount)
// newStats
// }
//}

@Preview(showSystemUi = true, device = "id:pixel_8_pro")
@Composable
fun StatsActivityPreview() {
val navController = rememberNavController()
StatsActivity(navController, 1)
}
10 changes: 0 additions & 10 deletions app/src/main/java/org/leftbrained/uptaskapp/TaskActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,4 @@ fun TaskActivity(
}
}
}
}

@Preview(device = "spec:width=411dp,height=891dp")
@Composable
fun TaskActivityPreview() {
AppTheme {
TaskActivity(TaskList.new {
this.userId = User.findById(0)!!; this.emoji = "F"; this.name = "TaskList 1"
}.id.value, rememberNavController(), userId = 0, sort = 0, filter = "", showDone = false)
}
}
11 changes: 0 additions & 11 deletions app/src/main/java/org/leftbrained/uptaskapp/TaskListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,4 @@ fun TaskListActivity(
}
}
}
}

@Preview(device = "id:pixel_7_pro", showSystemUi = true, showBackground = true)
@Composable
fun TaskListActivityPreview() {
AppTheme {
TaskListActivity(
userId = 1,
navController = rememberNavController()
)
}
}
78 changes: 78 additions & 0 deletions app/src/main/java/org/leftbrained/uptaskapp/classes/Checks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.leftbrained.uptaskapp.classes

import android.app.Activity
import android.content.Context
import android.widget.Toast
import org.leftbrained.uptaskapp.R

object Checks {
fun dateCheck(dueDate: String, context: Context): Boolean {
val dateRegex = Regex("[0-9]{4}-[0-9]{2}-[0-9]{2}")
if (!dueDate.matches(dateRegex)) {
Toast.makeText(
context,
"Date doesn't match the pattern",
Toast.LENGTH_SHORT
).show()
return false
}
return true
}

fun emptyCheck(
name: String,
desc: String,
context: Context
): Boolean {
if (name == "" || desc == "") {
Toast.makeText(
context,
"Empty values not allowed",
Toast.LENGTH_SHORT
).show()
return false
}
return true
}

fun priorityCheck(priority: Int, context: Context): Boolean {
if (!priority.toString().matches(Regex("[0-5]"))) {
Toast.makeText(
context,
"Priority must be between 0 and 5",
Toast.LENGTH_SHORT
).show()
return false
}
return true
}

fun tagCheck(tag: String, context: Context): Boolean {
if (tag == "") {
Toast.makeText(
context,
"Tag can't be empty",
Toast.LENGTH_SHORT
).show()
return false
}
return true
}

fun checkAuth(
login: String,
password: String,
activity: Activity,
context: Context
): Boolean {
if (login == "" || password == "") {
Toast.makeText(
activity,
context.getString(R.string.please_fill_all_fields),
Toast.LENGTH_SHORT
).show()
return false
}
return true
}
}
71 changes: 71 additions & 0 deletions app/src/main/java/org/leftbrained/uptaskapp/classes/Logs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.leftbrained.uptaskapp.classes

import android.content.SharedPreferences
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.leftbrained.uptaskapp.db.User
import org.leftbrained.uptaskapp.db.UserTask

class Logs(var sharedPref: SharedPreferences) {
private var logsSet = sharedPref.getStringSet("logs", mutableSetOf())!!

fun statusChangeLog(
userId: Int,
task: UserTask
) {
with(sharedPref.edit()) {
val newLogs = logsSet.toMutableSet()
newLogs.add(
"${logsSet.size + 1} - ${
LocalDate.parse(
Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault()).date.toString()
)
} - $userId - ${if (task.isDone) "Done" else "Undone"} - ${task.id.value}"
)
putStringSet("logs", newLogs)
apply()
}
}

fun addTaskLog(
userId: User,
newTaskId: UserTask
) {
with(sharedPref.edit()) {
val newLogs = logsSet.toMutableSet()
newLogs.add(
"${logsSet.size + 1} - ${
LocalDate.parse(
Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault()).date.toString()
)
} - ${userId.id.value} - Add - ${newTaskId.id.value}"
)
putStringSet("logs", newLogs)
apply()
}
}

fun deleteTaskLog(
userId: Int,
task: UserTask
) {
with(sharedPref.edit()) {
val newLogs = logsSet.toMutableSet()
newLogs.add(
"${logsSet.size + 1} - ${
LocalDate.parse(
Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault()).date.toString()
)
} - $userId - Delete - ${task.id.value}"
)
putStringSet("logs", newLogs)
apply()
}
}

}
Loading

0 comments on commit 9b14939

Please sign in to comment.