Commit d3df56d6 authored by Lukas Bersinger's avatar Lukas Bersinger
Browse files

add database / implement saving tasks

parent af766904
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
......@@ -34,6 +35,12 @@ android {
}
dependencies {
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
......
......@@ -8,7 +8,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:name=".TaskTrackerApplication">
<activity android:name=".activities.AddTaskActivity"/>
<activity android:name=".activities.MainActivity">
<intent-filter>
......
package ch.ost.rj.mge.miniproject.tasktracker
import android.app.Application
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
class TaskTrackerApplication : Application() {
override fun onCreate() {
super.onCreate()
TaskRepository.initialize(applicationContext)
}
}
\ No newline at end of file
......@@ -2,16 +2,14 @@ package ch.ost.rj.mge.miniproject.tasktracker.activities
import android.app.DatePickerDialog
import android.os.Bundle
import android.text.Editable
import android.widget.Button
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import androidx.core.widget.doOnTextChanged
import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import kotlinx.android.synthetic.main.activity_add_task.*
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
import java.text.DateFormat
import java.util.*
......@@ -35,24 +33,24 @@ class AddTaskActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_task)
btnCancel = findViewById(R.id.btnCancelCreateTask);
btnCancel = findViewById(R.id.btnCancelCreateTask)
btnCancel.setOnClickListener {
finish()
}
btnCreate = findViewById(R.id.btnCreateTask);
btnCreate = findViewById(R.id.btnCreateTask)
btnCreate.setOnClickListener {
createTask();
createTask()
}
fldName = findViewById(R.id.fldTaskName)
fldName.doOnTextChanged { text, start, before, count ->
fldName.doOnTextChanged { text, _, _, _ ->
task.name = text.toString()
updateValidity()
}
fldEstimateHours = findViewById(R.id.fldEstimateHours)
fldEstimateHours.doOnTextChanged { text, start, before, count ->
fldEstimateHours.doOnTextChanged { text, _, _, _ ->
val input = text.toString().toLongOrNull()
if (input != null) {
......@@ -62,7 +60,7 @@ class AddTaskActivity : AppCompatActivity() {
}
fldEstimateMinutes = findViewById(R.id.fldEstimateMinutes)
fldEstimateMinutes.doOnTextChanged { text, start, before, count ->
fldEstimateMinutes.doOnTextChanged { text, _, _, _ ->
val input = text.toString().toLongOrNull()
if (input != null) {
......@@ -88,16 +86,16 @@ class AddTaskActivity : AppCompatActivity() {
).show()
}
updateValidity();
updateValidity()
}
fun createTask() {
// TODO: Create Task
finish();
TaskRepository.createTask(task)
finish()
}
fun updateValidity() {
btnCreate.isEnabled = task.isValid();
btnCreate.isEnabled = task.isValid()
}
fun updateEstimate() {
......
......@@ -12,8 +12,6 @@ import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private var taskRepository = TaskRepository()
private lateinit var listFragment: TaskListFragment
private lateinit var listEmptyFragment: TaskListEmptyFragment
......@@ -27,15 +25,18 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
btnAddTask = findViewById(R.id.btnAddTask)
btnAddTask.setOnClickListener { view ->
btnAddTask.setOnClickListener { _ ->
val intent = Intent(this, AddTaskActivity::class.java)
startActivity(intent)
}
taskListContainer = findViewById(R.id.fltTaskListContainer)
}
override fun onResume() {
super.onResume()
val tasks: List<Task> = taskRepository.getTasks()
val tasks: List<Task> = TaskRepository.getOpenTasks()
if (tasks.isNotEmpty()) {
listFragment = TaskListFragment.newInstance(tasks)
......
package ch.ost.rj.mge.miniproject.tasktracker.database
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
@Database(entities = arrayOf(Task::class), version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun taskDao(): TaskDao
}
\ No newline at end of file
package ch.ost.rj.mge.miniproject.tasktracker.database
import androidx.room.TypeConverter
import java.util.*
class Converters {
@TypeConverter
fun fromTimestamp(value: Long?): Calendar? {
if (value != null) {
val calendar = Calendar.getInstance()
calendar.timeInMillis = value
return calendar
}
return null
}
@TypeConverter
fun calendarToTimestamp(calendar: Calendar?): Long? {
if (calendar != null) {
return calendar.timeInMillis
}
return null
}
}
package ch.ost.rj.mge.miniproject.tasktracker.database
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
@Dao
interface TaskDao {
@Query("SELECT * FROM tasks WHERE completed = 0 ORDER BY due_date")
fun getOpenTasksByDueDate(): List<Task>
@Insert
fun insert(task: Task)
}
\ No newline at end of file
package ch.ost.rj.mge.miniproject.tasktracker.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.*
@Entity(tableName = "tasks")
class Task {
var id: UUID = UUID.randomUUID()
@PrimaryKey(autoGenerate = true)
var id: Int = 0
@ColumnInfo(name = "name")
var name: String? = null
@ColumnInfo(name = "due_date")
var dueDate: Calendar? = null
@ColumnInfo(name = "completed")
var isCompleted: Boolean = false
var estimatedTimeMinutes: Long = 0
var actualTimeSeconds: Long = 0
......
package ch.ost.rj.mge.miniproject.tasktracker.repositories
import android.content.Context
import androidx.room.Room
import ch.ost.rj.mge.miniproject.tasktracker.database.AppDatabase
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
class TaskRepository {
object TaskRepository {
private lateinit var appDatabase: AppDatabase
fun getTasks(): ArrayList<Task> {
val tasks = ArrayList<Task>()
// val testTask = Task()
// testTask.name = "Test"
//
// tasks.add(testTask)
fun initialize(applicationContext: Context) {
appDatabase = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java,
"tasks.db"
)
.allowMainThreadQueries()
.build()
}
return tasks
fun getOpenTasks(): List<Task> {
return appDatabase.taskDao().getOpenTasksByDueDate()
}
fun createTask(task: Task) {
appDatabase.taskDao().insert(task)
}
}
\ No newline at end of file
......@@ -3,6 +3,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
app:contentPadding="16dp">
<TextView
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment