Merge branch 'bugs/calendar-fix'

# Conflicts:
#	src/main/java/ru/touchin/roboswag/components/navigation/fragments/ViewControllerFragment.java
#	src/main/java/ru/touchin/roboswag/components/storables/JsonPreferenceStorable.java
#	src/main/java/ru/touchin/roboswag/components/storables/PreferenceStore.java
This commit is contained in:
Gavriil Sitnikov 2016-06-09 16:08:34 +03:00
commit 850fe4540e
5 changed files with 79 additions and 15 deletions

View File

@ -77,13 +77,16 @@ public abstract class CalendarAdapter<TDayViewHolder extends RecyclerView.ViewHo
}
@Nullable
public Integer getPositionToScroll(final boolean beginningOfSelection) {
if (beginningOfSelection && startSelectionPosition != null) {
public Integer getPositionToScroll(final boolean isDeparture) {
if (isDeparture && startSelectionPosition != null) {
return CalendarUtils.findPositionOfSelectedMonth(calendarItems, startSelectionPosition);
}
if (!beginningOfSelection && endSelectionPosition != null) {
if (!isDeparture && endSelectionPosition != null) {
return CalendarUtils.findPositionOfSelectedMonth(calendarItems, endSelectionPosition);
}
if (!isDeparture && startSelectionPosition != null) {
return CalendarUtils.findPositionOfSelectedMonth(calendarItems, startSelectionPosition);
}
return null;
}

View File

@ -219,16 +219,37 @@ public final class CalendarUtils {
// add range with days before today
calendarItems.add(new CalendarDayItem(calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1,
1, shift, shift + firstDate - 1, CalendarDateState.BEFORE_TODAY));
shift += firstDate - 1;
shift += firstDate;
// add today item
calendar.setTime(cleanStartDate.getTime());
calendarItems.add(new CalendarDayItem(calendar.getTimeInMillis() / CalendarAdapter.ONE_DAY_LENGTH + 1,
firstDate + 1, shift + 1, shift + 1, CalendarDateState.TODAY));
firstDate + 1, shift, shift, CalendarDateState.TODAY));
shift += 1;
//add empty items and header if current day the last day in the month
if (calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
addItemsIfCurrentDayTheLastDayInTheMonth(calendar, calendarItems, shift);
}
return calendarItems;
}
private static void addItemsIfCurrentDayTheLastDayInTheMonth(@NonNull final Calendar calendar,
@NonNull final List<CalendarItem> calendarItems,
final int initialShift) {
int shift = initialShift;
final Calendar newMonthCalendar = Calendar.getInstance();
newMonthCalendar.setTime(calendar.getTime());
newMonthCalendar.add(Calendar.DAY_OF_MONTH, 1);
final int firstFayInNextMonth = getFirstDateStart(newMonthCalendar);
calendarItems.add(new CalendarEmptyItem(shift, shift + (7 - firstFayInNextMonth) - 1));
shift += 7 - firstFayInNextMonth;
calendarItems.add(new CalendarHeaderItem(calendar.get(Calendar.MONTH) + 1, shift, shift));
shift += 1;
calendarItems.add(new CalendarEmptyItem(shift, shift + firstFayInNextMonth - 1));
}
private static int getFirstDateStart(@NonNull final Calendar calendar) {
int firstDateStart = calendar.get(Calendar.DAY_OF_WEEK) - 2;
if (firstDateStart == -1) {
@ -249,4 +270,4 @@ public final class CalendarUtils {
private CalendarUtils() {
}
}
}

View File

@ -32,6 +32,7 @@ import ru.touchin.roboswag.core.log.Lc;
import ru.touchin.roboswag.core.utils.android.RxAndroidUtils;
import rx.Observable;
import rx.Scheduler;
import rx.subjects.BehaviorSubject;
/**
* Created by Ilia Kurtov on 12.05.2016.
@ -48,13 +49,25 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
private final SparseArray<Observable<List<T>>> loadingPages = new SparseArray<>();
@Nullable
private Integer maxLoadedPage;
private boolean isLastPageLoaded;
private final BehaviorSubject<Boolean> isLastPageLoadedSubject = BehaviorSubject.create(false);
public AbstractSimplePagingProvider(final int pageSize) {
super();
this.pageSize = pageSize;
}
public void updateLoadedPages(@NonNull final List<T> startedDataList) {
for (int page = 0; page < startedDataList.size() / pageSize; page++) {
final List<T> pageList = new ArrayList<>();
for (int i = pageSize * page; i < pageSize * (page + 1); i++) {
pageList.add(startedDataList.get(i));
}
loadedPages.put(page, pageList);
}
maxLoadedPage = startedDataList.size() / pageSize - 1;
}
public int getPageSize() {
return pageSize;
}
@ -63,7 +76,7 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
public int getSize() {
synchronized (lock) {
return (maxLoadedPage != null ? maxLoadedPage * pageSize + loadedPages.get(maxLoadedPage).size() : 0)
+ (isLastPageLoaded ? 0 : 1);
+ (isLastPageLoadedSubject.getValue() ? 0 : 1);
}
}
@ -109,7 +122,7 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
subscriber.onNext(page != null && page.size() > indexOnPage ? page.get(indexOnPage) : null);
subscriber.onCompleted();
}).switchMap(item -> {
if (item != null || (isLastPageLoaded && maxLoadedPage != null && maxLoadedPage <= indexOfPage)) {
if (item != null || (isLastPageLoadedSubject.getValue() && maxLoadedPage != null && maxLoadedPage <= indexOfPage)) {
return Observable.just(item);
}
Observable<List<T>> loadingPage = loadingPages.get(indexOfPage);
@ -142,19 +155,18 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
.doOnNext(pageItems -> {
synchronized (lock) {
final int oldSize = getSize();
final boolean oldIsLastPageLoaded = isLastPageLoaded;
final boolean oldIsLastPageLoaded = isLastPageLoadedSubject.getValue();
if (pageItems.size() < pageSize) {
if (maxLoadedPage != null && maxLoadedPage > indexOfPage) {
maxLoadedPage = indexOfPage == 0 || !pageItems.isEmpty() ? indexOfPage : null;
downgradeMaxLoadedPages(indexOfPage);
isLastPageLoaded = false;
isLastPageLoadedSubject.onNext(false);
}
if (shouldReplaceMaxLoaded(pageItems, indexOfPage)) {
maxLoadedPage = indexOfPage;
}
isLastPageLoaded = isLastPageLoaded
|| (maxLoadedPage != null
&& (maxLoadedPage == indexOfPage || maxLoadedPage == indexOfPage - 1));
isLastPageLoadedSubject.onNext(isLastPageLoadedSubject.getValue()
|| (maxLoadedPage != null && (maxLoadedPage == indexOfPage || maxLoadedPage == indexOfPage - 1)));
} else if (shouldReplaceMaxLoaded(pageItems, indexOfPage)) {
maxLoadedPage = indexOfPage;
}
@ -198,7 +210,7 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
changes.add(new Change(Change.Type.INSERTED, oldSize, size - oldSize));
} else {
changes.add(new Change(Change.Type.REMOVED, size, oldSize - size));
if (!isLastPageLoaded) {
if (!isLastPageLoadedSubject.getValue()) {
changes.add(new Change(Change.Type.CHANGED, size - 1, 1));
}
}
@ -217,4 +229,9 @@ public abstract class AbstractSimplePagingProvider<T> extends ItemsProvider<T> {
}
}
@NonNull
public Observable<Boolean> observeListLoadingFinished() {
return isLastPageLoadedSubject.distinctUntilChanged();
}
}

View File

@ -153,6 +153,10 @@ public class ViewController<TActivity extends ViewControllerActivity<?>,
isStartedSubject.onNext(true);
}
public void onAppear(@NonNull final ViewControllerFragment.AppearType appearType) {
//do nothing
}
public void onSaveInstanceState(@NonNull final Bundle savedInstanceState) {
// do nothing
}

View File

@ -178,7 +178,16 @@ public abstract class ViewControllerFragment<TState extends AbstractState, TActi
}
}
@Override
public void setMenuVisibility(final boolean menuVisible) {
if (menuVisible && !isMenuVisible() && viewController != null) {
viewController.onAppear(AppearType.ACTIVATED);
}
super.setMenuVisibility(menuVisible);
}
protected void onConfigureNavigation(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (viewController != null) {
viewController.onConfigureNavigation(menu, inflater);
}
@ -204,6 +213,9 @@ public abstract class ViewControllerFragment<TState extends AbstractState, TActi
if (isStarted) {
this.viewController.onStart();
}
if (isMenuVisible()) {
viewController.onAppear(AppearType.STARTED);
}
this.viewController.getActivity().supportInvalidateOptionsMenu();
}
}
@ -256,4 +268,11 @@ public abstract class ViewControllerFragment<TState extends AbstractState, TActi
}
public enum AppearType {
// fragment just started first time
STARTED,
// fragment activated (started and menu visibility is true)
ACTIVATED
}
}