rewrite EventActivity to kotlin

This commit is contained in:
tibbi 2016-09-19 23:07:41 +02:00
parent 3119013ec4
commit a8cffc36b1
2 changed files with 336 additions and 396 deletions

View File

@ -1,396 +0,0 @@
package com.simplemobiletools.calendar.activities;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.DBHelper;
import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.fragments.DayFragment;
import com.simplemobiletools.calendar.models.Event;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnItemSelected;
public class EventActivity extends SimpleActivity implements DBHelper.DBOperationsListener {
@BindView(R.id.event_start_date) TextView mStartDate;
@BindView(R.id.event_start_time) TextView mStartTime;
@BindView(R.id.event_end_date) TextView mEndDate;
@BindView(R.id.event_end_time) TextView mEndTime;
@BindView(R.id.event_title) EditText mTitleET;
@BindView(R.id.event_description) EditText mDescriptionET;
@BindView(R.id.event_reminder_other) EditText mReminderOtherET;
@BindView(R.id.event_reminder) AppCompatSpinner mReminder;
@BindView(R.id.event_repetition) AppCompatSpinner mRepetition;
@BindView(R.id.event_end_checkbox) CheckBox mEndCheckbox;
private static DateTime mEventStartDateTime;
private static DateTime mEventEndDateTime;
private static Event mEvent;
private static boolean mWasReminderInit;
private static boolean mWasEndDateSet;
private static boolean mWasEndTimeSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
ButterKnife.bind(this);
final Intent intent = getIntent();
if (intent == null)
return;
mWasReminderInit = false;
final Event event = (Event) intent.getSerializableExtra(Constants.EVENT);
if (event != null) {
mEvent = event;
setupEditEvent();
} else {
mEvent = new Event();
final String dayCode = intent.getStringExtra(Constants.DAY_CODE);
if (dayCode == null || dayCode.isEmpty())
return;
setupNewEvent(dayCode);
}
updateStartDate();
updateStartTime();
updateEndDate();
updateEndTime();
setupReminder();
setupRepetition();
setupEndCheckbox();
mWasEndDateSet = (event != null);
mWasEndTimeSet = (event != null);
}
private void setupEditEvent() {
setTitle(getResources().getString(R.string.edit_event));
mEventStartDateTime = Formatter.getDateTimeFromTS(mEvent.getStartTS());
mEventEndDateTime = Formatter.getDateTimeFromTS(mEvent.getEndTS());
mEndCheckbox.setChecked(!mEventStartDateTime.equals(mEventEndDateTime));
mTitleET.setText(mEvent.getTitle());
mDescriptionET.setText(mEvent.getDescription());
hideKeyboard();
}
private void setupNewEvent(String dayCode) {
setTitle(getResources().getString(R.string.new_event));
mEventStartDateTime = Formatter.getDateTimeFromCode(dayCode).withZoneRetainFields(DateTimeZone.getDefault()).withHourOfDay(13);
mEventEndDateTime = mEventStartDateTime;
}
private void hideKeyboard() {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
private void showKeyboard(EditText et) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}
private void setupReminder() {
switch (mEvent.getReminderMinutes()) {
case -1:
mReminder.setSelection(0);
break;
case 0:
mReminder.setSelection(1);
break;
default:
mReminder.setSelection(2);
mReminderOtherET.setVisibility(View.VISIBLE);
mReminderOtherET.setText(String.valueOf(mEvent.getReminderMinutes()));
break;
}
}
private void setupRepetition() {
switch (mEvent.getRepeatInterval()) {
case Constants.DAY:
mRepetition.setSelection(1);
break;
case Constants.WEEK:
mRepetition.setSelection(2);
break;
case Constants.MONTH:
mRepetition.setSelection(3);
break;
case Constants.YEAR:
mRepetition.setSelection(4);
break;
default:
mRepetition.setSelection(0);
break;
}
}
private void setupEndCheckbox() {
mEndCheckbox.setTextColor(mStartDate.getCurrentTextColor());
}
@OnCheckedChanged(R.id.event_end_checkbox)
public void checkChanged(boolean isChecked) {
mEndDate.setVisibility(isChecked ? View.VISIBLE : View.GONE);
mEndTime.setVisibility(isChecked ? View.VISIBLE : View.GONE);
}
@OnItemSelected(R.id.event_reminder)
public void handleReminder() {
if (!mWasReminderInit) {
mWasReminderInit = true;
return;
}
if (mReminder.getSelectedItemPosition() == mReminder.getCount() - 1) {
mReminderOtherET.setVisibility(View.VISIBLE);
mReminderOtherET.requestFocus();
showKeyboard(mReminderOtherET);
} else {
mReminderOtherET.setVisibility(View.GONE);
hideKeyboard();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_event, menu);
final MenuItem item = menu.findItem(R.id.delete);
if (mEvent.getId() == 0) {
item.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
deleteEvent();
return true;
case R.id.save:
saveEvent();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void deleteEvent() {
final Intent intent = new Intent();
intent.putExtra(DayFragment.Companion.getDELETED_ID(), mEvent.getId());
setResult(RESULT_OK, intent);
finish();
}
private void saveEvent() {
final String title = mTitleET.getText().toString().trim();
if (title.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.title_empty);
mTitleET.requestFocus();
return;
}
final int startTS = (int) (mEventStartDateTime.getMillis() / 1000);
final int endTS = (int) (mEventEndDateTime.getMillis() / 1000);
if (mEndCheckbox.isChecked() && startTS > endTS) {
Utils.showToast(getApplicationContext(), R.string.end_before_start);
return;
}
final DBHelper dbHelper = new DBHelper(getApplicationContext(), this);
final String description = mDescriptionET.getText().toString().trim();
mEvent.setStartTS(startTS);
mEvent.setEndTS(mEndCheckbox.isChecked() ? endTS : startTS);
mEvent.setTitle(title);
mEvent.setDescription(description);
mEvent.setReminderMinutes(getReminderMinutes());
mEvent.setRepeatInterval(getRepeatInterval());
if (mEvent.getId() == 0) {
dbHelper.insert(mEvent);
} else {
dbHelper.update(mEvent);
}
}
private int getReminderMinutes() {
switch (mReminder.getSelectedItemPosition()) {
case 0:
return -1;
case 1:
return 0;
default:
final String value = mReminderOtherET.getText().toString().trim();
if (value.isEmpty())
return 0;
return Integer.valueOf(value);
}
}
private int getRepeatInterval() {
switch (mRepetition.getSelectedItemPosition()) {
case 1:
return Constants.DAY;
case 2:
return Constants.WEEK;
case 3:
return Constants.MONTH;
case 4:
return Constants.YEAR;
default:
return 0;
}
}
private void updateStartDate() {
mStartDate.setText(Formatter.getEventDate(getApplicationContext(), mEventStartDateTime));
}
private void updateStartTime() {
mStartTime.setText(Formatter.getEventTime(mEventStartDateTime));
}
private void updateEndDate() {
mEndDate.setText(Formatter.getEventDate(getApplicationContext(), mEventEndDateTime));
}
private void updateEndTime() {
mEndTime.setText(Formatter.getEventTime(mEventEndDateTime));
}
@OnClick(R.id.event_start_date)
public void startDateClicked(View view) {
new DatePickerDialog(this, startDateSetListener, mEventStartDateTime.getYear(), mEventStartDateTime.getMonthOfYear() - 1,
mEventStartDateTime.getDayOfMonth()).show();
}
@OnClick(R.id.event_start_time)
public void startTimeClicked(View view) {
new TimePickerDialog(this, startTimeSetListener, mEventStartDateTime.getHourOfDay(), mEventStartDateTime.getMinuteOfHour(), true)
.show();
}
@OnClick(R.id.event_end_date)
public void endDateClicked(View view) {
new DatePickerDialog(this, endDateSetListener, mEventEndDateTime.getYear(), mEventEndDateTime.getMonthOfYear() - 1,
mEventEndDateTime.getDayOfMonth()).show();
}
@OnClick(R.id.event_end_time)
public void endTimeClicked(View view) {
new TimePickerDialog(this, endTimeSetListener, mEventEndDateTime.getHourOfDay(), mEventEndDateTime.getMinuteOfHour(), true).show();
}
private final DatePickerDialog.OnDateSetListener startDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateSet(year, monthOfYear, dayOfMonth, true);
if (!mWasEndDateSet) {
dateSet(year, monthOfYear, dayOfMonth, false);
}
}
};
private TimePickerDialog.OnTimeSetListener startTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeSet(hourOfDay, minute, true);
if (!mWasEndTimeSet) {
timeSet(hourOfDay, minute, false);
}
}
};
private DatePickerDialog.OnDateSetListener endDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateSet(year, monthOfYear, dayOfMonth, false);
}
};
private TimePickerDialog.OnTimeSetListener endTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeSet(hourOfDay, minute, false);
}
};
private void dateSet(int year, int month, int day, boolean isStart) {
if (isStart) {
mEventStartDateTime = mEventStartDateTime.withYear(year).withMonthOfYear(month + 1).withDayOfMonth(day);
updateStartDate();
} else {
mEventEndDateTime = mEventEndDateTime.withYear(year).withMonthOfYear(month + 1).withDayOfMonth(day);
updateEndDate();
mWasEndDateSet = true;
}
}
private void timeSet(int hours, int minutes, boolean isStart) {
if (isStart) {
mEventStartDateTime = mEventStartDateTime.withHourOfDay(hours).withMinuteOfHour(minutes);
updateStartTime();
} else {
mEventEndDateTime = mEventEndDateTime.withHourOfDay(hours).withMinuteOfHour(minutes);
updateEndTime();
mWasEndTimeSet = true;
}
}
@Override
public void eventInserted(Event event) {
if (DateTime.now().isAfter(mEventStartDateTime.getMillis())) {
Utils.showToast(getApplicationContext(), R.string.past_event_added);
} else {
Utils.showToast(getApplicationContext(), R.string.event_added);
}
Utils.scheduleNotification(getApplicationContext(), event);
finish();
}
@Override
public void eventUpdated(Event event) {
Utils.scheduleNotification(getApplicationContext(), event);
Utils.showToast(getApplicationContext(), R.string.event_updated);
finish();
}
@Override
public void eventsDeleted(int cnt) {
}
@Override
public void gotEvents(List<Event> events) {
}
}

