From 15e4a7066e4fe478399544d8116163a70d9cbc9a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 4 Jul 2016 23:48:21 +0200 Subject: [PATCH] allow deleting events from both the Day or Event screens --- .../calendar/CalendarImpl.java | 5 ++ .../simplemobiletools/calendar/DBHelper.java | 15 ++++ .../calendar/activities/DayActivity.java | 70 +++++++++++++++++- .../calendar/activities/EventActivity.java | 16 ++++ .../res/drawable/event_item_background.xml | 5 ++ app/src/main/res/layout/activity_day.xml | 4 +- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/event_item.xml | 1 + app/src/main/res/menu/menu_day_cab.xml | 7 ++ app/src/main/res/menu/menu_event.xml | 7 ++ app/src/main/res/mipmap-hdpi/delete.png | Bin 0 -> 296 bytes app/src/main/res/mipmap-mdpi/delete.png | Bin 0 -> 191 bytes app/src/main/res/mipmap-xhdpi/delete.png | Bin 0 -> 256 bytes app/src/main/res/mipmap-xxhdpi/delete.png | Bin 0 -> 447 bytes app/src/main/res/mipmap-xxxhdpi/delete.png | Bin 0 -> 243 bytes app/src/main/res/values/colors.xml | 2 +- app/src/main/res/values/dimens.xml | 1 + app/src/main/res/values/strings.xml | 1 + 18 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/event_item_background.xml create mode 100644 app/src/main/res/menu/menu_day_cab.xml create mode 100644 app/src/main/res/mipmap-hdpi/delete.png create mode 100644 app/src/main/res/mipmap-mdpi/delete.png create mode 100644 app/src/main/res/mipmap-xhdpi/delete.png create mode 100644 app/src/main/res/mipmap-xxhdpi/delete.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/delete.png diff --git a/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java b/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java index 738b03e46..849c77868 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java +++ b/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java @@ -116,6 +116,11 @@ public class CalendarImpl implements DBHelper.DBOperationsListener { } + @Override + public void eventsDeleted() { + + } + @Override public void gotEvents(List events) { mEvents = events; diff --git a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java index d77b88726..fa4748401 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java +++ b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java @@ -74,6 +74,19 @@ public class DBHelper extends SQLiteOpenHelper { return values; } + public void deleteEvent(int id) { + final String selection = COL_ID + " = ?"; + final String[] selectionArgs = {String.valueOf(id)}; + mDb.delete(TABLE_NAME, selection, selectionArgs); + mCallback.eventsDeleted(); + } + + public void deleteEvents(String[] ids) { + final String selection = COL_ID + " IN (?)"; + mDb.delete(TABLE_NAME, selection, ids); + mCallback.eventsDeleted(); + } + public void getEvents(int fromTS, int toTS) { final String[] projection = {COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION}; List events = new ArrayList<>(); @@ -101,6 +114,8 @@ public class DBHelper extends SQLiteOpenHelper { void eventUpdated(); + void eventsDeleted(); + void gotEvents(List events); } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/DayActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/DayActivity.java index eebc640dd..33a8ecc02 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/DayActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/DayActivity.java @@ -3,7 +3,13 @@ package com.simplemobiletools.calendar.activities; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.util.SparseBooleanArray; +import android.view.ActionMode; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; +import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; @@ -15,18 +21,21 @@ import com.simplemobiletools.calendar.Formatter; import com.simplemobiletools.calendar.R; import com.simplemobiletools.calendar.models.Event; +import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; -public class DayActivity extends AppCompatActivity implements DBHelper.DBOperationsListener, AdapterView.OnItemClickListener { +public class DayActivity extends AppCompatActivity + implements DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener { @BindView(R.id.day_date) TextView mDateTV; @BindView(R.id.day_events) ListView mEventsList; private static String mDayCode; private static List mEvents; + private static int mSelectedItemsCnt; @Override protected void onCreate(Bundle savedInstanceState) { @@ -75,6 +84,7 @@ public class DayActivity extends AppCompatActivity implements DBHelper.DBOperati final EventsAdapter adapter = new EventsAdapter(this, events); mEventsList.setAdapter(adapter); mEventsList.setOnItemClickListener(this); + mEventsList.setMultiChoiceModeListener(this); mEvents = events; } @@ -88,6 +98,11 @@ public class DayActivity extends AppCompatActivity implements DBHelper.DBOperati } + @Override + public void eventsDeleted() { + checkEvents(); + } + @Override public void gotEvents(List events) { updateEvents(events); @@ -97,4 +112,57 @@ public class DayActivity extends AppCompatActivity implements DBHelper.DBOperati public void onItemClick(AdapterView parent, View view, int position, long id) { editEvent(mEvents.get(position)); } + + @Override + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { + if (checked) { + mSelectedItemsCnt++; + } else { + mSelectedItemsCnt--; + } + + mode.setTitle(String.valueOf(mSelectedItemsCnt)); + mode.invalidate(); + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + final MenuInflater inflater = mode.getMenuInflater(); + inflater.inflate(R.menu.menu_day_cab, menu); + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return true; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + switch (item.getItemId()) { + case R.id.delete: + deleteEvents(); + mode.finish(); + return true; + default: + return false; + } + } + + private void deleteEvents() { + final List eventIDs = new ArrayList<>(); + final SparseBooleanArray checked = mEventsList.getCheckedItemPositions(); + for (int i = 0; i < mEvents.size(); i++) { + if (checked.get(i)) { + final Event event = mEvents.get(i); + eventIDs.add(String.valueOf(event.getId())); + } + } + DBHelper.newInstance(getApplicationContext(), this).deleteEvents(eventIDs.toArray(new String[eventIDs.size()])); + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + mSelectedItemsCnt = 0; + } } 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 099ece5e2..cb4267058 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/EventActivity.java @@ -85,12 +85,19 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera @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; @@ -99,6 +106,10 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera } } + private void deleteEvent() { + DBHelper.newInstance(getApplicationContext(), this).deleteEvent(mEvent.getId()); + } + private void saveEvent() { final String title = mTitleET.getText().toString().trim(); if (title.isEmpty()) { @@ -227,6 +238,11 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera finish(); } + @Override + public void eventsDeleted() { + finish(); + } + @Override public void gotEvents(List events) { diff --git a/app/src/main/res/drawable/event_item_background.xml b/app/src/main/res/drawable/event_item_background.xml new file mode 100644 index 000000000..1dc59bf98 --- /dev/null +++ b/app/src/main/res/drawable/event_item_background.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_day.xml b/app/src/main/res/layout/activity_day.xml index b36e0ca35..136add11c 100644 --- a/app/src/main/res/layout/activity_day.xml +++ b/app/src/main/res/layout/activity_day.xml @@ -17,6 +17,7 @@ android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_margin" android:gravity="center_horizontal" + android:paddingTop="@dimen/top_padding" android:text="January 1 1970" android:textSize="@dimen/month_text_size"/> @@ -24,7 +25,8 @@ android:id="@+id/day_events" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_below="@+id/day_date"/> + android:layout_below="@+id/day_date" + android:choiceMode="multipleChoiceModal"/> + + + diff --git a/app/src/main/res/menu/menu_event.xml b/app/src/main/res/menu/menu_event.xml index 24c10c23e..bde3bb334 100644 --- a/app/src/main/res/menu/menu_event.xml +++ b/app/src/main/res/menu/menu_event.xml @@ -1,9 +1,16 @@ + + + diff --git a/app/src/main/res/mipmap-hdpi/delete.png b/app/src/main/res/mipmap-hdpi/delete.png new file mode 100644 index 0000000000000000000000000000000000000000..f3d2e18b855f6ceaa1db04b24341a53c5a4bc5c6 GIT binary patch literal 296 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k1|%Oc%$NbBSc;uILpXq-h9ji|$mcBZh%9Dc z;5!1sj8nDwq=AB`JY5_^B3j>0-^hE&fyZ^fpsa@hOY?=zY}GDr4)VQ}V2M)mXr7R9 zaBz0rEc~1_YVy=&y5zAWchM2%KgOLkEQ7vtu{d? zl#z+Luod!(ujMqmxaQgkR2Ir49urRBEos~y(a9I#zUk2SlmyjG4nJ z@ErkR#;MwT(m=sPPZ!4!kIuJKH}W>+Fy<(R3HoQaH3KbU@O1TaS?83{1OOFwJY)a> literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xhdpi/delete.png b/app/src/main/res/mipmap-xhdpi/delete.png new file mode 100644 index 0000000000000000000000000000000000000000..ca80a3b5ab8ef12ce18ed68d166331acb4130da9 GIT binary patch literal 256 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTCmSQK*5Dp-y;YjHK@;M7UB8wRq z_>O=u<5X=vX`tXDPZ!6KjK;S&4TYK<1Y82I9e!YFIZ2LT{SwA!xe0+wCmyy+p8hNT z+$H-2;WkEwx~3(bg_En_PwssjtbD6&;;D+nkZj#8x#6};q!@pcKa9=@t(A7opMUjw zJu}M=!3S;)(u{d%jA;J@rnN_<)4#LKv6|O8i~Ib($y=tEy!^574nJ z@ErkR#;MwT(hLlYiJmTwAr*7p-nR8)3Y2JlSll}|x`175#)a2PId>L{O!{&8HQxrq zbq7>4+GV&66xE)kr0q-Oy05-4d7t%n-o|JHA)v_yrnkkng>N&rjn}?CJ@|dH$hP#| zynJ7ie%HMEJLT5V%3HNuS~j=m$v(PwdqQ_^ed!#-jJ6+jrX|HdA~-Pt}wc=*2->~iI8_x81HJH66KeHmv$AT zg=c&6PZATpcldtJ`P}>Bc9A!Z9{V=;ZROK>UjMJ>?Dn*pTB7^X>(b06lV6qA{k-ta nI$bmKVpO$!p)N3F5?)IuE$jAp*s+!g$Ybzy^>bP0l+XkK4Em=4 literal 0 HcmV?d00001 diff --git a/app/src/main/res/mipmap-xxxhdpi/delete.png b/app/src/main/res/mipmap-xxxhdpi/delete.png new file mode 100644 index 0000000000000000000000000000000000000000..8d322aa9baba09c58b0058760fb7d53a392b63a1 GIT binary patch literal 243 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD0wg^q?%xcgc6quuhE&{od*w9mAp-%|!1oIz zUAkW?IxdlN5p^sPGLtM@^D{+9cy9DT>q|G5dQ4KuY+ck{{`y~aU3Uxr^X+awlIm)m zLim6tF)Yw+SYp*L31Z?03ltA1m;Rr_zhgPmvYG>yq8FYs_ibEqHvUn-r*1=DgDFx+ z?bd02*59bTR4g&(=Y(kztIpQ%Z4JJY^zZNE9?f4`+x0ysZP__5=_$iXi`hvQAj>>m L{an^LB{Ts5t^ZVx literal 0 HcmV?d00001 diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index b9451e4b0..174dbdc8f 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -4,5 +4,5 @@ #ffe27725 @color/colorPrimary #11000000 - #fff3f3f3 + #18000000 diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index c723a6d97..ccea70d9a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -3,6 +3,7 @@ 8dp 40dp 8dp + 8dp 250dp 250dp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 24c2f1941..acccfc76d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,6 +4,7 @@ Event Save + Delete Title cannot be empty The event cannot end earlier than it starts Event added successfully