Search in sources :

Example 1 with CaldroidListener

use of com.roomorama.caldroid.CaldroidListener in project Caldroid by roomorama.

the class CaldroidSampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
    // Setup caldroid fragment
    // **** If you want normal CaldroidFragment, use below line ****
    caldroidFragment = new CaldroidFragment();
    // If Activity is created after rotation
    if (savedInstanceState != null) {
        caldroidFragment.restoreStatesFromKey(savedInstanceState, "CALDROID_SAVED_STATE");
    } else // If activity is created from fresh
    {
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
        args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
        // Uncomment this to customize startDayOfWeek
        // args.putInt(CaldroidFragment.START_DAY_OF_WEEK,
        // CaldroidFragment.TUESDAY); // Tuesday
        // Uncomment this line to use Caldroid in compact mode
        // args.putBoolean(CaldroidFragment.SQUARE_TEXT_VIEW_CELL, false);
        // Uncomment this line to use dark theme
        //            args.putInt(CaldroidFragment.THEME_RESOURCE, com.caldroid.R.style.CaldroidDefaultDark);
        caldroidFragment.setArguments(args);
    }
    setCustomResourceForDates();
    // Attach to the activity
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.calendar1, caldroidFragment);
    t.commit();
    // Setup listener
    final CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            Toast.makeText(getApplicationContext(), formatter.format(date), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onChangeMonth(int month, int year) {
            String text = "month: " + month + " year: " + year;
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClickDate(Date date, View view) {
            Toast.makeText(getApplicationContext(), "Long click " + formatter.format(date), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCaldroidViewCreated() {
            if (caldroidFragment.getLeftArrowButton() != null) {
                Toast.makeText(getApplicationContext(), "Caldroid view is created", Toast.LENGTH_SHORT).show();
            }
        }
    };
    // Setup Caldroid
    caldroidFragment.setCaldroidListener(listener);
    final TextView textView = (TextView) findViewById(R.id.textview);
    final Button customizeButton = (Button) findViewById(R.id.customize_button);
    // Customize the calendar
    customizeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (undo) {
                customizeButton.setText(getString(R.string.customize));
                textView.setText("");
                // Reset calendar
                caldroidFragment.clearDisableDates();
                caldroidFragment.clearSelectedDates();
                caldroidFragment.setMinDate(null);
                caldroidFragment.setMaxDate(null);
                caldroidFragment.setShowNavigationArrows(true);
                caldroidFragment.setEnableSwipe(true);
                caldroidFragment.refreshView();
                undo = false;
                return;
            }
            // Else
            undo = true;
            customizeButton.setText(getString(R.string.undo));
            Calendar cal = Calendar.getInstance();
            // Min date is last 7 days
            cal.add(Calendar.DATE, -7);
            Date minDate = cal.getTime();
            // Max date is next 7 days
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 14);
            Date maxDate = cal.getTime();
            // Set selected dates
            // From Date
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 2);
            Date fromDate = cal.getTime();
            // To Date
            cal = Calendar.getInstance();
            cal.add(Calendar.DATE, 3);
            Date toDate = cal.getTime();
            // Set disabled dates
            ArrayList<Date> disabledDates = new ArrayList<Date>();
            for (int i = 5; i < 8; i++) {
                cal = Calendar.getInstance();
                cal.add(Calendar.DATE, i);
                disabledDates.add(cal.getTime());
            }
            // Customize
            caldroidFragment.setMinDate(minDate);
            caldroidFragment.setMaxDate(maxDate);
            caldroidFragment.setDisableDates(disabledDates);
            caldroidFragment.setSelectedDates(fromDate, toDate);
            caldroidFragment.setShowNavigationArrows(false);
            caldroidFragment.setEnableSwipe(false);
            caldroidFragment.refreshView();
            // Move to date
            // cal = Calendar.getInstance();
            // cal.add(Calendar.MONTH, 12);
            // caldroidFragment.moveToDate(cal.getTime());
            String text = "Today: " + formatter.format(new Date()) + "\n";
            text += "Min Date: " + formatter.format(minDate) + "\n";
            text += "Max Date: " + formatter.format(maxDate) + "\n";
            text += "Select From Date: " + formatter.format(fromDate) + "\n";
            text += "Select To Date: " + formatter.format(toDate) + "\n";
            for (Date date : disabledDates) {
                text += "Disabled Date: " + formatter.format(date) + "\n";
            }
            textView.setText(text);
        }
    });
    Button showDialogButton = (Button) findViewById(R.id.show_dialog_button);
    final Bundle state = savedInstanceState;
    showDialogButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Setup caldroid to use as dialog
            dialogCaldroidFragment = new CaldroidFragment();
            dialogCaldroidFragment.setCaldroidListener(listener);
            // If activity is recovered from rotation
            final String dialogTag = "CALDROID_DIALOG_FRAGMENT";
            if (state != null) {
                dialogCaldroidFragment.restoreDialogStatesFromKey(getSupportFragmentManager(), state, "DIALOG_CALDROID_SAVED_STATE", dialogTag);
                Bundle args = dialogCaldroidFragment.getArguments();
                if (args == null) {
                    args = new Bundle();
                    dialogCaldroidFragment.setArguments(args);
                }
            } else {
                // Setup arguments
                Bundle bundle = new Bundle();
                // Setup dialogTitle
                dialogCaldroidFragment.setArguments(bundle);
            }
            dialogCaldroidFragment.show(getSupportFragmentManager(), dialogTag);
        }
    });
}
Also used : CaldroidListener(com.roomorama.caldroid.CaldroidListener) Bundle(android.os.Bundle) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) View(android.view.View) Date(java.util.Date) CaldroidFragment(com.roomorama.caldroid.CaldroidFragment) FragmentTransaction(android.support.v4.app.FragmentTransaction) Button(android.widget.Button) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with CaldroidListener

