Created DaysOfWeek class
This commit is contained in:
@@ -1,77 +1,186 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.SwitchCompat;
|
||||
import android.support.v7.widget.util.SortedListAdapterCallback;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.LayoutInflater;
|
||||
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.DaysOfWeek;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.alarms.dummy.DummyContent.DummyItem;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
||||
|
||||
/**
|
||||
* {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
|
||||
* {@link RecyclerView.Adapter} that can display a {@link Alarm} and makes a call to the
|
||||
* specified {@link AlarmsFragment.OnListFragmentInteractionListener}.
|
||||
* TODO: Replace the implementation with code for your data type.
|
||||
*/
|
||||
public class AlarmsAdapter extends RecyclerView.Adapter<AlarmsAdapter.ViewHolder> {
|
||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||
|
||||
private final List<DummyItem> mValues;
|
||||
private final SortedList<Alarm> mItems;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
|
||||
public AlarmsAdapter(List<DummyItem> items, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
mValues = items;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.fragment_alarms, parent, false);
|
||||
return new ViewHolder(view);
|
||||
.inflate(R.layout.item_alarm, parent, false);
|
||||
return new ViewHolder(view, mListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||
holder.mItem = mValues.get(position);
|
||||
holder.mIdView.setText(mValues.get(position).id);
|
||||
holder.mContentView.setText(mValues.get(position).content);
|
||||
|
||||
holder.mView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (null != mListener) {
|
||||
// Notify the active callbacks interface (the activity, if the
|
||||
// fragment is attached to one) that an item has been selected.
|
||||
mListener.onListFragmentInteraction(holder.mItem);
|
||||
}
|
||||
}
|
||||
});
|
||||
holder.onBind(mItems.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mValues.size();
|
||||
return mItems.size();
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||
public final View mView;
|
||||
public final TextView mIdView;
|
||||
public final TextView mContentView;
|
||||
public DummyItem mItem;
|
||||
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
private final Context mContext;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
private Alarm mItem;
|
||||
|
||||
public ViewHolder(View view) {
|
||||
@Bind(R.id.time) TextView mTime;
|
||||
@Bind(R.id.on_off_switch) SwitchCompat mSwitch;
|
||||
@Bind(R.id.label) TextView mLabel;
|
||||
@Bind(R.id.countdown) TextView mCountdown; // TODO: Change type to NextAlarmText, once you move that class to this project
|
||||
@Bind(R.id.recurring_days) TextView mDays;
|
||||
@Bind(R.id.dismiss) Button mDismissButton;
|
||||
|
||||
public ViewHolder(View view, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
super(view);
|
||||
mView = view;
|
||||
mIdView = (TextView) view.findViewById(R.id.id);
|
||||
mContentView = (TextView) view.findViewById(R.id.content);
|
||||
ButterKnife.bind(this, view);
|
||||
mContext = view.getContext();
|
||||
mListener = listener;
|
||||
view.setOnClickListener(this);
|
||||
}
|
||||
|
||||
public void onBind(Alarm alarm) {
|
||||
mItem = alarm;
|
||||
String time = DateFormat.getTimeFormat(mContext).format(new Date(alarm.ringsAt()));
|
||||
if (DateFormat.is24HourFormat(mContext)) {
|
||||
mTime.setText(time);
|
||||
} else {
|
||||
// No way around having to construct this on binding
|
||||
SpannableString s = new SpannableString(time);
|
||||
s.setSpan(AMPM_SIZE_SPAN, time.indexOf(" "), time.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
mTime.setText(s, TextView.BufferType.SPANNABLE);
|
||||
}
|
||||
|
||||
if (alarm.isEnabled()) {
|
||||
mSwitch.setChecked(true);
|
||||
//TODO:mCountdown.showAsText(alarm.ringsIn());
|
||||
mCountdown.setVisibility(VISIBLE);
|
||||
//todo:mCountdown.getTickHandler().startTicking(true)
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
// how many hours before alarm is considered upcoming
|
||||
// TODO: shared prefs
|
||||
/*int hoursBeforeUpcoming = Integer.parseInt(prefs.getString(
|
||||
mContext.getString(-1TODO:R.string.key_notify_me_of_upcoming_alarms),
|
||||
"2"));*/
|
||||
if (alarm.ringsWithinHours(2) || alarm.isSnoozed()) {
|
||||
// TODO: Register dynamic broadcast receiver in this class to listen for
|
||||
// when this alarm crosses the upcoming threshold, so we can show this button.
|
||||
mDismissButton.setVisibility(VISIBLE);
|
||||
} else {
|
||||
mDismissButton.setVisibility(GONE);
|
||||
}
|
||||
} else {
|
||||
mSwitch.setChecked(false);
|
||||
mCountdown.setVisibility(GONE);
|
||||
//TODO:mCountdown.getTickHandler().stopTicking();
|
||||
mDismissButton.setVisibility(GONE);
|
||||
}
|
||||
|
||||
mLabel.setText(alarm.label());
|
||||
if (mLabel.length() == 0 && mCountdown.getVisibility() != VISIBLE) {
|
||||
mLabel.setVisibility(GONE);
|
||||
} else {
|
||||
// needed for proper positioning of mCountdown
|
||||
mLabel.setVisibility(VISIBLE);
|
||||
}
|
||||
|
||||
int numRecurringDays = alarm.numRecurringDays();
|
||||
if (numRecurringDays > 0) {
|
||||
String text;
|
||||
if (numRecurringDays == NUM_DAYS+1) /*TODO: Remove +1*/ {
|
||||
text = mContext.getString(R.string.every_day);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < NUM_DAYS; i++) {
|
||||
// The day at this position in the week
|
||||
int weekDay = DaysOfWeek.getInstance(mContext).weekDay(i);
|
||||
if (alarm.isRecurring(weekDay)) {
|
||||
sb.append(DaysOfWeek.getLabel(weekDay)).append(", ");
|
||||
}
|
||||
}
|
||||
// Cut off the last comma and space
|
||||
sb.delete(sb.length() - 2, sb.length());
|
||||
text = sb.toString();
|
||||
}
|
||||
mDays.setText(text);
|
||||
mDays.setVisibility(VISIBLE);
|
||||
} else {
|
||||
mDays.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + " '" + mContentView.getText() + "'";
|
||||
public void onClick(View v) {
|
||||
if (mListener != null) {
|
||||
mListener.onListFragmentInteraction(mItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ 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.alarms.dummy.DummyContent;
|
||||
import com.philliphsu.clock2.alarms.dummy.DummyContent.DummyItem;
|
||||
|
||||
/**
|
||||
* A fragment representing a list of Items.
|
||||
@@ -32,8 +32,7 @@ public class AlarmsFragment extends Fragment {
|
||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||
* fragment (e.g. upon screen orientation changes).
|
||||
*/
|
||||
public AlarmsFragment() {
|
||||
}
|
||||
public AlarmsFragment() {}
|
||||
|
||||
// TODO: Customize parameter initialization
|
||||
@SuppressWarnings("unused")
|
||||
@@ -57,7 +56,7 @@ public class AlarmsFragment extends Fragment {
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_alarms_list, container, false);
|
||||
View view = inflater.inflate(R.layout.fragment_alarms, container, false);
|
||||
|
||||
// Set the adapter
|
||||
if (view instanceof RecyclerView) {
|
||||
@@ -102,7 +101,6 @@ public class AlarmsFragment extends Fragment {
|
||||
* >Communicating with Other Fragments</a> for more information.
|
||||
*/
|
||||
public interface OnListFragmentInteractionListener {
|
||||
// TODO: Update argument type and name
|
||||
void onListFragmentInteraction(DummyItem item);
|
||||
void onListFragmentInteraction(Alarm item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.philliphsu.clock2.alarms.dummy;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.DaysOfWeek;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Helper class for providing sample content for user interfaces created by
|
||||
@@ -16,57 +17,31 @@ public class DummyContent {
|
||||
/**
|
||||
* An array of sample (dummy) items.
|
||||
*/
|
||||
public static final List<DummyItem> ITEMS = new ArrayList<DummyItem>();
|
||||
public static final List<Alarm> ITEMS = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* A map of sample (dummy) items, by ID.
|
||||
*/
|
||||
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
|
||||
|
||||
private static final int COUNT = 25;
|
||||
private static final int COUNT = 10;
|
||||
|
||||
static {
|
||||
// Add some sample items.
|
||||
for (int i = 1; i <= COUNT; i++) {
|
||||
addItem(createDummyItem(i));
|
||||
addItem(createAlarm(i));
|
||||
}
|
||||
}
|
||||
|
||||
private static void addItem(DummyItem item) {
|
||||
private static void addItem(Alarm item) {
|
||||
ITEMS.add(item);
|
||||
ITEM_MAP.put(item.id, item);
|
||||
}
|
||||
|
||||
private static DummyItem createDummyItem(int position) {
|
||||
return new DummyItem(String.valueOf(position), "Item " + position, makeDetails(position));
|
||||
}
|
||||
|
||||
private static String makeDetails(int position) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("Details about Item: ").append(position);
|
||||
for (int i = 0; i < position; i++) {
|
||||
builder.append("\nMore details information here.");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A dummy item representing a piece of content.
|
||||
*/
|
||||
public static class DummyItem {
|
||||
public final String id;
|
||||
public final String content;
|
||||
public final String details;
|
||||
|
||||
public DummyItem(String id, String content, String details) {
|
||||
this.id = id;
|
||||
this.content = content;
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return content;
|
||||
private static Alarm createAlarm(int position) {
|
||||
Alarm.Builder b = Alarm.builder();
|
||||
if (position % 2 == 0) {
|
||||
b.hour(21).minutes(0);
|
||||
}
|
||||
boolean[] recurrences = new boolean[DaysOfWeek.NUM_DAYS];
|
||||
recurrences[0] = true;
|
||||
recurrences[5] = true;
|
||||
Alarm a = b.id(position).recurringDays(recurrences).build();
|
||||
a.setEnabled(true);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user