From b59d97279d13c18e9efa73de50cad9608b979835 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 28 Nov 2020 09:20:44 +0100 Subject: [PATCH] Fix issue #115 - Filter with remote accounts does not work --- app/build.gradle | 4 +- .../fedilabtube/ShowAccountActivity.java | 2 +- .../fedilabtube/ShowChannelActivity.java | 2 +- .../drawer/AccountsHorizontalListAdapter.java | 41 +++--- .../fragment/DisplaySepiaSearchFragment.java | 3 +- .../fragment/DisplayVideosFragment.java | 136 +++++++++--------- .../fedilabtube/viewmodel/TimelineVM.java | 16 ++- app/src/main/res/layout/fragment_video.xml | 4 +- 8 files changed, 100 insertions(+), 108 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c8fcf91..40e2fe5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,8 @@ android { minSdkVersion 21 targetSdkVersion 30 - versionCode 27 - versionName "1.8.0" + versionCode 28 + versionName "1.9.0-beta-1" multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java index e9f41ca..f1a9046 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java @@ -294,7 +294,7 @@ public class ShowAccountActivity extends AppCompatActivity { } DisplayVideosFragment displayVideosFragment = new DisplayVideosFragment(); bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.ACCOUNT_VIDEOS); - bundle.putString("channelId", account.getAcct()); + bundle.putParcelable("account", account); bundle.putString("peertube_instance", account.getHost()); displayVideosFragment.setArguments(bundle); return displayVideosFragment; diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java index faae6f7..4912d97 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java @@ -493,7 +493,7 @@ public class ShowChannelActivity extends AppCompatActivity { DisplayVideosFragment displayVideosFragment = new DisplayVideosFragment(); Bundle bundle = new Bundle(); bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.CHANNEL_VIDEOS); - bundle.putString("channelId", channel.getAcct()); + bundle.putParcelable("channel", channel); bundle.putString("peertube_instance", channel.getHost()); bundle.putBoolean("sepia_search", sepiaSearch); displayVideosFragment.setArguments(bundle); diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java index 9bf0711..fac5466 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java @@ -19,19 +19,13 @@ import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import androidx.core.graphics.ColorUtils; import androidx.recyclerview.widget.RecyclerView; - import java.util.List; - -import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.client.data.ChannelData; +import app.fedilab.fedilabtube.databinding.DrawerHorizontalAccountBinding; import app.fedilab.fedilabtube.helper.Helper; @@ -50,8 +44,8 @@ public class AccountsHorizontalListAdapter extends RecyclerView.Adapter peertubes; private List channels; private TimelineVM.TimelineType type; - private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private boolean firstLoad; - private SwipeRefreshLayout swipeRefreshLayout; - private SharedPreferences sharedpreferences; private String search_peertube; - private TextView textviewNoActionText; - private View rootView; - private RecyclerView lv_status; private boolean check_ScrollingUp; - private String forAccount; - private ConstraintLayout top_account_container; + private ChannelData.Channel forChannel; private TimelineVM viewModelFeeds; private SearchVM viewModelSearch; private AccountsVM viewModelAccounts; - private String channelId; + private ChannelData.Channel channel; + private AccountData.Account account; private Map relationship; private Map> playlists; private String playlistId; private String remoteInstance; private boolean sepiaSearch; private String startDate, endDate; + private FragmentVideoBinding binding; + private String channelId; public DisplayVideosFragment() { } @@ -108,9 +102,14 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - rootView = inflater.inflate(R.layout.fragment_video, container, false); + binding = FragmentVideoBinding.inflate(inflater, container, false); + return binding.getRoot(); + } + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + peertubes = new ArrayList<>(); channels = new ArrayList<>(); context = getContext(); @@ -119,7 +118,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta Bundle bundle = this.getArguments(); if (bundle != null) { search_peertube = bundle.getString("search_peertube", null); - channelId = bundle.getString("channelId", null); + channel = bundle.getParcelable("channel"); + account = bundle.getParcelable("account"); remoteInstance = bundle.getString("peertube_instance", null); sepiaSearch = bundle.getBoolean("sepia_search", false); type = (TimelineVM.TimelineType) bundle.get(Helper.TIMELINE_TYPE); @@ -127,54 +127,48 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta startDate = bundle.getString("startDate", null); endDate = bundle.getString("endDate", null); } + if (channel != null) { + channelId = channel.getAcct(); + } else if (account != null) { + channelId = account.getAcct(); + } + max_id = "0"; - forAccount = type == TimelineVM.TimelineType.ACCOUNT_VIDEOS ? channelId : null; - lv_status = rootView.findViewById(R.id.lv_status); - RecyclerView lv_accounts = rootView.findViewById(R.id.lv_accounts); - Button display_all = rootView.findViewById(R.id.display_all); - top_account_container = rootView.findViewById(R.id.top_account_container); + // forChannel = type == TimelineVM.TimelineType.ACCOUNT_VIDEOS ? channelId : null; max_id_accounts = null; flag_loading = true; firstLoad = true; check_ScrollingUp = false; - assert context != null; - sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); - - mainLoader = rootView.findViewById(R.id.loader); - nextElementLoader = rootView.findViewById(R.id.loading_next_status); - textviewNoAction = rootView.findViewById(R.id.no_action); - textviewNoActionText = rootView.findViewById(R.id.no_action_text); - mainLoader.setVisibility(View.VISIBLE); - nextElementLoader.setVisibility(View.GONE); + binding.loader.setVisibility(View.VISIBLE); + binding.loadingNextVideos.setVisibility(View.GONE); peertubeAdapater = new PeertubeAdapter(this.peertubes, type, sepiaSearch); peertubeAdapater.playlistListener = this; peertubeAdapater.relationShipListener = this; - lv_status.setAdapter(peertubeAdapater); + binding.lvVideos.setAdapter(peertubeAdapater); accountsHorizontalListAdapter = new AccountsHorizontalListAdapter(this.channels, this); LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); - lv_accounts.setLayoutManager(layoutManager); - lv_accounts.setAdapter(accountsHorizontalListAdapter); + binding.lvAccounts.setLayoutManager(layoutManager); + binding.lvAccounts.setAdapter(accountsHorizontalListAdapter); if (!Helper.isTablet(context)) { mLayoutManager = new LinearLayoutManager(context); - lv_status.setLayoutManager(mLayoutManager); + binding.lvVideos.setLayoutManager(mLayoutManager); } else { gLayoutManager = new GridLayoutManager(context, 2); int spanCount = (int) Helper.convertDpToPixel(2, context); int spacing = (int) Helper.convertDpToPixel(5, context); - lv_status.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true)); - lv_status.setLayoutManager(gLayoutManager); + binding.lvVideos.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true)); + binding.lvVideos.setLayoutManager(gLayoutManager); } viewModelAccounts = new ViewModelProvider(DisplayVideosFragment.this).get(AccountsVM.class); viewModelFeeds = new ViewModelProvider(DisplayVideosFragment.this).get(TimelineVM.class); viewModelSearch = new ViewModelProvider(DisplayVideosFragment.this).get(SearchVM.class); - swipeRefreshLayout.setOnRefreshListener(() -> pullToRefresh(true)); + binding.swipeContainer.setOnRefreshListener(() -> pullToRefresh(true)); - lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { + binding.lvAccounts.addOnScrollListener(new RecyclerView.OnScrollListener() { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { int firstVisibleItem = layoutManager.findFirstVisibleItemPosition(); if (dy > 0) { @@ -189,19 +183,19 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta if (type != VIDEOS_IN_LOCAL_PLAYLIST) { - lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() { + binding.lvVideos.addOnScrollListener(new RecyclerView.OnScrollListener() { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { if (type == TimelineVM.TimelineType.SUBSCRIBTIONS) { if (dy > 0) { if (check_ScrollingUp) { - top_account_container.setVisibility(View.GONE); + binding.topAccountContainer.setVisibility(View.GONE); final Handler handler = new Handler(); handler.postDelayed(() -> check_ScrollingUp = false, 300); } } else { if (!check_ScrollingUp) { - top_account_container.setVisibility(View.VISIBLE); + binding.topAccountContainer.setVisibility(View.VISIBLE); final Handler handler = new Handler(); handler.postDelayed(() -> check_ScrollingUp = true, 300); } @@ -216,10 +210,10 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta if (!flag_loading) { flag_loading = true; loadTimeline(max_id); - nextElementLoader.setVisibility(View.VISIBLE); + binding.loadingNextVideos.setVisibility(View.VISIBLE); } } else { - nextElementLoader.setVisibility(View.GONE); + binding.loadingNextVideos.setVisibility(View.GONE); } } } else if (gLayoutManager != null) { @@ -231,10 +225,10 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta if (!flag_loading) { flag_loading = true; loadTimeline(max_id); - nextElementLoader.setVisibility(View.VISIBLE); + binding.loadingNextVideos.setVisibility(View.VISIBLE); } } else { - nextElementLoader.setVisibility(View.GONE); + binding.loadingNextVideos.setVisibility(View.GONE); } } } @@ -246,27 +240,26 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta viewModel.getAccounts(RetrofitPeertubeAPI.DataType.SUBSCRIBER, max_id).observe(DisplayVideosFragment.this.requireActivity(), this::manageViewAccounts); } loadTimeline(max_id); - display_all.setOnClickListener(v -> { - forAccount = null; + binding.displayAll.setOnClickListener(v -> { + forChannel = null; pullToRefresh(false); }); - return rootView; } @Override public void onResume() { super.onResume(); - swipeRefreshLayout.setEnabled(true); + binding.swipeContainer.setEnabled(true); } @Override public void onPause() { super.onPause(); - if (swipeRefreshLayout != null) { - swipeRefreshLayout.setEnabled(false); - swipeRefreshLayout.setRefreshing(false); - swipeRefreshLayout.clearAnimation(); + if (binding.swipeContainer != null) { + binding.swipeContainer.setEnabled(false); + binding.swipeContainer.setRefreshing(false); + binding.swipeContainer.clearAnimation(); } if (getActivity() != null) { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); @@ -303,8 +296,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta private void manageViewAccounts(APIResponse apiResponse) { if (apiResponse != null && apiResponse.getChannels() != null && apiResponse.getChannels().size() > 0) { - if (top_account_container.getVisibility() == View.GONE) { - top_account_container.setVisibility(View.VISIBLE); + if (binding.topAccountContainer.getVisibility() == View.GONE) { + binding.topAccountContainer.setVisibility(View.VISIBLE); } int previousPosition = channels.size(); channels.addAll(apiResponse.getChannels()); @@ -312,6 +305,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta if (max_id_accounts == null) { max_id_accounts = "0"; } + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); //max_id_accounts needs to work like an offset int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE); max_id_accounts = String.valueOf(Integer.parseInt(max_id_accounts) + tootPerPage); @@ -321,8 +315,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta private void manageVIewVideos(APIResponse apiResponse) { //hide loaders - mainLoader.setVisibility(View.GONE); - nextElementLoader.setVisibility(View.GONE); + binding.loader.setVisibility(View.GONE); + binding.loadingNextVideos.setVisibility(View.GONE); //handle other API error if (this.peertubes == null || apiResponse == null || (apiResponse.getError() != null)) { if (apiResponse == null) @@ -330,7 +324,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta else { Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); } - swipeRefreshLayout.setRefreshing(false); + binding.swipeContainer.setRefreshing(false); flag_loading = false; return; } @@ -338,6 +332,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta if (max_id == null) max_id = "0"; //max_id needs to work like an offset + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int videoPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE); max_id = String.valueOf(Integer.parseInt(max_id) + videoPerPage); if (apiResponse.getPeertubes() == null && apiResponse.getVideoPlaylist() == null) { @@ -365,15 +360,15 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta peertubeAdapater = new PeertubeAdapter(this.peertubes, type, sepiaSearch); peertubeAdapater.playlistListener = DisplayVideosFragment.this; peertubeAdapater.relationShipListener = DisplayVideosFragment.this; - lv_status.setAdapter(peertubeAdapater); + binding.lvVideos.setAdapter(peertubeAdapater); } else peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size()); //remove handlers - swipeRefreshLayout.setRefreshing(false); - textviewNoAction.setVisibility(View.GONE); + binding.swipeContainer.setRefreshing(false); + binding.noAction.setVisibility(View.GONE); if (firstLoad && (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0)) { - textviewNoActionText.setText(R.string.no_video_to_display); - textviewNoAction.setVisibility(View.VISIBLE); + binding.noActionText.setText(R.string.no_video_to_display); + binding.noAction.setVisibility(View.VISIBLE); } flag_loading = false; firstLoad = false; @@ -438,14 +433,13 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta @Override public void onDestroyView() { - if (lv_status != null) { + if (binding.lvVideos != null) { try { - lv_status.setAdapter(null); + binding.lvVideos.setAdapter(null); } catch (Exception ignored) { } } super.onDestroyView(); - rootView = null; } @@ -471,7 +465,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta peertubes = new ArrayList<>(); max_id = "0"; peertubeAdapater.notifyItemRangeRemoved(0, size); - if (forAccount == null) { + if (forChannel == null) { for (ChannelData.Channel channel : channels) { channel.setSelected(false); } @@ -483,8 +477,8 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta } @Override - public void click(String forAccount) { - this.forAccount = forAccount; + public void click(ChannelData.Channel forChannel) { + this.forChannel = forChannel; pullToRefresh(false); } @@ -504,7 +498,7 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta } 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); + viewModelFeeds.getVideos(type, max_id, forChannel).observe(this.requireActivity(), this::manageVIewVideos); } } else { viewModelSearch.getVideos(max_id, search_peertube).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 82e08b2..eba1e1f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java @@ -30,6 +30,7 @@ import java.util.List; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; +import app.fedilab.fedilabtube.client.data.ChannelData; import app.fedilab.fedilabtube.client.data.VideoData; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.sqlite.ManagePlaylistsDAO; @@ -45,9 +46,9 @@ public class TimelineVM extends AndroidViewModel { super(application); } - public LiveData getVideos(TimelineType action, String max_id, String forAccount) { + public LiveData getVideos(TimelineType action, String max_id, ChannelData.Channel forChannel) { apiResponseMutableLiveData = new MutableLiveData<>(); - loadVideos(action, max_id, forAccount); + loadVideos(action, max_id, forChannel); return apiResponseMutableLiveData; } @@ -189,15 +190,20 @@ public class TimelineVM extends AndroidViewModel { }).start(); } - private void loadVideos(TimelineType timeline, String max_id, String forAccount) { + private void loadVideos(TimelineType timeline, String max_id, ChannelData.Channel forChannel) { Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { - RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext); + RetrofitPeertubeAPI retrofitPeertubeAPI; + if (forChannel != null) { + retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, forChannel.getHost(), null); + } else { + retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext); + } if (timeline == null) return; APIResponse apiResponse; - apiResponse = retrofitPeertubeAPI.getTL(timeline, max_id, forAccount); + apiResponse = retrofitPeertubeAPI.getTL(timeline, max_id, forChannel != null ? forChannel.getAcct() : null); Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse); mainHandler.post(myRunnable); diff --git a/app/src/main/res/layout/fragment_video.xml b/app/src/main/res/layout/fragment_video.xml index 484ec24..cc8e24c 100644 --- a/app/src/main/res/layout/fragment_video.xml +++ b/app/src/main/res/layout/fragment_video.xml @@ -68,7 +68,7 @@ android:layout_height="match_parent"> @@ -109,7 +109,7 @@