Skip to content

Commit e89e555

Browse files
Merge pull request #9 from NCVirtual-Coding-Club/App-Finalization
App finalization
2 parents cbcf64e + f21af7c commit e89e555

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1909
-63
lines changed

.idea/misc.xml

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ android {
4242

4343
dependencies {
4444

45+
46+
implementation "android.arch.lifecycle:extensions:1.1.0"
47+
implementation "android.arch.lifecycle:viewmodel:1.1.0"
4548
implementation 'androidx.appcompat:appcompat:1.4.0'
4649
implementation 'com.google.android.material:material:1.4.0'
4750
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "71b63d2a70dec1db56d5411900a1068f",
6+
"entities": [
7+
{
8+
"tableName": "note_table",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT, `description` TEXT, `time` TEXT, `priority` INTEGER NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "title",
19+
"columnName": "title",
20+
"affinity": "TEXT",
21+
"notNull": false
22+
},
23+
{
24+
"fieldPath": "description",
25+
"columnName": "description",
26+
"affinity": "TEXT",
27+
"notNull": false
28+
},
29+
{
30+
"fieldPath": "time",
31+
"columnName": "time",
32+
"affinity": "TEXT",
33+
"notNull": false
34+
},
35+
{
36+
"fieldPath": "priority",
37+
"columnName": "priority",
38+
"affinity": "INTEGER",
39+
"notNull": true
40+
}
41+
],
42+
"primaryKey": {
43+
"columnNames": [
44+
"id"
45+
],
46+
"autoGenerate": true
47+
},
48+
"indices": [],
49+
"foreignKeys": []
50+
}
51+
],
52+
"views": [],
53+
"setupQueries": [
54+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
55+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '71b63d2a70dec1db56d5411900a1068f')"
56+
]
57+
}
58+
}

app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<category android:name="android.intent.category.LAUNCHER" />
2020
</intent-filter>
2121
</activity>
22+
<activity
23+
android:name=".ui.agenda.AddEditNoteActivity"
24+
android:parentActivityName=".MainActivity" />
2225
</application>
2326

