mirror of https://github.com/tasks/tasks
Simplify LocationControlSet
parent
be0432c86c
commit
c7401762b2
@ -1,102 +0,0 @@
|
||||
package org.tasks.location;
|
||||
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
import static com.todoroo.astrid.data.Task.NO_ID;
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import org.tasks.data.Geofence;
|
||||
import org.tasks.data.Location;
|
||||
import org.tasks.data.LocationDao;
|
||||
import org.tasks.data.Place;
|
||||
|
||||
public class GeofenceService {
|
||||
|
||||
private final GeofenceApi geofenceApi;
|
||||
private final LocationDao locationDao;
|
||||
|
||||
@Inject
|
||||
public GeofenceService(GeofenceApi geofenceApi, LocationDao locationDao) {
|
||||
this.geofenceApi = geofenceApi;
|
||||
this.locationDao = locationDao;
|
||||
}
|
||||
|
||||
public List<Location> getGeofences(long taskId) {
|
||||
return taskId == NO_ID ? emptyList() : locationDao.getGeofences(taskId);
|
||||
}
|
||||
|
||||
public void setupGeofences() {
|
||||
geofenceApi.register(locationDao.getActiveGeofences());
|
||||
}
|
||||
|
||||
public void setupGeofences(long taskId) {
|
||||
geofenceApi.register(getGeofencesForTask(taskId));
|
||||
}
|
||||
|
||||
public void cancelGeofences(long taskId) {
|
||||
for (Location location : getGeofences(taskId)) {
|
||||
geofenceApi.cancel(location);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean synchronizeGeofences(final long taskId, Set<Location> locations) {
|
||||
boolean changed = synchronizeMetadata(taskId, newArrayList(locations), geofenceApi::cancel);
|
||||
|
||||
if (changed) {
|
||||
setupGeofences(taskId);
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
private boolean synchronizeMetadata(
|
||||
long taskId, List<Location> locations, final SynchronizeGeofenceCallback callback) {
|
||||
boolean dirty = false;
|
||||
for (Location metadatum : locations) {
|
||||
metadatum.setTask(taskId);
|
||||
metadatum.setId(0L);
|
||||
}
|
||||
|
||||
for (Location item : locationDao.getGeofences(taskId)) {
|
||||
long id = item.getId();
|
||||
|
||||
// clear item id when matching with incoming values
|
||||
item.setId(0L);
|
||||
|
||||
if (locations.contains(item)) {
|
||||
locations.remove(item);
|
||||
} else {
|
||||
// not matched. cut it
|
||||
item.setId(id);
|
||||
if (callback != null) {
|
||||
callback.beforeDelete(item);
|
||||
}
|
||||
locationDao.delete(item.geofence);
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
// everything that remains shall be written
|
||||
for (Location location : locations) {
|
||||
Place place = location.place;
|
||||
Geofence geofence = location.geofence;
|
||||
geofence.setTask(taskId);
|
||||
geofence.setPlace(place.getUid());
|
||||
locationDao.insert(geofence);
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
return dirty;
|
||||
}
|
||||
|
||||
private List<Location> getGeofencesForTask(long taskId) {
|
||||
return locationDao.getActiveGeofences(taskId);
|
||||
}
|
||||
|
||||
interface SynchronizeGeofenceCallback {
|
||||
|
||||
void beforeDelete(Location location);
|
||||
}
|
||||
}
|
@ -1,48 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:baselineAligned="false"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="100"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/location_name"
|
||||
style="@style/TaskEditTextPrimary"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="?attr/asTextColor"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/location_address"
|
||||
style="@style/TaskEditTextPrimary"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="?attr/asTextColorHint"
|
||||
android:visibility="gone"
|
||||
android:maxLines="1"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="end"/>
|
||||
|
||||
</LinearLayout>
|
||||
android:baselineAligned="false">
|
||||
|
||||
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/location_more"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_gravity="top|center"
|
||||
android:alpha="?attr/alpha_secondary"
|
||||
android:background="?attr/selectableItemBackgroundBorderless"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:src="@drawable/ic_outline_more_vert_24px"
|
||||
android:tint="?attr/icon_tint"/>
|
||||
|
||||
</LinearLayout>
|
||||
<TextView
|
||||
android:id="@+id/location_name"
|
||||
style="@style/TaskEditTextPrimary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_toLeftOf="@id/location_more"
|
||||
android:layout_toStartOf="@id/location_more"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:hint="@string/add_location"
|
||||
android:textColor="?attr/asTextColor"
|
||||
android:textColorHint="@color/text_secondary"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/location_address"
|
||||
style="@style/TaskEditTextPrimary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_below="@id/location_name"
|
||||
android:layout_toLeftOf="@id/location_more"
|
||||
android:layout_toStartOf="@id/location_more"
|
||||
android:clickable="true"
|
||||
android:ellipsize="end"
|
||||
android:focusable="true"
|
||||
android:maxLines="1"
|
||||
android:singleLine="true"
|
||||
android:textColor="?attr/asTextColorHint"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
Loading…
Reference in New Issue