From a27d02c37a77518ec937747b5e5763cfaf08281b Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Sun, 28 Aug 2016 01:46:39 +0200
Subject: [PATCH] Fixed several minor issues, improved performance of daychange
 updating

---
 .../libquassel/localtypes/BacklogFilter.java  | 58 ++++++++++++++++---
 .../kuschku/libquassel/message/Message.java   | 20 ++++++-
 .../main/res/layout-w720dp/activity_main.xml  | 26 ++++-----
 app/src/main/res/layout/activity_main.xml     | 26 ++++-----
 4 files changed, 93 insertions(+), 37 deletions(-)

diff --git a/app/src/main/java/de/kuschku/libquassel/localtypes/BacklogFilter.java b/app/src/main/java/de/kuschku/libquassel/localtypes/BacklogFilter.java
index 681f413f1..1a0c4bc0c 100644
--- a/app/src/main/java/de/kuschku/libquassel/localtypes/BacklogFilter.java
+++ b/app/src/main/java/de/kuschku/libquassel/localtypes/BacklogFilter.java
@@ -23,11 +23,16 @@ package de.kuschku.libquassel.localtypes;
 
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
+import android.text.format.DateUtils;
+import android.util.Log;
 
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeUtils;
+import org.joda.time.LocalDate;
 
 import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Set;
 
 import de.greenrobot.event.EventBus;
@@ -35,6 +40,7 @@ import de.kuschku.libquassel.client.Client;
 import de.kuschku.libquassel.message.Message;
 import de.kuschku.libquassel.primitives.types.BufferInfo;
 import de.kuschku.libquassel.syncables.types.interfaces.QNetwork;
+import de.kuschku.util.backports.Objects;
 import de.kuschku.util.observables.callbacks.UICallback;
 import de.kuschku.util.observables.lists.ObservableComparableSortedList;
 
