Threaded example
This commit is contained in:
parent
0e2bfece64
commit
ec0c59433d
@ -1,5 +1,6 @@
|
|||||||
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;
|
||||||
|
|
||||||
@ -18,8 +19,11 @@ 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;
|
||||||
|
|
||||||
@ -59,13 +63,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
startActivity(newNoteIntent);
|
startActivity(newNoteIntent);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (db.noteDAO().getAll().isEmpty()) {
|
NoteDB.execute(() -> {
|
||||||
noNotesText.setVisibility(VISIBLE);
|
List<Note> notes = db.noteDAO().getAll();
|
||||||
notesRecyclerView.setVisibility(View.GONE);
|
|
||||||
} else {
|
NoteDB.getMainLoopHandler().post(() -> {
|
||||||
noNotesText.setVisibility(View.GONE);
|
noNotesText.setVisibility(notes.isEmpty() ? VISIBLE : GONE);
|
||||||
notesRecyclerView.setVisibility(VISIBLE);
|
notesRecyclerView.setVisibility(notes.isEmpty() ? GONE : VISIBLE);
|
||||||
}
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -67,12 +67,23 @@ public class NewNote extends AppCompatActivity {
|
|||||||
note.noteContent = newNoteContent.getText().toString();
|
note.noteContent = newNoteContent.getText().toString();
|
||||||
|
|
||||||
if(noteWasNull) {
|
if(noteWasNull) {
|
||||||
NoteDB.getDB(getApplicationContext()).noteDAO().insertAll(note);
|
NoteDB.execute(() -> {
|
||||||
} else {
|
NoteDB.getDB().noteDAO().insertAll(note);
|
||||||
NoteDB.getDB(getApplicationContext()).noteDAO().update(note);
|
|
||||||
}
|
|
||||||
|
|
||||||
finish();
|
NoteDB.getMainLoopHandler().post(() -> {
|
||||||
|
finish();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
NoteDB.execute(() -> {
|
||||||
|
NoteDB.getDB().noteDAO().update(note);
|
||||||
|
|
||||||
|
NoteDB.getMainLoopHandler().post(() -> {
|
||||||
|
finish();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
saveNote.setOnLongClickListener((v) -> {
|
saveNote.setOnLongClickListener((v) -> {
|
||||||
@ -82,10 +93,15 @@ public class NewNote extends AppCompatActivity {
|
|||||||
switch(w) {
|
switch(w) {
|
||||||
case DialogInterface.BUTTON_POSITIVE:
|
case DialogInterface.BUTTON_POSITIVE:
|
||||||
if(note != null) {
|
if(note != null) {
|
||||||
NoteDB.getDB(getApplicationContext()).noteDAO().delete(note);
|
NoteDB.execute(() -> {
|
||||||
}
|
NoteDB.getDB().noteDAO().delete(note);
|
||||||
|
|
||||||
finish();
|
NoteDB.getMainLoopHandler().post(() -> {
|
||||||
|
finish();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case DialogInterface.BUTTON_NEGATIVE:
|
case DialogInterface.BUTTON_NEGATIVE:
|
||||||
break;
|
break;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
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;
|
||||||
@ -13,6 +14,8 @@ 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> {
|
||||||
@ -66,11 +69,25 @@ 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) {
|
||||||
holder.setNote(noteSource.getAll().get(position));
|
NoteDB.execute(() -> {
|
||||||
|
List<Note> notes = noteSource.getAll();
|
||||||
|
|
||||||
|
NoteDB.getMainLoopHandler().post(() -> {
|
||||||
|
holder.setNote(notes.get(position));
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
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;
|
||||||
@ -9,19 +11,59 @@ 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user