feat: implemented updating shared items through dialog

This commit is contained in:
antonio 2023-09-17 16:43:09 +02:00
parent 077d22167c
commit f172a00fb7
7 changed files with 209 additions and 0 deletions

View file

@ -30,4 +30,6 @@ public interface ClickCallback {
default void onMusicDirectoryClick(Bundle bundle) {}
default void onMusicIndexClick(Bundle bundle) {}
default void onDownloadGroupLongClick(Bundle bundle) {}
default void onShareClick(Bundle bundle) {}
default void onShareLongClick(Bundle bundle) {}
}

View file

@ -0,0 +1,137 @@
package com.cappielloantonio.tempo.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.tempo.R;
import com.cappielloantonio.tempo.databinding.DialogShareUpdateBinding;
import com.cappielloantonio.tempo.util.UIUtil;
import com.cappielloantonio.tempo.viewmodel.HomeViewModel;
import com.cappielloantonio.tempo.viewmodel.ShareBottomSheetViewModel;
import com.google.android.material.datepicker.CalendarConstraints;
import com.google.android.material.datepicker.DateValidatorPointForward;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialPickerOnPositiveButtonClickListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
public class ShareUpdateDialog extends DialogFragment {
private static final String TAG = "ShareUpdateDialog";
private DialogShareUpdateBinding bind;
private HomeViewModel homeViewModel;
private ShareBottomSheetViewModel shareBottomSheetViewModel;
private MaterialDatePicker<Long> datePicker;
private String descriptionTextView;
private String expirationTextView;
private long expiration;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
shareBottomSheetViewModel = new ViewModelProvider(requireActivity()).get(ShareBottomSheetViewModel.class);
bind = DialogShareUpdateBinding.inflate(getLayoutInflater());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(bind.getRoot())
.setTitle(R.string.share_update_dialog_title)
.setPositiveButton(R.string.share_update_dialog_positive_button, (dialog, id) -> {
})
.setNegativeButton(R.string.share_update_dialog_negative_button, (dialog, id) -> dialog.cancel());
return builder.create();
}
@Override
public void onStart() {
super.onStart();
setShareInfo();
setShareCalendar();
setButtonAction();
}
@Override
public void onDestroyView() {
super.onDestroyView();
bind = null;
}
private void setShareInfo() {
if (shareBottomSheetViewModel.getShare() != null) {
bind.shareDescriptionTextView.setText(shareBottomSheetViewModel.getShare().getDescription());
// bind.shareExpirationTextView.setText(shareBottomSheetViewModel.getShare().getExpires());
}
}
private void setShareCalendar() {
expiration = shareBottomSheetViewModel.getShare().getExpires().getTime();
bind.shareExpirationTextView.setText(UIUtil.getReadableDate(new Date(expiration)));
bind.shareExpirationTextView.setFocusable(false);
bind.shareExpirationTextView.setOnLongClickListener(null);
bind.shareExpirationTextView.setOnClickListener(view -> {
CalendarConstraints constraints = new CalendarConstraints.Builder()
.setValidator(DateValidatorPointForward.now())
.build();
datePicker = MaterialDatePicker.Builder.datePicker()
.setCalendarConstraints(constraints)
.setSelection(expiration)
.build();
datePicker.addOnPositiveButtonClickListener(selection -> {
expiration = selection;
bind.shareExpirationTextView.setText(UIUtil.getReadableDate(new Date(selection)));
});
datePicker.show(requireActivity().getSupportFragmentManager(), null);
});
}
private void setButtonAction() {
((AlertDialog) Objects.requireNonNull(getDialog())).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
if (validateInput()) {
updateShare();
Objects.requireNonNull(getDialog()).dismiss();
}
});
}
private boolean validateInput() {
descriptionTextView = Objects.requireNonNull(bind.shareDescriptionTextView.getText()).toString().trim();
expirationTextView = Objects.requireNonNull(bind.shareExpirationTextView.getText()).toString().trim();
if (TextUtils.isEmpty(descriptionTextView)) {
bind.shareDescriptionTextView.setError(getString(R.string.error_required));
return false;
}
if (TextUtils.isEmpty(expirationTextView)) {
bind.shareExpirationTextView.setError(getString(R.string.error_required));
return false;
}
return true;
}
private void updateShare() {
shareBottomSheetViewModel.updateShare(descriptionTextView, expiration);
homeViewModel.refreshShares(requireActivity());
}
}

View file

