implement the month picker

This commit is contained in:
tibbi 2016-09-15 21:09:16 +02:00
parent 23652f8196
commit b22a5f23f6
2 changed files with 56 additions and 50 deletions

View File

@ -5,9 +5,6 @@ import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import com.simplemobiletools.calendar.Config; import com.simplemobiletools.calendar.Config;
import com.simplemobiletools.calendar.Constants; import com.simplemobiletools.calendar.Constants;
@ -43,10 +40,7 @@ public class MainActivity extends SimpleActivity implements MonthFragment.Naviga
ButterKnife.bind(this); ButterKnife.bind(this);
final String today = new DateTime().toString(Formatter.DAYCODE_PATTERN); final String today = new DateTime().toString(Formatter.DAYCODE_PATTERN);
final List<String> codes = getMonths(today); fillViewPager(today);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), codes, this);
mPager.setAdapter(adapter);
mPager.setCurrentItem(codes.size() / 2);
} }
@Override @Override
@ -83,6 +77,13 @@ public class MainActivity extends SimpleActivity implements MonthFragment.Naviga
startActivity(intent); startActivity(intent);
} }
private void fillViewPager(String targetMonth) {
final List<String> codes = getMonths(targetMonth);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), codes, this);
mPager.setAdapter(adapter);
mPager.setCurrentItem(codes.size() / 2);
}
private List<String> getMonths(String code) { private List<String> getMonths(String code) {
final List<String> months = new ArrayList<>(PREFILLED_MONTHS); final List<String> months = new ArrayList<>(PREFILLED_MONTHS);
final DateTime today = Formatter.getDateTimeFromCode(code); final DateTime today = Formatter.getDateTimeFromCode(code);
@ -93,49 +94,6 @@ public class MainActivity extends SimpleActivity implements MonthFragment.Naviga
return months; return months;
} }
/*@OnClick(R.id.top_left_arrow)
public void leftArrowClicked() {
mCalendar.getPrevMonth();
}
@OnClick(R.id.top_right_arrow)
public void rightArrowClicked() {
mCalendar.getNextMonth();
}
@OnClick(R.id.top_text)
public void pickMonth() {
final int theme = mConfig.getIsDarkTheme() ? R.style.MyAlertDialog_Dark : R.style.MyAlertDialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, theme);
final View view = getLayoutInflater().inflate(R.layout.date_picker, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
hideDayPicker(datePicker);
final DateTime dateTime = new DateTime(mCalendar.getTargetDate().toString());
datePicker.init(dateTime.getYear(), dateTime.getMonthOfYear() - 1, 1, null);
alertDialog.setView(view);
alertDialog.setNegativeButton(R.string.cancel, null);
alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final int month = datePicker.getMonth() + 1;
final int year = datePicker.getYear();
mCalendar.updateCalendar(new DateTime().withMonthOfYear(month).withYear(year));
}
});
alertDialog.show();
}*/
private void hideDayPicker(DatePicker datePicker) {
final LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
final LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
final NumberPicker picker1 = (NumberPicker) ll2.getChildAt(0);
final NumberPicker picker2 = (NumberPicker) ll2.getChildAt(1);
final NumberPicker dayPicker = (picker1.getMaxValue() > picker2.getMaxValue()) ? picker1 : picker2;
dayPicker.setVisibility(View.GONE);
}
@Override @Override
public void goLeft() { public void goLeft() {
mPager.setCurrentItem(mPager.getCurrentItem() - 1); mPager.setCurrentItem(mPager.getCurrentItem() - 1);
@ -145,4 +103,9 @@ public class MainActivity extends SimpleActivity implements MonthFragment.Naviga
public void goRight() { public void goRight() {
mPager.setCurrentItem(mPager.getCurrentItem() + 1); mPager.setCurrentItem(mPager.getCurrentItem() + 1);
} }
@Override
public void goToDateTime(DateTime dateTime) {
fillViewPager(Formatter.getDayCodeFromDateTime(dateTime));
}
} }

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.calendar.fragments; package com.simplemobiletools.calendar.fragments;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Color; import android.graphics.Color;
@ -7,10 +8,14 @@ import android.graphics.PorterDuff;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView; import android.widget.TextView;
import com.simplemobiletools.calendar.Calendar; import com.simplemobiletools.calendar.Calendar;
@ -23,6 +28,8 @@ import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.activities.DayActivity; import com.simplemobiletools.calendar.activities.DayActivity;
import com.simplemobiletools.calendar.models.Day; import com.simplemobiletools.calendar.models.Day;
import org.joda.time.DateTime;
import java.util.List; import java.util.List;
import butterknife.BindDimen; import butterknife.BindDimen;
@ -122,6 +129,40 @@ public class MonthFragment extends Fragment implements Calendar {
mListener.goRight(); mListener.goRight();
} }
@OnClick(R.id.top_text)
public void pickMonth() {
final int theme = mConfig.getIsDarkTheme() ? R.style.MyAlertDialog_Dark : R.style.MyAlertDialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(), theme);
final View view = getLayoutInflater(getArguments()).inflate(R.layout.date_picker, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
hideDayPicker(datePicker);
final DateTime dateTime = new DateTime(mCalendar.getTargetDate().toString());
datePicker.init(dateTime.getYear(), dateTime.getMonthOfYear() - 1, 1, null);
alertDialog.setView(view);
alertDialog.setNegativeButton(R.string.cancel, null);
alertDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
final int month = datePicker.getMonth() + 1;
final int year = datePicker.getYear();
if (mListener != null)
mListener.goToDateTime(new DateTime().withMonthOfYear(month).withYear(year));
}
});
alertDialog.show();
}
private void hideDayPicker(DatePicker datePicker) {
final LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
final LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
final NumberPicker picker1 = (NumberPicker) ll2.getChildAt(0);
final NumberPicker picker2 = (NumberPicker) ll2.getChildAt(1);
final NumberPicker dayPicker = (picker1.getMaxValue() > picker2.getMaxValue()) ? picker1 : picker2;
dayPicker.setVisibility(View.GONE);
}
private void setupLabels() { private void setupLabels() {
int letters[] = Utils.getLetterIDs(); int letters[] = Utils.getLetterIDs();
@ -186,5 +227,7 @@ public class MonthFragment extends Fragment implements Calendar {
void goLeft(); void goLeft();
void goRight(); void goRight();
void goToDateTime(DateTime dateTime);
} }
} }