allow deleting events from both the Day or Event screens

This commit is contained in:
tibbi 2016-07-04 23:48:21 +02:00
parent 4846c6964d
commit 15e4a7066e
18 changed files with 132 additions and 4 deletions

View File

@ -116,6 +116,11 @@ public class CalendarImpl implements DBHelper.DBOperationsListener {
}
@Override
public void eventsDeleted() {
}
@Override
public void gotEvents(List<Event> events) {
mEvents = events;

View File

@ -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<Event> events = new ArrayList<>();
@ -101,6 +114,8 @@ public class DBHelper extends SQLiteOpenHelper {
void eventUpdated();
void eventsDeleted();
void gotEvents(List<Event> events);
}
}

View File

@ -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<Event> 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<Event> 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<String> 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;
}
}

View File

@ -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<Event> events) {

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/mediumGrey" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>

View File

@ -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"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton

View File

@ -24,7 +24,7 @@
android:layout_toRightOf="@+id/left_arrow"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingTop="@dimen/top_padding"
android:textSize="@dimen/month_text_size"/>
<ImageView

View File

@ -4,6 +4,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/event_item_background"
android:paddingBottom="@dimen/event_padding"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/delete"
android:icon="@mipmap/delete"
android:title="@string/delete"/>
</menu>

View File

@ -1,9 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/delete"
android:icon="@mipmap/delete"
android:title="@string/delete"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/save"
android:icon="@mipmap/check"
android:title="@string/save"
app:showAsAction="ifRoom"/>
</menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

View File

@ -4,5 +4,5 @@
<color name="colorPrimaryDark">#ffe27725</color>
<color name="colorAccent">@color/colorPrimary</color>
<color name="dark_grey_pressed_mask">#11000000</color>
<color name="lightGrey">#fff3f3f3</color>
<color name="mediumGrey">#18000000</color>
</resources>

View File

@ -3,6 +3,7 @@
<dimen name="social_padding">8dp</dimen>
<dimen name="social_logo">40dp</dimen>
<dimen name="event_padding">8dp</dimen>
<dimen name="top_padding">8dp</dimen>
<dimen name="min_widget_width">250dp</dimen>
<dimen name="min_widget_height">250dp</dimen>

View File

@ -4,6 +4,7 @@
<!-- Event -->
<string name="event">Event</string>
<string name="save">Save</string>
<string name="delete">Delete</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>