mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 09:53:33 +00:00
feat: radio stream add/edit dialog
This commit is contained in:
parent
3ef46a8536
commit
2542b28916
7 changed files with 301 additions and 2 deletions
|
|
@ -0,0 +1,6 @@
|
|||
package com.cappielloantonio.play.interfaces;
|
||||
|
||||
public interface RadioCallback {
|
||||
|
||||
void onDismiss();
|
||||
}
|
||||
|
|
@ -36,4 +36,55 @@ public class RadioRepository {
|
|||
|
||||
return radioStation;
|
||||
}
|
||||
|
||||
public void createInternetRadioStation(String name, String streamURL, String homepageURL) {
|
||||
App.getSubsonicClientInstance(false)
|
||||
.getInternetRadioClient()
|
||||
.createInternetRadioStation(streamURL, name, homepageURL)
|
||||
.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 updateInternetRadioStation(String id, String name, String streamURL, String homepageURL) {
|
||||
App.getSubsonicClientInstance(false)
|
||||
.getInternetRadioClient()
|
||||
.updateInternetRadioStation(id, streamURL, name, homepageURL)
|
||||
.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 deleteInternetRadioStation(String id) {
|
||||
App.getSubsonicClientInstance(false)
|
||||
.getInternetRadioClient()
|
||||
.deleteInternetRadioStation(id)
|
||||
.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) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,119 @@
|
|||
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.DialogRadioEditorBinding;
|
||||
import com.cappielloantonio.play.interfaces.RadioCallback;
|
||||
import com.cappielloantonio.play.subsonic.models.InternetRadioStation;
|
||||
import com.cappielloantonio.play.util.Constants;
|
||||
import com.cappielloantonio.play.viewmodel.RadioEditorViewModel;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class RadioEditorDialog extends DialogFragment {
|
||||
private DialogRadioEditorBinding bind;
|
||||
private RadioEditorViewModel radioEditorViewModel;
|
||||
private RadioCallback radioCallback;
|
||||
|
||||
private String radioName;
|
||||
private String radioStreamURL;
|
||||
private String radioHomepageURL;
|
||||
|
||||
public RadioEditorDialog(RadioCallback radioCallback) {
|
||||
this.radioCallback = radioCallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
bind = DialogRadioEditorBinding.inflate(getLayoutInflater());
|
||||
radioEditorViewModel = new ViewModelProvider(requireActivity()).get(RadioEditorViewModel.class);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
|
||||
builder.setView(bind.getRoot())
|
||||
.setTitle(R.string.radio_editor_dialog_title)
|
||||
.setPositiveButton(R.string.radio_editor_dialog_positive_button, (dialog, id) -> {
|
||||
})
|
||||
.setNeutralButton(R.string.radio_editor_dialog_neutral_button, (dialog, id) -> dialog.cancel())
|
||||
.setNegativeButton(R.string.radio_editor_dialog_negative_button, (dialog, id) -> dialog.cancel());
|
||||
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
setParameterInfo();
|
||||
setButtonAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
bind = null;
|
||||
}
|
||||
|
||||
private void setParameterInfo() {
|
||||
if (getArguments() != null && getArguments().getParcelable(Constants.INTERNET_RADIO_STATION_OBJECT) != null) {
|
||||
InternetRadioStation toEdit = requireArguments().getParcelable(Constants.INTERNET_RADIO_STATION_OBJECT);
|
||||
|
||||
radioEditorViewModel.setRadioToEdit(toEdit);
|
||||
|
||||
bind.internetRadioStationNameTextView.setText(toEdit.getName());
|
||||
bind.internetRadioStationStreamUrlTextView.setText(toEdit.getStreamUrl());
|
||||
bind.internetRadioStationHomepageUrlTextView.setText(toEdit.getHomePageUrl());
|
||||
}
|
||||
}
|
||||
|
||||
private void setButtonAction() {
|
||||
((AlertDialog) Objects.requireNonNull(getDialog())).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
|
||||
if (validateInput()) {
|
||||
if (radioEditorViewModel.getRadioToEdit() == null) {
|
||||
radioEditorViewModel.createRadio(radioName, radioStreamURL, radioHomepageURL);
|
||||
} else {
|
||||
radioEditorViewModel.updateRadio(radioName, radioStreamURL, radioHomepageURL);
|
||||
}
|
||||
|
||||
dismissDialog();
|
||||
}
|
||||
});
|
||||
|
||||
((AlertDialog) Objects.requireNonNull(getDialog())).getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(v -> {
|
||||
radioEditorViewModel.deleteRadio();
|
||||
dismissDialog();
|
||||
});
|
||||
}
|
||||
|
||||
private boolean validateInput() {
|
||||
radioName = Objects.requireNonNull(bind.internetRadioStationNameTextView.getText()).toString().trim();
|
||||
radioStreamURL = Objects.requireNonNull(bind.internetRadioStationStreamUrlTextView.getText()).toString().trim();
|
||||
radioHomepageURL = Objects.requireNonNull(bind.internetRadioStationHomepageUrlTextView.getText()).toString().trim();
|
||||
|
||||
if (TextUtils.isEmpty(radioName)) {
|
||||
bind.internetRadioStationNameTextView.setError(getString(R.string.error_required));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(radioStreamURL)) {
|
||||
bind.internetRadioStationStreamUrlTextView.setError(getString(R.string.error_required));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void dismissDialog() {
|
||||
radioCallback.onDismiss();
|
||||
Objects.requireNonNull(getDialog()).dismiss();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package com.cappielloantonio.play.viewmodel;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
|
||||
import com.cappielloantonio.play.repository.RadioRepository;
|
||||
import com.cappielloantonio.play.subsonic.models.InternetRadioStation;
|
||||
|
||||
public class RadioEditorViewModel extends AndroidViewModel {
|
||||
private static final String TAG = "RadioEditorViewModel";
|
||||
|
||||
private final RadioRepository radioRepository;
|
||||
|
||||
private InternetRadioStation toEdit;
|
||||
|
||||
public RadioEditorViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
|
||||
radioRepository = new RadioRepository();
|
||||
}
|
||||
|
||||
public InternetRadioStation getRadioToEdit() {
|
||||
return toEdit;
|
||||
}
|
||||
|
||||
public void setRadioToEdit(InternetRadioStation internetRadioStation) {
|
||||
this.toEdit = internetRadioStation;
|
||||
}
|
||||
|
||||
public void createRadio(String name, String streamURL, String homepageURL) {
|
||||
radioRepository.createInternetRadioStation(name, streamURL, homepageURL);
|
||||
}
|
||||
|
||||
public void updateRadio(String name, String streamURL, String homepageURL) {
|
||||
if (toEdit != null) radioRepository.updateInternetRadioStation(toEdit.getId(), name, streamURL, homepageURL);
|
||||
}
|
||||
|
||||
public void deleteRadio() {
|
||||
if (toEdit != null) radioRepository.deleteInternetRadioStation(toEdit.getId());
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,9 @@ import android.app.Application;
|
|||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.cappielloantonio.play.repository.RadioRepository;
|
||||
import com.cappielloantonio.play.subsonic.models.InternetRadioStation;
|
||||
|
|
@ -14,13 +16,16 @@ import java.util.List;
|
|||
public class RadioViewModel extends AndroidViewModel {
|
||||
private final RadioRepository radioRepository;
|
||||
|
||||
private final MutableLiveData<List<InternetRadioStation>> internetRadioStations = new MutableLiveData<>(null);
|
||||
|
||||
public RadioViewModel(@NonNull Application application) {
|
||||
super(application);
|
||||
|
||||
radioRepository = new RadioRepository();
|
||||
}
|
||||
|
||||
public LiveData<List<InternetRadioStation>> getInternetRadioStations() {
|
||||
return radioRepository.getInternetRadioStations();
|
||||
public LiveData<List<InternetRadioStation>> getInternetRadioStations(LifecycleOwner owner) {
|
||||
radioRepository.getInternetRadioStations().observe(owner, internetRadioStations::postValue);
|
||||
return internetRadioStations;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue