Some fixes

This commit is contained in:
Thomas 2020-09-11 15:56:18 +02:00
parent 42b0574143
commit bb2c129762
5 changed files with 114 additions and 21 deletions

View File

@ -53,6 +53,7 @@ import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.Playlist;
import app.fedilab.fedilabtube.client.entities.PlaylistElement; import app.fedilab.fedilabtube.client.entities.PlaylistElement;
import app.fedilab.fedilabtube.drawer.PlaylistAdapter; import app.fedilab.fedilabtube.drawer.PlaylistAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.viewmodel.ChannelsVM; import app.fedilab.fedilabtube.viewmodel.ChannelsVM;
import app.fedilab.fedilabtube.viewmodel.PlaylistsVM; import app.fedilab.fedilabtube.viewmodel.PlaylistsVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
@ -71,7 +72,7 @@ public class AllPlaylistsActivity extends AppCompatActivity {
private Spinner set_upload_privacy; private Spinner set_upload_privacy;
private String idChannel; private String idChannel;
private List<Playlist> playlists; private List<Playlist> playlists;
private Playlist playlistToEdit;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { 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); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AllPlaylistsActivity.this);
LayoutInflater inflater1 = getLayoutInflater(); LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.add_playlist, new LinearLayout(AllPlaylistsActivity.this), false); 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_channel = dialogView.findViewById(R.id.set_upload_channel);
set_upload_privacy = dialogView.findViewById(R.id.set_upload_privacy); set_upload_privacy = dialogView.findViewById(R.id.set_upload_privacy);
ChannelsVM viewModelC = new ViewModelProvider(AllPlaylistsActivity.this).get(ChannelsVM.class); ChannelsVM viewModelC = new ViewModelProvider(AllPlaylistsActivity.this).get(ChannelsVM.class);
viewModelC.get().observe(AllPlaylistsActivity.this, this::manageVIewChannels); viewModelC.get().observe(AllPlaylistsActivity.this, this::manageVIewChannels);
@ -188,15 +191,30 @@ public class AllPlaylistsActivity extends AppCompatActivity {
} }
new Thread(() -> { new Thread(() -> {
try { 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()); Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> { Runnable myRunnable = () -> {
Playlist playlist = new Playlist(); Playlist playlist;
if (playlistToEdit == null) {
playlist = new Playlist();
} else {
playlist = playlistToEdit;
}
playlist.setId(playlistId); playlist.setId(playlistId);
playlist.setDescription(playlistElement.getDescription()); playlist.setDescription(playlistElement.getDescription());
playlist.setDisplayName(playlistElement.getDisplayName()); playlist.setDisplayName(playlistElement.getDisplayName());
playlist.setVideoChannelId(playlistElement.getVideoChannelId());
playlist.setPrivacy(privacyToSend); playlist.setPrivacy(privacyToSend);
if (playlistToEdit == null) {
playlists.add(playlist); playlists.add(playlist);
}
playlistAdapter.notifyDataSetChanged(); playlistAdapter.notifyDataSetChanged();
}; };
mainHandler.post(myRunnable); mainHandler.post(myRunnable);
@ -218,7 +236,6 @@ public class AllPlaylistsActivity extends AppCompatActivity {
} else { } else {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show(); Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show();
} }
}); });
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create(); AlertDialog alertDialog = dialogBuilder.create();
@ -245,14 +262,14 @@ public class AllPlaylistsActivity extends AppCompatActivity {
} }
//Populate channels //Populate channels
List<Account> accounts = apiResponse.getAccounts(); List<Account> channels = apiResponse.getAccounts();
String[] channelName = new String[accounts.size() + 1]; String[] channelName = new String[channels.size() + 1];
String[] channelId = new String[accounts.size() + 1]; String[] channelId = new String[channels.size() + 1];
int i = 1; int i = 1;
channelName[0] = ""; channelName[0] = "";
channelId[0] = "null"; channelId[0] = "null";
for (Account account : accounts) { for (Account account : channels) {
channelName[i] = account.getUsername(); channelName[i] = account.getUsername();
channelId[i] = account.getId(); channelId[i] = account.getId();
i++; i++;
@ -262,6 +279,7 @@ public class AllPlaylistsActivity extends AppCompatActivity {
android.R.layout.simple_spinner_dropdown_item, channelName); android.R.layout.simple_spinner_dropdown_item, channelName);
set_upload_channel.setAdapter(adapterChannel); set_upload_channel.setAdapter(adapterChannel);
LinkedHashMap<String, String> translations = null; LinkedHashMap<String, String> translations = null;
if (peertubeInformation.getTranslations() != null) if (peertubeInformation.getTranslations() != null)
translations = new LinkedHashMap<>(peertubeInformation.getTranslations()); translations = new LinkedHashMap<>(peertubeInformation.getTranslations());
@ -289,6 +307,17 @@ public class AllPlaylistsActivity extends AppCompatActivity {
android.R.layout.simple_spinner_dropdown_item, privaciesA); android.R.layout.simple_spinner_dropdown_item, privaciesA);
set_upload_privacy.setAdapter(adapterPrivacies); set_upload_privacy.setAdapter(adapterPrivacies);
if (playlistToEdit != null) {
it = playlistToEdit.getPrivacy().entrySet().iterator();
Map.Entry<Integer, String> pair = null;
while (it.hasNext()) {
pair = it.next();
}
if (pair != null) {
set_upload_privacy.setSelection(pair.getKey() - 1);
}
}
//Manage privacies //Manage privacies
set_upload_privacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { set_upload_privacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
@ -313,6 +342,18 @@ public class AllPlaylistsActivity extends AppCompatActivity {
} }
}); });
if (playlistToEdit != null) {
it = playlistToEdit.getPrivacy().entrySet().iterator();
Map.Entry<Integer, String> pair = null;
while (it.hasNext()) {
pair = it.next();
}
if (pair != null) {
set_upload_privacy.setSelection(pair.getKey() - 1);
}
}
//Manage languages //Manage languages
set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @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);
}
} }
} }

