implement real deletion of events

This commit is contained in:
tibbi 2016-07-05 12:41:24 +02:00
parent 0f140aac4a
commit df363403d0
5 changed files with 85 additions and 12 deletions

View File

@ -117,7 +117,7 @@ public class CalendarImpl implements DBHelper.DBOperationsListener {
}
@Override
public void eventsDeleted() {
public void eventsDeleted(int cnt) {
}

View File

@ -78,13 +78,13 @@ public class DBHelper extends SQLiteOpenHelper {
final String selection = COL_ID + " = ?";
final String[] selectionArgs = {String.valueOf(id)};
mDb.delete(TABLE_NAME, selection, selectionArgs);
mCallback.eventsDeleted();
mCallback.eventsDeleted(1);
}
public void deleteEvents(String[] ids) {
final String selection = COL_ID + " IN (?)";
mDb.delete(TABLE_NAME, selection, ids);
mCallback.eventsDeleted();
mCallback.eventsDeleted(ids.length);
}
public void getEvents(int fromTS, int toTS) {
@ -114,7 +114,7 @@ public class DBHelper extends SQLiteOpenHelper {
void eventUpdated();
void eventsDeleted();
void eventsDeleted(int cnt);
void gotEvents(List<Event> events);
}

View File

@ -1,7 +1,11 @@
package com.simplemobiletools.calendar.activities;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
@ -32,10 +36,13 @@ 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;
@BindView(R.id.day_coordinator) CoordinatorLayout mCoordinatorLayout;
private static String mDayCode;
private static List<Event> mEvents;
private static int mSelectedItemsCnt;
private static Snackbar mSnackbar;
private static List<Integer> mToBeDeleted;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -53,6 +60,7 @@ public class DayActivity extends AppCompatActivity
final String date = Formatter.getEventDate(mDayCode);
mDateTV.setText(date);
mToBeDeleted = new ArrayList<>();
}
@Override
@ -61,6 +69,12 @@ public class DayActivity extends AppCompatActivity
checkEvents();
}
@Override
protected void onPause() {
super.onPause();
checkDeleteEvents();
}
@OnClick(R.id.day_fab)
public void fabClicked(View view) {
final Intent intent = new Intent(getApplicationContext(), EventActivity.class);
@ -81,11 +95,18 @@ public class DayActivity extends AppCompatActivity
}
private void updateEvents(List<Event> events) {
mEvents = new ArrayList<>(events);
final int cnt = events.size();
for (int i = cnt - 1; i >= 0; i--) {
if (mToBeDeleted.contains(events.get(i).getId())) {
events.remove(i);
}
}
final EventsAdapter adapter = new EventsAdapter(this, events);
mEventsList.setAdapter(adapter);
mEventsList.setOnItemClickListener(this);
mEventsList.setMultiChoiceModeListener(this);
mEvents = events;
}
@Override
@ -99,10 +120,45 @@ public class DayActivity extends AppCompatActivity
}
@Override
public void eventsDeleted() {
public void eventsDeleted(int cnt) {
checkEvents();
}
private void checkDeleteEvents() {
if (mSnackbar != null && mSnackbar.isShown()) {
deleteEvents();
} else {
undoDeletion();
}
}
private void deleteEvents() {
mSnackbar.dismiss();
final int cnt = mToBeDeleted.size();
final String[] eventIDs = new String[cnt];
for (int i = 0; i < cnt; i++) {
eventIDs[i] = String.valueOf(mToBeDeleted.get(i));
}
DBHelper.newInstance(getApplicationContext(), this).deleteEvents(eventIDs);
}
private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override
public void onClick(View v) {
undoDeletion();
}
};
private void undoDeletion() {
if (mSnackbar != null) {
mToBeDeleted.clear();
mSnackbar.dismiss();
updateEvents(mEvents);
}
}
@Override
public void gotEvents(List<Event> events) {
updateEvents(events);
@ -127,6 +183,7 @@ public class DayActivity extends AppCompatActivity
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
checkDeleteEvents();
final MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_day_cab, menu);
return true;
@ -141,7 +198,7 @@ public class DayActivity extends AppCompatActivity
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
deleteEvents();
prepareDeleteEvents();
mode.finish();
return true;
default:
@ -149,16 +206,26 @@ public class DayActivity extends AppCompatActivity
}
}
private void deleteEvents() {
final List<String> eventIDs = new ArrayList<>();
private void prepareDeleteEvents() {
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()));
mToBeDeleted.add(event.getId());
}
}
DBHelper.newInstance(getApplicationContext(), this).deleteEvents(eventIDs.toArray(new String[eventIDs.size()]));
notifyEventDeletion(mToBeDeleted.size());
}
private void notifyEventDeletion(int cnt) {
final Resources res = getResources();
final String msg = res.getQuantityString(R.plurals.events_deleted, cnt, cnt);
mSnackbar = Snackbar.make(mCoordinatorLayout, msg, Snackbar.LENGTH_INDEFINITE);
mSnackbar.setAction(res.getString(R.string.undo), undoDeletion);
mSnackbar.setActionTextColor(Color.WHITE);
mSnackbar.show();
updateEvents(mEvents);
}
@Override

View File

@ -245,7 +245,7 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera
}
@Override
public void eventsDeleted() {
public void eventsDeleted(int cnt) {
finish();
}

View File

@ -1,5 +1,6 @@
<resources>
<string name="app_name">Simple Calendar</string>
<string name="undo">Undo</string>
<!-- Event -->
<string name="event">Event</string>
@ -40,4 +41,9 @@
<string name="joda_title"><u>Joda-Time (Java date replacement)</u></string>
<string name="joda_text">Copyright 2013 JodaOrg\n\nLicensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License.</string>
<string name="joda_url">https://github.com/JodaOrg/joda-time</string>
<plurals name="events_deleted">
<item quantity="one">1 event deleted</item>
<item quantity="other">%1$d events deleted</item>
</plurals>
</resources>