diff --git a/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/EventDecorator.java b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/EventDecorator.java new file mode 100644 index 0000000..9b751e6 --- /dev/null +++ b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/EventDecorator.java @@ -0,0 +1,40 @@ +package com.loganfreeman.utahfishing.common.decorators; + +import android.content.Context; +import android.graphics.drawable.Drawable; + +import com.loganfreeman.utahfishing.R; +import com.loganfreeman.utahfishing.common.PLog; +import com.prolificinteractive.materialcalendarview.CalendarDay; +import com.prolificinteractive.materialcalendarview.DayViewDecorator; +import com.prolificinteractive.materialcalendarview.DayViewFacade; +import com.prolificinteractive.materialcalendarview.spans.DotSpan; + +import java.util.Collection; +import java.util.HashSet; + +/** + * Created by scheng on 3/23/17. + */ + +public class EventDecorator implements DayViewDecorator { + + private HashSet dates; + private final Drawable drawable; + + public EventDecorator(Context context, Collection dates) { + this.dates = new HashSet<>(dates); + + drawable = context.getResources().getDrawable(R.drawable.my_selector); + } + + @Override + public boolean shouldDecorate(CalendarDay day) { + return dates.contains(day); + } + + @Override + public void decorate(DayViewFacade view) { + view.setBackgroundDrawable(drawable); + } +} diff --git a/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/HighlightWeekendsDecorator.java b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/HighlightWeekendsDecorator.java new file mode 100644 index 0000000..f3c38cb --- /dev/null +++ b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/HighlightWeekendsDecorator.java @@ -0,0 +1,38 @@ +package com.loganfreeman.utahfishing.common.decorators; + +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; + +import com.prolificinteractive.materialcalendarview.CalendarDay; +import com.prolificinteractive.materialcalendarview.DayViewDecorator; +import com.prolificinteractive.materialcalendarview.DayViewFacade; + +import java.util.Calendar; + +/** + * Created by scheng on 3/23/17. + */ + +public class HighlightWeekendsDecorator implements DayViewDecorator { + + private final Calendar calendar = Calendar.getInstance(); + private final Drawable highlightDrawable; + private static final int color = Color.parseColor("#228BC34A"); + + public HighlightWeekendsDecorator() { + highlightDrawable = new ColorDrawable(color); + } + + @Override + public boolean shouldDecorate(CalendarDay day) { + day.copyTo(calendar); + int weekDay = calendar.get(Calendar.DAY_OF_WEEK); + return weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY; + } + + @Override + public void decorate(DayViewFacade view) { + view.setBackgroundDrawable(highlightDrawable); + } +} diff --git a/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/MySelectorDecorator.java b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/MySelectorDecorator.java new file mode 100644 index 0000000..a88a695 --- /dev/null +++ b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/MySelectorDecorator.java @@ -0,0 +1,32 @@ +package com.loganfreeman.utahfishing.common.decorators; + +import android.app.Activity; +import android.graphics.drawable.Drawable; + +import com.loganfreeman.utahfishing.R; +import com.prolificinteractive.materialcalendarview.CalendarDay; +import com.prolificinteractive.materialcalendarview.DayViewDecorator; +import com.prolificinteractive.materialcalendarview.DayViewFacade; + +/** + * Created by scheng on 3/23/17. + */ + +public class MySelectorDecorator implements DayViewDecorator { + + private final Drawable drawable; + + public MySelectorDecorator(Activity context) { + drawable = context.getResources().getDrawable(R.drawable.my_selector); + } + + @Override + public boolean shouldDecorate(CalendarDay day) { + return true; + } + + @Override + public void decorate(DayViewFacade view) { + view.setSelectionDrawable(drawable); + } +} diff --git a/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/OneDayDecorator.java b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/OneDayDecorator.java new file mode 100644 index 0000000..064d9f6 --- /dev/null +++ b/app/src/main/java/com/loganfreeman/utahfishing/common/decorators/OneDayDecorator.java @@ -0,0 +1,42 @@ +package com.loganfreeman.utahfishing.common.decorators; + +import android.graphics.Typeface; +import android.text.style.RelativeSizeSpan; +import android.text.style.StyleSpan; + +import com.prolificinteractive.materialcalendarview.CalendarDay; +import com.prolificinteractive.materialcalendarview.DayViewDecorator; +import com.prolificinteractive.materialcalendarview.DayViewFacade; + +import java.util.Date; + +/** + * Created by scheng on 3/23/17. + */ + +public class OneDayDecorator implements DayViewDecorator { + + private CalendarDay date; + + public OneDayDecorator() { + date = CalendarDay.today(); + } + + @Override + public boolean shouldDecorate(CalendarDay day) { + return date != null && day.equals(date); + } + + @Override + public void decorate(DayViewFacade view) { + view.addSpan(new StyleSpan(Typeface.BOLD)); + view.addSpan(new RelativeSizeSpan(1.4f)); + } + + /** + * We're changing the internals, so make sure to call {@linkplain MaterialCalendarView#invalidateDecorators()} + */ + public void setDate(Date date) { + this.date = CalendarDay.from(date); + } +} diff --git a/app/src/main/java/com/loganfreeman/utahfishing/modules/fishing/domain/StockReport.java b/app/src/main/java/com/loganfreeman/utahfishing/modules/fishing/domain/StockReport.java index 8e28aac..d78cc40 100644 --- a/app/src/main/java/com/loganfreeman/utahfishing/modules/fishing/domain/StockReport.java +++ b/app/src/main/java/com/loganfreeman/utahfishing/modules/fishing/domain/StockReport.java @@ -6,6 +6,8 @@ import android.os.Parcelable; import android.util.Pair; +import com.prolificinteractive.materialcalendarview.CalendarDay; + import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -16,14 +18,17 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.regex.Pattern; import java.util.stream.Collectors; import rx.Observable; import rx.subjects.AsyncSubject; import static android.R.id.list; +import static java.util.Arrays.stream; /** * Created by shanhong on 3/21/17. @@ -37,12 +42,17 @@ public class StockReport implements Parcelable { public static final DateFormat df = new SimpleDateFormat("mm/dd/yyyy"); + public static final Pattern dayMonYear = Pattern.compile("(\\d{2})/(\\d{2})/(\\d{4})"); + public String watername; public String county; public String species; public int quantity; public double length; public Date stockdate; + public int day; + public int month; + public int year; public String getCounty() { return county; @@ -114,6 +124,7 @@ public static List fromWildlife() throws IOException { return reports; } + @TargetApi(Build.VERSION_CODES.N) private static StockReport fromElement(Element element) { StockReport report = new StockReport(); report.watername = element.select("td.watername").first().text(); @@ -121,10 +132,20 @@ private static StockReport fromElement(Element element) { report.species = element.select("td.species").first().text(); report.quantity = Integer.parseInt(element.select("td.quantity").first().text()); report.length = Double.parseDouble(element.select("td.length").first().text()); - report.stockdate = getDate(element.select("td.stockdate").first().text()); + String stockdate = element.select("td.stockdate").first().text(); + report.stockdate = getDate(stockdate); + // parse day, month, year + List parts = Arrays.stream(stockdate.split("/")).map(u -> Integer.parseInt(u)).collect(Collectors.toList()); + report.month = parts.get(0); + report.day = parts.get(1); + report.year = parts.get(2); return report; } + public CalendarDay getCalendarDay() { + return CalendarDay.from(year, month, day); + } + @Override public int describeContents() { return 0; diff --git a/app/src/main/java/com/loganfreeman/utahfishing/modules/main/ui/CalendarFragment.java b/app/src/main/java/com/loganfreeman/utahfishing/modules/main/ui/CalendarFragment.java index 09e8071..e7f6137 100644 --- a/app/src/main/java/com/loganfreeman/utahfishing/modules/main/ui/CalendarFragment.java +++ b/app/src/main/java/com/loganfreeman/utahfishing/modules/main/ui/CalendarFragment.java @@ -1,5 +1,9 @@ package com.loganfreeman.utahfishing.modules.main.ui; +import android.annotation.TargetApi; +import android.graphics.Color; +import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -12,6 +16,11 @@ import com.loganfreeman.utahfishing.R; import com.loganfreeman.utahfishing.base.AbstractBaseFragment; import com.loganfreeman.utahfishing.common.PLog; +import com.loganfreeman.utahfishing.common.decorators.EventDecorator; +import com.loganfreeman.utahfishing.common.decorators.HighlightWeekendsDecorator; +import com.loganfreeman.utahfishing.common.decorators.MySelectorDecorator; +import com.loganfreeman.utahfishing.common.decorators.OneDayDecorator; +import com.loganfreeman.utahfishing.common.utils.StringUtils; import com.loganfreeman.utahfishing.common.utils.ToastUtil; import com.loganfreeman.utahfishing.modules.fishing.domain.StockReport; import com.prolificinteractive.materialcalendarview.CalendarDay; @@ -19,8 +28,14 @@ import com.prolificinteractive.materialcalendarview.OnDateSelectedListener; import com.prolificinteractive.materialcalendarview.OnMonthChangedListener; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; import butterknife.Bind; import butterknife.ButterKnife; @@ -34,10 +49,13 @@ public class CalendarFragment extends AbstractBaseFragment implements OnDateSelectedListener, OnMonthChangedListener { + public static final DateFormat df = new SimpleDateFormat("mm/dd/yyyy"); + @Bind(R.id.calendarView) MaterialCalendarView widget; private View view; + @Override protected void lazyLoad() { @@ -69,7 +87,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { @Override public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) { - + widget.invalidateDecorators(); } @Override @@ -98,10 +116,25 @@ public void onNext(List stockReports) { }); } + @TargetApi(Build.VERSION_CODES.N) private void initCalendar(List stockReports) { widget.setOnDateChangedListener(this); widget.setOnMonthChangedListener(this); + List calendarDays = stockReports.stream().map( u -> u.getCalendarDay() ).collect(Collectors.toList()); + + //PLog.e(calendarDays.stream().map(Object::toString).collect(Collectors.joining("\n"))); + + //PLog.e(stockReports.stream().map(u -> df.format(u.stockdate)).collect(Collectors.joining("\n"))); + + + + widget.addDecorator(new EventDecorator(CalendarFragment.this.getActivity(), calendarDays)); + + widget.addDecorators( + new HighlightWeekendsDecorator() + ); + } } diff --git a/app/src/main/res/drawable-hdpi/ic_my_selector.png b/app/src/main/res/drawable-hdpi/ic_my_selector.png new file mode 100644 index 0000000..46ceaba Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_my_selector.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_my_selector.png b/app/src/main/res/drawable-mdpi/ic_my_selector.png new file mode 100644 index 0000000..3b48334 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_my_selector.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_my_selector.png b/app/src/main/res/drawable-xhdpi/ic_my_selector.png new file mode 100644 index 0000000..4010b79 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_my_selector.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_my_selector.png b/app/src/main/res/drawable-xxhdpi/ic_my_selector.png new file mode 100644 index 0000000..3b531e9 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_my_selector.png differ diff --git a/app/src/main/res/drawable/my_selector.xml b/app/src/main/res/drawable/my_selector.xml new file mode 100644 index 0000000..c73c955 --- /dev/null +++ b/app/src/main/res/drawable/my_selector.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file