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
4602bd00
Commit
4602bd00
authored
Jan 12, 2017
by
Tiago Cunha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using diff util to update the list
parent
47d10642
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
143 additions
and
3 deletions
+143
-3
RoomFragment.java
...a/chat/rocket/android/fragment/chatroom/RoomFragment.java
+1
-0
ExtRealmModelListAdapter.java
...rocket/android/layouthelper/ExtRealmModelListAdapter.java
+38
-0
MessageListAdapter.java
...ket/android/layouthelper/chatroom/MessageListAdapter.java
+51
-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/fragment/chatroom/RoomFragment.java
View file @
4602bd00
...
...
@@ -145,6 +145,7 @@ public class RoomFragment extends AbstractChatRoomFragment
LinearLayoutManager
layoutManager
=
new
LinearLayoutManager
(
getContext
(),
LinearLayoutManager
.
VERTICAL
,
true
);
listView
.
setLayoutManager
(
layoutManager
);
adapter
.
setLayoutManager
(
layoutManager
);
scrollListener
=
new
LoadMoreScrollListener
(
layoutManager
,
40
)
{
@Override
...
...
app/src/main/java/chat/rocket/android/layouthelper/ExtRealmModelListAdapter.java
View file @
4602bd00
...
...
@@ -2,6 +2,8 @@ package chat.rocket.android.layouthelper;
import
android.content.Context
;
import
android.support.annotation.LayoutRes
;
import
android.support.v7.util.ListUpdateCallback
;
import
android.support.v7.widget.RecyclerView
;
import
io.realm.RealmObject
;
import
chat.rocket.android.realm_helper.RealmModelListAdapter
;
...
...
@@ -16,6 +18,8 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
protected
static
final
int
VIEW_TYPE_HEADER
=
-
1
;
protected
static
final
int
VIEW_TYPE_FOOTER
=
-
2
;
private
RecyclerView
.
LayoutManager
layoutManager
;
protected
ExtRealmModelListAdapter
(
Context
context
)
{
super
(
context
);
}
...
...
@@ -37,6 +41,35 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
notifyItemChanged
(
position
+
1
);
}
public
void
setLayoutManager
(
RecyclerView
.
LayoutManager
layoutManager
)
{
this
.
layoutManager
=
layoutManager
;
}
protected
ListUpdateCallback
listUpdateCallback
=
new
ListUpdateCallback
()
{
@Override
public
void
onInserted
(
int
position
,
int
count
)
{
notifyItemRangeInserted
(
position
+
1
,
count
);
if
(
layoutManager
!=
null
)
{
layoutManager
.
scrollToPosition
(
0
);
}
}
@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
public
int
getItemViewType
(
int
position
)
{
if
(
position
==
0
)
{
...
...
@@ -83,4 +116,9 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
// rely on VH.bind().
super
.
onBindViewHolder
(
holder
,
position
-
1
);
}
@Override
public
ListUpdateCallback
getListUpdateCallback
()
{
return
listUpdateCallback
;
}
}
app/src/main/java/chat/rocket/android/layouthelper/chatroom/MessageListAdapter.java
View file @
4602bd00
package
chat
.
rocket
.
android
.
layouthelper
.
chatroom
;
import
android.content.Context
;
import
android.support.v7.util.DiffUtil
;
import
android.support.v7.widget.RecyclerView
;
import
android.view.View
;
import
java.util.ArrayList
;
...
...
@@ -111,4 +113,53 @@ public class MessageListAdapter
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 @
4602bd00
...
...
@@ -32,4 +32,16 @@ public class PairedMessage {
&&
nextSibling
.
getUser
()
!=
null
&&
target
.
getUser
()
!=
null
&&
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 @
4602bd00
...
...
@@ -131,4 +131,20 @@ public class Message extends RealmObject {
public
void
setUrls
(
String
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 @
4602bd00
...
...
@@ -79,4 +79,16 @@ public class User extends RealmObject {
public
Settings
getSettings
()
{
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 @
4602bd00
...
...
@@ -2,6 +2,8 @@ package chat.rocket.android.realm_helper;
import
android.content.Context
;
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.view.LayoutInflater
;
import
android.view.View
;
...
...
@@ -89,12 +91,20 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
adapterData
=
mapResultsToViewModel
(
newData
);
notifyDataSetChanged
();
}
else
{
// TODO: use DillUtils!
adapterData
=
mapResultsToViewModel
(
newData
);
notifyDataSetChanged
();
final
List
<
VM
>
newMappedData
=
mapResultsToViewModel
(
newData
);
final
DiffUtil
.
Callback
diffCallback
=
getDiffCallback
(
adapterData
,
newMappedData
);
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
)
{
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