diff --git a/app/src/main/java/app/fedilab/fedilabtube/AllPlaylistsActivity.java b/app/src/main/java/app/fedilab/fedilabtube/AllPlaylistsActivity.java index 0bca829..1b95cff 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/AllPlaylistsActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/AllPlaylistsActivity.java @@ -53,6 +53,7 @@ import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.PlaylistElement; import app.fedilab.fedilabtube.drawer.PlaylistAdapter; +import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.viewmodel.ChannelsVM; import app.fedilab.fedilabtube.viewmodel.PlaylistsVM; import es.dmoral.toasty.Toasty; @@ -71,7 +72,7 @@ public class AllPlaylistsActivity extends AppCompatActivity { private Spinner set_upload_privacy; private String idChannel; private List playlists; - + private Playlist playlistToEdit; @Override protected void onCreate(Bundle savedInstanceState) { @@ -145,8 +146,9 @@ public class AllPlaylistsActivity extends AppCompatActivity { } } - public void manageAlert(Playlist playlistToEdit) { + public void manageAlert(Playlist playlistParam) { + playlistToEdit = playlistParam; AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AllPlaylistsActivity.this); LayoutInflater inflater1 = getLayoutInflater(); View dialogView = inflater1.inflate(R.layout.add_playlist, new LinearLayout(AllPlaylistsActivity.this), false); @@ -156,6 +158,7 @@ public class AllPlaylistsActivity extends AppCompatActivity { set_upload_channel = dialogView.findViewById(R.id.set_upload_channel); set_upload_privacy = dialogView.findViewById(R.id.set_upload_privacy); + ChannelsVM viewModelC = new ViewModelProvider(AllPlaylistsActivity.this).get(ChannelsVM.class); viewModelC.get().observe(AllPlaylistsActivity.this, this::manageVIewChannels); @@ -188,15 +191,30 @@ public class AllPlaylistsActivity extends AppCompatActivity { } new Thread(() -> { try { - String playlistId = new PeertubeAPI(AllPlaylistsActivity.this).createPlaylist(playlistElement); + String playlistId; + if (playlistToEdit == null) { + playlistId = new PeertubeAPI(AllPlaylistsActivity.this).createPlaylist(playlistElement); + } else { + playlistId = playlistToEdit.getId(); + playlistElement.setPlaylistId(playlistId); + new PeertubeAPI(AllPlaylistsActivity.this).updatePlaylist(playlistElement); + } Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { - Playlist playlist = new Playlist(); + Playlist playlist; + if (playlistToEdit == null) { + playlist = new Playlist(); + } else { + playlist = playlistToEdit; + } playlist.setId(playlistId); playlist.setDescription(playlistElement.getDescription()); playlist.setDisplayName(playlistElement.getDisplayName()); + playlist.setVideoChannelId(playlistElement.getVideoChannelId()); playlist.setPrivacy(privacyToSend); - playlists.add(playlist); + if (playlistToEdit == null) { + playlists.add(playlist); + } playlistAdapter.notifyDataSetChanged(); }; mainHandler.post(myRunnable); @@ -218,7 +236,6 @@ public class AllPlaylistsActivity extends AppCompatActivity { } else { Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show(); } - }); dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); AlertDialog alertDialog = dialogBuilder.create(); @@ -245,14 +262,14 @@ public class AllPlaylistsActivity extends AppCompatActivity { } //Populate channels - List accounts = apiResponse.getAccounts(); - String[] channelName = new String[accounts.size() + 1]; - String[] channelId = new String[accounts.size() + 1]; + List channels = apiResponse.getAccounts(); + String[] channelName = new String[channels.size() + 1]; + String[] channelId = new String[channels.size() + 1]; int i = 1; channelName[0] = ""; channelId[0] = "null"; - for (Account account : accounts) { + for (Account account : channels) { channelName[i] = account.getUsername(); channelId[i] = account.getId(); i++; @@ -262,6 +279,7 @@ public class AllPlaylistsActivity extends AppCompatActivity { android.R.layout.simple_spinner_dropdown_item, channelName); set_upload_channel.setAdapter(adapterChannel); + LinkedHashMap translations = null; if (peertubeInformation.getTranslations() != null) translations = new LinkedHashMap<>(peertubeInformation.getTranslations()); @@ -289,6 +307,17 @@ public class AllPlaylistsActivity extends AppCompatActivity { android.R.layout.simple_spinner_dropdown_item, privaciesA); set_upload_privacy.setAdapter(adapterPrivacies); + if (playlistToEdit != null) { + it = playlistToEdit.getPrivacy().entrySet().iterator(); + Map.Entry pair = null; + while (it.hasNext()) { + pair = it.next(); + } + if (pair != null) { + set_upload_privacy.setSelection(pair.getKey() - 1); + } + } + //Manage privacies set_upload_privacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override @@ -313,6 +342,18 @@ public class AllPlaylistsActivity extends AppCompatActivity { } }); + + if (playlistToEdit != null) { + it = playlistToEdit.getPrivacy().entrySet().iterator(); + Map.Entry pair = null; + while (it.hasNext()) { + pair = it.next(); + } + if (pair != null) { + set_upload_privacy.setSelection(pair.getKey() - 1); + } + } + //Manage languages set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override @@ -325,5 +366,18 @@ public class AllPlaylistsActivity extends AppCompatActivity { } }); + + if (playlistToEdit != null) { + int position = 0; + int k = 1; + for (Account ac : channels) { + if (playlistToEdit.getVideoChannelId() != null && ac.getId().compareTo(playlistToEdit.getVideoChannelId()) == 0) { + position = k; + break; + } + k++; + } + set_upload_channel.setSelection(position); + } } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java index ff7b90c..e412541 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java @@ -309,7 +309,7 @@ public class HttpsConnection { return response; } - public String postBoundary(String urlConnection, int timeout, LinkedHashMap paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { + public String postBoundary(boundaryType type, String urlConnection, int timeout, LinkedHashMap paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { URL url = new URL(urlConnection); String boundary = "----TubeLabBoundary" + System.currentTimeMillis(); @@ -321,6 +321,12 @@ public class HttpsConnection { httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); httpsURLConnection.setDoOutput(true); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); + + if (type == boundaryType.POST) { + httpsURLConnection.setRequestMethod("POST"); + } else if (type == boundaryType.PUT) { + httpsURLConnection.setRequestMethod("PUT"); + } if (token != null) httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); @@ -807,6 +813,11 @@ public class HttpsConnection { } } + public enum boundaryType { + POST, + PUT + } + public class HttpsConnectionException extends Exception { private int statusCode; diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 84219d8..f1d543c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -237,7 +237,9 @@ public class PeertubeAPI { try { peertube.setId(resobj.getString("id")); peertube.setCache(resobj); - peertube.setUuid(resobj.getString("uuid")); + if (resobj.has("uuid")) { + peertube.setUuid(resobj.getString("uuid")); + } peertube.setName(resobj.getString("name")); peertube.setDescription(resobj.getString("description")); peertube.setEmbedPath(resobj.getString("embedPath")); @@ -391,7 +393,9 @@ public class PeertubeAPI { playlist.setDescription(resobj.getString("description")); playlist.setDisplayName(resobj.getString("displayName")); playlist.setLocal(resobj.getBoolean("isLocal")); - playlist.setVideoChannelId(resobj.getString("videoChannel")); + if (resobj.has("videoChannel") && !resobj.isNull("videoChannel")) { + playlist.setVideoChannelId(resobj.getJSONObject("videoChannel").getString("id")); + } playlist.setThumbnailPath(resobj.getString("thumbnailPath")); playlist.setOwnerAccount(parseAccountResponsePeertube(resobj.getJSONObject("ownerAccount"))); playlist.setVideosLength(resobj.getInt("videosLength")); @@ -1721,7 +1725,7 @@ public class PeertubeAPI { params.put("privacy", playlistElement.getPrivacy()); params.put("videoChannelId", playlistElement.getVideoChannelId()); params.put("description", playlistElement.getDescription()); - String response = httpsConnection.postBoundary(getAbsoluteUrl("/video-playlists/"), 60, params, prefKeyOauthTokenT); + String response = httpsConnection.postBoundary(HttpsConnection.boundaryType.POST, getAbsoluteUrl("/video-playlists/"), 60, params, prefKeyOauthTokenT); playlistId = new JSONObject(response).getJSONObject("videoPlaylist").getString("id"); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); @@ -1729,6 +1733,25 @@ public class PeertubeAPI { return playlistId; } + /** + * Update a Playlist + * + * @param playlistElement PlaylistElement, the playlist elements + */ + public void updatePlaylist(PlaylistElement playlistElement) throws HttpsConnection.HttpsConnectionException { + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + LinkedHashMap params = new LinkedHashMap<>(); + params.put("displayName", playlistElement.getDisplayName()); + params.put("privacy", playlistElement.getPrivacy()); + params.put("videoChannelId", playlistElement.getVideoChannelId()); + params.put("description", playlistElement.getDescription()); + httpsConnection.postBoundary(HttpsConnection.boundaryType.PUT, getAbsoluteUrl(String.format("/video-playlists/%s", playlistElement.getPlaylistId())), 60, params, prefKeyOauthTokenT); + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { + e.printStackTrace(); + } + } + /** * Delete a Playlist @@ -2075,6 +2098,7 @@ public class PeertubeAPI { } + @SuppressWarnings("SameParameterValue") private String getAbsoluteUrlForInstance(String instance, String action) { return "https://" + instance + "/api/v1" + action; } diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/PlaylistAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/PlaylistAdapter.java index 5d8f9a9..a75bdcd 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/PlaylistAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/PlaylistAdapter.java @@ -38,6 +38,7 @@ import com.bumptech.glide.Glide; import java.util.List; import java.util.Map; +import app.fedilab.fedilabtube.AllPlaylistsActivity; import app.fedilab.fedilabtube.PlaylistsActivity; import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.client.APIResponse; @@ -94,9 +95,11 @@ public class PlaylistAdapter extends BaseAdapter { holder = (ViewHolder) convertView.getTag(); } - Glide.with(context) - .load("https://" + playlist.getOwnerAccount().getHost() + playlist.getThumbnailPath()) - .into(holder.preview_playlist); + if (playlist.getOwnerAccount() != null) { + Glide.with(context) + .load("https://" + playlist.getOwnerAccount().getHost() + playlist.getThumbnailPath()) + .into(holder.preview_playlist); + } holder.preview_title.setText(playlist.getDisplayName()); if (playlist.getDescription() != null && playlist.getDescription().trim().compareTo("null") != 0 && playlist.getDescription().length() > 0) { @@ -142,7 +145,9 @@ public class PlaylistAdapter extends BaseAdapter { .show(); break; case R.id.action_edit: - + if (context instanceof AllPlaylistsActivity) { + ((AllPlaylistsActivity) context).manageAlert(playlist); + } break; } return true; @@ -153,7 +158,6 @@ public class PlaylistAdapter extends BaseAdapter { return convertView; } - @SuppressWarnings("unused") public void manageVIewPlaylists(PlaylistsVM.action actionType, APIResponse apiResponse) { } diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java index 30b9b1b..253a8de 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java @@ -118,9 +118,9 @@ public class DisplayNotificationsFragment extends Fragment { @Override public void onResume() { super.onResume(); - if( getActivity() != null && getActivity() != null) { + if (getActivity() != null && getActivity() != null) { View action_button = getActivity().findViewById(R.id.action_button); - if( action_button != null) { + if (action_button != null) { action_button.setVisibility(View.GONE); } }