2427
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.example.rise.database;
2+
3+
4+
import androidx.room.Entity;
5+
import androidx.room.PrimaryKey;
6+
7+
@Entity(tableName = "note_table")
8+
public class Note {
9+
10+
@PrimaryKey(autoGenerate = true)
11+
private int id;
12+
13+
private String title;
14+
15+
private String description;
16+
17+
private String time;
18+
19+
private int priority;
20+
21+
public Note(String title, String description, int priority, String time) {
22+
this.title = title;
23+
this.description = description;
24+
this.priority = priority;
25+
this.time = time;
26+
}
27+
28+
public void setId(int id) {
29+
this.id = id;
30+
}
31+
32+
public int getId() {
33+
return id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public int getPriority() {
45+
return priority;
46+
}
47+
48+
public String getTime() {
49+
return time;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.rise.database;
2+
3+
import androidx.lifecycle.LiveData;
4+
import androidx.room.Dao;
5+
import androidx.room.Delete;
6+
import androidx.room.Insert;
7+
import androidx.room.Query;
8+
import androidx.room.Update;
9+
10+
import java.util.List;
11+
12+
@Dao
13+
public interface NoteDao {
14+
15+
@Insert
16+
void insert(Note note);
17+
18+
@Update
19+
void update(Note note);
20+
21+
@Delete
22+
void delete(Note note);
23+
24+
@Query("DELETE FROM note_table")
25+
void deleteAllNotes();
26+
27+
@Query("SELECT * FROM note_table ORDER BY priority DESC")
28+
LiveData<List<Note>> getAllNotes();
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.rise.database;
2+
3+
import android.content.Context;
4+
5+
import androidx.room.Database;
6+
import androidx.room.Room;
7+
import androidx.room.RoomDatabase;
8+
9+
@Database(entities = {Note.class}, version = 1)
10+
public abstract class NoteDatabase extends RoomDatabase {
11+
12+
private static NoteDatabase instance;
13+
14+
public abstract NoteDao noteDao();
15+
16+
public static synchronized NoteDatabase getInstance(Context context) {
17+
if (instance == null) {
18+
instance = Room.databaseBuilder(context.getApplicationContext(),
19+
NoteDatabase.class, "note_database")
20+
.fallbackToDestructiveMigration()
21+
.build();
22+
}
23+
return instance;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.example.rise.database;
2+
3+
import android.app.Application;
4+
import android.os.AsyncTask;
5+
6+
import androidx.lifecycle.LiveData;
7+
8+
import java.util.List;
9+
10+
public class NoteRepository {
11+
private NoteDao noteDao;
12+
private LiveData<List<Note>> allNotes;
13+
14+
public NoteRepository(Application application) {
15+
NoteDatabase database = NoteDatabase.getInstance(application);
16+
noteDao = database.noteDao();
17+
allNotes = noteDao.getAllNotes();
18+
}
19+
20+
public void insert(Note note) {
21+
new InsertNoteAsyncTask(noteDao).execute(note);
22+
}
23+
24+
public void update(Note note) {
25+
new UpdateNoteAsyncTask(noteDao).execute(note);
26+
}
27+
28+
public void delete(Note note) {
29+
new DeleteNoteAsyncTask(noteDao).execute(note);
30+
}
31+
32+
public void deleteAllNotes() {
33+
new DeleteAllNotesAsyncTask(noteDao).execute();
34+
}
35+
36+
public LiveData<List<Note>> getAllNotes() {
37+
return allNotes;
38+
}
39+
40+
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
41+
private NoteDao noteDao;
42+
43+
private InsertNoteAsyncTask(NoteDao noteDao) {
44+
this.noteDao = noteDao;
45+
}
46+
47+
@Override
48+
protected Void doInBackground(Note... notes) {
49+
noteDao.insert(notes[0]);
50+
return null;
51+
}
52+
}
53+
54+
private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {
55+
private NoteDao noteDao;
56+
57+
private UpdateNoteAsyncTask(NoteDao noteDao) {
58+
this.noteDao = noteDao;
59+
}
60+
61+
@Override
62+
protected Void doInBackground(Note... notes) {
63+
noteDao.update(notes[0]);
64+
return null;
65+
}
66+
}
67+
68+
private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
69+
private NoteDao noteDao;
70+
71+
private DeleteNoteAsyncTask(NoteDao noteDao) {
72+
this.noteDao = noteDao;
73+
}
74+
75+
@Override
76+
protected Void doInBackground(Note... notes) {
77+
noteDao.delete(notes[0]);
78+
return null;
79+
}
80+
}
81+
82+
private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {
83+
private NoteDao noteDao;
84+
85+
private DeleteAllNotesAsyncTask(NoteDao noteDao) {
86+
this.noteDao = noteDao;
87+
}
88+
89+
@Override
90+
protected Void doInBackground(Void... voids) {
91+
noteDao.deleteAllNotes();
92+
return null;
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.rise.database;
2+
3+
import android.app.Application;
4+
5+
import androidx.annotation.NonNull;
6+
import androidx.lifecycle.AndroidViewModel;
7+
import androidx.lifecycle.LiveData;
8+
9+
import java.util.List;
10+
11+
class NoteViewModel extends AndroidViewModel {
12+
private NoteRepository repository;
13+
private LiveData<List<Note>> allNotes;
14+
15+
public NoteViewModel(@NonNull Application application) {
16+
super(application);
17+
repository = new NoteRepository(application);
18+
allNotes = repository.getAllNotes();
19+
}
20+
21+
public void insert(Note note) {
22+
repository.insert(note);
23+
}
24+
25+
public void update(Note note) {
26+
repository.update(note);
27+
}
28+
29+
public void delete(Note note) {
30+
repository.delete(note);
31+
}
32+
33+
public void deleteAllNotes() {
34+
repository.deleteAllNotes();
35+
}
36+
37+
public LiveData<List<Note>> getAllNotes() {
38+
return allNotes;
39+
}
40+
}

0 commit comments

Comments
 (0)