View File

@ -309,7 +309,7 @@ public class HttpsConnection {
return response; return response;
} }
public String postBoundary(String urlConnection, int timeout, LinkedHashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { public String postBoundary(boundaryType type, String urlConnection, int timeout, LinkedHashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection); URL url = new URL(urlConnection);
String boundary = "----TubeLabBoundary" + System.currentTimeMillis(); String boundary = "----TubeLabBoundary" + System.currentTimeMillis();
@ -321,6 +321,12 @@ public class HttpsConnection {
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpsURLConnection.setDoOutput(true); httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (type == boundaryType.POST) {
httpsURLConnection.setRequestMethod("POST");
} else if (type == boundaryType.PUT) {
httpsURLConnection.setRequestMethod("PUT");
}
if (token != null) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
@ -807,6 +813,11 @@ public class HttpsConnection {
} }
} }
public enum boundaryType {
POST,
PUT
}
public class HttpsConnectionException extends Exception { public class HttpsConnectionException extends Exception {
private int statusCode; private int statusCode;

View File

@ -237,7 +237,9 @@ public class PeertubeAPI {
try { try {
peertube.setId(resobj.getString("id")); peertube.setId(resobj.getString("id"));
peertube.setCache(resobj); peertube.setCache(resobj);
if (resobj.has("uuid")) {
peertube.setUuid(resobj.getString("uuid")); peertube.setUuid(resobj.getString("uuid"));
}
peertube.setName(resobj.getString("name")); peertube.setName(resobj.getString("name"));
peertube.setDescription(resobj.getString("description")); peertube.setDescription(resobj.getString("description"));
peertube.setEmbedPath(resobj.getString("embedPath")); peertube.setEmbedPath(resobj.getString("embedPath"));
@ -391,7 +393,9 @@ public class PeertubeAPI {
playlist.setDescription(resobj.getString("description")); playlist.setDescription(resobj.getString("description"));
playlist.setDisplayName(resobj.getString("displayName")); playlist.setDisplayName(resobj.getString("displayName"));
playlist.setLocal(resobj.getBoolean("isLocal")); 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.setThumbnailPath(resobj.getString("thumbnailPath"));
playlist.setOwnerAccount(parseAccountResponsePeertube(resobj.getJSONObject("ownerAccount"))); playlist.setOwnerAccount(parseAccountResponsePeertube(resobj.getJSONObject("ownerAccount")));
playlist.setVideosLength(resobj.getInt("videosLength")); playlist.setVideosLength(resobj.getInt("videosLength"));
@ -1721,7 +1725,7 @@ public class PeertubeAPI {
params.put("privacy", playlistElement.getPrivacy()); params.put("privacy", playlistElement.getPrivacy());
params.put("videoChannelId", playlistElement.getVideoChannelId()); params.put("videoChannelId", playlistElement.getVideoChannelId());
params.put("description", playlistElement.getDescription()); 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"); playlistId = new JSONObject(response).getJSONObject("videoPlaylist").getString("id");
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -1729,6 +1733,25 @@ public class PeertubeAPI {
return playlistId; 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<String, String> 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 * Delete a Playlist
@ -2075,6 +2098,7 @@ public class PeertubeAPI {
} }
@SuppressWarnings("SameParameterValue")
private String getAbsoluteUrlForInstance(String instance, String action) { private String getAbsoluteUrlForInstance(String instance, String action) {
return "https://" + instance + "/api/v1" + action; return "https://" + instance + "/api/v1" + action;
} }

View File

@ -38,6 +38,7 @@ import com.bumptech.glide.Glide;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import app.fedilab.fedilabtube.AllPlaylistsActivity;
import app.fedilab.fedilabtube.PlaylistsActivity; import app.fedilab.fedilabtube.PlaylistsActivity;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
@ -94,9 +95,11 @@ public class PlaylistAdapter extends BaseAdapter {
holder = (ViewHolder) convertView.getTag(); holder = (ViewHolder) convertView.getTag();
} }
if (playlist.getOwnerAccount() != null) {
Glide.with(context) Glide.with(context)
.load("https://" + playlist.getOwnerAccount().getHost() + playlist.getThumbnailPath()) .load("https://" + playlist.getOwnerAccount().getHost() + playlist.getThumbnailPath())
.into(holder.preview_playlist); .into(holder.preview_playlist);
}
holder.preview_title.setText(playlist.getDisplayName()); holder.preview_title.setText(playlist.getDisplayName());
if (playlist.getDescription() != null && playlist.getDescription().trim().compareTo("null") != 0 && playlist.getDescription().length() > 0) { if (playlist.getDescription() != null && playlist.getDescription().trim().compareTo("null") != 0 && playlist.getDescription().length() > 0) {
@ -142,7 +145,9 @@ public class PlaylistAdapter extends BaseAdapter {
.show(); .show();
break; break;
case R.id.action_edit: case R.id.action_edit:
if (context instanceof AllPlaylistsActivity) {
((AllPlaylistsActivity) context).manageAlert(playlist);
}
break; break;
} }
return true; return true;
@ -153,7 +158,6 @@ public class PlaylistAdapter extends BaseAdapter {
return convertView; return convertView;
} }
@SuppressWarnings("unused")
public void manageVIewPlaylists(PlaylistsVM.action actionType, APIResponse apiResponse) { public void manageVIewPlaylists(PlaylistsVM.action actionType, APIResponse apiResponse) {
} }

View File

@ -118,9 +118,9 @@ public class DisplayNotificationsFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if( getActivity() != null && getActivity() != null) { if (getActivity() != null && getActivity() != null) {
View action_button = getActivity().findViewById(R.id.action_button); View action_button = getActivity().findViewById(R.id.action_button);
if( action_button != null) { if (action_button != null) {
action_button.setVisibility(View.GONE); action_button.setVisibility(View.GONE);
} }
} }