EmailRepository.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.noahvogt.miniprojekt.DataBase;
  2. import android.app.Application;
  3. import androidx.lifecycle.LiveData;
  4. import java.util.List;
  5. public class EmailRepository {
  6. private MessageDao messageDao;
  7. private final LiveData<List<Message>> mDraftMessage;
  8. private LiveData<List<Message>> mInboxMessage;
  9. private LiveData<List<Message>> mSentMessage;
  10. private LiveData<List<Message>> mArchiveMessage;
  11. private LiveData<List<Message>> mSpamMessage;
  12. // Note that in order to unit test the WordRepository, you have to remove the Application
  13. // dependency. This adds complexity and much more code, and this sample is not about testing.
  14. // See the BasicSample in the android-architecture-components repository at
  15. // https://github.com/googlesamples
  16. /*get all messages sorted by date out of Database*/
  17. public EmailRepository(Application application) {
  18. EmailRoomDatabase db = EmailRoomDatabase.getDatabase(application);
  19. messageDao = db.messageDao();
  20. mInboxMessage = messageDao.getInboxMessages();
  21. mDraftMessage = messageDao.getDraftMessages();
  22. mArchiveMessage = messageDao.getArchiveMessages();
  23. mSentMessage = messageDao.getSentMessages();
  24. mSpamMessage = messageDao.getSpamMessages();
  25. }
  26. // Room executes all queries on a separate thread.
  27. // Observed LiveData will notify the observer when the data has changed.
  28. /* returns all messages of with tag Draft */
  29. public LiveData<List<Message>> getDraftMessages() {
  30. return mDraftMessage;
  31. }
  32. public LiveData<List<Message>> getSpamMessage(){return mSpamMessage;}
  33. public LiveData<List<Message>> getInboxMessages() {
  34. return mInboxMessage;
  35. }
  36. public LiveData<List<Message>> getSentMessages() {
  37. return mSentMessage;
  38. }
  39. public LiveData<List<Message>> getArchiveMessages() {
  40. return mArchiveMessage;
  41. }
  42. // You must call this on a non-UI thread or your app will throw an exception. Room ensures
  43. // that you're not doing any long running operations on the main thread, blocking the UI.
  44. public void insert(Message message) {
  45. EmailRoomDatabase.databaseWriteExecutor.execute(() -> {
  46. messageDao.insert(message);
  47. });
  48. }
  49. public void deleteMessage(final Message message){
  50. EmailRoomDatabase.databaseWriteExecutor.execute(() -> {
  51. messageDao.delete(message);
  52. });
  53. }
  54. public void updateMessage(final int id, String folder){
  55. EmailRoomDatabase.databaseWriteExecutor.execute(() -> {
  56. messageDao.updateMessage(id ,folder);
  57. });
  58. }
  59. }