mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
implement real deletion of events
This commit is contained in:
@ -117,7 +117,7 @@ public class CalendarImpl implements DBHelper.DBOperationsListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventsDeleted() {
|
public void eventsDeleted(int cnt) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,13 +78,13 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||||||
final String selection = COL_ID + " = ?";
|
final String selection = COL_ID + " = ?";
|
||||||
final String[] selectionArgs = {String.valueOf(id)};
|
final String[] selectionArgs = {String.valueOf(id)};
|
||||||
mDb.delete(TABLE_NAME, selection, selectionArgs);
|
mDb.delete(TABLE_NAME, selection, selectionArgs);
|
||||||
mCallback.eventsDeleted();
|
mCallback.eventsDeleted(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteEvents(String[] ids) {
|
public void deleteEvents(String[] ids) {
|
||||||
final String selection = COL_ID + " IN (?)";
|
final String selection = COL_ID + " IN (?)";
|
||||||
mDb.delete(TABLE_NAME, selection, ids);
|
mDb.delete(TABLE_NAME, selection, ids);
|
||||||
mCallback.eventsDeleted();
|
mCallback.eventsDeleted(ids.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getEvents(int fromTS, int toTS) {
|
public void getEvents(int fromTS, int toTS) {
|
||||||
@ -114,7 +114,7 @@ public class DBHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
void eventUpdated();
|
void eventUpdated();
|
||||||
|
|
||||||
void eventsDeleted();
|
void eventsDeleted(int cnt);
|
||||||
|
|
||||||
void gotEvents(List<Event> events);
|
void gotEvents(List<Event> events);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package com.simplemobiletools.calendar.activities;
|
package com.simplemobiletools.calendar.activities;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.design.widget.CoordinatorLayout;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.util.SparseBooleanArray;
|
import android.util.SparseBooleanArray;
|
||||||
import android.view.ActionMode;
|
import android.view.ActionMode;
|
||||||
@ -32,10 +36,13 @@ public class DayActivity extends AppCompatActivity
|
|||||||
implements DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {
|
implements DBHelper.DBOperationsListener, AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {
|
||||||
@BindView(R.id.day_date) TextView mDateTV;
|
@BindView(R.id.day_date) TextView mDateTV;
|
||||||
@BindView(R.id.day_events) ListView mEventsList;
|
@BindView(R.id.day_events) ListView mEventsList;
|
||||||
|
@BindView(R.id.day_coordinator) CoordinatorLayout mCoordinatorLayout;
|
||||||
|
|
||||||
private static String mDayCode;
|
private static String mDayCode;
|
||||||
private static List<Event> mEvents;
|
private static List<Event> mEvents;
|
||||||
private static int mSelectedItemsCnt;
|
private static int mSelectedItemsCnt;
|
||||||
|
private static Snackbar mSnackbar;
|
||||||
|
private static List<Integer> mToBeDeleted;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -53,6 +60,7 @@ public class DayActivity extends AppCompatActivity
|
|||||||
|
|
||||||
final String date = Formatter.getEventDate(mDayCode);
|
final String date = Formatter.getEventDate(mDayCode);
|
||||||
mDateTV.setText(date);
|
mDateTV.setText(date);
|
||||||
|
mToBeDeleted = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -61,6 +69,12 @@ public class DayActivity extends AppCompatActivity
|
|||||||
checkEvents();
|
checkEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
checkDeleteEvents();
|
||||||
|
}
|
||||||
|
|
||||||
@OnClick(R.id.day_fab)
|
@OnClick(R.id.day_fab)
|
||||||
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);
|
||||||
@ -81,11 +95,18 @@ public class DayActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateEvents(List<Event> events) {
|
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);
|
final EventsAdapter adapter = new EventsAdapter(this, events);
|
||||||
mEventsList.setAdapter(adapter);
|
mEventsList.setAdapter(adapter);
|
||||||
mEventsList.setOnItemClickListener(this);
|
mEventsList.setOnItemClickListener(this);
|
||||||
mEventsList.setMultiChoiceModeListener(this);
|
mEventsList.setMultiChoiceModeListener(this);
|
||||||
mEvents = events;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -99,10 +120,45 @@ public class DayActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventsDeleted() {
|
public void eventsDeleted(int cnt) {
|
||||||
checkEvents();
|
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
|
@Override
|
||||||
public void gotEvents(List<Event> events) {
|
public void gotEvents(List<Event> events) {
|
||||||
updateEvents(events);
|
updateEvents(events);
|
||||||
@ -127,6 +183,7 @@ public class DayActivity extends AppCompatActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||||
|
checkDeleteEvents();
|
||||||
final MenuInflater inflater = mode.getMenuInflater();
|
final MenuInflater inflater = mode.getMenuInflater();
|
||||||
inflater.inflate(R.menu.menu_day_cab, menu);
|
inflater.inflate(R.menu.menu_day_cab, menu);
|
||||||
return true;
|
return true;
|
||||||
@ -141,7 +198,7 @@ public class DayActivity extends AppCompatActivity
|
|||||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.delete:
|
case R.id.delete:
|
||||||
deleteEvents();
|
prepareDeleteEvents();
|
||||||
mode.finish();
|
mode.finish();
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@ -149,16 +206,26 @@ public class DayActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteEvents() {
|
private void prepareDeleteEvents() {
|
||||||
final List<String> eventIDs = new ArrayList<>();
|
|
||||||
final SparseBooleanArray checked = mEventsList.getCheckedItemPositions();
|
final SparseBooleanArray checked = mEventsList.getCheckedItemPositions();
|
||||||
for (int i = 0; i < mEvents.size(); i++) {
|
for (int i = 0; i < mEvents.size(); i++) {
|
||||||
if (checked.get(i)) {
|
if (checked.get(i)) {
|
||||||
final Event event = mEvents.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
|
@Override
|
||||||
|
@ -245,7 +245,7 @@ public class EventActivity extends AppCompatActivity implements DBHelper.DBOpera
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventsDeleted() {
|
public void eventsDeleted(int cnt) {
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Simple Calendar</string>
|
<string name="app_name">Simple Calendar</string>
|
||||||
|
<string name="undo">Undo</string>
|
||||||
|
|
||||||
<!-- Event -->
|
<!-- Event -->
|
||||||
<string name="event">Event</string>
|
<string name="event">Event</string>
|
||||||
@ -40,4 +41,9 @@
|
|||||||
<string name="joda_title"><u>Joda-Time (Java date replacement)</u></string>
|
<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_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>
|
<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>
|
</resources>
|
||||||
|
Reference in New Issue
Block a user