From 150a81adc6b18684ab0c70ea7fb55da3af77ebcc Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 12 Nov 2020 17:08:43 +0100 Subject: [PATCH] Some fixes --- .../fedilabtube/VideosTimelineActivity.java | 70 +++++++++++++++--- .../fedilabtube/client/PeertubeService.java | 8 ++- .../client/RetrofitPeertubeAPI.java | 24 ++++++- .../fragment/DisplayVideosFragment.java | 7 ++ .../fedilabtube/viewmodel/TimelineVM.java | 22 ++++++ .../res/drawable/background_rounded_item.xml | 7 ++ .../res/layout/activity_search_result.xml | 71 ++++++++++++++++++- 7 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/drawable/background_rounded_item.xml diff --git a/app/src/main/java/app/fedilab/fedilabtube/VideosTimelineActivity.java b/app/src/main/java/app/fedilab/fedilabtube/VideosTimelineActivity.java index 7a9e4d3..e6768f5 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/VideosTimelineActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/VideosTimelineActivity.java @@ -16,24 +16,37 @@ package app.fedilab.fedilabtube; import android.os.Bundle; import android.view.MenuItem; +import android.view.View; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Locale; + +import app.fedilab.fedilabtube.databinding.ActivitySearchResultBinding; import app.fedilab.fedilabtube.fragment.DisplayVideosFragment; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.viewmodel.TimelineVM; +import static app.fedilab.fedilabtube.viewmodel.TimelineVM.TimelineType.HISTORY; + public class VideosTimelineActivity extends AppCompatActivity { private TimelineVM.TimelineType type; + private DisplayVideosFragment displayVideosFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_search_result); + ActivitySearchResultBinding binding = ActivitySearchResultBinding.inflate(getLayoutInflater()); + View mainView = binding.getRoot(); + setContentView(mainView); if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -41,22 +54,63 @@ public class VideosTimelineActivity extends AppCompatActivity { Bundle b = getIntent().getExtras(); if (b != null) type = (TimelineVM.TimelineType) b.get("type"); + displayVideosFragment = null; + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + if (savedInstanceState == null) { + displayVideosFragment = new DisplayVideosFragment(); + Bundle bundle = new Bundle(); + bundle.putSerializable(Helper.TIMELINE_TYPE, type); + displayVideosFragment.setArguments(bundle); + ft.add(R.id.container, displayVideosFragment).addToBackStack(null).commit(); + } if (type == TimelineVM.TimelineType.MY_VIDEOS) { setTitle(R.string.my_videos); - } else if (type == TimelineVM.TimelineType.HISTORY) { + } else if (type == HISTORY) { setTitle(R.string.my_history); + //TODO: uncomment when available + // binding.historyFilter.setVisibility(View.VISIBLE); + binding.historyFilterAll.setOnClickListener(v -> historyFilter(null)); + binding.historyFilterToday.setOnClickListener(v -> { + Calendar cal = GregorianCalendar.getInstance(); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.getTime(); + historyFilter(cal.getTime()); + }); + binding.historyFilterLast7Days.setOnClickListener(v -> { + Calendar cal = GregorianCalendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DAY_OF_YEAR, -7); + cal.getTime(); + historyFilter(cal.getTime()); + }); + } else if (type == TimelineVM.TimelineType.MOST_LIKED) { setTitle(R.string.title_most_liked); } - if (savedInstanceState == null) { - DisplayVideosFragment displayVideosFragment = new DisplayVideosFragment(); - Bundle bundle = new Bundle(); - bundle.putSerializable(Helper.TIMELINE_TYPE, type); - displayVideosFragment.setArguments(bundle); + + } + + private void historyFilter(Date date) { + String startDate = null; + if (date != null) { + SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH); + startDate = fmtOut.format(date); + } + if (displayVideosFragment != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); - ft.add(R.id.container, displayVideosFragment).commit(); + displayVideosFragment = new DisplayVideosFragment(); + Bundle bundle = new Bundle(); + bundle.putSerializable(Helper.TIMELINE_TYPE, HISTORY); + bundle.putSerializable("startDate", startDate); + displayVideosFragment.setArguments(bundle); + ft.replace(R.id.container, displayVideosFragment); + ft.addToBackStack(null); + ft.commit(); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java index 9bb94d0..711800f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java @@ -175,7 +175,13 @@ public interface PeertubeService { //History @GET("users/me/history/videos") - Call getHistory(@Header("Authorization") String credentials, @Query("start") String maxId, @Query("count") String count); + Call getHistory( + @Header("Authorization") String credentials, + @Query("start") String maxId, + @Query("count") String count, + @Query("startDate") String startDate, + @Query("endDate") String endDate + ); //Search @GET("search/videos") diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index ebc97c9..308dc53 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -347,6 +347,28 @@ public class RetrofitPeertubeAPI { return apiResponse; } + public APIResponse getHistory(String max_id, String startDate, String endDate) { + APIResponse apiResponse = new APIResponse(); + PeertubeService peertubeService = init(); + Call videoCall = peertubeService.getHistory(getToken(), max_id, count, startDate, endDate); + if (videoCall != null) { + try { + Response response = videoCall.execute(); + if (response.isSuccessful() && response.body() != null) { + apiResponse.setPeertubes(response.body().data); + } else { + setError(apiResponse, response.code(), response.errorBody()); + } + } catch (IOException e) { + Error error = new Error(); + error.setError(_context.getString(R.string.toast_error)); + apiResponse.setError(error); + e.printStackTrace(); + } + } + return apiResponse; + } + public APIResponse getTL(TimelineVM.TimelineType timelineType, String max_id, String forAccount) { APIResponse apiResponse = new APIResponse(); PeertubeService peertubeService = init(); @@ -376,7 +398,7 @@ public class RetrofitPeertubeAPI { videoCall = peertubeService.getTrendingVideos(getToken(), max_id, count, filter); break; case HISTORY: - videoCall = peertubeService.getHistory(getToken(), max_id, count); + videoCall = peertubeService.getHistory(getToken(), max_id, count, null, null); break; case RECENT: videoCall = peertubeService.getRecentlyAddedVideos(getToken(), max_id, count, filter); diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayVideosFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayVideosFragment.java index 9703835..a46a950 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayVideosFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayVideosFragment.java @@ -99,6 +99,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta private String playlistId; private String remoteInstance; private boolean sepiaSearch; + private String startDate, endDate; public DisplayVideosFragment() { } @@ -112,6 +113,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta peertubes = new ArrayList<>(); channels = new ArrayList<>(); context = getContext(); + startDate = null; + endDate = null; Bundle bundle = this.getArguments(); if (bundle != null) { search_peertube = bundle.getString("search_peertube", null); @@ -120,6 +123,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta sepiaSearch = bundle.getBoolean("sepia_search", false); type = (TimelineVM.TimelineType) bundle.get(Helper.TIMELINE_TYPE); playlistId = bundle.getString("playlistId", null); + startDate = bundle.getString("startDate", null); + endDate = bundle.getString("endDate", null); } max_id = "0"; forAccount = type == TimelineVM.TimelineType.ACCOUNT_VIDEOS ? channelId : null; @@ -502,6 +507,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta viewModelFeeds.getVideosInChannel(sepiaSearch ? remoteInstance : null, channelId, max_id).observe(this.requireActivity(), this::manageVIewVideos); } else if (type == TimelineVM.TimelineType.VIDEOS_IN_PLAYLIST) { viewModelFeeds.loadVideosInPlaylist(playlistId, max_id).observe(this.requireActivity(), this::manageVIewVideos); + } else if (type == TimelineVM.TimelineType.HISTORY) { + viewModelFeeds.getVideoHistory(max_id, startDate, endDate).observe(this.requireActivity(), this::manageVIewVideos); } else { viewModelFeeds.getVideos(type, max_id, forAccount).observe(this.requireActivity(), this::manageVIewVideos); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java index b50df63..ad6f334 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java @@ -51,6 +51,12 @@ public class TimelineVM extends AndroidViewModel { } + public LiveData getVideoHistory(String max_id, String startDate, String endDate) { + apiResponseMutableLiveData = new MutableLiveData<>(); + loadHistory(max_id, startDate, endDate); + return apiResponseMutableLiveData; + } + public LiveData getVideo(String instance, String videoId, boolean isMyVideo) { apiResponseMutableLiveData = new MutableLiveData<>(); getSingle(instance, videoId, isMyVideo); @@ -156,6 +162,22 @@ public class TimelineVM extends AndroidViewModel { }).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) { + e.printStackTrace(); + } + }).start(); + } + private void loadOverviewVideos(String page) { Context _mContext = getApplication().getApplicationContext(); diff --git a/app/src/main/res/drawable/background_rounded_item.xml b/app/src/main/res/drawable/background_rounded_item.xml new file mode 100644 index 0000000..2ea72dd --- /dev/null +++ b/app/src/main/res/drawable/background_rounded_item.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_search_result.xml b/app/src/main/res/layout/activity_search_result.xml index f38a6ec..dcd181a 100644 --- a/app/src/main/res/layout/activity_search_result.xml +++ b/app/src/main/res/layout/activity_search_result.xml @@ -14,7 +14,72 @@ You should have received a copy of the GNU General Public License along with TubeLab; if not, see . --> - \ No newline at end of file + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_height="match_parent"> + + + + + + + + + + + + + + + + + + + \ No newline at end of file