fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/PlaylistsActivity.java

237 lines
9.9 KiB
Java
Raw Normal View History

2019-05-26 15:56:02 +02:00
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Fedilab
*
* 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
package app.fedilab.android.activities;
2019-05-27 19:42:41 +02:00
import android.content.Context;
2019-05-26 15:56:02 +02:00
import android.content.SharedPreferences;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
2019-05-26 15:56:02 +02:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
2019-05-27 19:42:41 +02:00
import android.view.ViewGroup;
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2019-05-26 15:56:02 +02:00
import android.widget.RelativeLayout;
2019-05-27 19:42:41 +02:00
import android.widget.TextView;
2019-05-26 15:56:02 +02:00
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2019-05-26 15:56:02 +02:00
import java.util.ArrayList;
import java.util.List;
import app.fedilab.android.R;
2019-05-26 17:56:02 +02:00
import app.fedilab.android.asynctasks.ManagePlaylistsAsyncTask;
2019-05-26 15:56:02 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Peertube;
2019-05-26 17:56:02 +02:00
import app.fedilab.android.client.Entities.Playlist;
2019-05-26 15:56:02 +02:00
import app.fedilab.android.drawers.PeertubeAdapter;
import app.fedilab.android.helper.Helper;
2019-05-26 17:56:02 +02:00
import app.fedilab.android.interfaces.OnPlaylistActionInterface;
2019-05-26 15:56:02 +02:00
import es.dmoral.toasty.Toasty;
2019-05-26 18:05:27 +02:00
import static app.fedilab.android.asynctasks.ManagePlaylistsAsyncTask.action.GET_LIST_VIDEOS;
2019-05-26 15:56:02 +02:00
/**
* Created by Thomas on 26/05/2019.
* Display playlists for Peertube
*/
2019-05-26 17:56:02 +02:00
public class PlaylistsActivity extends BaseActivity implements OnPlaylistActionInterface {
2019-05-26 15:56:02 +02:00
2019-11-15 16:32:25 +01:00
LinearLayoutManager mLayoutManager;
2019-05-26 15:56:02 +02:00
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private List<Peertube> peertubes;
private String max_id;
2019-05-26 17:56:02 +02:00
private Playlist playlist;
2019-05-26 15:56:02 +02:00
private boolean firstLoad;
private boolean flag_loading;
private PeertubeAdapter peertubeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2019-05-26 15:56:02 +02:00
case Helper.THEME_LIGHT:
2019-07-20 15:15:47 +02:00
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
2019-05-26 15:56:02 +02:00
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
}
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2019-05-27 19:42:41 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
2019-09-06 17:55:14 +02:00
if (actionBar != null) {
2019-05-27 19:42:41 +02:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
2020-04-08 12:42:15 +02:00
View view = inflater.inflate(R.layout.simple_bar, new LinearLayout(PlaylistsActivity.this), false);
2019-11-15 19:36:39 +01:00
view.setBackground(new ColorDrawable(ContextCompat.getColor(PlaylistsActivity.this, R.color.cyanea_primary)));
2019-05-27 19:42:41 +02:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
2020-06-17 18:20:35 +02:00
toolbar_close.setOnClickListener(v -> finish());
2019-05-27 19:42:41 +02:00
toolbar_title.setText(R.string.upload_video);
2019-11-07 20:23:07 +01:00
2019-05-27 19:42:41 +02:00
}
2019-05-26 15:56:02 +02:00
setContentView(R.layout.activity_playlists);
Toolbar toolbar = findViewById(R.id.toolbar);
2019-11-20 12:11:37 +01:00
toolbar.setBackgroundColor(ContextCompat.getColor(PlaylistsActivity.this, R.color.cyanea_primary));
2019-05-26 15:56:02 +02:00
setSupportActionBar(toolbar);
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2019-05-26 15:56:02 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
peertubes = new ArrayList<>();
RecyclerView lv_playlist = findViewById(R.id.lv_playlist);
2019-09-06 17:55:14 +02:00
mainLoader = findViewById(R.id.loader);
2019-05-26 15:56:02 +02:00
nextElementLoader = findViewById(R.id.loading_next_status);
2019-09-06 17:55:14 +02:00
textviewNoAction = findViewById(R.id.no_action);
2019-05-26 15:56:02 +02:00
mainLoader.setVisibility(View.VISIBLE);
swipeRefreshLayout = findViewById(R.id.swipeContainer);
2019-11-12 19:22:14 +01:00
int c1 = getResources().getColor(R.color.cyanea_accent);
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
int c3 = getResources().getColor(R.color.cyanea_primary);
2019-11-12 19:08:05 +01:00
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
swipeRefreshLayout.setColorSchemeColors(
c1, c2, c1
);
2019-05-26 15:56:02 +02:00
max_id = null;
flag_loading = true;
firstLoad = true;
swiped = false;
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2019-08-19 09:11:23 +02:00
peertubeAdapter = new PeertubeAdapter(Helper.getLiveInstance(PlaylistsActivity.this), false, this.peertubes);
2019-05-26 15:56:02 +02:00
lv_playlist.setAdapter(peertubeAdapter);
mLayoutManager = new LinearLayoutManager(PlaylistsActivity.this);
lv_playlist.setLayoutManager(mLayoutManager);
Bundle b = getIntent().getExtras();
2019-09-06 17:55:14 +02:00
if (b != null) {
2019-05-26 17:56:02 +02:00
playlist = b.getParcelable("playlist");
2019-09-06 17:55:14 +02:00
} else {
2020-04-08 12:42:15 +02:00
Toasty.error(PlaylistsActivity.this, getString(R.string.toast_error_search), Toast.LENGTH_LONG).show();
2019-05-26 18:05:27 +02:00
return;
2019-05-26 15:56:02 +02:00
}
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2019-05-26 15:56:02 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2019-05-27 19:42:41 +02:00
setTitle(playlist.getDisplayName());
2019-05-26 15:56:02 +02:00
lv_playlist.addOnScrollListener(new RecyclerView.OnScrollListener() {
2019-09-06 17:55:14 +02:00
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
2019-05-26 15:56:02 +02:00
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
2019-09-06 17:55:14 +02:00
if (dy > 0) {
2019-05-26 15:56:02 +02:00
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
2019-09-06 17:55:14 +02:00
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
2019-05-26 15:56:02 +02:00
flag_loading = true;
2021-01-19 17:43:51 +01:00
new ManagePlaylistsAsyncTask(PlaylistsActivity.this, GET_LIST_VIDEOS, playlist, null, max_id, PlaylistsActivity.this);
2019-05-26 15:56:02 +02:00
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
nextElementLoader.setVisibility(View.GONE);
}
}
}
});
2020-06-17 18:20:35 +02:00
swipeRefreshLayout.setOnRefreshListener(() -> {
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = true;
MainActivity.countNewStatus = 0;
2021-01-19 17:43:51 +01:00
new ManagePlaylistsAsyncTask(PlaylistsActivity.this, GET_LIST_VIDEOS, playlist, null, null, PlaylistsActivity.this);
2019-05-26 15:56:02 +02:00
});
2021-01-19 17:43:51 +01:00
new ManagePlaylistsAsyncTask(PlaylistsActivity.this, GET_LIST_VIDEOS, playlist, null, null, PlaylistsActivity.this);
2019-05-26 15:56:02 +02:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2020-06-17 18:20:35 +02:00
if (item.getItemId() == android.R.id.home) {
finish();
return true;
2019-05-26 15:56:02 +02:00
}
2020-06-17 18:20:35 +02:00
return super.onOptionsItemSelected(item);
2019-05-26 15:56:02 +02:00
}
@Override
2019-05-26 17:56:02 +02:00
public void onActionDone(ManagePlaylistsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
2019-05-26 15:56:02 +02:00
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
if (apiResponse.getError() != null) {
2020-03-08 10:29:06 +01:00
if (!apiResponse.getError().getError().startsWith("404 -") && !apiResponse.getError().getError().startsWith("501 -"))
2020-04-08 12:42:15 +02:00
Toasty.error(PlaylistsActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-05-26 15:56:02 +02:00
swipeRefreshLayout.setRefreshing(false);
swiped = false;
flag_loading = false;
return;
}
2019-09-06 17:55:14 +02:00
if (actionType == GET_LIST_VIDEOS) {
2019-05-26 15:56:02 +02:00
2019-05-26 17:56:02 +02:00
int previousPosition = this.peertubes.size();
2019-05-26 18:05:27 +02:00
List<Peertube> videos = apiResponse.getPeertubes();
2019-05-26 15:56:02 +02:00
max_id = apiResponse.getMax_id();
flag_loading = (max_id == null);
2019-05-26 18:05:27 +02:00
if (!swiped && firstLoad && (videos == null || videos.size() == 0))
2019-05-26 15:56:02 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
if (swiped) {
if (previousPosition > 0) {
2020-06-17 18:20:35 +02:00
this.peertubes.subList(0, previousPosition).clear();
2019-05-26 18:05:27 +02:00
peertubeAdapter.notifyItemRangeRemoved(0, previousPosition);
2019-05-26 15:56:02 +02:00
}
swiped = false;
}
2019-05-26 18:05:27 +02:00
if (videos != null && videos.size() > 0) {
this.peertubes.addAll(videos);
peertubeAdapter.notifyItemRangeInserted(previousPosition, videos.size());
2019-05-26 15:56:02 +02:00
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
}
}
}