use of com.roomorama.caldroid.CaldroidListener in project instructure-android by instructure.

the class CalendarListViewFragment method setUpListeners.

private void setUpListeners() {
    final CaldroidListener listener = new CaldroidListener() {

        @Override
        public void onSelectDate(Date date, View view) {
            // New date selected, clear out prior
            mCalendarFragment.clearSelectedDates();
            mRecyclerAdapter.setSelectedDay(DateTime.forInstant(date.getTime(), TimeZone.getDefault()));
            if (currentCalendarView == CalendarView.DAY_VIEW) {
                mCalendarFragment.setSelectedDates(date, date);
            } else if (currentCalendarView == CalendarView.WEEK_VIEW) {
                DateWindow dateWindow = CanvasCalendarUtils.setSelectedWeekWindow(date, mRecyclerAdapter.isStartDayMonday());
                mCalendarFragment.setSelectedDates(dateWindow.getStart(), dateWindow.getEnd());
            }
            mCalendarFragment.refreshView();
            mRecyclerAdapter.refreshListView();
        }

        @Override
        public void onCaldroidViewCreated() {
            super.onCaldroidViewCreated();
            // Removing styling for upper buttons
            Button leftButton = mCalendarFragment.getLeftArrowButton();
            Button rightButton = mCalendarFragment.getRightArrowButton();
            TextView textView = mCalendarFragment.getMonthTitleTextView();
            leftButton.setVisibility(View.GONE);
            rightButton.setVisibility(View.GONE);
            textView.setVisibility(View.GONE);
            // Initialize post view created mCalendarFragment elements
            InfiniteViewPager viewPager = mCalendarFragment.getDateViewPager();
            viewPager.setPageMargin((int) ViewUtils.convertDipsToPixels(32, getContext()));
            if (mRecyclerAdapter.getSelectedDay() == null) {
                mRecyclerAdapter.setSelectedDay(DateTime.today(TimeZone.getDefault()));
                Date today = new Date(mRecyclerAdapter.getSelectedDay().getMilliseconds(TimeZone.getDefault()));
                mCalendarFragment.setSelectedDates(today, today);
            }
            mRecyclerAdapter.setCalendarViewCreated(true);
            applyTheme();
        }

        @Override
        public void onChangeMonth(int month, int year, boolean fromCreation) {
            super.onChangeMonth(month, year, fromCreation);
            if (mRecyclerAdapter != null && mRecyclerAdapter.getSelectedDay() != null && month == mRecyclerAdapter.getSelectedDay().getMonth()) {
                // listener. We don't want to trigger the month change logic if the month is not changing.
                return;
            }
            if (mMonthText != null) {
                if (fromCreation || hasOrientationChanged()) {
                    hidePanda();
                    return;
                }
                // Update Actionbar
                mMonthText.setText(new DateFormatSymbols().getMonths()[month - 1] + " " + year);
            }
            // First time loading the calendar will trigger this, but the API calls have already been made
            if (!mRecyclerAdapter.isTodayPressed() && !fromCreation) {
                // Refresh for month, unless this was triggered by "today" button
                mCalendarFragment.clearSelectedDates();
                DateTime today = DateTime.today(TimeZone.getDefault());
                if (today.getMonth() == month && today.getYear() == year) {
                    mRecyclerAdapter.setSelectedDay(today);
                } else {
                    mRecyclerAdapter.setSelectedDay(new DateTime(year, month, 1, null, null, null, null));
                }
                mRecyclerAdapter.refreshCalendar();
            }
        }
    };
    mCalendarFragment.setCaldroidListener(listener);
}
Also used : DateWindow(com.instructure.candroid.model.DateWindow) InfiniteViewPager(com.antonyt.infiniteviewpager.InfiniteViewPager) CaldroidListener(com.roomorama.caldroid.CaldroidListener) Button(android.widget.Button) TextView(android.widget.TextView) DateFormatSymbols(java.text.DateFormatSymbols) ImageView(android.widget.ImageView) View(android.view.View) PageView(com.instructure.canvasapi2.utils.pageview.PageView) TextView(android.widget.TextView) Date(java.util.Date) DateTime(hirondelle.date4j.DateTime)

Aggregations

View (android.view.View)2 Button (android.widget.Button)2 TextView (android.widget.TextView)2 CaldroidListener (com.roomorama.caldroid.CaldroidListener)2 Date (java.util.Date)2 Bundle (android.os.Bundle)1 FragmentTransaction (android.support.v4.app.FragmentTransaction)1 OnClickListener (android.view.View.OnClickListener)1 ImageView (android.widget.ImageView)1 InfiniteViewPager (com.antonyt.infiniteviewpager.InfiniteViewPager)1 DateWindow (com.instructure.candroid.model.DateWindow)1 PageView (com.instructure.canvasapi2.utils.pageview.PageView)1 CaldroidFragment (com.roomorama.caldroid.CaldroidFragment)1 DateTime (hirondelle.date4j.DateTime)1 DateFormatSymbols (java.text.DateFormatSymbols)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1