aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/example/wiimmterfaceandroid/viewmodel/FriendCodeViewModel.java
blob: c7bbe7df271d8940297d33e8cf71a3ffd67c30d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;

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;

import java.util.LinkedHashSet;

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();
        }
    }