View File

@ -0,0 +1,336 @@
package com.simplemobiletools.calendar.activities
import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.AdapterView
import android.widget.EditText
import com.simplemobiletools.calendar.*
import com.simplemobiletools.calendar.fragments.DayFragment
import com.simplemobiletools.calendar.models.Event
import kotlinx.android.synthetic.main.activity_event.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
class EventActivity : SimpleActivity(), DBHelper.DBOperationsListener {
private var mWasReminderInit: Boolean = false
private var mWasEndDateSet: Boolean = false
private var mWasEndTimeSet: Boolean = false
lateinit var mEventStartDateTime: DateTime
lateinit var mEventEndDateTime: DateTime
lateinit var mEvent: Event
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_event)
val intent = intent ?: return
mWasReminderInit = false
val event = intent.getSerializableExtra(Constants.EVENT) as Event?
if (event != null) {
mEvent = event
setupEditEvent()
} else {
mEvent = Event()
val dayCode = intent.getStringExtra(Constants.DAY_CODE)
if (dayCode == null || dayCode.isEmpty())
return
setupNewEvent(dayCode)
}
updateStartDate()
updateStartTime()
updateEndDate()
updateEndTime()
setupReminder()
setupRepetition()
setupEndCheckbox()
mWasEndDateSet = event != null
mWasEndTimeSet = event != null
event_start_date.setOnClickListener { setupStartDate() }
event_start_time.setOnClickListener { setupStartTime() }
event_end_date.setOnClickListener { setupEndDate() }
event_end_time.setOnClickListener { setupEndTime() }
event_end_checkbox.setOnCheckedChangeListener { compoundButton, isChecked -> endCheckboxChecked(isChecked) }
event_reminder.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) {
}
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
reminderItemSelected()
}
}
}
private fun setupEditEvent() {
title = resources.getString(R.string.edit_event)
mEventStartDateTime = Formatter.getDateTimeFromTS(mEvent.startTS)
mEventEndDateTime = Formatter.getDateTimeFromTS(mEvent.endTS)
event_end_checkbox.isChecked = mEventStartDateTime != mEventEndDateTime
event_title.setText(mEvent.title)
event_description.setText(mEvent.description)
hideKeyboard()
}
private fun setupNewEvent(dayCode: String) {
title = resources.getString(R.string.new_event)
mEventStartDateTime = Formatter.getDateTimeFromCode(dayCode).withZoneRetainFields(DateTimeZone.getDefault()).withHourOfDay(13)
mEventEndDateTime = mEventStartDateTime
}
private fun hideKeyboard() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
private fun showKeyboard(et: EditText) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT)
}
private fun setupReminder() {
when (mEvent.reminderMinutes) {
-1 -> event_reminder.setSelection(0)
0 -> event_reminder.setSelection(1)
else -> {
event_reminder.setSelection(2)
event_reminder_other.visibility = View.VISIBLE
event_reminder_other.setText(mEvent.reminderMinutes.toString())
}
}
}
private fun setupRepetition() {
when (mEvent.repeatInterval) {
Constants.DAY -> event_repetition.setSelection(1)
Constants.WEEK -> event_repetition.setSelection(2)
Constants.MONTH -> event_repetition.setSelection(3)
Constants.YEAR -> event_repetition.setSelection(4)
else -> event_repetition.setSelection(0)
}
}
private fun setupEndCheckbox() {
event_end_checkbox.setTextColor(event_start_date.currentTextColor)
}
fun endCheckboxChecked(isChecked: Boolean) {
event_end_date.visibility = if (isChecked) View.VISIBLE else View.GONE
event_end_time.visibility = if (isChecked) View.VISIBLE else View.GONE
}
fun reminderItemSelected() {
if (!mWasReminderInit) {
mWasReminderInit = true
return
}
if (event_reminder.selectedItemPosition == event_reminder.count - 1) {
event_reminder_other.visibility = View.VISIBLE
event_reminder_other.requestFocus()
showKeyboard(event_reminder_other)
} else {
event_reminder_other.visibility = View.GONE
hideKeyboard()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_event, menu)
val item = menu.findItem(R.id.delete)
if (mEvent.id == 0) {
item.isVisible = false
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.delete -> {
deleteEvent()
return true
}
R.id.save -> {
saveEvent()
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
private fun deleteEvent() {
val intent = Intent()
intent.putExtra(DayFragment.DELETED_ID, mEvent.id)
setResult(Activity.RESULT_OK, intent)
finish()
}
private fun saveEvent() {
val title = event_title.text.toString().trim { it <= ' ' }
if (title.isEmpty()) {
Utils.showToast(applicationContext, R.string.title_empty)
event_title.requestFocus()
return
}
val startTS = (mEventStartDateTime.millis / 1000).toInt()
val endTS = (mEventEndDateTime.millis / 1000).toInt()
if (event_end_checkbox.isChecked && startTS > endTS) {
Utils.showToast(applicationContext, R.string.end_before_start)
return
}
val dbHelper = DBHelper(applicationContext, this)
val description = event_description.text.toString().trim { it <= ' ' }
mEvent.startTS = startTS
mEvent.endTS = if (event_end_checkbox.isChecked) endTS else startTS
mEvent.title = title
mEvent.description = description
mEvent.reminderMinutes = reminderMinutes
mEvent.repeatInterval = repeatInterval
if (mEvent.id == 0) {
dbHelper.insert(mEvent)
} else {
dbHelper.update(mEvent)
}
}
private val reminderMinutes: Int
get() {
when (event_reminder.selectedItemPosition) {
0 -> return -1
1 -> return 0
else -> {
val value = event_reminder_other.text.toString().trim { it <= ' ' }
if (value.isEmpty())
return 0
return Integer.valueOf(value)!!
}
}
}
private val repeatInterval: Int
get() {
when (event_repetition.selectedItemPosition) {
1 -> return Constants.DAY
2 -> return Constants.WEEK
3 -> return Constants.MONTH
4 -> return Constants.YEAR
else -> return 0
}
}
private fun updateStartDate() {
event_start_date.text = Formatter.getEventDate(applicationContext, mEventStartDateTime)
}
private fun updateStartTime() {
event_start_time.text = Formatter.getEventTime(mEventStartDateTime)
}
private fun updateEndDate() {
event_end_date.text = Formatter.getEventDate(applicationContext, mEventEndDateTime)
}
private fun updateEndTime() {
event_end_time.text = Formatter.getEventTime(mEventEndDateTime)
}
fun setupStartDate() {
DatePickerDialog(this, startDateSetListener, mEventStartDateTime.year, mEventStartDateTime.monthOfYear - 1,
mEventStartDateTime.dayOfMonth).show()
}
fun setupStartTime() {
TimePickerDialog(this, startTimeSetListener, mEventStartDateTime.hourOfDay, mEventStartDateTime.minuteOfHour, true).show()
}
fun setupEndDate() {
DatePickerDialog(this, endDateSetListener, mEventEndDateTime.year, mEventEndDateTime.monthOfYear - 1,
mEventEndDateTime.dayOfMonth).show()
}
fun setupEndTime() {
TimePickerDialog(this, endTimeSetListener, mEventEndDateTime.hourOfDay, mEventEndDateTime.minuteOfHour, true).show()
}
private val startDateSetListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
dateSet(year, monthOfYear, dayOfMonth, true)
if (!mWasEndDateSet) {
dateSet(year, monthOfYear, dayOfMonth, false)
}
}
private val startTimeSetListener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
timeSet(hourOfDay, minute, true)
if (!mWasEndTimeSet) {
timeSet(hourOfDay, minute, false)
}
}
private val endDateSetListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> dateSet(year, monthOfYear, dayOfMonth, false) }
private val endTimeSetListener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute -> timeSet(hourOfDay, minute, false) }
private fun dateSet(year: Int, month: Int, day: Int, isStart: Boolean) {
if (isStart) {
mEventStartDateTime = mEventStartDateTime.withYear(year).withMonthOfYear(month + 1).withDayOfMonth(day)
updateStartDate()
} else {
mEventEndDateTime = mEventEndDateTime.withYear(year).withMonthOfYear(month + 1).withDayOfMonth(day)
updateEndDate()
mWasEndDateSet = true
}
}
private fun timeSet(hours: Int, minutes: Int, isStart: Boolean) {
if (isStart) {
mEventStartDateTime = mEventStartDateTime.withHourOfDay(hours).withMinuteOfHour(minutes)
updateStartTime()
} else {
mEventEndDateTime = mEventEndDateTime.withHourOfDay(hours).withMinuteOfHour(minutes)
updateEndTime()
mWasEndTimeSet = true
}
}
override fun eventInserted(event: Event) {
if (DateTime.now().isAfter(mEventStartDateTime.millis)) {
Utils.showToast(applicationContext, R.string.past_event_added)
} else {
Utils.showToast(applicationContext, R.string.event_added)
}
Utils.scheduleNotification(applicationContext, event)
finish()
}
override fun eventUpdated(event: Event) {
Utils.scheduleNotification(applicationContext, event)
Utils.showToast(applicationContext, R.string.event_updated)
finish()
}
override fun eventsDeleted(cnt: Int) {
}
override fun gotEvents(events: MutableList<Event>) {
}
}