From ec0c59433deb3b143ba7643f585dc6ee8ede9258 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sun, 13 Apr 2025 21:23:56 -0400 Subject: [PATCH] Threaded example --- .../notesapp/MainActivity.java | 19 +++++--- .../coldlightalchemist/notesapp/NewNote.java | 32 ++++++++++---- .../notesapp/NotesRecyclerViewAdapter.java | 21 ++++++++- .../notesapp/db/NoteDB.java | 44 ++++++++++++++++++- 4 files changed, 98 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/coldlightalchemist/notesapp/MainActivity.java b/app/src/main/java/com/coldlightalchemist/notesapp/MainActivity.java index 74a03a7..041ac07 100644 --- a/app/src/main/java/com/coldlightalchemist/notesapp/MainActivity.java +++ b/app/src/main/java/com/coldlightalchemist/notesapp/MainActivity.java @@ -1,5 +1,6 @@ package com.coldlightalchemist.notesapp; +import static android.view.View.GONE; import static android.view.View.INVISIBLE; import static android.view.View.VISIBLE; @@ -18,8 +19,11 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.coldlightalchemist.notesapp.db.NoteDB; +import com.coldlightalchemist.notesapp.db.entities.Note; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.List; + public class MainActivity extends AppCompatActivity { private NoteDB db; @@ -59,13 +63,14 @@ public class MainActivity extends AppCompatActivity { startActivity(newNoteIntent); }); - if (db.noteDAO().getAll().isEmpty()) { - noNotesText.setVisibility(VISIBLE); - notesRecyclerView.setVisibility(View.GONE); - } else { - noNotesText.setVisibility(View.GONE); - notesRecyclerView.setVisibility(VISIBLE); - } + NoteDB.execute(() -> { + List notes = db.noteDAO().getAll(); + + NoteDB.getMainLoopHandler().post(() -> { + noNotesText.setVisibility(notes.isEmpty() ? VISIBLE : GONE); + notesRecyclerView.setVisibility(notes.isEmpty() ? GONE : VISIBLE); + }); + }); } @Override diff --git a/app/src/main/java/com/coldlightalchemist/notesapp/NewNote.java b/app/src/main/java/com/coldlightalchemist/notesapp/NewNote.java index 69fda60..f28cb25 100644 --- a/app/src/main/java/com/coldlightalchemist/notesapp/NewNote.java +++ b/app/src/main/java/com/coldlightalchemist/notesapp/NewNote.java @@ -67,12 +67,23 @@ public class NewNote extends AppCompatActivity { note.noteContent = newNoteContent.getText().toString(); if(noteWasNull) { - NoteDB.getDB(getApplicationContext()).noteDAO().insertAll(note); - } else { - NoteDB.getDB(getApplicationContext()).noteDAO().update(note); - } + NoteDB.execute(() -> { + NoteDB.getDB().noteDAO().insertAll(note); - finish(); + NoteDB.getMainLoopHandler().post(() -> { + finish(); + }); + }); + } else { + NoteDB.execute(() -> { + NoteDB.getDB().noteDAO().update(note); + + NoteDB.getMainLoopHandler().post(() -> { + finish(); + }); + }); + + } }); saveNote.setOnLongClickListener((v) -> { @@ -82,10 +93,15 @@ public class NewNote extends AppCompatActivity { switch(w) { case DialogInterface.BUTTON_POSITIVE: if(note != null) { - NoteDB.getDB(getApplicationContext()).noteDAO().delete(note); - } + NoteDB.execute(() -> { + NoteDB.getDB().noteDAO().delete(note); - finish(); + NoteDB.getMainLoopHandler().post(() -> { + finish(); + }); + }); + + } break; case DialogInterface.BUTTON_NEGATIVE: break; diff --git a/app/src/main/java/com/coldlightalchemist/notesapp/NotesRecyclerViewAdapter.java b/app/src/main/java/com/coldlightalchemist/notesapp/NotesRecyclerViewAdapter.java index 5d9c607..c9d3db4 100644 --- a/app/src/main/java/com/coldlightalchemist/notesapp/NotesRecyclerViewAdapter.java +++ b/app/src/main/java/com/coldlightalchemist/notesapp/NotesRecyclerViewAdapter.java @@ -1,6 +1,7 @@ package com.coldlightalchemist.notesapp; import android.content.Intent; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,6 +14,8 @@ import com.coldlightalchemist.notesapp.db.NoteDB; import com.coldlightalchemist.notesapp.db.dao.NoteDAO; import com.coldlightalchemist.notesapp.db.entities.Note; +import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.function.Consumer; public class NotesRecyclerViewAdapter extends RecyclerView.Adapter { @@ -66,11 +69,25 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter { + List notes = noteSource.getAll(); + + NoteDB.getMainLoopHandler().post(() -> { + holder.setNote(notes.get(position)); + }); + }); } @Override public int getItemCount() { - return noteSource.getAll().size(); + try { + return NoteDB.execute(() -> { + return NoteDB.getDB().noteDAO().getAll().size(); + }).get(); + } catch (ExecutionException | InterruptedException e) { + // Cop out + Log.d("ITEMCOUNTISSUE", e.toString()); + return 0; + } } } diff --git a/app/src/main/java/com/coldlightalchemist/notesapp/db/NoteDB.java b/app/src/main/java/com/coldlightalchemist/notesapp/db/NoteDB.java index d6fc086..f7bfd63 100644 --- a/app/src/main/java/com/coldlightalchemist/notesapp/db/NoteDB.java +++ b/app/src/main/java/com/coldlightalchemist/notesapp/db/NoteDB.java @@ -1,6 +1,8 @@ package com.coldlightalchemist.notesapp.db; import android.content.Context; +import android.os.Handler; +import android.os.Looper; import androidx.room.Database; import androidx.room.Room; @@ -9,19 +11,59 @@ import androidx.room.RoomDatabase; import com.coldlightalchemist.notesapp.db.dao.NoteDAO; import com.coldlightalchemist.notesapp.db.entities.Note; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + @Database(entities = {Note.class}, version = 1) public abstract class NoteDB extends RoomDatabase { public abstract NoteDAO noteDAO(); private static NoteDB db; + private static ExecutorService executorService; + + private static Handler handler; + + public static NoteDB getDB() { + if (db == null) { + throw new RuntimeException("DB not built"); + } + + return db; + } + public static NoteDB getDB(Context applicationContext) { if (db == null) { db = Room.databaseBuilder(applicationContext, NoteDB.class, "Notes") - .allowMainThreadQueries() // This is critical to make this whole thing work .build(); } return db; } + + public static Handler getMainLoopHandler() { + if (handler == null) { + handler = new Handler(Looper.getMainLooper()); + } + + return handler; + } + + public static void execute(Runnable threadExecute) { + if (executorService == null) { + executorService = Executors.newSingleThreadExecutor(); + } + + executorService.execute(threadExecute); + } + + public static Future execute(Callable threadExecute) { + if (executorService == null) { + executorService = Executors.newSingleThreadExecutor(); + } + + return executorService.submit(threadExecute); + } }