@@ -54,8 +60,6 @@ public class BacklogFilter implements UICallback {
     private final EventBus bus = new EventBus();
     @Nullable
     private CharSequence searchQuery;
-    @Nullable
-    private DateTime earliestMessage;
 
     public BacklogFilter(@NonNull Client client, int bufferId, @NonNull ObservableComparableSortedList<Message> unfiltered, @NonNull ObservableComparableSortedList<Message> filtered) {
         this.client = client;
@@ -64,6 +68,7 @@ public class BacklogFilter implements UICallback {
         this.filtered = filtered;
         this.bus.register(this);
         setFiltersInternal(client.metaDataManager().hiddendata(client.coreId(), bufferId));
+        updateDayChangeMessages();
     }
 
     @Override
@@ -73,11 +78,45 @@ public class BacklogFilter implements UICallback {
     }
 
     private void updateDayChangeMessages() {
-        DateTime now = DateTime.now().withMillisOfDay(0);
-        while (now.isAfter(earliestMessage)) {
+        LocalDate date = null;
+        Message lastMessage = null;
+        for (Message message : filtered) {
+            if (Objects.equals(date, message.getLocalDate()))
+                continue;
+            date = message.getLocalDate();
+            if (message.type == Message.Type.DayChange) {
+                if (lastMessage != null && lastMessage.type == Message.Type.DayChange) {
+                    bus.post(new MessageRemoveEvent(lastMessage));
+                }
+                lastMessage = message;
+                continue;
+            }
+
+            lastMessage = message;
+            date = message.getLocalDate();
+            DateTime time = message.time.withMillisOfDay(0);
             bus.post(new MessageInsertEvent(Message.create(
-                    (int) DateTimeUtils.toJulianDay(now.getMillis()),
-                    now,
+                    (int) DateTimeUtils.toJulianDay(time.getMillis()),
+                    time,
+                    Message.Type.DayChange,
+                    new Message.Flags(false, false, false, false, false),
+                    BufferInfo.create(
+                            bufferId,
+                            -1,
+                            BufferInfo.Type.INVALID,
+                            -1,
+                            null
+                    ),
+                    "",
+                    ""
+            )));
+        }
+        DateTime time = DateTime.now();
+        if (!Objects.equals(date, time.toLocalDate())) {
+            time = time.withMillisOfDay(0);
+            bus.post(new MessageInsertEvent(Message.create(
+                    (int) DateTimeUtils.toJulianDay(time.getMillis()),
+                    time,
                     Message.Type.DayChange,
                     new Message.Flags(false, false, false, false, false),
                     BufferInfo.create(
@@ -90,7 +129,6 @@ public class BacklogFilter implements UICallback {
                     "",
                     ""
             )));
-            now = now.minusDays(1);
         }
     }
 
@@ -141,17 +179,19 @@ public class BacklogFilter implements UICallback {
 
     public void onEventAsync(@NonNull MessageFilterEvent event) {
         if (!filterItem(event.msg)) bus.post(new MessageInsertEvent(event.msg));
-        if (event.msg.time.isBefore(earliestMessage)) earliestMessage = event.msg.time;
-        updateDayChangeMessages();
     }
 
     public void onEventMainThread(@NonNull MessageInsertEvent event) {
         filtered.add(event.msg);
         client.bufferSyncer().addActivity(event.msg);
+        if (event.msg.type != Message.Type.DayChange)
+            updateDayChangeMessages();
     }
 
     public void onEventMainThread(@NonNull MessageRemoveEvent event) {
         filtered.remove(event.msg);
+        if (event.msg.type != Message.Type.DayChange)
+            updateDayChangeMessages();
     }
 
     @Override
diff --git a/app/src/main/java/de/kuschku/libquassel/message/Message.java b/app/src/main/java/de/kuschku/libquassel/message/Message.java
index a95e8daf1..5b99b6952 100644
--- a/app/src/main/java/de/kuschku/libquassel/message/Message.java
+++ b/app/src/main/java/de/kuschku/libquassel/message/Message.java
@@ -32,6 +32,7 @@ import com.raizlabs.android.dbflow.annotation.Table;
 import com.raizlabs.android.dbflow.structure.BaseModel;
 
 import org.joda.time.DateTime;
+import org.joda.time.LocalDate;
 
 import java.io.Serializable;
 import java.util.Comparator;
@@ -63,6 +64,14 @@ public class Message extends BaseModel implements ContentComparable<Message> {
     @Column
     public String content;
 
+    public LocalDate date;
+
+    public LocalDate getLocalDate() {
+        if (date == null)
+            date = time.toLocalDate();
+        return date;
+    }
+
     public static Message create(int id, DateTime time, Type type, Flags flags, BufferInfo bufferInfo, String sender, String content) {
         Message message = new Message();
         message.id = id;
@@ -91,12 +100,19 @@ public class Message extends BaseModel implements ContentComparable<Message> {
 
     @Override
     public boolean areContentsTheSame(@Nullable Message message) {
-        return this == message;
+        return message != null && this.id == message.id && this.sender.equals(message.sender) && this.type == message.type && this.time.equals(message.time) && this.content.equals(message.content) && this.flags == message.flags;
     }
 
     @Override
     public boolean areItemsTheSame(@NonNull Message other) {
-        return this.id == other.id;
+        if (this.type == Type.DayChange && other.type == Type.DayChange)
+            return this.bufferInfo.id == other.bufferInfo.id && this.time.equals(other.time);
+        else if (this.type == Type.DayChange)
+            return false;
+        else if (other.type == Type.DayChange)
+            return false;
+        else
+            return this.id == other.id;
     }
 
     @Override
diff --git a/app/src/main/res/layout-w720dp/activity_main.xml b/app/src/main/res/layout-w720dp/activity_main.xml
index e12fab005..0ee2c2c72 100644
--- a/app/src/main/res/layout-w720dp/activity_main.xml
+++ b/app/src/main/res/layout-w720dp/activity_main.xml
@@ -35,16 +35,16 @@
         android:layout_height="match_parent"
         android:orientation="vertical">
 
-        <FrameLayout
+        <android.support.design.widget.AppBarLayout
+            xmlns:android="http://schemas.android.com/apk/res/android"
+            xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content">
+            android:layout_height="wrap_content"
+            android:theme="?attr/actionBarTheme">
 
-            <android.support.design.widget.AppBarLayout
-                xmlns:android="http://schemas.android.com/apk/res/android"
-                xmlns:app="http://schemas.android.com/apk/res-auto"
+            <FrameLayout
                 android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:theme="?attr/actionBarTheme">
+                android:layout_height="wrap_content">
 
                 <android.support.v7.widget.Toolbar
                     android:id="@+id/chatListToolbar"
@@ -61,14 +61,14 @@
 
                 </android.support.v7.widget.Toolbar>
 
-            </android.support.design.widget.AppBarLayout>
+                <ViewStub
+                    android:id="@+id/cab_stub"
+                    android:layout_width="match_parent"
+                    android:layout_height="?actionBarSize"/>
 
-            <ViewStub
-                android:id="@+id/cab_stub"
-                android:layout_width="match_parent"
-                android:layout_height="?actionBarSize"/>
+            </FrameLayout>
 
-        </FrameLayout>
+        </android.support.design.widget.AppBarLayout>
 
         <android.support.v7.widget.RecyclerView
             android:id="@+id/chatList"
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index bab2182d4..f18a25cff 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -54,16 +54,16 @@
             android:layout_height="match_parent"
             android:orientation="vertical">
 
-            <FrameLayout
+            <android.support.design.widget.AppBarLayout
+                xmlns:android="http://schemas.android.com/apk/res/android"
+                xmlns:app="http://schemas.android.com/apk/res-auto"
                 android:layout_width="match_parent"
-                android:layout_height="wrap_content">
+                android:layout_height="wrap_content"
+                android:theme="?attr/actionBarTheme">
 
-                <android.support.design.widget.AppBarLayout
-                    xmlns:android="http://schemas.android.com/apk/res/android"
-                    xmlns:app="http://schemas.android.com/apk/res-auto"
+                <FrameLayout
                     android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
-                    android:theme="?attr/actionBarTheme">
+                    android:layout_height="wrap_content">
 
                     <android.support.v7.widget.Toolbar
                         android:id="@+id/chatListToolbar"
@@ -82,14 +82,14 @@
 
                     </android.support.v7.widget.Toolbar>
 
-                </android.support.design.widget.AppBarLayout>
+                    <ViewStub
+                        android:id="@+id/cab_stub"
+                        android:layout_width="match_parent"
+                        android:layout_height="?actionBarSize"/>
 
-                <ViewStub
-                    android:id="@+id/cab_stub"
-                    android:layout_width="match_parent"
-                    android:layout_height="?actionBarSize"/>
+                </FrameLayout>
 
-            </FrameLayout>
+            </android.support.design.widget.AppBarLayout>
 
             <android.support.v7.widget.RecyclerView
                 android:id="@+id/chatList"
-- 
GitLab