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

136 lines
5.6 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>. */
import android.app.Application;
import android.content.Context;
2020-12-31 15:32:38 +01:00
import android.content.SharedPreferences;
2020-09-07 16:57:00 +02:00
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.ArrayList;
2020-10-07 17:46:15 +02:00
import java.util.List;
2020-09-07 16:57:00 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.client.data.PlaylistData.Playlist;
2020-11-14 12:19:37 +01:00
import app.fedilab.fedilabtube.client.data.VideoPlaylistData;
2020-09-07 16:57:00 +02:00
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
2020-11-14 12:19:37 +01:00
import app.fedilab.fedilabtube.sqlite.ManagePlaylistsDAO;
2020-09-07 16:57:00 +02:00
import app.fedilab.fedilabtube.sqlite.Sqlite;
public class PlaylistsVM extends AndroidViewModel {
private MutableLiveData<APIResponse> apiResponseMutableLiveData;
2020-11-14 12:19:37 +01:00
private MutableLiveData<List<VideoPlaylistData.VideoPlaylistExport>> videoPlaylistExportMutableLiveData;
2020-09-07 16:57:00 +02:00
public PlaylistsVM(@NonNull Application application) {
super(application);
}
2020-09-25 18:58:04 +02:00
public LiveData<APIResponse> manage(action apiAction, Playlist playlist, String videoId) {
2020-09-09 15:00:15 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-09-25 18:58:04 +02:00
managePlaylists(apiAction, playlist, videoId);
2020-09-07 16:57:00 +02:00
return apiResponseMutableLiveData;
}
2020-11-14 12:19:37 +01:00
public LiveData<List<VideoPlaylistData.VideoPlaylistExport>> localePlaylist() {
videoPlaylistExportMutableLiveData = new MutableLiveData<>();
loadLocalePlaylist();
return videoPlaylistExportMutableLiveData;
}
2020-10-07 17:46:15 +02:00
public LiveData<APIResponse> videoExists(List<String> videoIds) {
2020-09-25 18:58:04 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-10-03 16:22:19 +02:00
checkVideosExist(videoIds);
2020-09-25 18:58:04 +02:00
return apiResponseMutableLiveData;
}
2020-10-07 17:46:15 +02:00
private void checkVideosExist(List<String> videoIds) {
2020-09-25 18:58:04 +02:00
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
2020-10-03 16:22:19 +02:00
APIResponse apiResponse = new RetrofitPeertubeAPI(_mContext).getVideosExist(videoIds);
2020-09-25 18:58:04 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
}).start();
}
2020-11-14 12:19:37 +01:00
private void loadLocalePlaylist() {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<VideoPlaylistData.VideoPlaylistExport> videoPlaylistExports = new ManagePlaylistsDAO(_mContext, db).getAllPlaylists();
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> videoPlaylistExportMutableLiveData.setValue(videoPlaylistExports);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-09-25 18:58:04 +02:00
private void managePlaylists(action apiAction, Playlist playlist, String videoId) {
2020-09-09 15:00:15 +02:00
Context _mContext = getApplication().getApplicationContext();
2020-09-07 16:57:00 +02:00
new Thread(() -> {
try {
String token = Helper.getToken(_mContext);
2020-09-07 16:57:00 +02:00
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-10-01 17:42:03 +02:00
Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
2020-09-07 16:57:00 +02:00
int statusCode = -1;
2020-09-25 18:58:04 +02:00
APIResponse apiResponse;
2020-09-07 16:57:00 +02:00
if (account == null) {
statusCode = 403;
apiResponse = new APIResponse();
apiResponse.setPlaylists(new ArrayList<>());
2020-09-25 18:58:04 +02:00
} else {
2020-10-08 18:13:04 +02:00
apiResponse = new RetrofitPeertubeAPI(_mContext).playlistAction(apiAction, playlist != null ? playlist.getId() : null, videoId, account.getAcct(), null);
2020-09-07 16:57:00 +02:00
}
Handler mainHandler = new Handler(Looper.getMainLooper());
2020-09-08 10:11:11 +02:00
if (apiResponse != null) {
2020-09-07 16:57:00 +02:00
apiResponse.setStatusCode(statusCode);
}
APIResponse finalApiResponse = apiResponse;
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(finalApiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
public enum action {
2020-09-25 18:58:04 +02:00
GET_PLAYLISTS,
GET_PLAYLIST_INFO,
2020-09-07 16:57:00 +02:00
GET_LIST_VIDEOS,
CREATE_PLAYLIST,
2020-09-25 18:58:04 +02:00
UPDATE_PLAYLIST,
2020-09-07 16:57:00 +02:00
DELETE_PLAYLIST,
ADD_VIDEOS,
2020-10-03 16:22:19 +02:00
DELETE_VIDEOS
2020-09-07 16:57:00 +02:00
}
}