2017-10-19 21:52:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-04-07 10:16:36 +02:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2018-04-07 10:16:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummaryDao;
|
2018-04-07 10:16:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class ActivitySummariesAdapter extends AbstractItemAdapter<BaseActivitySummary> {
|
2018-04-13 20:27:31 +02:00
|
|
|
private final GBDevice device;
|
|
|
|
|
2018-04-07 10:16:36 +02:00
|
|
|
public ActivitySummariesAdapter(Context context, GBDevice device) {
|
2017-10-19 21:52:38 +02:00
|
|
|
super(context);
|
2018-04-13 20:27:31 +02:00
|
|
|
this.device = device;
|
|
|
|
loadItems();
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:27:31 +02:00
|
|
|
@Override
|
|
|
|
public void loadItems() {
|
2017-10-19 21:52:38 +02:00
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
BaseActivitySummaryDao summaryDao = handler.getDaoSession().getBaseActivitySummaryDao();
|
2018-04-07 10:16:36 +02:00
|
|
|
Device dbDevice = DBHelper.findDevice(device, handler.getDaoSession());
|
|
|
|
|
|
|
|
QueryBuilder<BaseActivitySummary> qb = summaryDao.queryBuilder();
|
|
|
|
qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(dbDevice.getId())).orderDesc(BaseActivitySummaryDao.Properties.StartTime);
|
|
|
|
List<BaseActivitySummary> allSummaries = qb.build().list();
|
2017-10-19 21:52:38 +02:00
|
|
|
setItems(allSummaries, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
GB.toast("Error loading activity summaries.", Toast.LENGTH_SHORT, GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getName(BaseActivitySummary item) {
|
|
|
|
String name = item.getName();
|
|
|
|
if (name != null && name.length() > 0) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Date startTime = item.getStartTime();
|
|
|
|
if (startTime != null) {
|
|
|
|
return DateTimeUtils.formatDateTime(startTime);
|
|
|
|
}
|
|
|
|
return "Unknown activity";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getDetails(BaseActivitySummary item) {
|
|
|
|
return ActivityKind.asString(item.getActivityKind(), getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getIcon(BaseActivitySummary item) {
|
|
|
|
return ActivityKind.getIconId(item.getActivityKind());
|
|
|
|
}
|
|
|
|
}
|