Skip to content

Commit

Permalink
More dialog improvements/refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
dgudim committed Jan 13, 2024
1 parent 9a45b28 commit ebe9055
Show file tree
Hide file tree
Showing 55 changed files with 1,057 additions and 716 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package prototype.xd.scheduler.adapters;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

public class BindingViewHolder<T extends ViewBinding> extends RecyclerView.ViewHolder {
@NonNull
protected final T binding;

public BindingViewHolder(@NonNull T binding) {
super(binding.getRoot());
this.binding = binding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static prototype.xd.scheduler.utilities.Utilities.getPluralString;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

Expand All @@ -11,58 +12,57 @@
import prototype.xd.scheduler.R;
import prototype.xd.scheduler.databinding.CalendarColorEntryBinding;
import prototype.xd.scheduler.entities.SystemCalendar;
import prototype.xd.scheduler.fragments.dialogs.CalendarSettingsDialogFragment;
import prototype.xd.scheduler.utilities.misc.ContextWrapper;
import prototype.xd.scheduler.views.settings.SystemCalendarSettings;

/**
* Grid adapter class for displaying per color event settings
*/
public class CalendarColorsViewAdapter extends RecyclerView.Adapter<CalendarColorsViewAdapter.EntryViewHolder> {

@NonNull
private final SystemCalendarSettings systemCalendarSettings;
private final CalendarSettingsDialogFragment calendarSettingsDialogFragment;

@NonNull
private final SystemCalendar calendar;
private SystemCalendar calendar;

@NonNull
private final ContextWrapper wrapper;

public CalendarColorsViewAdapter(@NonNull final SystemCalendarSettings systemCalendarSettings,
@NonNull final SystemCalendar calendar,
public CalendarColorsViewAdapter(@NonNull final CalendarSettingsDialogFragment calendarSettingsDialogFragment,
@NonNull final ContextWrapper wrapper) {
this.systemCalendarSettings = systemCalendarSettings;
this.calendar = calendar;
this.calendarSettingsDialogFragment = calendarSettingsDialogFragment;
this.wrapper = wrapper;
// No colors repeat
setHasStableIds(true);
}

@SuppressLint("NotifyDataSetChanged")
public void setCalendar(@NonNull SystemCalendar calendar) {
this.calendar = calendar;
notifyDataSetChanged();
}

/**
* View holder for this adapter
*/
static class EntryViewHolder extends RecyclerView.ViewHolder {

@NonNull
private final CalendarColorEntryBinding binding;
static class EntryViewHolder extends BindingViewHolder<CalendarColorEntryBinding> {

EntryViewHolder(@NonNull final CalendarColorEntryBinding binding) {
super(binding.getRoot());
this.binding = binding;
EntryViewHolder(@NonNull CalendarColorEntryBinding binding) {
super(binding);
}

private void bind(int eventColor, int eventCount,
@NonNull ContextWrapper wrapper,
@NonNull final SystemCalendar calendar,
@NonNull final SystemCalendarSettings systemCalendarSettings) {
@NonNull final CalendarSettingsDialogFragment calendarSettingsDialogFragment) {

String eventPrefKey = calendar.makeEventPrefKey(eventColor);

binding.color.setCardBackgroundColor(eventColor);
binding.titleDefault.setVisibility(eventCount == calendar.data.color ? View.VISIBLE : View.GONE);
binding.eventCount.setText(getPluralString(binding.getRoot().getContext(), R.plurals.calendar_event_count, eventCount));
binding.openSettingsButton.setOnClickListener(v ->
systemCalendarSettings.show(
calendarSettingsDialogFragment.show(
eventPrefKey, calendar.makeEventSubKeys(eventPrefKey),
eventColor, wrapper));
}
Expand All @@ -80,7 +80,7 @@ public void onBindViewHolder(@NonNull EntryViewHolder holder, int position) {
holder.bind(
calendar.eventColorCountMap.keyAt(position),
calendar.eventColorCountMap.valueAt(position),
wrapper, calendar, systemCalendarSettings);
wrapper, calendar, calendarSettingsDialogFragment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package prototype.xd.scheduler.adapters;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import prototype.xd.scheduler.R;
import prototype.xd.scheduler.databinding.ListSelectionEventBinding;
import prototype.xd.scheduler.entities.SystemCalendar;
import prototype.xd.scheduler.entities.SystemCalendarEvent;
import prototype.xd.scheduler.utilities.DateManager;
import prototype.xd.scheduler.utilities.misc.ContextWrapper;

public class CalendarEventViewAdapter extends RecyclerView.Adapter<CalendarEventViewAdapter.EntryViewHolder> {

private SystemCalendar calendar;
private final ContextWrapper wrapper;

public CalendarEventViewAdapter(@NonNull ContextWrapper wrapper) {
this.wrapper = wrapper;
// Each event is unique
setHasStableIds(true);
}

@SuppressLint("NotifyDataSetChanged")
public void setCalendar(@NonNull SystemCalendar calendar) {
this.calendar = calendar;
notifyDataSetChanged();
}

static class EntryViewHolder extends BindingViewHolder<ListSelectionEventBinding> {

EntryViewHolder(@NonNull ListSelectionEventBinding binding) {
super(binding);
}

private void bind(@NonNull SystemCalendarEvent event, @NonNull ContextWrapper wrapper) {
binding.eventText.setText(event.data.title);
binding.eventColor.setCardBackgroundColor(event.data.color);
binding.recurrenceText.setVisibility(event.isRecurring() ? View.VISIBLE : View.GONE);
String timeSpan = DateManager.getTimeSpan(event.getFirstInstanceTimeRange());
if (event.isAllDay()) {
binding.timeText.setText(wrapper.getString(R.string.calendar_event_all_day) + " (" + timeSpan + ")");
} else {
binding.timeText.setText(timeSpan);
}
}

}

@NonNull
@Override
public EntryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new EntryViewHolder(ListSelectionEventBinding.inflate(wrapper.getLayoutInflater(), parent, false));
}

@Override
public void onBindViewHolder(@NonNull EntryViewHolder holder, int position) {
holder.bind(calendar.systemCalendarEventMap.valueAt(position), wrapper);
}

@Override
public int getItemCount() {
return calendar.systemCalendarEventMap.size();
}

@Override
public long getItemId(int position) {
return calendar.systemCalendarEventMap.valueAt(position).hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,47 @@ public class PerDayBgGridViewAdapter extends RecyclerView.Adapter<PerDayBgGridVi

public static final String NAME = PerDayBgGridViewAdapter.class.getSimpleName();

// called when a user clicks on an image to select a new one
@NonNull
private final IntConsumer bgSelectionClickedCallback;
// fallback day
private final String defaultDay;

public PerDayBgGridViewAdapter(@NonNull final Context context,
@NonNull final IntConsumer bgSelectionClickedCallback) {
defaultDay = context.getString(R.string.day_default);
this.bgSelectionClickedCallback = bgSelectionClickedCallback;
// each day has a unique id
setHasStableIds(true);
}

/**
* View holder for this adapter
*/
static class EntryViewHolder extends RecyclerView.ViewHolder {
static class EntryViewHolder extends BindingViewHolder<BackgroundImageEntryBinding> {

@NonNull
private final BackgroundImageEntryBinding viewBinding;

EntryViewHolder(@NonNull final BackgroundImageEntryBinding viewBinding) {
super(viewBinding.getRoot());
this.viewBinding = viewBinding;
EntryViewHolder(@NonNull BackgroundImageEntryBinding binding) {
super(binding);
}

private void bind(final int dayId, @NonNull String defaultDay, @NonNull IntConsumer bgSelectionClickedCallback) {

viewBinding.bgTitle.setText(DateManager.getLocalWeekdayByIndex(dayId, defaultDay));
binding.bgTitle.setText(DateManager.getLocalWeekdayByIndex(dayId, defaultDay));

try {
// load bitmap from file
viewBinding.bgImage.setImageBitmap(readBitmapFromFile(getFile(DateManager.BG_NAMES_ROOT.get(dayId) + "_min.png")));
binding.bgImage.setImageBitmap(readBitmapFromFile(getFile(DateManager.BG_NAMES_ROOT.get(dayId) + "_min.png")));
} catch (FileNotFoundException e) { // NOSONAR, this is fine, bg just doesn't exist
// set default empty image
viewBinding.bgImage.setImageResource(R.drawable.ic_not_90);
binding.bgImage.setImageResource(R.drawable.ic_not_90);
} catch (IOException e) {
logException(NAME, e);
}

viewBinding.bgImageContainer.setOnClickListener(v -> bgSelectionClickedCallback.accept(dayId));
binding.bgImageContainer.setOnClickListener(v -> bgSelectionClickedCallback.accept(dayId));
}
}


// called when a user clicks on an image to select a new one
@NonNull
private final IntConsumer bgSelectionClickedCallback;
// fallback day
private final String defaultDay;

public PerDayBgGridViewAdapter(@NonNull final Context context,
@NonNull final IntConsumer bgSelectionClickedCallback) {
defaultDay = context.getString(R.string.day_default);
this.bgSelectionClickedCallback = bgSelectionClickedCallback;
// each day has a unique id
setHasStableIds(true);
}

@NonNull
@Override
public EntryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand Down
Loading

0 comments on commit ebe9055

Please sign in to comment.