Created
November 1, 2018 13:33
-
-
Save MwBakker/dfc6d644f33a91c09640f6e3dbd86cf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mwb.digitalstorage.database; | |
import android.app.Application; | |
import androidx.lifecycle.LiveData; | |
import android.os.AsyncTask; | |
import com.mwb.digitalstorage.model.Storage; | |
import java.util.List; | |
public class StorageRepository { | |
StorageDao storageDao; | |
LiveData<List<Storage>> allStorages; | |
public StorageRepository(Application application) | |
{ | |
StorageRoomDB db = StorageRoomDB.getDatabase(application); | |
storageDao = db.storageDao(); | |
allStorages = storageDao.getAllStorages(); | |
} | |
// | |
// returns allStorages | |
// | |
public LiveData<List<Storage>> getAllStorages() { return allStorages; } | |
public void insert(Storage storage) { | |
new insertAsyncTask(storageDao).execute(storage); | |
} | |
// | |
// inserts the storage async | |
// | |
static class insertAsyncTask extends AsyncTask<Storage, Void, Void> | |
{ | |
StorageDao mAsyncTaskDao; | |
insertAsyncTask(StorageDao dao) { | |
mAsyncTaskDao = dao; | |
} | |
@Override | |
protected Void doInBackground(final Storage... params) | |
{ | |
mAsyncTaskDao.insert(params[0]); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment