Threaded example

This commit is contained in:
Bradley Bickford 2025-04-13 21:23:56 -04:00
parent 0e2bfece64
commit ec0c59433d
4 changed files with 98 additions and 18 deletions

View File

@ -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<Note> notes = db.noteDAO().getAll();
NoteDB.getMainLoopHandler().post(() -> {
noNotesText.setVisibility(notes.isEmpty() ? VISIBLE : GONE);
notesRecyclerView.setVisibility(notes.isEmpty() ? GONE : VISIBLE);
});
});
}
@Override

View File

@ -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;

View File

@ -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<NotesRecyclerViewAdapter.NoteRecyclerViewHolder> {
@ -66,11 +69,25 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecycler
@Override
public void onBindViewHolder(@NonNull NoteRecyclerViewHolder holder, int position) {
holder.setNote(noteSource.getAll().get(position));
NoteDB.execute(() -> {
List<Note> 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;
}
}
}

View File

@ -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 <T> Future<T> execute(Callable<T> threadExecute) {
if (executorService == null) {
executorService = Executors.newSingleThreadExecutor();
}
return executorService.submit(threadExecute);
}
}