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

284 lines
12 KiB
Java
Raw Normal View History

2020-09-25 18:58:04 +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 app.fedilab.fedilabtube.viewmodel.PlaylistsVM.action.GET_LIST_VIDEOS;
2020-09-25 18:58:04 +02:00
import android.app.Application;
import android.content.Context;
2020-11-14 12:19:37 +01:00
import android.database.sqlite.SQLiteDatabase;
2020-09-25 18:58:04 +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;
2020-11-14 12:19:37 +01:00
import java.util.ArrayList;
import java.util.List;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-11-28 16:41:21 +01:00
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.client.data.ChannelData;
2020-11-14 12:19:37 +01:00
import app.fedilab.fedilabtube.client.data.VideoData;
2020-09-27 14:12:06 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-12-01 18:35:54 +01:00
import app.fedilab.fedilabtube.helper.HelperInstance;
2020-11-14 12:19:37 +01:00
import app.fedilab.fedilabtube.sqlite.ManagePlaylistsDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
2020-09-25 18:58:04 +02:00
public class TimelineVM extends AndroidViewModel {
private MutableLiveData<APIResponse> apiResponseMutableLiveData;
public TimelineVM(@NonNull Application application) {
super(application);
}
2020-11-28 16:41:21 +01:00
public LiveData<APIResponse> getVideos(TimelineType action, String max_id, ChannelData.Channel forChannel, AccountData.Account forAccount) {
2020-09-25 18:58:04 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-11-28 16:41:21 +01:00
loadVideos(action, max_id, forChannel, forAccount);
2020-09-25 18:58:04 +02:00
return apiResponseMutableLiveData;
}
2020-09-27 11:11:39 +02:00
public LiveData<APIResponse> getOverviewVideos(String page) {
apiResponseMutableLiveData = new MutableLiveData<>();
loadOverviewVideos(page);
return apiResponseMutableLiveData;
}
2020-09-25 18:58:04 +02:00
2020-11-12 17:08:43 +01:00
public LiveData<APIResponse> getVideoHistory(String max_id, String startDate, String endDate) {
apiResponseMutableLiveData = new MutableLiveData<>();
loadHistory(max_id, startDate, endDate);
return apiResponseMutableLiveData;
}
public LiveData<APIResponse> deleterHistory() {
apiResponseMutableLiveData = new MutableLiveData<>();
deleteHistory();
return apiResponseMutableLiveData;
}
2020-10-09 18:22:05 +02:00
public LiveData<APIResponse> getVideo(String instance, String videoId, boolean isMyVideo) {
2020-09-25 18:58:04 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-10-09 18:22:05 +02:00
getSingle(instance, videoId, isMyVideo);
2020-09-29 17:42:15 +02:00
return apiResponseMutableLiveData;
}
2020-10-08 18:13:04 +02:00
public LiveData<APIResponse> loadVideosInPlaylist(String playlistId, String maxId) {
2020-10-08 16:21:14 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-10-08 18:13:04 +02:00
loadVideosInPlayList(playlistId, maxId);
2020-10-08 16:21:14 +02:00
return apiResponseMutableLiveData;
}
2020-11-14 12:19:37 +01:00
public LiveData<APIResponse> loadVideosInLocalPlaylist(String playlistUuid) {
apiResponseMutableLiveData = new MutableLiveData<>();
loadVideosInLocalPlayList(playlistUuid);
return apiResponseMutableLiveData;
}
2020-10-09 18:22:05 +02:00
public LiveData<APIResponse> getMyVideo(String instance, String videoId) {
2020-09-29 17:42:15 +02:00
apiResponseMutableLiveData = new MutableLiveData<>();
2020-10-09 18:22:05 +02:00
getSingle(instance, videoId, true);
2020-09-25 18:58:04 +02:00
return apiResponseMutableLiveData;
}
2020-10-10 15:05:39 +02:00
public LiveData<APIResponse> getVideosInChannel(String instance, String channelId, String max_id) {
apiResponseMutableLiveData = new MutableLiveData<>();
loadVideosForChannel(instance, channelId, max_id);
return apiResponseMutableLiveData;
}
private void loadVideosForChannel(String instance, String channelId, String max_id) {
2020-09-26 16:46:51 +02:00
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
2020-10-10 15:05:39 +02:00
RetrofitPeertubeAPI retrofitPeertubeAPI;
2020-10-17 18:50:20 +02:00
if (instance == null) {
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
} else {
2020-10-10 15:05:39 +02:00
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, instance, null);
}
2020-09-26 16:46:51 +02:00
APIResponse apiResponse = retrofitPeertubeAPI.getVideosForChannel(channelId, max_id);
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
2020-10-09 18:22:05 +02:00
private void getSingle(String instance, String videoId, boolean myVideo) {
2020-09-25 18:58:04 +02:00
Context _mContext = getApplication().getApplicationContext();
2022-05-04 09:43:54 +02:00
boolean canUseToken = instance == null || instance.compareTo(HelperInstance.getLiveInstance(_mContext)) == 0;
2020-11-22 10:59:30 +01:00
boolean finalCanUseToken = canUseToken;
2020-09-25 18:58:04 +02:00
new Thread(() -> {
try {
2020-10-09 18:22:05 +02:00
RetrofitPeertubeAPI retrofitPeertubeAPI;
2020-10-17 18:50:20 +02:00
if (instance == null) {
2020-10-09 18:22:05 +02:00
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
2020-10-17 18:50:20 +02:00
} else {
2020-10-09 18:22:05 +02:00
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, instance, null);
}
2020-11-22 10:59:30 +01:00
APIResponse apiResponse = retrofitPeertubeAPI.getVideos(videoId, myVideo, finalCanUseToken);
2020-11-18 18:15:45 +01:00
if (Helper.isLoggedIn(_mContext) && instance == null) {
2020-09-27 14:12:06 +02:00
if (apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0 && apiResponse.getPeertubes().get(0) != null) {
APIResponse response = new RetrofitPeertubeAPI(_mContext).getRating(videoId);
if (response != null)
apiResponse.getPeertubes().get(0).setMyRating(response.getRating().getRating());
}
2020-09-25 18:58:04 +02:00
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-10-08 18:13:04 +02:00
private void loadVideosInPlayList(String playlistId, String maxId) {
2020-10-08 16:21:14 +02:00
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
2020-10-08 18:13:04 +02:00
APIResponse apiResponse = retrofitPeertubeAPI.playlistAction(GET_LIST_VIDEOS, playlistId, null, null, maxId);
2020-10-08 16:21:14 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-11-14 12:19:37 +01:00
private void loadVideosInLocalPlayList(String playlistUuid) {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<VideoData.VideoExport> videoExports = new ManagePlaylistsDAO(_mContext, db).getAllVideosInPlaylist(playlistUuid);
APIResponse apiResponse = new APIResponse();
List<VideoData.Video> videos = new ArrayList<>();
for (VideoData.VideoExport videoExport : videoExports) {
videos.add(videoExport.getVideoData());
}
apiResponse.setPeertubes(videos);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-11-28 16:41:21 +01:00
private void loadVideos(TimelineType timeline, String max_id, ChannelData.Channel forChannel, AccountData.Account forAccount) {
2020-09-25 18:58:04 +02:00
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitPeertubeAPI retrofitPeertubeAPI;
2020-11-28 16:41:21 +01:00
String acct = null;
if (forChannel != null) {
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, forChannel.getHost(), null);
2020-11-28 16:41:21 +01:00
acct = forChannel.getAcct();
} else if (forAccount != null) {
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, forAccount.getHost(), null);
acct = forAccount.getAcct();
} else {
retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
}
2020-09-25 18:58:04 +02:00
if (timeline == null)
return;
APIResponse apiResponse;
2020-11-28 16:41:21 +01:00
apiResponse = retrofitPeertubeAPI.getTL(timeline, max_id, acct);
2020-09-27 11:11:39 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
2020-11-12 17:08:43 +01:00
e.printStackTrace();
}
}).start();
}
private void loadHistory(String max_id, String startDate, String endDate) {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
APIResponse apiResponse = retrofitPeertubeAPI.getHistory(max_id, startDate, endDate);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
2020-09-27 11:11:39 +02:00
e.printStackTrace();
}
}).start();
}
private void deleteHistory() {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
APIResponse apiResponse = retrofitPeertubeAPI.deleteHistory();
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
2020-09-27 11:11:39 +02:00
}
}).start();
}
private void loadOverviewVideos(String page) {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
APIResponse apiResponse = retrofitPeertubeAPI.getOverviewVideo(page);
2020-09-25 18:58:04 +02:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
2020-09-26 10:22:11 +02:00
public enum TimelineType {
2020-10-16 17:03:07 +02:00
CHANNEL_VIDEOS,
ACCOUNT_VIDEOS,
2020-09-26 10:22:11 +02:00
SUBSCRIBTIONS,
MY_VIDEOS,
LOCAL,
TRENDING,
MOST_LIKED,
HISTORY,
2020-10-06 15:30:15 +02:00
RECENT,
2020-10-09 18:22:05 +02:00
VIDEOS_IN_PLAYLIST,
2020-11-14 12:19:37 +01:00
VIDEOS_IN_LOCAL_PLAYLIST,
2020-10-09 18:22:05 +02:00
SEPIA_SEARCH
2020-09-26 10:22:11 +02:00
}
2020-09-25 18:58:04 +02:00
}