Search in sources :

Example 26 with DateTime

use of hirondelle.date4j.DateTime in project instructure-android by instructure.

the class CanvasCalendarUtils method setSelectedWeekWindow.

/**
 * Another helper method, used to determine and select the date window to select to properly
 * highlight a full week in the calendar grid.
 *
 * @param date - selected date
 * @return DateWindow object
 */
public static DateWindow setSelectedWeekWindow(Date date, boolean isStartDayMonday) {
    DateTime start = DateTime.forInstant(date.getTime(), TimeZone.getDefault());
    DateTime end = DateTime.forInstant(date.getTime(), TimeZone.getDefault());
    DateTime s1 = start;
    DateTime e1 = end;
    if (isStartDayMonday) {
        // returns 1 - 7, sun - sat
        int s = start.getWeekDay();
        switch(s) {
            case 1:
                s1 = start.minus(0, 0, 6, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                break;
            case 2:
                e1 = start.plus(0, 0, 6, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                break;
            case 3:
                s1 = start.minus(0, 0, 1, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 5, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 4:
                s1 = start.minus(0, 0, 2, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 4, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 5:
                s1 = start.minus(0, 0, 3, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 3, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 6:
                s1 = start.minus(0, 0, 4, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 2, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 7:
                s1 = start.minus(0, 0, 5, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 1, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
        }
    } else {
        // returns 1 - 7, sun - sat
        int s = start.getWeekDay();
        switch(s) {
            case 1:
                e1 = start.plus(0, 0, 6, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                break;
            case 2:
                s1 = start.minus(0, 0, 1, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 5, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 3:
                s1 = start.minus(0, 0, 2, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 4, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 4:
                s1 = start.minus(0, 0, 3, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 3, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 5:
                s1 = start.minus(0, 0, 4, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 2, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 6:
                s1 = start.minus(0, 0, 5, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                e1 = end.plus(0, 0, 1, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
                break;
            case 7:
                s1 = start.minus(0, 0, 6, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);
                break;
        }
    }
    Date startDate = new Date(s1.getEndOfDay().getMilliseconds(TimeZone.getDefault()));
    Date endDate = new Date(e1.getEndOfDay().getMilliseconds(TimeZone.getDefault()));
    return new DateWindow(startDate, endDate);
}
Also used : DateWindow(com.instructure.candroid.model.DateWindow) DateTime(hirondelle.date4j.DateTime) Date(java.util.Date)

Example 27 with DateTime

use of hirondelle.date4j.DateTime in project instructure-android by instructure.

the class DiscussionListRecyclerAdapterTest method testCompare_oneNullDatePostedAt.

@Test
public void testCompare_oneNullDatePostedAt() {
    DiscussionTopicHeader d1 = new DiscussionTopicHeader();
    d1.setTitle("discussion1");
    DateTime dateTime2 = new DateTime("2014-12-29");
    Date date = new Date(dateTime2.getMilliseconds(TimeZone.getDefault()));
    d1.setPostedAt(APIHelper.dateToString(date));
    DiscussionTopicHeader d2 = new DiscussionTopicHeader();
    d2.setTitle("discussion2");
    assertEquals(-1, mAdapter.createItemCallback().compare("", d1, d2));
    assertEquals(1, mAdapter.createItemCallback().compare("", d2, d1));
    assertEquals(0, mAdapter.createItemCallback().compare("", d1, d1));
}
Also used : DiscussionTopicHeader(com.instructure.canvasapi2.models.DiscussionTopicHeader) DateTime(hirondelle.date4j.DateTime) Date(java.util.Date) Test(org.junit.Test)

Example 28 with DateTime

use of hirondelle.date4j.DateTime in project Caldroid by roomorama.

the class CaldroidSampleCustomAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View cellView = convertView;
    // For reuse
    if (convertView == null) {
        cellView = inflater.inflate(R.layout.custom_cell, null);
    }
    int topPadding = cellView.getPaddingTop();
    int leftPadding = cellView.getPaddingLeft();
    int bottomPadding = cellView.getPaddingBottom();
    int rightPadding = cellView.getPaddingRight();
    TextView tv1 = (TextView) cellView.findViewById(R.id.tv1);
    TextView tv2 = (TextView) cellView.findViewById(R.id.tv2);
    tv1.setTextColor(Color.BLACK);
    // Get dateTime of this cell
    DateTime dateTime = this.datetimeList.get(position);
    Resources resources = context.getResources();
    // Set color of the dates in previous / next month
    if (dateTime.getMonth() != month) {
        tv1.setTextColor(resources.getColor(com.caldroid.R.color.caldroid_darker_gray));
    }
    boolean shouldResetDiabledView = false;
    boolean shouldResetSelectedView = false;
    // Customize for disabled dates and date outside min/max dates
    if ((minDateTime != null && dateTime.lt(minDateTime)) || (maxDateTime != null && dateTime.gt(maxDateTime)) || (disableDates != null && disableDates.indexOf(dateTime) != -1)) {
        tv1.setTextColor(CaldroidFragment.disabledTextColor);
        if (CaldroidFragment.disabledBackgroundDrawable == -1) {
            cellView.setBackgroundResource(com.caldroid.R.drawable.disable_cell);
        } else {
            cellView.setBackgroundResource(CaldroidFragment.disabledBackgroundDrawable);
        }
        if (dateTime.equals(getToday())) {
            cellView.setBackgroundResource(com.caldroid.R.drawable.red_border_gray_bg);
        }
    } else {
        shouldResetDiabledView = true;
    }
    // Customize for selected dates
    if (selectedDates != null && selectedDates.indexOf(dateTime) != -1) {
        cellView.setBackgroundColor(resources.getColor(com.caldroid.R.color.caldroid_sky_blue));
        tv1.setTextColor(Color.BLACK);
    } else {
        shouldResetSelectedView = true;
    }
    if (shouldResetDiabledView && shouldResetSelectedView) {
        // Customize for today
        if (dateTime.equals(getToday())) {
            cellView.setBackgroundResource(com.caldroid.R.drawable.red_border);
        } else {
            cellView.setBackgroundResource(com.caldroid.R.drawable.cell_bg);
        }
    }
    tv1.setText("" + dateTime.getDay());
    tv2.setText("Hi");
    // Somehow after setBackgroundResource, the padding collapse.
    // This is to recover the padding
    cellView.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
    // Set custom color if required
    setCustomResources(dateTime, cellView, tv1);
    return cellView;
}
Also used : LayoutInflater(android.view.LayoutInflater) TextView(android.widget.TextView) Resources(android.content.res.Resources) TextView(android.widget.TextView) View(android.view.View) DateTime(hirondelle.date4j.DateTime)

Example 29 with DateTime

use of hirondelle.date4j.DateTime in project Caldroid by roomorama.

the class CaldroidFragment method setTextColorForDates.

/**
     * Set textColorForDateMap
     *
     * @return
     */
public void setTextColorForDates(Map<Date, Integer> textColorForDateMap) {
    if (textColorForDateMap == null || textColorForDateMap.size() == 0) {
        return;
    }
    textColorForDateTimeMap.clear();
    for (Date date : textColorForDateMap.keySet()) {
        Integer resource = textColorForDateMap.get(date);
        DateTime dateTime = CalendarHelper.convertDateToDateTime(date);
        textColorForDateTimeMap.put(dateTime, resource);
    }
}
Also used : Date(java.util.Date) DateTime(hirondelle.date4j.DateTime)

Example 30 with DateTime

use of hirondelle.date4j.DateTime in project Caldroid by roomorama.

the class CaldroidFragment method moveToDateTime.

/**
     * Move calendar to specified dateTime, with animation
     *
     * @param dateTime
     */
public void moveToDateTime(DateTime dateTime) {
    DateTime firstOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
    DateTime lastOfMonth = firstOfMonth.getEndOfMonth();
    // Calendar swipe left when dateTime is in the past
    if (dateTime.lt(firstOfMonth)) {
        // Get next month of dateTime. When swipe left, month will
        // decrease
        DateTime firstDayNextMonth = dateTime.plus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
        // Refresh adapters
        pageChangeListener.setCurrentDateTime(firstDayNextMonth);
        int currentItem = dateViewPager.getCurrentItem();
        pageChangeListener.refreshAdapters(currentItem);
        // Swipe left
        dateViewPager.setCurrentItem(currentItem - 1);
    } else // Calendar swipe right when dateTime is in the future
    if (dateTime.gt(lastOfMonth)) {
        // Get last month of dateTime. When swipe right, the month will
        // increase
        DateTime firstDayLastMonth = dateTime.minus(0, 1, 0, 0, 0, 0, 0, DateTime.DayOverflow.LastDay);
        // Refresh adapters
        pageChangeListener.setCurrentDateTime(firstDayLastMonth);
        int currentItem = dateViewPager.getCurrentItem();
        pageChangeListener.refreshAdapters(currentItem);
        // Swipe right
        dateViewPager.setCurrentItem(currentItem + 1);
    }
}
Also used : DateTime(hirondelle.date4j.DateTime) SuppressLint(android.annotation.SuppressLint)

Aggregations

DateTime (hirondelle.date4j.DateTime)52 Date (java.util.Date)17 SuppressLint (android.annotation.SuppressLint)8 ArrayList (java.util.ArrayList)6 Calendar (java.util.Calendar)6 DateWindow (com.instructure.candroid.model.DateWindow)4 Test (org.junit.Test)4 View (android.view.View)3 TextView (android.widget.TextView)3 DiscussionTopicHeader (com.instructure.canvasapi2.models.DiscussionTopicHeader)3 Dialog (android.app.Dialog)2 Drawable (android.graphics.drawable.Drawable)2 Bundle (android.os.Bundle)2 LayoutInflater (android.view.LayoutInflater)2 InfinitePagerAdapter (com.antonyt.infiniteviewpager.InfinitePagerAdapter)2 EventData (com.instructure.candroid.model.EventData)2 ScheduleItem (com.instructure.canvasapi2.models.ScheduleItem)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Resources (android.content.res.Resources)1 Button (android.widget.Button)1