Skip to content

Commit

Permalink
add basic task db
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedAdhamc committed May 8, 2024
1 parent 28db88d commit 728541d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 7 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
val roomVersion = "2.6.1"
implementation("androidx.room:room-runtime:$roomVersion")
annotationProcessor("androidx.room:room-compiler:$roomVersion")
// optional - RxJava2 support for Room
implementation("androidx.room:room-rxjava2:$roomVersion")
// optional - RxJava3 support for Room
implementation("androidx.room:room-rxjava3:$roomVersion")
// optional - Guava support for Room, including Optional and ListenableFuture
implementation("androidx.room:room-guava:$roomVersion")
// optional - Test helpers
testImplementation("androidx.room:room-testing:$roomVersion")
// optional - Paging 3 Integration
implementation("androidx.room:room-paging:$roomVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.taskmanager.Database.RoomDB;
import com.example.taskmanager.R;
import com.example.taskmanager.Utility.TaskModel;

import java.time.LocalDate;
import java.util.List;

public class CreateTaskActivity extends AppCompatActivity {
String currentPriority = "Low";
Expand Down Expand Up @@ -53,6 +56,12 @@ public void onNothingSelected(AdapterView<?> parentView) {
// Do nothing
}
});

//for database testing purposes (note that anyways this wouldnt work as you can't directly call db method from the same UI thread)
// RoomDB instance = RoomDB.getInstance(getApplicationContext());
// instance.taskDAO().insertTask(new TaskModel( 0, "test", "ayyyyy", "", "", "", true));
// List<TaskModel> allTasks = instance.taskDAO().getAllTasks();
// Log.wtf("DB", allTasks.toString());
}

public void selectDate(View v) {
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/example/taskmanager/Database/RoomDB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.taskmanager.Database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.taskmanager.Utility.TaskModel;

@Database(entities = {TaskModel.class}, version = 1)
public abstract class RoomDB extends RoomDatabase {
private static volatile RoomDB INSTANCE;
public abstract TaskDAO taskDAO ();

public static RoomDB getInstance(Context context){
if (INSTANCE == null){
synchronized (RoomDB.class) {
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), RoomDB.class, "TASK_DB").build();
}
}
}
return INSTANCE;
}

public void insertTask(TaskModel task){

}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/example/taskmanager/Database/TaskDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.taskmanager.Database;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.taskmanager.Utility.TaskModel;

import java.util.List;

@Dao
public interface TaskDAO {
@Insert
void insertTask(TaskModel task);

@Delete
void deleteTask(TaskModel task);

@Update
void updateTask(TaskModel task);

@Query("SELECT * FROM task_table")
List<TaskModel> getAllTasks();

@Query("SELECT * FROM task_table WHERE id LIKE :taskId")
TaskModel findTaskById(int taskId);

@Query("SELECT * FROM task_table WHERE status LIKE 0")
List<TaskModel> getAllOngoingTasks();

@Query("SELECT * FROM task_table WHERE status LIKE 1")
List<TaskModel> getAllCompletedTasks();
}
29 changes: 26 additions & 3 deletions app/src/main/java/com/example/taskmanager/Utility/TaskModel.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
package com.example.taskmanager.Utility;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

import java.io.Serializable;

@Entity (tableName = "task_table")
public class TaskModel implements Serializable {
private int id, status;
@PrimaryKey( autoGenerate = true)
private int id;
@ColumnInfo (name = "status")
private int status;
@ColumnInfo (name = "title")
private String title;
@ColumnInfo (name = "desc")
private String description;
@ColumnInfo (name = "priority")
private String currentPriority;
@ColumnInfo (name = "date")
private String currentDate;
@ColumnInfo (name = "time")
private String currentTime;
@ColumnInfo (name = "editable")
private Boolean editable;

public TaskModel()
{

}
public TaskModel(int id, int status, String title, String description, String currentPriority, String currentDate, String currentTime) {
this.id = id;
public TaskModel(int status, String title, String description, String currentPriority, String currentDate, String currentTime, Boolean editable) {
this.status = status;
this.title = title;
this.description = description;
this.currentPriority = currentPriority;
this.currentDate = currentDate;
this.currentTime = currentTime;
this.editable = editable;
}


Expand Down Expand Up @@ -81,4 +96,12 @@ public String getCurrentTime() {
public void setCurrentTime(String currentTime) {
this.currentTime = currentTime;
}

public Boolean getEditable() {
return editable;
}

public void setEditable(Boolean editable) {
this.editable = editable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
TaskAdapter taskAdapter = new TaskAdapter(this);
taskRecyclerView.setAdapter(taskAdapter);

TaskModel task = new TaskModel(0, 1, "Completed Task", "Dummy Task", "1",
"Deadline: 10-5-2024", "10pm");
TaskModel task = new TaskModel(1, "Completed Task", "Dummy Task", "1",
"Deadline: 10-5-2024", "10pm", true);

List<TaskModel> tasksList = new ArrayList<>();
tasksList.add(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
taskAdapter = new TaskAdapter(this);
taskRecyclerView.setAdapter(taskAdapter);

TaskModel task = new TaskModel(0, 0, "Test Task", "Dummy Task", "1",
"Deadline: 10-5-2024", "10pm");
TaskModel task = new TaskModel(0, "Test Task", "Dummy Task", "1",
"Deadline: 10-5-2024", "10pm", true);

tasksList = new ArrayList<>();
tasksList.add(task);
Expand Down

0 comments on commit 728541d

Please sign in to comment.