TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/activities/AllPlaylistsActivity.java

432 lines
19 KiB
Java
Raw Normal View History

2022-05-05 08:53:25 +02:00
package app.fedilab.fedilabtube.activities;
2020-07-01 16:36:08 +02:00
/* 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>. */
2020-06-30 17:26:20 +02:00
2022-05-05 08:53:25 +02:00
import static app.fedilab.fedilabtube.activities.PeertubeUploadActivity.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE;
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
import android.Manifest;
import android.app.Activity;
2020-06-30 17:26:20 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
2020-06-30 17:26:20 +02:00
import android.os.Bundle;
2020-09-04 15:56:18 +02:00
import android.os.Handler;
import android.os.Looper;
2020-06-30 17:26:20 +02:00
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
2020-09-12 16:53:04 +02:00
import android.widget.Button;
2020-06-30 17:26:20 +02:00
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2020-09-07 16:57:00 +02:00
import androidx.lifecycle.ViewModelProvider;
2020-11-14 12:19:37 +01:00
import androidx.recyclerview.widget.LinearLayoutManager;
2020-06-30 17:26:20 +02:00
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
2020-06-30 17:26:20 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
2022-05-05 08:53:25 +02:00
import app.fedilab.fedilabtube.R;
2020-06-30 17:26:20 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-29 17:42:15 +02:00
import app.fedilab.fedilabtube.client.data.ChannelData;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.data.PlaylistData.Playlist;
import app.fedilab.fedilabtube.client.entities.Item;
import app.fedilab.fedilabtube.client.entities.PlaylistParams;
import app.fedilab.fedilabtube.databinding.ActivityAllPlaylistBinding;
import app.fedilab.fedilabtube.databinding.AddPlaylistBinding;
2020-06-30 17:26:20 +02:00
import app.fedilab.fedilabtube.drawer.PlaylistAdapter;
import app.fedilab.fedilabtube.helper.Helper;
2022-05-09 10:25:07 +02:00
import app.fedilab.fedilabtube.helper.HelperInstance;
2022-05-10 10:47:51 +02:00
import app.fedilab.fedilabtube.helper.Theme;
2020-09-07 16:57:00 +02:00
import app.fedilab.fedilabtube.viewmodel.ChannelsVM;
2020-09-08 10:11:11 +02:00
import app.fedilab.fedilabtube.viewmodel.PlaylistsVM;
2020-06-30 17:26:20 +02:00
import es.dmoral.toasty.Toasty;
2022-05-05 08:53:25 +02:00
public class AllPlaylistsActivity extends BaseActivity implements PlaylistAdapter.AllPlaylistRemoved {
2020-06-30 17:26:20 +02:00
2020-12-18 14:31:36 +01:00
private static final int PICK_AVATAR = 467;
2020-09-04 15:58:49 +02:00
PlaylistAdapter playlistAdapter;
2020-06-30 17:26:20 +02:00
private HashMap<Integer, String> privacyToSend;
2020-09-04 15:56:18 +02:00
private String idChannel;
private List<Playlist> playlists;
2020-09-11 15:56:18 +02:00
private Playlist playlistToEdit;
2020-09-29 17:42:15 +02:00
private List<ChannelData.Channel> myChannels;
private ChannelData.Channel selectedChannel;
private AddPlaylistBinding bindingDialog;
private Uri inputData;
private ActivityAllPlaylistBinding binding;
2020-09-11 11:50:26 +02:00
2022-05-09 10:25:07 +02:00
2020-06-30 17:26:20 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2022-05-10 10:47:51 +02:00
Theme.setTheme(this, HelperInstance.getLiveInstance(this),false);
binding = ActivityAllPlaylistBinding.inflate(getLayoutInflater());
View viewRoot = binding.getRoot();
setContentView(viewRoot);
2020-06-30 17:26:20 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(R.string.playlists);
binding.loader.setVisibility(View.VISIBLE);
binding.loadingNextItems.setVisibility(View.GONE);
2020-09-04 15:56:18 +02:00
idChannel = null;
2020-06-30 19:06:54 +02:00
2020-09-08 10:11:11 +02:00
PlaylistsVM viewModel = new ViewModelProvider(AllPlaylistsActivity.this).get(PlaylistsVM.class);
2020-09-25 18:58:04 +02:00
viewModel.manage(PlaylistsVM.action.GET_PLAYLISTS, null, null).observe(AllPlaylistsActivity.this, apiResponse -> manageVIewPlaylists(PlaylistsVM.action.GET_PLAYLISTS, apiResponse));
2020-09-08 10:11:11 +02:00
2020-06-30 17:26:20 +02:00
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies());
2020-09-03 19:08:53 +02:00
if (privaciesInit.size() > 0) {
Map.Entry<Integer, String> entryInt = privaciesInit.entrySet().iterator().next();
privacyToSend = new HashMap<>();
privacyToSend.put(entryInt.getKey(), entryInt.getValue());
}
2020-06-30 17:26:20 +02:00
2020-09-04 15:56:18 +02:00
playlists = new ArrayList<>();
2020-11-14 12:19:37 +01:00
playlistAdapter = new PlaylistAdapter(playlists, false);
2020-11-14 15:05:44 +01:00
playlistAdapter.allPlaylistRemoved = this;
binding.lvPlaylist.setAdapter(playlistAdapter);
2020-11-14 12:19:37 +01:00
LinearLayoutManager mLayoutManager = new LinearLayoutManager(AllPlaylistsActivity.this);
binding.lvPlaylist.setLayoutManager(mLayoutManager);
binding.addNew.setOnClickListener(view -> manageAlert(null));
2020-06-30 17:26:20 +02:00
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
2020-09-08 10:11:11 +02:00
public void manageVIewPlaylists(PlaylistsVM.action actionType, APIResponse apiResponse) {
binding.loader.setVisibility(View.GONE);
2020-06-30 17:26:20 +02:00
if (apiResponse.getError() != null) {
Toasty.error(AllPlaylistsActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return;
}
2020-09-25 18:58:04 +02:00
if (actionType == PlaylistsVM.action.GET_PLAYLISTS) {
2020-06-30 17:26:20 +02:00
if (apiResponse.getPlaylists() != null && apiResponse.getPlaylists().size() > 0) {
2020-06-30 19:06:54 +02:00
playlists.addAll(apiResponse.getPlaylists());
2020-09-04 15:56:18 +02:00
playlistAdapter.notifyDataSetChanged();
binding.noAction.setVisibility(View.GONE);
2020-06-30 17:26:20 +02:00
} else {
binding.noAction.setVisibility(View.VISIBLE);
2020-06-30 17:26:20 +02:00
}
}
}
2020-09-11 15:56:18 +02:00
public void manageAlert(Playlist playlistParam) {
2020-09-11 11:50:26 +02:00
2020-09-11 15:56:18 +02:00
playlistToEdit = playlistParam;
2020-09-11 11:50:26 +02:00
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AllPlaylistsActivity.this);
bindingDialog = AddPlaylistBinding.inflate(LayoutInflater.from(AllPlaylistsActivity.this), null, false);
dialogBuilder.setView(bindingDialog.getRoot());
dialogBuilder.setView(bindingDialog.getRoot());
2020-09-11 11:50:26 +02:00
ChannelsVM viewModelC = new ViewModelProvider(AllPlaylistsActivity.this).get(ChannelsVM.class);
2020-09-29 17:42:15 +02:00
viewModelC.get(RetrofitPeertubeAPI.DataType.MY_CHANNELS, null).observe(AllPlaylistsActivity.this, this::manageVIewChannels);
2020-09-11 11:50:26 +02:00
bindingDialog.displayName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(120)});
bindingDialog.description.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1000)});
2020-09-11 11:50:26 +02:00
if (playlistToEdit != null) {
bindingDialog.displayName.setText(playlistToEdit.getDisplayName());
bindingDialog.description.setText(playlistToEdit.getDescription());
2020-09-11 11:50:26 +02:00
}
2020-09-12 16:53:04 +02:00
dialogBuilder.setPositiveButton(R.string.validate, null);
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
2020-09-11 11:50:26 +02:00
2020-09-12 16:53:04 +02:00
AlertDialog alertDialog = dialogBuilder.create();
bindingDialog.selectFile.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(AllPlaylistsActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AllPlaylistsActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
return;
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"image/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_AVATAR);
});
2020-12-13 08:53:22 +01:00
Helper.loadGiF(AllPlaylistsActivity.this, playlistParam != null ? playlistParam.getThumbnailPath() : null, bindingDialog.profilePicture);
2020-09-12 16:53:04 +02:00
alertDialog.setOnShowListener(dialogInterface -> {
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(view -> {
if (bindingDialog.displayName.getText() != null && bindingDialog.displayName.getText().toString().trim().length() > 0) {
2020-09-25 18:58:04 +02:00
PlaylistParams playlistElement = new PlaylistParams();
playlistElement.setDisplayName(bindingDialog.displayName.getText().toString().trim());
if (bindingDialog.description.getText() != null && bindingDialog.description.getText().toString().trim().length() > 0) {
playlistElement.setDescription(bindingDialog.description.getText().toString().trim());
2020-09-11 11:50:26 +02:00
}
2020-09-12 16:53:04 +02:00
playlistElement.setVideoChannelId(idChannel);
String label;
Map.Entry<Integer, String> privacyM = privacyToSend.entrySet().iterator().next();
2020-09-25 18:58:04 +02:00
Item privacyItem = new Item();
privacyItem.setId(privacyM.getKey());
privacyItem.setLabel(privacyM.getValue());
2020-09-12 16:53:04 +02:00
label = privacyM.getValue();
if ((label.trim().compareTo("Public") == 0 && (playlistElement.getVideoChannelId() == null || playlistElement.getVideoChannelId().trim().compareTo("null") == 0))) {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_channel_mandatory), Toast.LENGTH_LONG).show();
} else {
if (privacyToSend != null) {
2020-10-06 18:49:15 +02:00
playlistElement.setPrivacy(privacyItem.getId());
2020-09-12 16:53:04 +02:00
}
new Thread(() -> {
2020-09-25 18:58:04 +02:00
String playlistId;
if (playlistToEdit == null) {
APIResponse apiResponse = new RetrofitPeertubeAPI(AllPlaylistsActivity.this).createOrUpdatePlaylist(PlaylistsVM.action.CREATE_PLAYLIST, null, playlistElement, inputData);
2020-09-25 18:58:04 +02:00
playlistId = apiResponse.getActionReturn();
} else {
playlistId = playlistToEdit.getId();
new RetrofitPeertubeAPI(AllPlaylistsActivity.this).createOrUpdatePlaylist(PlaylistsVM.action.UPDATE_PLAYLIST, playlistId, playlistElement, inputData);
2020-09-25 18:58:04 +02:00
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
Playlist playlist;
2020-09-11 15:56:18 +02:00
if (playlistToEdit == null) {
2020-09-25 18:58:04 +02:00
playlist = new Playlist();
2020-09-11 11:50:26 +02:00
} else {
2020-09-25 18:58:04 +02:00
playlist = playlistToEdit;
2020-09-11 11:50:26 +02:00
}
2020-09-25 18:58:04 +02:00
playlist.setId(playlistId);
2020-09-29 17:42:15 +02:00
playlist.setUuid(playlistId);
2020-09-25 18:58:04 +02:00
playlist.setDescription(playlistElement.getDescription());
playlist.setDisplayName(playlistElement.getDisplayName());
2020-09-29 17:42:15 +02:00
playlist.setVideoChannel(selectedChannel);
2020-09-25 18:58:04 +02:00
playlist.setPrivacy(privacyItem);
if (playlistToEdit == null) {
playlists.add(playlist);
}
playlistAdapter.notifyDataSetChanged();
};
mainHandler.post(myRunnable);
2020-09-12 16:53:04 +02:00
}).start();
alertDialog.dismiss();
}
} else {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show();
2020-09-11 11:50:26 +02:00
}
2020-09-12 16:53:04 +02:00
});
2020-09-11 11:50:26 +02:00
});
2020-09-12 16:53:04 +02:00
if (playlistToEdit == null) {
alertDialog.setTitle(getString(R.string.action_playlist_create));
} else {
alertDialog.setTitle(getString(R.string.action_playlist_edit));
}
2020-09-11 11:50:26 +02:00
alertDialog.setOnDismissListener(dialogInterface -> {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(bindingDialog.displayName.getWindowToken(), 0);
2020-09-11 11:50:26 +02:00
});
if (alertDialog.getWindow() != null)
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
alertDialog.show();
}
2020-06-30 17:26:20 +02:00
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null || data.getData() == null) {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
return;
}
inputData = data.getData();
Glide.with(AllPlaylistsActivity.this)
.load(inputData)
.thumbnail(0.1f)
.apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10)))
.into(bindingDialog.profilePicture);
}
}
2020-09-07 16:57:00 +02:00
public void manageVIewChannels(APIResponse apiResponse) {
2020-09-29 17:42:15 +02:00
if (apiResponse.getError() != null || apiResponse.getChannels() == null || apiResponse.getChannels().size() == 0) {
2020-06-30 17:26:20 +02:00
if (apiResponse.getError() != null && apiResponse.getError().getError() != null)
Toasty.error(AllPlaylistsActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
else
Toasty.error(AllPlaylistsActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
}
//Populate channels
2020-09-29 17:42:15 +02:00
myChannels = apiResponse.getChannels();
String[] channelName = new String[myChannels.size() + 1];
String[] channelId = new String[myChannels.size() + 1];
2020-06-30 17:26:20 +02:00
int i = 1;
channelName[0] = "";
2020-09-04 15:56:18 +02:00
channelId[0] = "null";
2020-09-29 17:42:15 +02:00
for (ChannelData.Channel channel : myChannels) {
channelName[i] = channel.getName();
channelId[i] = channel.getId();
2020-06-30 17:26:20 +02:00
i++;
}
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(AllPlaylistsActivity.this,
android.R.layout.simple_spinner_dropdown_item, channelName);
bindingDialog.setUploadChannel.setAdapter(adapterChannel);
2020-06-30 17:26:20 +02:00
2020-09-11 15:56:18 +02:00
2020-06-30 17:26:20 +02:00
LinkedHashMap<String, String> translations = null;
if (peertubeInformation.getTranslations() != null)
translations = new LinkedHashMap<>(peertubeInformation.getTranslations());
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPlaylistPrivacies());
Map.Entry<Integer, String> entryInt = privaciesInit.entrySet().iterator().next();
privacyToSend = new HashMap<>();
privacyToSend.put(entryInt.getKey(), entryInt.getValue());
LinkedHashMap<Integer, String> privacies = new LinkedHashMap<>(peertubeInformation.getPlaylistPrivacies());
//Populate privacies
String[] privaciesA = new String[privacies.size()];
Iterator<Map.Entry<Integer, String>> it = privacies.entrySet().iterator();
i = 0;
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
if (translations == null || translations.size() == 0 || !translations.containsKey(pair.getValue()))
privaciesA[i] = pair.getValue();
else
privaciesA[i] = translations.get(pair.getValue());
it.remove();
i++;
}
ArrayAdapter<String> adapterPrivacies = new ArrayAdapter<>(AllPlaylistsActivity.this,
android.R.layout.simple_spinner_dropdown_item, privaciesA);
bindingDialog.setUploadPrivacy.setAdapter(adapterPrivacies);
2020-06-30 17:26:20 +02:00
2020-09-11 15:56:18 +02:00
if (playlistToEdit != null) {
2020-09-25 18:58:04 +02:00
Item privacy = playlistToEdit.getPrivacy();
if (privacy.getId() > 0) {
bindingDialog.setUploadPrivacy.setSelection(privacy.getId() - 1);
2020-09-11 15:56:18 +02:00
}
2020-09-12 16:53:04 +02:00
} else {
bindingDialog.setUploadPrivacy.setSelection(2);
2020-09-11 15:56:18 +02:00
}
2020-06-30 17:26:20 +02:00
//Manage privacies
bindingDialog.setUploadPrivacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2020-06-30 17:26:20 +02:00
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LinkedHashMap<Integer, String> privaciesCheck = new LinkedHashMap<>(peertubeInformation.getPrivacies());
Iterator<Map.Entry<Integer, String>> it = privaciesCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
if (i == position) {
privacyToSend = new HashMap<>();
privacyToSend.put(pair.getKey(), pair.getValue());
break;
}
it.remove();
i++;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2020-09-11 15:56:18 +02:00
if (playlistToEdit != null) {
2020-09-25 18:58:04 +02:00
Item privacy = playlistToEdit.getPrivacy();
if (privacy.getId() > 0) {
bindingDialog.setUploadPrivacy.setSelection(privacy.getId() - 1);
2020-09-11 15:56:18 +02:00
}
}
2020-06-30 17:26:20 +02:00
//Manage languages
bindingDialog.setUploadChannel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2020-06-30 17:26:20 +02:00
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
2020-09-04 15:56:18 +02:00
idChannel = channelId[position];
2020-10-03 18:37:34 +02:00
if (position > 0) {
2020-10-03 13:41:33 +02:00
selectedChannel = myChannels.get(position - 1);
}
2020-06-30 17:26:20 +02:00
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2020-09-11 15:56:18 +02:00
if (playlistToEdit != null) {
int position = 0;
int k = 1;
2020-09-29 17:42:15 +02:00
for (ChannelData.Channel ac : myChannels) {
if (playlistToEdit.getVideoChannel() != null && playlistToEdit.getVideoChannel().getId() != null && ac.getId().compareTo(playlistToEdit.getVideoChannel().getId()) == 0) {
2020-09-11 15:56:18 +02:00
position = k;
break;
}
k++;
}
bindingDialog.setUploadChannel.setSelection(position);
2020-09-11 15:56:18 +02:00
}
2020-06-30 17:26:20 +02:00
}
2020-11-14 12:19:37 +01:00
@Override
public void onAllPlaylistRemoved() {
binding.noAction.setVisibility(View.VISIBLE);
2020-11-14 12:19:37 +01:00
}
2020-06-30 17:26:20 +02:00
}