implement adding events

This commit is contained in:
tibbi 2016-07-04 15:21:37 +02:00
parent 6098b54712
commit 835c1cc276
6 changed files with 50 additions and 5 deletions

View File

@ -14,6 +14,7 @@ import java.util.List;
public class DBHelper extends SQLiteOpenHelper { public class DBHelper extends SQLiteOpenHelper {
private static SQLiteDatabase mDb; private static SQLiteDatabase mDb;
private static String[] mProjection; private static String[] mProjection;
private static DBOperationsListener mCallback;
private static final String DB_NAME = "events.db"; private static final String DB_NAME = "events.db";
private static final int DB_VERSION = 1; 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_TITLE = "title";
private static final String COL_DESCRIPTION = "description"; 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); return new DBHelper(context);
} }
@ -58,6 +60,7 @@ public class DBHelper extends SQLiteOpenHelper {
values.put(COL_TITLE, event.getTitle()); values.put(COL_TITLE, event.getTitle());
values.put(COL_DESCRIPTION, event.getDescription()); values.put(COL_DESCRIPTION, event.getDescription());
mDb.insert(TABLE_NAME, null, values); mDb.insert(TABLE_NAME, null, values);
mCallback.eventInserted();
} }
public List<Event> getEvents(int fromTS, int toTS) { public List<Event> getEvents(int fromTS, int toTS) {
@ -80,4 +83,8 @@ public class DBHelper extends SQLiteOpenHelper {
} }
return events; return events;
} }
public interface DBOperationsListener {
void eventInserted();
}
} }

View File

@ -1,6 +1,8 @@
package com.simplemobiletools.calendar; package com.simplemobiletools.calendar;
import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.widget.Toast;
public class Utils { public class Utils {
@ -11,4 +13,8 @@ public class Utils {
final int blue = Color.blue(color); final int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue); 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();
}
} }

View File

@ -17,7 +17,6 @@ import butterknife.OnClick;
public class DetailsActivity extends AppCompatActivity { public class DetailsActivity extends AppCompatActivity {
@BindView(R.id.details_date) TextView mDateTV; @BindView(R.id.details_date) TextView mDateTV;
private static final int ADD_EVENT = 1;
private String dayCode; private String dayCode;
@Override @Override
@ -42,6 +41,6 @@ public class DetailsActivity extends AppCompatActivity {
public void fabClicked(View view) { public void fabClicked(View view) {
final Intent intent = new Intent(getApplicationContext(), EventActivity.class); final Intent intent = new Intent(getApplicationContext(), EventActivity.class);
intent.putExtra(Constants.DAY_CODE, dayCode); intent.putExtra(Constants.DAY_CODE, dayCode);
startActivityForResult(intent, ADD_EVENT); startActivity(intent);
} }
} }

View File

@ -9,12 +9,16 @@ 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.DatePicker;
import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TimePicker; import android.widget.TimePicker;
import com.simplemobiletools.calendar.Constants; import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.DBHelper;
import com.simplemobiletools.calendar.Formatter; import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R; import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.Utils;
import com.simplemobiletools.calendar.models.Event;
import org.joda.time.DateTime; import org.joda.time.DateTime;
@ -22,11 +26,13 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; 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_date) TextView mStartDate;
@BindView(R.id.event_start_time) TextView mStartTime; @BindView(R.id.event_start_time) TextView mStartTime;
@BindView(R.id.event_end_date) TextView mEndDate; @BindView(R.id.event_end_date) TextView mEndDate;
@BindView(R.id.event_end_time) TextView mEndTime; @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 mEventStartDateTime;
private DateTime mEventEndDateTime; private DateTime mEventEndDateTime;
@ -72,7 +78,24 @@ public class EventActivity extends AppCompatActivity {
} }
private void saveEvent() { 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() { private void updateStartDate() {
@ -161,4 +184,10 @@ public class EventActivity extends AppCompatActivity {
updateEndTime(); updateEndTime();
} }
} }
@Override
public void eventInserted() {
Utils.showToast(getApplicationContext(), R.string.event_added);
finish();
}
} }

View File

@ -23,7 +23,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/event_title_label" android:layout_below="@+id/event_title_label"
android:inputType="text" android:inputType="textCapSentences"
android:maxLength="30" android:maxLength="30"
android:maxLines="1" android:maxLines="1"
android:minEms="20" android:minEms="20"
@ -100,6 +100,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/event_description_label" android:layout_below="@+id/event_description_label"
android:gravity="top" android:gravity="top"
android:inputType="textCapSentences|textMultiLine"
android:minEms="20" android:minEms="20"
android:textSize="@dimen/day_text_size"/> android:textSize="@dimen/day_text_size"/>

View File

@ -4,6 +4,9 @@
<!-- Event --> <!-- Event -->
<string name="event">Event</string> <string name="event">Event</string>
<string name="save">Save</string> <string name="save">Save</string>
<string name="title_empty">Title cannot be empty</string>
<string name="end_before_start">The event cannot end earlier than it starts</string>
<string name="event_added">Event added successfully</string>
<!-- Details --> <!-- Details -->
<string name="details">Details</string> <string name="details">Details</string>