Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
2bec0df7
Commit
2bec0df7
authored
Jan 14, 2017
by
Yusuke Iwaki
Committed by
GitHub
Jan 14, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #146 from RocketChat/feature/message-list-diff-util
Using diff util to update the list
parents
094011b7
ae5211eb
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
131 additions
and
3 deletions
+131
-3
ExtRealmModelListAdapter.java
...rocket/android/layouthelper/ExtRealmModelListAdapter.java
+28
-0
MessageListAdapter.java
...ket/android/layouthelper/chatroom/MessageListAdapter.java
+50
-0
PairedMessage.java
...t/rocket/android/layouthelper/chatroom/PairedMessage.java
+12
-0
Message.java
app/src/main/java/chat/rocket/android/model/ddp/Message.java
+16
-0
User.java
app/src/main/java/chat/rocket/android/model/ddp/User.java
+12
-0
RealmModelListAdapter.java
...at/rocket/android/realm_helper/RealmModelListAdapter.java
+13
-3
No files found.
app/src/main/java/chat/rocket/android/layouthelper/ExtRealmModelListAdapter.java
View file @
2bec0df7
...
@@ -2,6 +2,7 @@ package chat.rocket.android.layouthelper;
...
@@ -2,6 +2,7 @@ package chat.rocket.android.layouthelper;
import
android.content.Context
;
import
android.content.Context
;
import
android.support.annotation.LayoutRes
;
import
android.support.annotation.LayoutRes
;
import
android.support.v7.util.ListUpdateCallback
;
import
io.realm.RealmObject
;
import
io.realm.RealmObject
;
import
chat.rocket.android.realm_helper.RealmModelListAdapter
;
import
chat.rocket.android.realm_helper.RealmModelListAdapter
;
...
@@ -37,6 +38,28 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
...
@@ -37,6 +38,28 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
notifyItemChanged
(
position
+
1
);
notifyItemChanged
(
position
+
1
);
}
}
protected
ListUpdateCallback
listUpdateCallback
=
new
ListUpdateCallback
()
{
@Override
public
void
onInserted
(
int
position
,
int
count
)
{
notifyItemRangeInserted
(
position
+
1
,
count
);
}
@Override
public
void
onRemoved
(
int
position
,
int
count
)
{
notifyItemRangeRemoved
(
position
+
1
,
count
);
}
@Override
public
void
onMoved
(
int
fromPosition
,
int
toPosition
)
{
notifyItemMoved
(
fromPosition
+
1
,
toPosition
+
1
);
}
@Override
public
void
onChanged
(
int
position
,
int
count
,
Object
payload
)
{
notifyItemRangeChanged
(
position
+
1
,
count
,
payload
);
}
};
@Override
@Override
public
int
getItemViewType
(
int
position
)
{
public
int
getItemViewType
(
int
position
)
{
if
(
position
==
0
)
{
if
(
position
==
0
)
{
...
@@ -83,4 +106,9 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
...
@@ -83,4 +106,9 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
// rely on VH.bind().
// rely on VH.bind().
super
.
onBindViewHolder
(
holder
,
position
-
1
);
super
.
onBindViewHolder
(
holder
,
position
-
1
);
}
}
@Override
public
ListUpdateCallback
getListUpdateCallback
()
{
return
listUpdateCallback
;
}
}
}
app/src/main/java/chat/rocket/android/layouthelper/chatroom/MessageListAdapter.java
View file @
2bec0df7
package
chat
.
rocket
.
android
.
layouthelper
.
chatroom
;
package
chat
.
rocket
.
android
.
layouthelper
.
chatroom
;
import
android.content.Context
;
import
android.content.Context
;
import
android.support.v7.util.DiffUtil
;
import
android.view.View
;
import
android.view.View
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
...
@@ -111,4 +112,53 @@ public class MessageListAdapter
...
@@ -111,4 +112,53 @@ public class MessageListAdapter
return
extMessages
;
return
extMessages
;
}
}
@Override
protected
DiffUtil
.
Callback
getDiffCallback
(
List
<
PairedMessage
>
oldData
,
List
<
PairedMessage
>
newData
)
{
return
new
PairedMessageDiffCallback
(
oldData
,
newData
);
}
private
static
class
PairedMessageDiffCallback
extends
DiffUtil
.
Callback
{
private
final
List
<
PairedMessage
>
oldList
;
private
final
List
<
PairedMessage
>
newList
;
public
PairedMessageDiffCallback
(
List
<
PairedMessage
>
oldList
,
List
<
PairedMessage
>
newList
)
{
this
.
oldList
=
oldList
;
this
.
newList
=
newList
;
}
@Override
public
int
getOldListSize
()
{
if
(
oldList
==
null
)
{
return
0
;
}
return
oldList
.
size
();
}
@Override
public
int
getNewListSize
()
{
if
(
newList
==
null
)
{
return
0
;
}
return
newList
.
size
();
}
@Override
public
boolean
areItemsTheSame
(
int
oldItemPosition
,
int
newItemPosition
)
{
PairedMessage
oldMessage
=
oldList
.
get
(
oldItemPosition
);
PairedMessage
newMessage
=
newList
.
get
(
newItemPosition
);
return
oldMessage
.
getId
().
equals
(
newMessage
.
getId
());
}
@Override
public
boolean
areContentsTheSame
(
int
oldItemPosition
,
int
newItemPosition
)
{
PairedMessage
oldMessage
=
oldList
.
get
(
oldItemPosition
);
PairedMessage
newMessage
=
newList
.
get
(
newItemPosition
);
return
oldMessage
.
toString
().
equals
(
newMessage
.
toString
());
}
}
}
}
app/src/main/java/chat/rocket/android/layouthelper/chatroom/PairedMessage.java
View file @
2bec0df7
...
@@ -32,4 +32,16 @@ public class PairedMessage {
...
@@ -32,4 +32,16 @@ public class PairedMessage {
&&
nextSibling
.
getUser
()
!=
null
&&
target
.
getUser
()
!=
null
&&
nextSibling
.
getUser
()
!=
null
&&
target
.
getUser
()
!=
null
&&
nextSibling
.
getUser
().
getId
().
equals
(
target
.
getUser
().
getId
());
&&
nextSibling
.
getUser
().
getId
().
equals
(
target
.
getUser
().
getId
());
}
}
public
String
getId
()
{
return
target
.
getId
();
}
@Override
public
String
toString
()
{
return
"PairedMessage{"
+
"target="
+
target
+
", nextSibling="
+
nextSibling
+
'}'
;
}
}
}
app/src/main/java/chat/rocket/android/model/ddp/Message.java
View file @
2bec0df7
...
@@ -131,4 +131,20 @@ public class Message extends RealmObject {
...
@@ -131,4 +131,20 @@ public class Message extends RealmObject {
public
void
setUrls
(
String
urls
)
{
public
void
setUrls
(
String
urls
)
{
this
.
urls
=
urls
;
this
.
urls
=
urls
;
}
}
@Override
public
String
toString
()
{
return
"Message{"
+
"_id='"
+
_id
+
'\''
+
", t='"
+
t
+
'\''
+
", rid='"
+
rid
+
'\''
+
", syncstate="
+
syncstate
+
", ts="
+
ts
+
", msg='"
+
msg
+
'\''
+
", u="
+
u
+
", groupable="
+
groupable
+
", attachments='"
+
attachments
+
'\''
+
", urls='"
+
urls
+
'\''
+
'}'
;
}
}
}
app/src/main/java/chat/rocket/android/model/ddp/User.java
View file @
2bec0df7
...
@@ -79,4 +79,16 @@ public class User extends RealmObject {
...
@@ -79,4 +79,16 @@ public class User extends RealmObject {
public
Settings
getSettings
()
{
public
Settings
getSettings
()
{
return
settings
;
return
settings
;
}
}
@Override
public
String
toString
()
{
return
"User{"
+
"_id='"
+
_id
+
'\''
+
", username='"
+
username
+
'\''
+
", status='"
+
status
+
'\''
+
", utcOffset="
+
utcOffset
+
", emails="
+
emails
+
", settings="
+
settings
+
'}'
;
}
}
}
realm-helpers/src/main/java/chat/rocket/android/realm_helper/RealmModelListAdapter.java
View file @
2bec0df7
...
@@ -2,6 +2,8 @@ package chat.rocket.android.realm_helper;
...
@@ -2,6 +2,8 @@ package chat.rocket.android.realm_helper;
import
android.content.Context
;
import
android.content.Context
;
import
android.support.annotation.LayoutRes
;
import
android.support.annotation.LayoutRes
;
import
android.support.v7.util.DiffUtil
;
import
android.support.v7.util.ListUpdateCallback
;
import
android.support.v7.widget.RecyclerView
;
import
android.support.v7.widget.RecyclerView
;
import
android.view.LayoutInflater
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.View
;
...
@@ -89,12 +91,20 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
...
@@ -89,12 +91,20 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
adapterData
=
mapResultsToViewModel
(
newData
);
adapterData
=
mapResultsToViewModel
(
newData
);
notifyDataSetChanged
();
notifyDataSetChanged
();
}
else
{
}
else
{
// TODO: use DillUtils!
final
List
<
VM
>
newMappedData
=
mapResultsToViewModel
(
newData
);
adapterData
=
mapResultsToViewModel
(
newData
);
final
DiffUtil
.
Callback
diffCallback
=
getDiffCallback
(
adapterData
,
newMappedData
);
notifyDataSetChanged
();
final
DiffUtil
.
DiffResult
diffResult
=
DiffUtil
.
calculateDiff
(
diffCallback
);
adapterData
=
newMappedData
;
diffResult
.
dispatchUpdatesTo
(
getListUpdateCallback
());
}
}
}
}
protected
abstract
DiffUtil
.
Callback
getDiffCallback
(
List
<
VM
>
oldData
,
List
<
VM
>
newData
);
protected
abstract
ListUpdateCallback
getListUpdateCallback
();
public
void
setOnItemClickListener
(
OnItemClickListener
<
VM
>
onItemClickListener
)
{
public
void
setOnItemClickListener
(
OnItemClickListener
<
VM
>
onItemClickListener
)
{
this
.
onItemClickListener
=
onItemClickListener
;
this
.
onItemClickListener
=
onItemClickListener
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment