package com.noahvogt.miniprojekt.DataBase; import android.app.Application; import androidx.lifecycle.LiveData; import java.util.List; public class EmailRepository { private MessageDao messageDao; private final LiveData> mDraftMessage; private LiveData> mInboxMessage; private LiveData> mSentMessage; private LiveData> mArchiveMessage; private LiveData> mSpamMessage; // Note that in order to unit test the WordRepository, you have to remove the Application // dependency. This adds complexity and much more code, and this sample is not about testing. // See the BasicSample in the android-architecture-components repository at // https://github.com/googlesamples /*get all messages sorted by date out of Database*/ public EmailRepository(Application application) { EmailRoomDatabase db = EmailRoomDatabase.getDatabase(application); messageDao = db.messageDao(); mInboxMessage = messageDao.getInboxMessages(); mDraftMessage = messageDao.getDraftMessages(); mArchiveMessage = messageDao.getArchiveMessages(); mSentMessage = messageDao.getSentMessages(); mSpamMessage = messageDao.getSpamMessages(); } // Room executes all queries on a separate thread. // Observed LiveData will notify the observer when the data has changed. /* returns all messages of with tag Draft */ public LiveData> getDraftMessages() { return mDraftMessage; } public LiveData> getSpamMessage(){return mSpamMessage;} public LiveData> getInboxMessages() { return mInboxMessage; } public LiveData> getSentMessages() { return mSentMessage; } public LiveData> getArchiveMessages() { return mArchiveMessage; } // You must call this on a non-UI thread or your app will throw an exception. Room ensures // that you're not doing any long running operations on the main thread, blocking the UI. public void insert(Message message) { EmailRoomDatabase.databaseWriteExecutor.execute(() -> { messageDao.insert(message); }); } public void deleteMessage(final Message message){ EmailRoomDatabase.databaseWriteExecutor.execute(() -> { messageDao.delete(message); }); } public void updateMessage(final int id, String folder){ EmailRoomDatabase.databaseWriteExecutor.execute(() -> { messageDao.updateMessage(id ,folder); }); } }