1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-03 17:02:13 +01:00

Add Activity Description table and order samples

This commit is contained in:
cpfeiffer 2016-05-23 23:31:22 +02:00
parent 0267ddb356
commit 4504c5b5a4
2 changed files with 59 additions and 9 deletions

View File

@ -34,6 +34,8 @@ public class GBDaoGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(7, MAIN_PACKAGE + ".entities");
addActivityDescription(schema);
Entity userAttributes = addUserAttributes(schema);
Entity user = addUserInfo(schema, userAttributes);
@ -46,6 +48,14 @@ public class GBDaoGenerator {
new DaoGenerator().generateAll(schema, "app/src/main/java");
}
private static Entity addActivityDescription(Schema schema) {
Entity activityDescription = addEntity(schema, "ActivityDescription");
activityDescription.addIdProperty();
activityDescription.addIntProperty("fromTimestamp").notNull();
activityDescription.addIntProperty("toTimestamp");
return activityDescription;
}
private static Entity addUserInfo(Schema schema, Entity userAttributes) {
Entity user = addEntity(schema, "User");
user.addIdProperty();
@ -53,11 +63,23 @@ public class GBDaoGenerator {
user.addDateProperty("birthday").notNull();
user.addIntProperty("gender").notNull();
Property userId = userAttributes.addLongProperty("userId").notNull().getProperty();
user.addToMany(userAttributes, userId);
// sorted by the from-date, newest first
Property userAttributesSortProperty = getPropertyByName(userAttributes, VALID_FROM_UTC);
user.addToMany(userAttributes, userId).orderDesc(userAttributesSortProperty);
return user;
}
private static Property getPropertyByName(Entity entity, String propertyName) {
for (Property prop : entity.getProperties()) {
if (propertyName.equals(prop.getPropertyName())) {
return prop;
}
}
throw new IllegalStateException("Could not find property " + propertyName + " in entity " + entity.getClassName());
}
private static Entity addUserAttributes(Schema schema) {
// additional properties of a user, which may change during the lifetime of a user
// this allows changing attributes while preserving user identity
@ -80,7 +102,9 @@ public class GBDaoGenerator {
device.addStringProperty("manufacturer").notNull();
device.addStringProperty("identifier").notNull().unique().javaDocGetterAndSetter("The fixed identifier, i.e. MAC address of the device.");
Property deviceId = deviceAttributes.addLongProperty("deviceId").notNull().getProperty();
device.addToMany(deviceAttributes, deviceId);
// sorted by the from-date, newest first
Property deviceAttributesSortProperty = getPropertyByName(deviceAttributes, VALID_FROM_UTC);
device.addToMany(deviceAttributes, deviceId).orderDesc(deviceAttributesSortProperty);
return device;
}

View File

@ -134,22 +134,35 @@ public class DBHelper {
public static User getUser(DaoSession session) {
UserDao userDao = session.getUserDao();
List<User> users = userDao.loadAll();
ActivityUser prefsUser = new ActivityUser();
User user;
if (users.isEmpty()) {
User user = createUser(session);
user = createUser(prefsUser, session);
} else {
user = users.get(0); // TODO: multiple users support?
}
ensureUserAttributes(user, prefsUser, session);
return user;
}
return users.get(0); // TODO: multiple users support?
}
private static User createUser(DaoSession session) {
ActivityUser prefsUser = new ActivityUser();
private static User createUser(ActivityUser prefsUser, DaoSession session) {
User user = new User();
user.setName(prefsUser.getName());
user.setBirthday(prefsUser.getUserBirthday());
user.setGender(prefsUser.getGender());
session.getUserDao().insert(user);
List<UserAttributes> userAttributes = user.getUserAttributesList();
ensureUserAttributes(user, prefsUser, session);
return user;
}
private static void ensureUserAttributes(User user, ActivityUser prefsUser, DaoSession session) {
List<UserAttributes> userAttributes = user.getUserAttributesList();
if (hasUpToDateUserAttributes(userAttributes, prefsUser)) {
return;
}
UserAttributes attributes = new UserAttributes();
attributes.setValidFromUTC(DateTimeUtils.todayUTC());
attributes.setHeightCM(prefsUser.getHeightCm());
@ -158,8 +171,21 @@ public class DBHelper {
session.getUserAttributesDao().insert(attributes);
userAttributes.add(attributes);
}
return user;
private static boolean hasUpToDateUserAttributes(List<UserAttributes> userAttributes, ActivityUser prefsUser) {
for (UserAttributes attr : userAttributes) {
if (!isActive(attr)) {
return false;
}
if (isEqual(attr, prefsUser)) {
return true;
}
}
return false;
}
private static boolean isEqual(UserAttributes attr, ActivityUser prefsUser) {
}
public static Device getDevice(GBDevice gbDevice, DaoSession session) {