diff --git a/app/build.gradle b/app/build.gradle index 69baaf0..6ecb6cf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 23 @@ -34,11 +35,10 @@ buildscript { mavenCentral() } dependencies { - classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' + classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' } } -apply plugin: 'org.greenrobot.greendao' greendao { schemaVersion android.defaultConfig.versionCode targetGenDir = new File('app/src/main/java') @@ -49,5 +49,5 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' - compile 'org.greenrobot:greendao:3.0.1' + compile 'org.greenrobot:greendao:3.2.0' } diff --git a/app/src/main/java/org/literacyapp/chat/ChatActivity.java b/app/src/main/java/org/literacyapp/chat/ChatActivity.java index e78489e..f500f9e 100644 --- a/app/src/main/java/org/literacyapp/chat/ChatActivity.java +++ b/app/src/main/java/org/literacyapp/chat/ChatActivity.java @@ -13,8 +13,8 @@ import android.widget.ImageButton; import android.widget.ListView; -import org.literacyapp.chat.dao.TextMessageDao; -import org.literacyapp.chat.model.TextMessage; +import org.literacyapp.chat.dao.MessageDao; +import org.literacyapp.chat.model.Message; import org.literacyapp.chat.util.DeviceInfoHelper; import java.util.Calendar; @@ -22,15 +22,15 @@ public class ChatActivity extends Activity { - private TextMessageDao textMessageDao; + private MessageDao messageDao; - private List textMessages; + private List messages; private ArrayAdapter arrayAdapter; private ListView mListPreviousMessages; - private EditText mTextMessage; + private EditText message; private ImageButton mButtonSend; @@ -41,10 +41,10 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_chat); - textMessageDao = ((ChatApplication) getApplication()).getDaoSession().getTextMessageDao(); + messageDao = ((ChatApplication) getApplication()).getDaoSession().getMessageDao(); mListPreviousMessages = (ListView) findViewById(R.id.listPreviousMessages); - mTextMessage = (EditText) findViewById(R.id.textMessage); + message = (EditText) findViewById(R.id.message); mButtonSend = (ImageButton) findViewById(R.id.buttonSend); } @@ -57,15 +57,15 @@ protected void onStart() { // Load messages sent within the last 24 hours Calendar calendar24HoursAgo = Calendar.getInstance(); calendar24HoursAgo.add(Calendar.HOUR_OF_DAY, -24); - textMessages = textMessageDao.queryBuilder() - .where(TextMessageDao.Properties.TimeSent.gt(calendar24HoursAgo.getTimeInMillis())) + messages = messageDao.queryBuilder() + .where(MessageDao.Properties.TimeSent.gt(calendar24HoursAgo.getTimeInMillis())) .list(); - Log.i(getClass().getName(), "textMessages.size(): " + textMessages.size()); + Log.i(getClass().getName(), "messages.size(): " + messages.size()); - arrayAdapter = new MessageListArrayAdapter(getApplicationContext(), textMessages); + arrayAdapter = new MessageListArrayAdapter(getApplicationContext(), messages); mListPreviousMessages.setAdapter(arrayAdapter); - mTextMessage.addTextChangedListener(new TextWatcher() { + message.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { @@ -93,24 +93,24 @@ public void afterTextChanged(Editable editable) { public void onClick(View view) { Log.i(getClass().getName(), "mButtonSend onClick"); - String text = mTextMessage.getText().toString(); + String text = message.getText().toString(); Log.i(getClass().getName(), "text: " + text); // Check if EditText is empty if(!TextUtils.isEmpty(text)){ - TextMessage textMessage = new TextMessage(); - textMessage.setDeviceId(DeviceInfoHelper.getDeviceId(getApplicationContext())); - textMessage.setTimeSent(Calendar.getInstance()); - textMessage.setText(text); + Message message = new Message(); + message.setDeviceId(DeviceInfoHelper.getDeviceId(getApplicationContext())); + message.setTimeSent(Calendar.getInstance()); + message.setText(text); // Store in database - textMessageDao.insert(textMessage); + messageDao.insert(message); // Add to UI - addToMessageListAndRefresh(textMessage); + addToMessageListAndRefresh(message); // Reset input field - mTextMessage.setText(""); + ChatActivity.this.message.setText(""); } else { mButtonSend.setVisibility(View.GONE); @@ -121,8 +121,8 @@ public void onClick(View view) { }); } - private void addToMessageListAndRefresh(TextMessage textMessage) { - textMessages.add(textMessage); + private void addToMessageListAndRefresh(Message message) { + messages.add(message); refreshMessageList(); } diff --git a/app/src/main/java/org/literacyapp/chat/ChatApplication.java b/app/src/main/java/org/literacyapp/chat/ChatApplication.java index b7fda50..d459fe8 100644 --- a/app/src/main/java/org/literacyapp/chat/ChatApplication.java +++ b/app/src/main/java/org/literacyapp/chat/ChatApplication.java @@ -29,7 +29,11 @@ public void onCreate() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); int oldVersionCode = sharedPreferences.getInt(PREF_APP_VERSION_CODE, 0); int newVersionCode = VersionHelper.getAppVersionCode(getApplicationContext()); - if ((oldVersionCode > 0) && (oldVersionCode < newVersionCode)) { + if (oldVersionCode == 0) { + sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, newVersionCode).commit(); + oldVersionCode = newVersionCode; + } + if (oldVersionCode < newVersionCode) { Log.i(getClass().getName(), "Upgrading application from version " + oldVersionCode + " to " + newVersionCode); // if (newVersionCode == ???) { // // Put relevant tasks required for upgrading here diff --git a/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java b/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java index d67ae9e..3dfef55 100644 --- a/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java +++ b/app/src/main/java/org/literacyapp/chat/MessageListArrayAdapter.java @@ -9,27 +9,27 @@ import android.widget.ImageView; import android.widget.TextView; -import org.literacyapp.chat.model.TextMessage; +import org.literacyapp.chat.model.Message; import java.util.List; -public class MessageListArrayAdapter extends ArrayAdapter { +public class MessageListArrayAdapter extends ArrayAdapter { private Context context; - private List textMessages; + private List messages; static class ViewHolder { ImageView imageViewAvatar; TextView textViewListItem; } - public MessageListArrayAdapter(Context context, List textMessages) { - super(context, R.layout.activity_chat_list_item, textMessages); + public MessageListArrayAdapter(Context context, List messages) { + super(context, R.layout.activity_chat_list_item, messages); Log.i(getClass().getName(), "MessageListArrayAdapter"); this.context = context; - this.textMessages = textMessages; + this.messages = messages; } @Override @@ -46,10 +46,10 @@ public View getView(int position, View convertView, ViewGroup parent) { listItem.setTag(viewHolder); } - TextMessage textMessage = textMessages.get(position); + Message message = messages.get(position); ViewHolder viewHolder = (ViewHolder) listItem.getTag(); - viewHolder.textViewListItem.setText(textMessage.getText()); + viewHolder.textViewListItem.setText(message.getText()); return listItem; } diff --git a/app/src/main/java/org/literacyapp/chat/dao/DaoMaster.java b/app/src/main/java/org/literacyapp/chat/dao/DaoMaster.java index 7f90865..4f96e0e 100644 --- a/app/src/main/java/org/literacyapp/chat/dao/DaoMaster.java +++ b/app/src/main/java/org/literacyapp/chat/dao/DaoMaster.java @@ -21,12 +21,12 @@ public class DaoMaster extends AbstractDaoMaster { /** Creates underlying database table using DAOs. */ public static void createAllTables(Database db, boolean ifNotExists) { - TextMessageDao.createTable(db, ifNotExists); + MessageDao.createTable(db, ifNotExists); } /** Drops underlying database table using DAOs. */ public static void dropAllTables(Database db, boolean ifExists) { - TextMessageDao.dropTable(db, ifExists); + MessageDao.dropTable(db, ifExists); } /** @@ -45,7 +45,7 @@ public DaoMaster(SQLiteDatabase db) { public DaoMaster(Database db) { super(db, SCHEMA_VERSION); - registerDaoClass(TextMessageDao.class); + registerDaoClass(MessageDao.class); } public DaoSession newSession() { diff --git a/app/src/main/java/org/literacyapp/chat/dao/DaoSession.java b/app/src/main/java/org/literacyapp/chat/dao/DaoSession.java index 9ac187a..f680ba3 100644 --- a/app/src/main/java/org/literacyapp/chat/dao/DaoSession.java +++ b/app/src/main/java/org/literacyapp/chat/dao/DaoSession.java @@ -8,9 +8,9 @@ import org.greenrobot.greendao.identityscope.IdentityScopeType; import org.greenrobot.greendao.internal.DaoConfig; -import org.literacyapp.chat.model.TextMessage; +import org.literacyapp.chat.model.Message; -import org.literacyapp.chat.dao.TextMessageDao; +import org.literacyapp.chat.dao.MessageDao; // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. @@ -21,28 +21,28 @@ */ public class DaoSession extends AbstractDaoSession { - private final DaoConfig textMessageDaoConfig; + private final DaoConfig messageDaoConfig; - private final TextMessageDao textMessageDao; + private final MessageDao messageDao; public DaoSession(Database db, IdentityScopeType type, Map>, DaoConfig> daoConfigMap) { super(db); - textMessageDaoConfig = daoConfigMap.get(TextMessageDao.class).clone(); - textMessageDaoConfig.initIdentityScope(type); + messageDaoConfig = daoConfigMap.get(MessageDao.class).clone(); + messageDaoConfig.initIdentityScope(type); - textMessageDao = new TextMessageDao(textMessageDaoConfig, this); + messageDao = new MessageDao(messageDaoConfig, this); - registerDao(TextMessage.class, textMessageDao); + registerDao(Message.class, messageDao); } public void clear() { - textMessageDaoConfig.getIdentityScope().clear(); + messageDaoConfig.clearIdentityScope(); } - public TextMessageDao getTextMessageDao() { - return textMessageDao; + public MessageDao getMessageDao() { + return messageDao; } } diff --git a/app/src/main/java/org/literacyapp/chat/dao/TextMessageDao.java b/app/src/main/java/org/literacyapp/chat/dao/MessageDao.java similarity index 50% rename from app/src/main/java/org/literacyapp/chat/dao/TextMessageDao.java rename to app/src/main/java/org/literacyapp/chat/dao/MessageDao.java index 7f22d8b..f451618 100644 --- a/app/src/main/java/org/literacyapp/chat/dao/TextMessageDao.java +++ b/app/src/main/java/org/literacyapp/chat/dao/MessageDao.java @@ -12,55 +12,59 @@ import java.util.Calendar; import org.literacyapp.chat.dao.converter.CalendarConverter; -import org.literacyapp.chat.model.TextMessage; +import org.literacyapp.chat.model.Message; // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. /** - * DAO for table "TEXT_MESSAGE". + * DAO for table "MESSAGE". */ -public class TextMessageDao extends AbstractDao { +public class MessageDao extends AbstractDao { - public static final String TABLENAME = "TEXT_MESSAGE"; + public static final String TABLENAME = "MESSAGE"; /** - * Properties of entity TextMessage.
+ * Properties of entity Message.
* Can be used for QueryBuilder and for referencing column names. - */ + */ public static class Properties { public final static Property Id = new Property(0, Long.class, "id", true, "_id"); public final static Property DeviceId = new Property(1, String.class, "deviceId", false, "DEVICE_ID"); - public final static Property TimeSent = new Property(2, long.class, "timeSent", false, "TIME_SENT"); - public final static Property Text = new Property(3, String.class, "text", false, "TEXT"); - }; + public final static Property StudentId = new Property(2, String.class, "studentId", false, "STUDENT_ID"); + public final static Property StudentAvatar = new Property(3, String.class, "studentAvatar", false, "STUDENT_AVATAR"); + public final static Property TimeSent = new Property(4, long.class, "timeSent", false, "TIME_SENT"); + public final static Property Text = new Property(5, String.class, "text", false, "TEXT"); + } private final CalendarConverter timeSentConverter = new CalendarConverter(); - public TextMessageDao(DaoConfig config) { + public MessageDao(DaoConfig config) { super(config); } - public TextMessageDao(DaoConfig config, DaoSession daoSession) { + public MessageDao(DaoConfig config, DaoSession daoSession) { super(config, daoSession); } /** Creates the underlying database table. */ public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; - db.execSQL("CREATE TABLE " + constraint + "\"TEXT_MESSAGE\" (" + // + db.execSQL("CREATE TABLE " + constraint + "\"MESSAGE\" (" + // "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id "\"DEVICE_ID\" TEXT NOT NULL ," + // 1: deviceId - "\"TIME_SENT\" INTEGER NOT NULL ," + // 2: timeSent - "\"TEXT\" TEXT NOT NULL );"); // 3: text + "\"STUDENT_ID\" TEXT," + // 2: studentId + "\"STUDENT_AVATAR\" TEXT," + // 3: studentAvatar + "\"TIME_SENT\" INTEGER NOT NULL ," + // 4: timeSent + "\"TEXT\" TEXT NOT NULL );"); // 5: text } /** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { - String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"TEXT_MESSAGE\""; + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"MESSAGE\""; db.execSQL(sql); } @Override - protected final void bindValues(DatabaseStatement stmt, TextMessage entity) { + protected final void bindValues(DatabaseStatement stmt, Message entity) { stmt.clearBindings(); Long id = entity.getId(); @@ -68,12 +72,22 @@ protected final void bindValues(DatabaseStatement stmt, TextMessage entity) { stmt.bindLong(1, id); } stmt.bindString(2, entity.getDeviceId()); - stmt.bindLong(3, timeSentConverter.convertToDatabaseValue(entity.getTimeSent())); - stmt.bindString(4, entity.getText()); + + String studentId = entity.getStudentId(); + if (studentId != null) { + stmt.bindString(3, studentId); + } + + String studentAvatar = entity.getStudentAvatar(); + if (studentAvatar != null) { + stmt.bindString(4, studentAvatar); + } + stmt.bindLong(5, timeSentConverter.convertToDatabaseValue(entity.getTimeSent())); + stmt.bindString(6, entity.getText()); } @Override - protected final void bindValues(SQLiteStatement stmt, TextMessage entity) { + protected final void bindValues(SQLiteStatement stmt, Message entity) { stmt.clearBindings(); Long id = entity.getId(); @@ -81,8 +95,18 @@ protected final void bindValues(SQLiteStatement stmt, TextMessage entity) { stmt.bindLong(1, id); } stmt.bindString(2, entity.getDeviceId()); - stmt.bindLong(3, timeSentConverter.convertToDatabaseValue(entity.getTimeSent())); - stmt.bindString(4, entity.getText()); + + String studentId = entity.getStudentId(); + if (studentId != null) { + stmt.bindString(3, studentId); + } + + String studentAvatar = entity.getStudentAvatar(); + if (studentAvatar != null) { + stmt.bindString(4, studentAvatar); + } + stmt.bindLong(5, timeSentConverter.convertToDatabaseValue(entity.getTimeSent())); + stmt.bindString(6, entity.getText()); } @Override @@ -91,32 +115,36 @@ public Long readKey(Cursor cursor, int offset) { } @Override - public TextMessage readEntity(Cursor cursor, int offset) { - TextMessage entity = new TextMessage( // + public Message readEntity(Cursor cursor, int offset) { + Message entity = new Message( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.getString(offset + 1), // deviceId - timeSentConverter.convertToEntityProperty(cursor.getLong(offset + 2)), // timeSent - cursor.getString(offset + 3) // text + cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // studentId + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // studentAvatar + timeSentConverter.convertToEntityProperty(cursor.getLong(offset + 4)), // timeSent + cursor.getString(offset + 5) // text ); return entity; } @Override - public void readEntity(Cursor cursor, TextMessage entity, int offset) { + public void readEntity(Cursor cursor, Message entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setDeviceId(cursor.getString(offset + 1)); - entity.setTimeSent(timeSentConverter.convertToEntityProperty(cursor.getLong(offset + 2))); - entity.setText(cursor.getString(offset + 3)); + entity.setStudentId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2)); + entity.setStudentAvatar(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); + entity.setTimeSent(timeSentConverter.convertToEntityProperty(cursor.getLong(offset + 4))); + entity.setText(cursor.getString(offset + 5)); } @Override - protected final Long updateKeyAfterInsert(TextMessage entity, long rowId) { + protected final Long updateKeyAfterInsert(Message entity, long rowId) { entity.setId(rowId); return rowId; } @Override - public Long getKey(TextMessage entity) { + public Long getKey(Message entity) { if(entity != null) { return entity.getId(); } else { @@ -124,6 +152,11 @@ public Long getKey(TextMessage entity) { } } + @Override + public boolean hasKey(Message entity) { + return entity.getId() != null; + } + @Override protected final boolean isEntityUpdateable() { return true; diff --git a/app/src/main/java/org/literacyapp/chat/model/Message.java b/app/src/main/java/org/literacyapp/chat/model/Message.java index d259cf0..30d506f 100644 --- a/app/src/main/java/org/literacyapp/chat/model/Message.java +++ b/app/src/main/java/org/literacyapp/chat/model/Message.java @@ -1,13 +1,15 @@ package org.literacyapp.chat.model; import org.greenrobot.greendao.annotation.Convert; +import org.greenrobot.greendao.annotation.Entity; +import org.greenrobot.greendao.annotation.Generated; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.NotNull; import org.literacyapp.chat.dao.converter.CalendarConverter; import java.util.Calendar; -@Deprecated +@Entity public class Message { @Id(autoincrement = true) @@ -16,12 +18,35 @@ public class Message { @NotNull private String deviceId; + private String studentId; + + private String studentAvatar; + @NotNull @Convert(converter = CalendarConverter.class, columnType = Long.class) private Calendar timeSent; + @NotNull + private String text; + + @Generated(hash = 449884345) + public Message(Long id, @NotNull String deviceId, String studentId, + String studentAvatar, @NotNull Calendar timeSent, + @NotNull String text) { + this.id = id; + this.deviceId = deviceId; + this.studentId = studentId; + this.studentAvatar = studentAvatar; + this.timeSent = timeSent; + this.text = text; + } + + @Generated(hash = 637306882) + public Message() { + } + public Long getId() { - return id; + return this.id; } public void setId(Long id) { @@ -29,18 +54,42 @@ public void setId(Long id) { } public String getDeviceId() { - return deviceId; + return this.deviceId; } public void setDeviceId(String deviceId) { this.deviceId = deviceId; } + public String getStudentId() { + return this.studentId; + } + + public void setStudentId(String studentId) { + this.studentId = studentId; + } + + public String getStudentAvatar() { + return this.studentAvatar; + } + + public void setStudentAvatar(String studentAvatar) { + this.studentAvatar = studentAvatar; + } + public Calendar getTimeSent() { - return timeSent; + return this.timeSent; } public void setTimeSent(Calendar timeSent) { this.timeSent = timeSent; } + + public String getText() { + return this.text; + } + + public void setText(String text) { + this.text = text; + } } diff --git a/app/src/main/java/org/literacyapp/chat/model/TextMessage.java b/app/src/main/java/org/literacyapp/chat/model/TextMessage.java deleted file mode 100644 index 8182191..0000000 --- a/app/src/main/java/org/literacyapp/chat/model/TextMessage.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.literacyapp.chat.model; - -import org.greenrobot.greendao.annotation.Convert; -import org.greenrobot.greendao.annotation.Entity; -import org.greenrobot.greendao.annotation.Generated; -import org.greenrobot.greendao.annotation.Id; -import org.greenrobot.greendao.annotation.NotNull; -import org.literacyapp.chat.dao.converter.CalendarConverter; - -import java.util.Calendar; - -@Entity -public class TextMessage /*extends Message*/ { - - @Id(autoincrement = true) - private Long id; - - @NotNull - private String deviceId; - - @NotNull - @Convert(converter = CalendarConverter.class, columnType = Long.class) - private Calendar timeSent; - - - @NotNull - private String text; - - @Generated(hash = 394821772) - public TextMessage(Long id, @NotNull String deviceId, - @NotNull Calendar timeSent, @NotNull String text) { - this.id = id; - this.deviceId = deviceId; - this.timeSent = timeSent; - this.text = text; - } - - @Generated(hash = 1667976043) - public TextMessage() { - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDeviceId() { - return deviceId; - } - - public void setDeviceId(String deviceId) { - this.deviceId = deviceId; - } - - public Calendar getTimeSent() { - return timeSent; - } - - public void setTimeSent(Calendar timeSent) { - this.timeSent = timeSent; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } -} diff --git a/app/src/main/res/layout/activity_chat.xml b/app/src/main/res/layout/activity_chat.xml index c630dff..2b3593f 100644 --- a/app/src/main/res/layout/activity_chat.xml +++ b/app/src/main/res/layout/activity_chat.xml @@ -38,7 +38,7 @@ android:paddingLeft="@dimen/activity_horizontal_margin" android:background="#FFF"> -