@ -86,6 +86,8 @@ object Constants {
const val DOWNLOAD_GROUP_TITLE = "download_group_title"
const val DOWNLOAD_GROUP_SUBTITLE = "download_group_subtitle"
const val SHARE_OBJECT = "share_object"
const val PLAYABLE_MEDIA_LIMIT = 100
const val PRE_PLAYABLE_MEDIA = 15
}

View file

@ -0,0 +1,50 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:textColorHint="?android:textColorHint"
app:endIconMode="clear_text"
app:endIconTint="?android:textColorSecondary"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/share_description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/share_update_dialog_hint_description"
android:inputType="textNoSuggestions"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:textColorHint="?android:textColorHint"
app:endIconMode="clear_text"
app:endIconTint="?android:textColorSecondary"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/share_expiration_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
android:hint="@string/share_update_dialog_hint_expiration_date"
android:inputType="textShortMessage"
android:textCursorDrawable="@null" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

View file

@ -330,4 +330,9 @@
android:name="com.cappielloantonio.tempo.ui.fragment.bottomsheetdialog.PodcastChannelBottomSheetDialog"
android:label="PodcastChannelBottomSheetDialog"
tools:layout="@layout/bottom_sheet_podcast_channel_dialog" />
<dialog
android:id="@+id/shareBottomSheetDialog"
android:name="com.cappielloantonio.tempo.ui.fragment.bottomsheetdialog.ShareBottomSheetDialog"
android:label="ShareBottomSheetDialog"
tools:layout="@layout/bottom_sheet_share_dialog" />
</navigation>

View file

@ -89,6 +89,7 @@
<string name="home_title_recently_added">Kürzlich hinzugefügt</string>
<string name="home_title_recently_added_see_all_button">Alle zeigen</string>
<string name="home_title_discovery_shuffle_all_button">Alle mischen</string>
<string name="home_title_shares">Shares</string>
<string name="home_title_starred_albums">★ Lieblingsalben</string>
<string name="home_title_starred_albums_see_all_button">Alle zeigen</string>
<string name="home_title_starred_artists">★ Lieblingskünstler</string>

View file

@ -13,6 +13,7 @@
<string name="album_bottom_sheet_play_next">Play next</string>
<string name="album_bottom_sheet_remove_all">Remove all</string>
<string name="album_bottom_sheet_shuffle">Shuffle</string>
<string name="album_bottom_sheet_share">Share</string>
<string name="album_catalogue_title">Albums</string>
<string name="album_catalogue_title_expanded">Browse Albums</string>
<string name="album_error_retrieving_artist">Error retrieving artist</string>
@ -89,6 +90,7 @@
<string name="home_title_recently_added">Recently added</string>
<string name="home_title_recently_added_see_all_button">See all</string>
<string name="home_title_discovery_shuffle_all_button">Shuffle all</string>
<string name="home_title_shares">Shares</string>
<string name="home_title_starred_albums">★ Starred albums</string>
<string name="home_title_starred_albums_see_all_button">See all</string>
<string name="home_title_starred_artists">★ Starred artists</string>
@ -246,6 +248,15 @@
<string name="settings_version_title">Version</string>
<string name="settings_wifi_only_title">Stream via Wi-Fi only alert</string>
<string name="settings_wifi_only_summary">Ask for user confirmation before streaming over mobile network.</string>
<string name="share_bottom_sheet_copy_link">Copy link</string>
<string name="share_bottom_sheet_delete">Delete share</string>
<string name="share_bottom_sheet_update">Update share</string>
<string name="share_subtitle_item">Expiration date: %1$s</string>
<string name="share_update_dialog_negative_button">Cancel</string>
<string name="share_update_dialog_positive_button">Save</string>
<string name="share_update_dialog_hint_description">Description</string>
<string name="share_update_dialog_hint_expiration_date">Expiration date</string>
<string name="share_update_dialog_title">Share</string>
<string name="song_bottom_sheet_add_to_playlist">Add to playlist</string>
<string name="song_bottom_sheet_add_to_queue">Add to queue</string>
<string name="song_bottom_sheet_download">Download</string>
@ -253,6 +264,7 @@
<string name="song_bottom_sheet_error_retrieving_artist">Error retrieving artist</string>
<string name="song_bottom_sheet_go_to_album">Go to album</string>
<string name="song_bottom_sheet_go_to_artist">Go to artist</string>
<string name="song_bottom_sheet_share">Share</string>
<string name="song_bottom_sheet_instant_mix">Instant mix</string>
<string name="song_bottom_sheet_play_next">Play next</string>
<string name="song_bottom_sheet_rate">Rate</string>