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

92 lines
3.8 KiB
Java
Raw Normal View History

2020-09-07 16:57:00 +02:00
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>. */
2022-05-04 09:43:54 +02:00
import static android.content.Context.MODE_PRIVATE;
2020-09-07 16:57:00 +02:00
import android.app.Application;
import android.content.Context;
2020-09-29 17:42:15 +02:00
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
2020-09-07 16:57:00 +02:00
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 app.fedilab.fedilabtube.client.APIResponse;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-29 17:42:15 +02:00
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
2020-09-07 16:57:00 +02:00
public class ChannelsVM extends AndroidViewModel {
private MutableLiveData<APIResponse> apiResponseMutableLiveData;
public ChannelsVM(@NonNull Application application) {
super(application);
}
2020-10-10 15:05:39 +02:00
public LiveData<APIResponse> get(String instance, RetrofitPeertubeAPI.DataType type, String element) {
apiResponseMutableLiveData = new MutableLiveData<>();
getChannels(instance, type, element);
return apiResponseMutableLiveData;
}
2020-09-25 18:58:04 +02:00
public LiveData<APIResponse> get(RetrofitPeertubeAPI.DataType type, String element) {
2020-09-09 15:00:15 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-10-10 15:05:39 +02:00
getChannels(null, type, element);
2020-09-07 16:57:00 +02:00
return apiResponseMutableLiveData;
}
2020-10-10 15:05:39 +02:00
private void getChannels(String instance, RetrofitPeertubeAPI.DataType type, String element) {
2020-09-09 15:00:15 +02:00
Context _mContext = getApplication().getApplicationContext();
2020-09-07 16:57:00 +02:00
new Thread(() -> {
2020-09-29 17:42:15 +02:00
String finalElement = element;
2020-09-07 16:57:00 +02:00
try {
2020-09-29 17:42:15 +02:00
if (type == RetrofitPeertubeAPI.DataType.MY_CHANNELS) {
SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String token = Helper.getToken(_mContext);
2020-09-29 17:42:15 +02:00
AccountData.Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
finalElement = account.getUsername() + "@" + account.getHost();
}
2020-10-10 15:05:39 +02:00
RetrofitPeertubeAPI retrofitPeertubeAPI;
2020-10-17 18:50:20 +02:00
if (instance == null) {
2020-10-10 15:05:39 +02:00
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
} else {
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, instance, null);
}
2020-09-29 17:42:15 +02:00
APIResponse apiResponse = retrofitPeertubeAPI.getChannelData(type, finalElement);
2020-09-07 16:57:00 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-09-25 18:58:04 +02:00
public enum action {
CREATE_CHANNEL,
UPDATE_CHANNEL
}
2020-09-07 16:57:00 +02:00
}