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>. */
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
|
|
|
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;
|
|
|
|
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
|
2020-09-27 14:12:06 +02:00
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
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-09-26 10:22:11 +02:00
|
|
|
public LiveData<APIResponse> getVideos(TimelineType action, String max_id) {
|
2020-09-25 18:58:04 +02:00
|
|
|
apiResponseMutableLiveData = new MutableLiveData<>();
|
|
|
|
loadVideos(action, max_id);
|
|
|
|
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
|
|
|
|
|
|
|
public LiveData<APIResponse> getVideo(String videoId) {
|
|
|
|
apiResponseMutableLiveData = new MutableLiveData<>();
|
2020-09-29 17:42:15 +02:00
|
|
|
getSingle(videoId, false);
|
|
|
|
return apiResponseMutableLiveData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<APIResponse> getMyVideo(String videoId) {
|
|
|
|
apiResponseMutableLiveData = new MutableLiveData<>();
|
|
|
|
getSingle(videoId, true);
|
2020-09-25 18:58:04 +02:00
|
|
|
return apiResponseMutableLiveData;
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
public LiveData<APIResponse> getVideosInChannel(String channelId, String max_id) {
|
|
|
|
apiResponseMutableLiveData = new MutableLiveData<>();
|
|
|
|
loadVideosForChannel(channelId, max_id);
|
|
|
|
return apiResponseMutableLiveData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadVideosForChannel(String channelId, String max_id) {
|
|
|
|
Context _mContext = getApplication().getApplicationContext();
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
|
|
|
|
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-09-29 17:42:15 +02:00
|
|
|
private void getSingle(String videoId, boolean myVideo) {
|
2020-09-25 18:58:04 +02:00
|
|
|
Context _mContext = getApplication().getApplicationContext();
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
|
2020-09-29 17:42:15 +02:00
|
|
|
APIResponse apiResponse = retrofitPeertubeAPI.getVideos(videoId, myVideo);
|
|
|
|
if (Helper.isLoggedIn(_mContext) && !myVideo) {
|
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-09-26 10:22:11 +02:00
|
|
|
private void loadVideos(TimelineType timeline, String max_id) {
|
2020-09-25 18:58:04 +02:00
|
|
|
Context _mContext = getApplication().getApplicationContext();
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext);
|
|
|
|
if (timeline == null)
|
|
|
|
return;
|
|
|
|
APIResponse apiResponse;
|
2020-09-27 11:11:39 +02:00
|
|
|
apiResponse = retrofitPeertubeAPI.getTL(timeline, max_id);
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse);
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).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-09-26 16:46:51 +02:00
|
|
|
USER_VIDEOS,
|
2020-09-26 10:22:11 +02:00
|
|
|
SUBSCRIBTIONS,
|
|
|
|
MY_VIDEOS,
|
|
|
|
LOCAL,
|
|
|
|
TRENDING,
|
|
|
|
MOST_LIKED,
|
|
|
|
HISTORY,
|
|
|
|
RECENT
|
|
|
|
}
|
2020-09-25 18:58:04 +02:00
|
|
|
}
|