Compare commits

..

No commits in common. "threaded" and "main" have entirely different histories.

4 changed files with 17 additions and 97 deletions

View File

@ -1,6 +1,5 @@
package com.coldlightalchemist.notesapp;
import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
@ -19,11 +18,8 @@ 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;
@ -63,14 +59,13 @@ public class MainActivity extends AppCompatActivity {
startActivity(newNoteIntent);
});
NoteDB.execute(() -> {
List<Note> notes = db.noteDAO().getAll();
NoteDB.getMainLoopHandler().post(() -> {
noNotesText.setVisibility(notes.isEmpty() ? VISIBLE : GONE);
notesRecyclerView.setVisibility(notes.isEmpty() ? GONE : VISIBLE);
});
});
if (db.noteDAO().getAll().isEmpty()) {
noNotesText.setVisibility(VISIBLE);
notesRecyclerView.setVisibility(View.GONE);
} else {
noNotesText.setVisibility(View.GONE);
notesRecyclerView.setVisibility(VISIBLE);
}
}
@Override

View File

@ -67,23 +67,12 @@ public class NewNote extends AppCompatActivity {
note.noteContent = newNoteContent.getText().toString();
if(noteWasNull) {
NoteDB.execute(() -> {
NoteDB.getDB().noteDAO().insertAll(note);
NoteDB.getMainLoopHandler().post(() -> {
finish();
});
});
NoteDB.getDB(getApplicationContext()).noteDAO().insertAll(note);
} else {
NoteDB.execute(() -> {
NoteDB.getDB().noteDAO().update(note);
NoteDB.getMainLoopHandler().post(() -> {
finish();
});
});
NoteDB.getDB(getApplicationContext()).noteDAO().update(note);
}
finish();
});
saveNote.setOnLongClickListener((v) -> {
@ -93,15 +82,10 @@ public class NewNote extends AppCompatActivity {
switch(w) {
case DialogInterface.BUTTON_POSITIVE:
if(note != null) {
NoteDB.execute(() -> {
NoteDB.getDB().noteDAO().delete(note);
NoteDB.getMainLoopHandler().post(() -> {
finish();
});
});
NoteDB.getDB(getApplicationContext()).noteDAO().delete(note);
}
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;

View File

@ -1,7 +1,6 @@
package com.coldlightalchemist.notesapp;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -14,8 +13,6 @@ 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> {
@ -69,25 +66,11 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecycler
@Override
public void onBindViewHolder(@NonNull NoteRecyclerViewHolder holder, int position) {
NoteDB.execute(() -> {
List<Note> notes = noteSource.getAll();
NoteDB.getMainLoopHandler().post(() -> {
holder.setNote(notes.get(position));
});
});
holder.setNote(noteSource.getAll().get(position));
}
@Override
public int getItemCount() {
try {
return NoteDB.execute(() -> {
return NoteDB.getDB().noteDAO().getAll().size();
}).get();
} catch (ExecutionException | InterruptedException e) {
// Cop out
Log.d("ITEMCOUNTISSUE", e.toString());
return 0;
}
return noteSource.getAll().size();
}
}

View File

@ -1,8 +1,6 @@
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;
@ -11,59 +9,19 @@ 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);
}
}