TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/viewmodel/InfoInstanceVM.java

65 lines
2.4 KiB
Java

package app.fedilab.fedilabtube.viewmodel;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import java.util.List;
import app.fedilab.fedilabtube.client.data.InstanceData;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
public class InfoInstanceVM extends AndroidViewModel {
private MutableLiveData<List<InstanceData.AboutInstance>> aboutInstanceMutableLiveData;
public InfoInstanceVM(@NonNull Application application) {
super(application);
}
public LiveData<List<InstanceData.AboutInstance>> getInstances() {
aboutInstanceMutableLiveData = new MutableLiveData<>();
loadInstances();
return aboutInstanceMutableLiveData;
}
private void loadInstances() {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<InstanceData.AboutInstance> instances = new StoredInstanceDAO(_mContext, db).getAllInstances();
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> aboutInstanceMutableLiveData.setValue(instances);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}