feat: add ability to add podcast channels

This commit is contained in:
antonio 2023-06-04 20:43:08 +02:00
parent 0248187f41
commit 7f4be7ad3e
10 changed files with 204 additions and 2 deletions

View file

@ -0,0 +1,6 @@
package com.cappielloantonio.play.interfaces;
public interface PodcastCallback {
void onDismiss();
}

View file

@ -83,6 +83,23 @@ public class PodcastRepository {
});
}
public void createPodcastChannel(String url) {
App.getSubsonicClientInstance(false)
.getPodcastClient()
.createPodcastChannel(url)
.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
}
@Override
public void onFailure(@NonNull Call<ApiResponse> call, @NonNull Throwable t) {
}
});
}
public void deletePodcastChannel(String channelId) {
App.getSubsonicClientInstance(false)
.getPodcastClient()

View file

@ -20,6 +20,9 @@ public interface PodcastService {
@GET("refreshPodcasts")
Call<ApiResponse> refreshPodcasts(@QueryMap Map<String, String> params);
@GET("createPodcastChannel")
Call<ApiResponse> createPodcastChannel(@QueryMap Map<String, String> params, @Query("url") String url);
@GET("deletePodcastChannel")
Call<ApiResponse> deletePodcastChannel(@QueryMap Map<String, String> params, @Query("id") String id);

View file

@ -0,0 +1,84 @@
package com.cappielloantonio.play.ui.dialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.databinding.DialogPodcastChannelEditorBinding;
import com.cappielloantonio.play.interfaces.PodcastCallback;
import com.cappielloantonio.play.viewmodel.PodcastChannelEditorViewModel;
import java.util.Objects;
public class PodcastChannelEditorDialog extends DialogFragment {
private DialogPodcastChannelEditorBinding bind;
private PodcastChannelEditorViewModel podcastChannelEditorViewModel;
private PodcastCallback podcastCallback;
private String channelUrl;
public PodcastChannelEditorDialog(PodcastCallback podcastCallback) {
this.podcastCallback = podcastCallback;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
bind = DialogPodcastChannelEditorBinding.inflate(getLayoutInflater());
podcastChannelEditorViewModel = new ViewModelProvider(requireActivity()).get(PodcastChannelEditorViewModel.class);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(bind.getRoot())
.setTitle(R.string.podcast_channel_editor_dialog_title)
.setPositiveButton(R.string.radio_editor_dialog_positive_button, (dialog, id) -> {
})
.setNegativeButton(R.string.radio_editor_dialog_negative_button, (dialog, id) -> dialog.cancel());
return builder.create();
}
@Override
public void onStart() {
super.onStart();
setButtonAction();
}
@Override
public void onDestroyView() {
super.onDestroyView();
bind = null;
}
private void setButtonAction() {
((AlertDialog) Objects.requireNonNull(getDialog())).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
if (validateInput()) {
podcastChannelEditorViewModel.createChannel(channelUrl);
dismissDialog();
}
});
}
private boolean validateInput() {
channelUrl = Objects.requireNonNull(bind.podcastChannelRssUrlNameTextView.getText()).toString().trim();
if (TextUtils.isEmpty(channelUrl)) {
bind.podcastChannelRssUrlNameTextView.setError(getString(R.string.error_required));
return false;
}
return true;
}
private void dismissDialog() {
podcastCallback.onDismiss();
Objects.requireNonNull(getDialog()).dismiss();
}
}

View file

@ -2,6 +2,7 @@ package com.cappielloantonio.play.ui.fragment;
import android.content.ComponentName;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -19,11 +20,13 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.databinding.FragmentHomeTabPodcastBinding;
import com.cappielloantonio.play.interfaces.ClickCallback;
import com.cappielloantonio.play.interfaces.PodcastCallback;
import com.cappielloantonio.play.service.MediaManager;
import com.cappielloantonio.play.service.MediaService;
import com.cappielloantonio.play.ui.activity.MainActivity;
import com.cappielloantonio.play.ui.adapter.PodcastChannelHorizontalAdapter;
import com.cappielloantonio.play.ui.adapter.PodcastEpisodeAdapter;
import com.cappielloantonio.play.ui.dialog.PodcastChannelEditorDialog;
import com.cappielloantonio.play.util.Constants;
import com.cappielloantonio.play.util.Preferences;
import com.cappielloantonio.play.util.UIUtil;
@ -34,7 +37,7 @@ import java.util.Objects;
import java.util.stream.Collectors;
@UnstableApi
public class HomeTabPodcastFragment extends Fragment implements ClickCallback {
public class HomeTabPodcastFragment extends Fragment implements ClickCallback, PodcastCallback {
private static final String TAG = "HomeTabPodcastFragment";
private FragmentHomeTabPodcastBinding bind;
@ -88,6 +91,11 @@ public class HomeTabPodcastFragment extends Fragment implements ClickCallback {
}
private void init() {
bind.podcastChannelsPreTextView.setOnClickListener(v -> {
PodcastChannelEditorDialog dialog = new PodcastChannelEditorDialog(this);
dialog.show(activity.getSupportFragmentManager(), null);
});
bind.podcastChannelsTextViewClickable.setOnClickListener(v -> activity.navController.navigate(R.id.action_homeFragment_to_podcastChannelCatalogueFragment));
bind.hideSectionButton.setOnClickListener(v -> Preferences.setPodcastSectionHidden());
}
@ -169,4 +177,12 @@ public class HomeTabPodcastFragment extends Fragment implements ClickCallback {
public void onPodcastChannelLongClick(Bundle bundle) {
Navigation.findNavController(requireView()).navigate(R.id.podcastChannelBottomSheetDialog, bundle);
}
@Override
public void onDismiss() {
new Handler().postDelayed(() -> {
if (podcastViewModel != null) podcastViewModel.refreshPodcastChannels(getViewLifecycleOwner());
if (podcastViewModel != null) podcastViewModel.refreshNewestPodcastEpisodes(getViewLifecycleOwner());
}, 1000);
}
}

View file

@ -0,0 +1,28 @@
package com.cappielloantonio.play.viewmodel;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import com.cappielloantonio.play.repository.PodcastRepository;
import com.cappielloantonio.play.repository.RadioRepository;
import com.cappielloantonio.play.subsonic.models.InternetRadioStation;
public class PodcastChannelEditorViewModel extends AndroidViewModel {
private static final String TAG = "RadioEditorViewModel";
private final PodcastRepository podcastRepository;
private InternetRadioStation toEdit;
public PodcastChannelEditorViewModel(@NonNull Application application) {
super(application);
podcastRepository = new PodcastRepository();
}
public void createChannel(String url) {
podcastRepository.createPodcastChannel(url);
}
}

View file

@ -41,4 +41,12 @@ public class PodcastViewModel extends AndroidViewModel {
return podcastChannels;
}
public void refreshNewestPodcastEpisodes(LifecycleOwner owner) {
podcastRepository.getNewestPodcastEpisodes(20).observe(owner, newestPodcastEpisodes::postValue);
}
public void refreshPodcastChannels(LifecycleOwner owner) {
podcastRepository.getPodcastChannels(false, null).observe(owner, podcastChannels::postValue);
}
}