From 835c1cc276b1612b1a64d386c49dfd5b93ae8a9c Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 4 Jul 2016 15:21:37 +0200 Subject: [PATCH] implement adding events --- .../simplemobiletools/calendar/DBHelper.java | 9 +++++- .../com/simplemobiletools/calendar/Utils.java | 6 ++++ .../calendar/activities/DetailsActivity.java | 3 +- .../calendar/activities/EventActivity.java | 31 ++++++++++++++++++- app/src/main/res/layout/activity_event.xml | 3 +- app/src/main/res/values/strings.xml | 3 ++ 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java index e3f4400a3..fd737b4d5 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java +++ b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java @@ -14,6 +14,7 @@ import java.util.List; public class DBHelper extends SQLiteOpenHelper { private static SQLiteDatabase mDb; private static String[] mProjection; + private static DBOperationsListener mCallback; private static final String DB_NAME = "events.db"; private static final int DB_VERSION = 1; @@ -25,7 +26,8 @@ public class DBHelper extends SQLiteOpenHelper { private static final String COL_TITLE = "title"; private static final String COL_DESCRIPTION = "description"; - public static DBHelper newInstance(Context context) { + public static DBHelper newInstance(Context context, DBOperationsListener callback) { + mCallback = callback; return new DBHelper(context); } @@ -58,6 +60,7 @@ public class DBHelper extends SQLiteOpenHelper { values.put(COL_TITLE, event.getTitle()); values.put(COL_DESCRIPTION, event.getDescription()); mDb.insert(TABLE_NAME, null, values); + mCallback.eventInserted(); } public List getEvents(int fromTS, int toTS) { @@ -80,4 +83,8 @@ public class DBHelper extends SQLiteOpenHelper { } return events; } + + public interface DBOperationsListener { + void eventInserted(); + } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/Utils.java b/app/src/main/java/com/simplemobiletools/calendar/Utils.java index b12113d67..168f93079 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Utils.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Utils.java @@ -1,6 +1,8 @@ package com.simplemobiletools.calendar; +import android.content.Context; import android.graphics.Color; +import android.widget.Toast; public class Utils { @@ -11,4 +13,8 @@ public class Utils { final int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } + + public static void showToast(Context context, int resId) { + Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show(); + } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/DetailsActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/DetailsActivity.java index 2a0231d1e..4937323d4 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/DetailsActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/DetailsActivity.java @@ -17,7 +17,6 @@ import butterknife.OnClick; public class DetailsActivity extends AppCompatActivity { @BindView(R.id.details_date) TextView mDateTV; - private static final int ADD_EVENT = 1; private String dayCode; @Override @@ -42,6 +41,6 @@ public class DetailsActivity extends AppCompatActivity { public void fabClicked(View view) { final Intent intent = new Intent(getApplicationContext(), EventActivity.class); intent.putExtra(Constants.DAY_CODE, dayCode); - startActivityForResult(intent, ADD_EVENT); + startActivity(intent); } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java index 12b6d1b32..094acec2a 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java @@ -9,12 +9,16 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; 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.models.Event; import org.joda.time.DateTime; @@ -22,11 +26,13 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; -public class EventActivity extends AppCompatActivity { +public class EventActivity extends AppCompatActivity 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; private DateTime mEventStartDateTime; private DateTime mEventEndDateTime; @@ -72,7 +78,24 @@ public class EventActivity extends AppCompatActivity { } 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 (startTS > endTS) { + Utils.showToast(getApplicationContext(), R.string.end_before_start); + return; + } + + final String description = mDescriptionET.getText().toString().trim(); + final Event event = new Event(0, startTS, endTS, title, description); + DBHelper.newInstance(getApplicationContext(), this).insert(event); } private void updateStartDate() { @@ -161,4 +184,10 @@ public class EventActivity extends AppCompatActivity { updateEndTime(); } } + + @Override + public void eventInserted() { + Utils.showToast(getApplicationContext(), R.string.event_added); + finish(); + } } diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index ccd7b6b4e..59ad050fe 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -23,7 +23,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/event_title_label" - android:inputType="text" + android:inputType="textCapSentences" android:maxLength="30" android:maxLines="1" android:minEms="20" @@ -100,6 +100,7 @@ android:layout_height="wrap_content" android:layout_below="@+id/event_description_label" android:gravity="top" + android:inputType="textCapSentences|textMultiLine" android:minEms="20" android:textSize="@dimen/day_text_size"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 99372f411..b1b31f8cb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,6 +4,9 @@ Event Save + Title cannot be empty + The event cannot end earlier than it starts + Event added successfully Details