BaseVH and BaseAdapter finished
This commit is contained in:
@@ -1,27 +1,25 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.SwitchCompat;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.BaseViewHolder;
|
||||
import com.philliphsu.clock2.DaysOfWeek;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
@@ -30,13 +28,9 @@ import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
||||
/**
|
||||
* Created by Phillip Hsu on 5/31/2016.
|
||||
*/
|
||||
public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
public class AlarmViewHolder extends BaseViewHolder<Alarm> {
|
||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||
|
||||
private final Context mContext;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
private Alarm mItem;
|
||||
|
||||
@Bind(R.id.time) TextView mTime;
|
||||
@Bind(R.id.on_off_switch) SwitchCompat mSwitch;
|
||||
@Bind(R.id.label) TextView mLabel;
|
||||
@@ -44,27 +38,15 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
@Bind(R.id.recurring_days) TextView mDays;
|
||||
@Bind(R.id.dismiss) Button mDismissButton;
|
||||
|
||||
/*public AlarmViewHolder(ViewGroup parent, BaseViewHolder.OnClickListener<Alarm> listener) {
|
||||
|
||||
}*/
|
||||
|
||||
public AlarmViewHolder(View view, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
super(view);
|
||||
ButterKnife.bind(this, view);
|
||||
mContext = view.getContext();
|
||||
mListener = listener;
|
||||
view.setOnClickListener(this);
|
||||
public AlarmViewHolder(ViewGroup parent, OnListItemInteractionListener<Alarm> listener) {
|
||||
super(parent, R.layout.item_alarm, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call to super must be the first line in the overridden implementation,
|
||||
* so that the base class can keep a reference to the item parameter.
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onBind(Alarm alarm) {
|
||||
mItem = alarm;
|
||||
String time = DateFormat.getTimeFormat(mContext).format(new Date(alarm.ringsAt()));
|
||||
if (DateFormat.is24HourFormat(mContext)) {
|
||||
super.onBind(alarm);
|
||||
String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt()));
|
||||
if (DateFormat.is24HourFormat(getContext())) {
|
||||
mTime.setText(time);
|
||||
} else {
|
||||
// No way around having to construct this on binding
|
||||
@@ -78,7 +60,7 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
//TODO:mCountdown.showAsText(alarm.ringsIn());
|
||||
mCountdown.setVisibility(VISIBLE);
|
||||
//todo:mCountdown.getTickHandler().startTicking(true)
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
// how many hours before alarm is considered upcoming
|
||||
// TODO: shared prefs
|
||||
/*int hoursBeforeUpcoming = Integer.parseInt(prefs.getString(
|
||||
@@ -110,14 +92,14 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
if (numRecurringDays > 0) {
|
||||
String text;
|
||||
if (numRecurringDays == NUM_DAYS) {
|
||||
text = mContext.getString(R.string.every_day);
|
||||
text = getContext().getString(R.string.every_day);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; // ordinal number, i.e. the position in the week, not an actual day!
|
||||
i < NUM_DAYS; i++) {
|
||||
if (alarm.isRecurring(i)) { // Is the i-th day in the week recurring?
|
||||
// This is the actual day at the i-th position in the week.
|
||||
int weekDay = DaysOfWeek.getInstance(mContext).weekDay(i);
|
||||
int weekDay = DaysOfWeek.getInstance(getContext()).weekDay(i);
|
||||
sb.append(DaysOfWeek.getLabel(weekDay)).append(", ");
|
||||
}
|
||||
}
|
||||
@@ -131,11 +113,4 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
mDays.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mListener != null) {
|
||||
mListener.onListFragmentInteraction(mItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,43 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.util.SortedListAdapterCallback;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.BaseAdapter;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link RecyclerView.Adapter} that can display a {@link Alarm} and makes a call to the
|
||||
* specified {@link AlarmsFragment.OnListFragmentInteractionListener}.
|
||||
*/
|
||||
public class AlarmsAdapter extends RecyclerView.Adapter<AlarmViewHolder> {
|
||||
public class AlarmsAdapter extends BaseAdapter<Alarm, AlarmViewHolder> {
|
||||
|
||||
private final SortedList<Alarm> mItems;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
|
||||
public AlarmsAdapter(List<Alarm> alarms, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
mItems = new SortedList<>(Alarm.class, new SortedListAdapterCallback<Alarm>(this) {
|
||||
@Override
|
||||
public int compare(Alarm o1, Alarm o2) {
|
||||
return Long.compare(o1.ringsAt(), o2.ringsAt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(Alarm oldItem, Alarm newItem) {
|
||||
return oldItem.hour() == newItem.hour()
|
||||
&& oldItem.minutes() == newItem.minutes()
|
||||
&& oldItem.isEnabled() == newItem.isEnabled()
|
||||
&& oldItem.label().equals(newItem.label())
|
||||
&& oldItem.ringsIn() == newItem.ringsIn()
|
||||
&& Arrays.equals(oldItem.recurringDays(), newItem.recurringDays())
|
||||
&& oldItem.snoozingUntil() == newItem.snoozingUntil();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(Alarm item1, Alarm item2) {
|
||||
return item1.id() == item2.id();
|
||||
}
|
||||
});
|
||||
mItems.addAll(alarms);
|
||||
mListener = listener;
|
||||
public AlarmsAdapter(List<Alarm> alarms, OnListItemInteractionListener<Alarm> listener) {
|
||||
super(Alarm.class, alarms, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AlarmViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_alarm, parent, false);
|
||||
return new AlarmViewHolder(view, mListener);
|
||||
public AlarmViewHolder onCreateViewHolder(ViewGroup parent, OnListItemInteractionListener<Alarm> listener) {
|
||||
return new AlarmViewHolder(parent, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final AlarmViewHolder holder, int position) {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
holder.onBind(mItems.get(position));
|
||||
public int compare(Alarm o1, Alarm o2) {
|
||||
return Long.compare(o1.ringsAt(), o2.ringsAt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
return mItems.size();
|
||||
public boolean areContentsTheSame(Alarm oldItem, Alarm newItem) {
|
||||
return oldItem.hour() == newItem.hour()
|
||||
&& oldItem.minutes() == newItem.minutes()
|
||||
&& oldItem.isEnabled() == newItem.isEnabled()
|
||||
&& oldItem.label().equals(newItem.label())
|
||||
&& oldItem.ringsIn() == newItem.ringsIn()
|
||||
&& Arrays.equals(oldItem.recurringDays(), newItem.recurringDays())
|
||||
&& oldItem.snoozingUntil() == newItem.snoozingUntil();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(Alarm item1, Alarm item2) {
|
||||
return item1.id() == item2.id();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,14 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.alarms.dummy.DummyContent;
|
||||
|
||||
/**
|
||||
* A fragment representing a list of Items.
|
||||
* <p/>
|
||||
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
|
||||
* Activities containing this fragment MUST implement the {@link OnAlarmInteractionListener}
|
||||
* interface.
|
||||
*/
|
||||
public class AlarmsFragment extends Fragment {
|
||||
@@ -26,7 +27,7 @@ public class AlarmsFragment extends Fragment {
|
||||
private static final String ARG_COLUMN_COUNT = "column-count";
|
||||
// TODO: Customize parameters
|
||||
private int mColumnCount = 1;
|
||||
private OnListFragmentInteractionListener mListener;
|
||||
private OnAlarmInteractionListener mListener;
|
||||
|
||||
/**
|
||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||
@@ -76,11 +77,11 @@ public class AlarmsFragment extends Fragment {
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
if (context instanceof OnListFragmentInteractionListener) {
|
||||
mListener = (OnListFragmentInteractionListener) context;
|
||||
if (context instanceof OnAlarmInteractionListener) {
|
||||
mListener = (OnAlarmInteractionListener) context;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString()
|
||||
+ " must implement OnListFragmentInteractionListener");
|
||||
+ " must implement OnAlarmInteractionListener");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +101,5 @@ public class AlarmsFragment extends Fragment {
|
||||
* "http://developer.android.com/training/basics/fragments/communicating.html"
|
||||
* >Communicating with Other Fragments</a> for more information.
|
||||
*/
|
||||
public interface OnListFragmentInteractionListener {
|
||||
void onListFragmentInteraction(Alarm item);
|
||||
}
|
||||
public interface OnAlarmInteractionListener extends OnListItemInteractionListener<Alarm> {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user