diff options
author | Bryson Steck <steck.bryson@gmail.com> | 2021-05-04 17:59:55 -0600 |
---|---|---|
committer | Bryson Steck <steck.bryson@gmail.com> | 2021-05-04 17:59:55 -0600 |
commit | 99643e5be84af7e8da217ed48597ceb3c9d3bc85 (patch) | |
tree | 91c314dc3a0f4b60d731ccb8854869302eb9bd8d /app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java | |
parent | af7a395b8c707fe5be3c03e9de878fae1d16b997 (diff) | |
download | wiimmfi-watcher-99643e5be84af7e8da217ed48597ceb3c9d3bc85.tar wiimmfi-watcher-99643e5be84af7e8da217ed48597ceb3c9d3bc85.tar.gz wiimmfi-watcher-99643e5be84af7e8da217ed48597ceb3c9d3bc85.tar.bz2 |
before i get too deep in the database stuff
Diffstat (limited to 'app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java')
-rw-r--r-- | app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java index 82b892a..cf31868 100644 --- a/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java +++ b/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java @@ -1,9 +1,82 @@ package com.example.wiimmterfaceandroid.viewmodel; +import android.app.Application; + +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.MutableLiveData; + +import com.example.wiimmterfaceandroid.database.AppDatabase; import com.example.wiimmterfaceandroid.model.FriendCode; -public class FriendCodeViewModel { - FriendCode friendCode = new FriendCode(); +import androidx.databinding.ObservableArrayList; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; +import androidx.room.Room; +import androidx.room.RoomDatabase; + +import androidx.databinding.ObservableArrayList; + +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, "fc-database").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) { + FriendCode current = currentEntry.getValue(); + current.name = name; + current.friendCode = friendCode; + db.getFriendCodeDao().update(current); + int index = entries.indexOf(current); + entries.set(index, current); + } else { + FriendCode newEntry = new FriendCode(); + newEntry.name = name; + newEntry.friendCode = friendCode; + entries.add(newEntry); + } + saving.postValue(false); + }).start(); + } -} + public void deleteEntry(FriendCode entry) { + new Thread(() -> { + db.getFriendCodeDao().delete(entry); + entries.remove(entry); + }).start(); + } + } |