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

View File

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

View File

@ -1,7 +1,6 @@
package com.coldlightalchemist.notesapp; package com.coldlightalchemist.notesapp;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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.dao.NoteDAO;
import com.coldlightalchemist.notesapp.db.entities.Note; import com.coldlightalchemist.notesapp.db.entities.Note;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer; import java.util.function.Consumer;
public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecyclerViewAdapter.NoteRecyclerViewHolder> { public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecyclerViewAdapter.NoteRecyclerViewHolder> {
@ -69,25 +66,11 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecycler
@Override @Override
public void onBindViewHolder(@NonNull NoteRecyclerViewHolder holder, int position) { public void onBindViewHolder(@NonNull NoteRecyclerViewHolder holder, int position) {
NoteDB.execute(() -> { holder.setNote(noteSource.getAll().get(position));
List<Note> notes = noteSource.getAll();
NoteDB.getMainLoopHandler().post(() -> {
holder.setNote(notes.get(position));
});
});
} }
@Override @Override
public int getItemCount() { public int getItemCount() {
try { return noteSource.getAll().size();
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,8 +1,6 @@
package com.coldlightalchemist.notesapp.db; package com.coldlightalchemist.notesapp.db;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.Room; import androidx.room.Room;
@ -11,59 +9,19 @@ import androidx.room.RoomDatabase;
import com.coldlightalchemist.notesapp.db.dao.NoteDAO; import com.coldlightalchemist.notesapp.db.dao.NoteDAO;
import com.coldlightalchemist.notesapp.db.entities.Note; 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) @Database(entities = {Note.class}, version = 1)
public abstract class NoteDB extends RoomDatabase { public abstract class NoteDB extends RoomDatabase {
public abstract NoteDAO noteDAO(); public abstract NoteDAO noteDAO();
private static NoteDB db; 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) { public static NoteDB getDB(Context applicationContext) {
if (db == null) { if (db == null) {
db = Room.databaseBuilder(applicationContext, NoteDB.class, "Notes") db = Room.databaseBuilder(applicationContext, NoteDB.class, "Notes")
.allowMainThreadQueries() // This is critical to make this whole thing work
.build(); .build();
} }
return db; 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);
}
} }