aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel
diff options
context:
space:
mode:
authorBryson Steck <steck.bryson@gmail.com>2021-05-08 14:58:18 -0600
committerBryson Steck <steck.bryson@gmail.com>2021-05-08 14:58:18 -0600
commitfc908199f136acd2a5030e5085f024857cd2cfb5 (patch)
tree60e5569fe239bec0c287b69aefd3cff6b95449f7 /app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel
parent1231683daeee4054a9da7204339b9beeb172981e (diff)
downloadwiimmfi-watcher-fc908199f136acd2a5030e5085f024857cd2cfb5.tar
wiimmfi-watcher-fc908199f136acd2a5030e5085f024857cd2cfb5.tar.gz
wiimmfi-watcher-fc908199f136acd2a5030e5085f024857cd2cfb5.tar.bz2
changed api and package name
Diffstat (limited to 'app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel')
-rw-r--r--app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel/FriendCodeViewModel.java71
1 files changed, 71 insertions, 0 deletions
diff --git a/app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel/FriendCodeViewModel.java b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel/FriendCodeViewModel.java
new file mode 100644
index 0000000..ae300c0
--- /dev/null
+++ b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/viewmodel/FriendCodeViewModel.java
@@ -0,0 +1,71 @@
+package me.brysonsteck.wiimmfiwatcher.viewmodel;
+
+import android.app.Application;
+
+import androidx.databinding.ObservableArrayList;
+import androidx.lifecycle.AndroidViewModel;
+import androidx.lifecycle.MutableLiveData;
+import androidx.room.Room;
+
+import me.brysonsteck.wiimmfiwatcher.database.AppDatabase;
+import me.brysonsteck.wiimmfiwatcher.model.FriendCode;
+
+public class FriendCodeViewModel extends AndroidViewModel {
+ ObservableArrayList<FriendCode> entries = new ObservableArrayList<>();
+ MutableLiveData<Boolean> saving = new MutableLiveData<>();
+ MutableLiveData<FriendCode> currentEntry = new MutableLiveData<>();
+ AppDatabase db;
+ public FriendCodeViewModel(Application app) {
+ super(app);
+ saving.setValue(false);
+ db = Room.databaseBuilder(app, AppDatabase.class, "friend-codes-db").build();
+ new Thread(() -> {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ entries.addAll(db.getFriendCodeDao().getAll());
+ }).start();
+ }
+
+ public void setCurrentEntry(FriendCode entry) {
+ currentEntry.postValue(entry);
+ }
+
+ public MutableLiveData<FriendCode> getCurrentEntry() {
+ return currentEntry;
+ }
+
+ public MutableLiveData<Boolean> getSaving() {
+ return saving;
+ }
+
+ public ObservableArrayList<FriendCode> getEntries() {
+ return entries;
+ }
+
+ public void saveFriendCode(String name, String friendCode) {
+ saving.setValue(true);
+ new Thread(() -> {
+ if (currentEntry.getValue() != null) {
+
+ } else {
+ FriendCode newEntry = new FriendCode();
+ newEntry.name = name;
+ newEntry.friendCode = friendCode;
+ db.getFriendCodeDao().insert(newEntry);
+ entries.add(newEntry);
+ }
+
+ saving.postValue(false);
+ }).start();
+ }
+
+ public void deleteEntry(FriendCode entry) {
+ new Thread(() -> {
+ db.getFriendCodeDao().delete(entry);
+ entries.remove(entry);
+ }).start();
+ }
+ }