Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle & select date implementation #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.wefika.calendar;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build.VERSION_CODES;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class CollapseCalendarView extends LinearLayout implements View.OnClickLi
@NonNull private ImageButton mNext;
@NonNull private LinearLayout mWeeksView;

@NonNull private final LayoutInflater mInflater;
@NonNull private LayoutInflater mInflater;
@NonNull private final RecycleBin mRecycleBin = new RecycleBin();

@Nullable private OnDateSelect mListener;
Expand All @@ -67,27 +68,27 @@ public CollapseCalendarView(Context context, AttributeSet attrs) {
public CollapseCalendarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

mInflater = LayoutInflater.from(context);
construct(context);

mResizeManager = new ResizeManager(this);
}

inflate(context, R.layout.calendar_layout, this);
@TargetApi(VERSION_CODES.LOLLIPOP)
public CollapseCalendarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);

construct(context);

setOrientation(VERTICAL);
}

public void init(@NonNull CalendarManager manager) {
if (manager != null) {

mManager = manager;

populateLayout();
mManager = manager;

if (mListener != null) {
mListener.onDateSelected(mManager.getSelectedDay());
}
populateLayout();

if (mListener != null) {
mListener.onDateSelected(mManager.getSelectedDay());
}

}

@Nullable
Expand All @@ -97,24 +98,35 @@ public CalendarManager getManager() {

@Override
public void onClick(View v) {
Log.d(TAG, "On click");
if (mManager != null) {
int id = v.getId();
if (id == R.id.prev) {
if (mManager.prev()) {
populateLayout();
}
} else if (id == R.id.next) {
Log.d(TAG, "next");
if (mManager.next()) {
Log.d(TAG, "populate");
populateLayout();
}
}

}
}

public void selectDate(@NonNull LocalDate date) {

boolean period = mManager.selectPeriod(date);
boolean day = mManager.selectDay(date);

if (period || day) {
populateLayout();
}

if (day && mListener != null) {
mListener.onDateSelected(date);
}
}

@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
mResizeManager.onDraw();
Expand Down Expand Up @@ -149,6 +161,10 @@ public void setTitle(@Nullable String text) {
}
}

public void toggle() {
mResizeManager.toggle();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mResizeManager.onInterceptTouchEvent(ev);
Expand Down Expand Up @@ -179,6 +195,18 @@ protected void onFinishInflate() {
populateLayout();
}

private void construct(@NonNull Context context) {

mInflater = LayoutInflater.from(context);

mResizeManager = new ResizeManager(this);

inflate(context, R.layout.calendar_layout, this);

setOrientation(VERTICAL);

}

private void populateDays() {

if (!initialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,20 @@ public CalendarManager(@NonNull LocalDate selected, @NonNull State state, @Nulla
init(selected, minDate, maxDate);
}

public boolean selectPeriod(@NonNull LocalDate date) {

if (!mUnit.isIn(date) && mUnit.setPeriod(date)) {
mUnit.select(mSelected);
setActiveMonth(mUnit.getFrom());
return true;
} else {
return false;
}

}

public boolean selectDay(@NonNull LocalDate date) {
if (!mSelected.isEqual(date)) {
if (!mSelected.isEqual(date) && mUnit.hasDate(date)) {
mUnit.deselect(mSelected);
mSelected = date;
mUnit.select(mSelected);
Expand Down Expand Up @@ -74,22 +86,27 @@ public boolean hasPrev() {

public boolean next() {

boolean next = mUnit.next();
mUnit.select(mSelected);

setActiveMonth(mUnit.getFrom());
if (mUnit.next()) {
mUnit.select(mSelected);

return next;
setActiveMonth(mUnit.getFrom());
return true;
} else {
return false;
}
}

public boolean prev() {

boolean prev = mUnit.prev();
mUnit.select(mSelected);
if (mUnit.prev()) {
mUnit.select(mSelected);

setActiveMonth(mUnit.getTo());
setActiveMonth(mUnit.getTo());
return true;
} else {
return false;
}

return prev;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public boolean isSelected() {
return mSelected;
}

public abstract boolean hasDate(@NonNull LocalDate date);

public abstract boolean hasNext();

public abstract boolean hasPrev();

public abstract boolean setPeriod(@NonNull LocalDate date);

public abstract boolean next();

public abstract boolean prev();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ public boolean hasPrev() {
}
}

@Override public boolean setPeriod(@NonNull LocalDate date) {

if (hasDate(date)) {
setFrom(date.withDayOfMonth(1));
setTo(getFrom().withDayOfMonth(getFrom().dayOfMonth().getMaximumValue()));

build();
return true;
} else {
return false;
}
}

@Override
public boolean next() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class ProgressManager {
protected SizeViewHolder mCalendarHolder;
protected SizeViewHolder mWeeksHolder;

private OnInitListener listener;

final int mActiveIndex;

private boolean mInitialized = false;
Expand Down Expand Up @@ -57,12 +59,20 @@ public void apply(float progress) {

}

public void setListener(OnInitListener listener) {
this.listener = listener;
}

public boolean isInitialized() {
return mInitialized;
}

void setInitialized(boolean initialized) {
mInitialized = initialized;

if (listener != null && initialized) {
listener.onInit();
}
}

public int getCurrentHeight() {
Expand Down Expand Up @@ -97,4 +107,8 @@ private int getDeltaInBounds(float delta) {

}

interface OnInitListener {
void onInit();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public boolean onPreDraw() {
initializeChildren();

setInitialized(true);
apply(0);

return false;
}
Expand Down Expand Up @@ -110,6 +111,7 @@ public boolean onPreDraw() {
mWeeksView.getLayoutParams().height = mCalendarHolder.getMaxHeight();

setInitialized(true);
apply(1);

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ public LocalDate getMaxDate() {
return mMaxDate;
}

@Override public boolean hasDate(@NonNull LocalDate date) {

boolean min = true;
boolean max = true;

LocalDate maxDate = getMaxDate();
if (maxDate != null) {
max = !date.isAfter(maxDate);
}

LocalDate minDate = getMinDate();
if (minDate != null) {
min = !date.isBefore(minDate);
}

return min && max;

}

/**
*
*
Expand Down
Loading