From d9c5d98b1bfc8c878070bc3b4242bbd1b4f35fff Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Wed, 21 Feb 2018 19:43:36 +0100
Subject: [PATCH] Reorganized code for readability

---
 app/build.gradle.kts                          | 100 +++++++++---------
 app/proguard-rules.pro                        |   4 +-
 .../quasseldroid_ng/ui/chat/ChatActivity.kt   |  18 ++++
 .../ui/chat/MessageListFragment.kt            |  11 +-
 .../ui/settings/data/BacklogSettings.kt       |   5 +
 app/src/main/res/menu/activity_main.xml       |   3 +
 buildSrc/build.gradle.kts                     |   5 +
 buildSrc/src/main/kotlin/VersionContext.kt    |   5 +
 lib/build.gradle.kts                          |  13 +--
 9 files changed, 96 insertions(+), 68 deletions(-)
 create mode 100644 app/src/main/java/de/kuschku/quasseldroid_ng/ui/settings/data/BacklogSettings.kt
 create mode 100644 buildSrc/build.gradle.kts
 create mode 100644 buildSrc/src/main/kotlin/VersionContext.kt

diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 2831cb257..3f7ec2d3b 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -88,52 +88,71 @@ android {
   }
 }
 
-val appCompatVersion = "27.0.2"
-val appArchVersion = "1.1.0"
 dependencies {
   implementation(kotlin("stdlib", "1.2.21"))
 
-  implementation(appCompat("appcompat-v7"))
-  implementation(appCompat("design"))
-  implementation(appCompat("customtabs"))
-  implementation(appCompat("cardview-v7"))
-  implementation(appCompat("recyclerview-v7"))
-  implementation(appCompat("constraint", "constraint-layout", version = "1.0.2"))
-
-  implementation("io.reactivex.rxjava2", "rxjava", "2.1.9")
+  // App Compat
+  withVersion("27.0.2") {
+    implementation("com.android.support", "appcompat-v7", version)
+    implementation("com.android.support", "design", version)
+    implementation("com.android.support", "customtabs", version)
+    implementation("com.android.support", "cardview-v7", version)
+    implementation("com.android.support", "recyclerview-v7", version)
+  }
+  implementation("com.android.support.constraint", "constraint-layout", "1.0.2")
 
-  implementation(appArch("lifecycle", "extensions"))
-  implementation(appArch("lifecycle", "reactivestreams"))
-  kapt(appArch("lifecycle", "compiler"))
+  // App Arch Lifecycle
+  withVersion("1.1.0") {
+    implementation("android.arch.lifecycle", "extensions", version)
+    implementation("android.arch.lifecycle", "reactivestreams", version)
+    kapt("android.arch.lifecycle", "compiler", version)
+  }
 
-  implementation(appArch("persistence.room", "runtime", "1.1.0-alpha2"))
-  implementation(appArch("persistence.room", "rxjava2", "1.1.0-alpha2"))
-  kapt(appArch("persistence.room", "compiler", "1.1.0-alpha2"))
+  // App Arch Persistence
+  withVersion("1.1.0-alpha2") {
+    implementation("android.arch.persistence.room", "runtime", version)
+    implementation("android.arch.persistence.room", "rxjava2", version)
+    kapt("android.arch.persistence.room", "compiler", version)
+    testImplementation("android.arch.persistence.room", "testing", version)
+  }
 
-  implementation(appArch("paging", "runtime", version = "1.0.0-alpha5")) {
+  // App Arch Paging
+  implementation("android.arch.paging", "runtime", "1.0.0-alpha5") {
     exclude(group = "junit", module = "junit")
   }
 
-  implementation("me.zhanghai.android.materialprogressbar", "library", "1.4.2")
-
+  // Utility
+  implementation("io.reactivex.rxjava2", "rxjava", "2.1.9")
   implementation("org.threeten", "threetenbp", "1.3.6", classifier = "no-tzdb")
+  implementation("org.jetbrains", "annotations", "15.0")
+  withVersion("2.14.1") {
+    implementation("com.google.dagger", "dagger-android", version)
+    implementation("com.google.dagger", "dagger-android-support", version)
 
-  implementation("com.jakewharton", "butterknife", "8.8.1")
-  kapt("com.jakewharton", "butterknife-compiler", "8.8.1")
+    kapt("com.google.dagger", "dagger-android-processor", version)
+    kapt("com.google.dagger", "dagger-compiler", version)
+  }
+  withVersion("8.8.1") {
+    implementation("com.jakewharton", "butterknife", version)
+    kapt("com.jakewharton", "butterknife-compiler", version)
+  }
 
+  // Quassel
   implementation(project(":lib")) {
     exclude(group = "org.threeten", module = "threetenbp")
   }
-  implementation(project(":malheur"))
 
-  implementation("org.jetbrains", "annotations", "15.0")
-
-  testImplementation(appArch("persistence.room", "testing", "1.0.0"))
-  testImplementation("junit", "junit", "4.12")
+  // UI
+  implementation("me.zhanghai.android.materialprogressbar", "library", "1.4.2")
 
-  androidTestImplementation("com.android.support.test", "runner", "1.0.1")
-  androidTestImplementation("com.android.support.test", "rules", "1.0.1")
+  // Quality Assurance
+  implementation(project(":malheur"))
 
+  testImplementation("junit", "junit", "4.12")
+  withVersion("1.0.1") {
+    androidTestImplementation("com.android.support.test", "runner", version)
+    androidTestImplementation("com.android.support.test", "rules", version)
+  }
   androidTestImplementation("com.android.support.test.espresso", "espresso-core", "3.0.1")
 }
 
@@ -167,27 +186,4 @@ fun Project.properties(fileName: String): Properties? {
   val props = Properties()
   props.load(file.inputStream())
   return props
-}
-
-/**
- * Builds the dependency notation for the named AppCompat [module] at the given [version].
- *
- * @param module simple name of the AppCompat module, for example "cardview-v7".
- * @param version optional desired version, null implies [appCompatVersion].
- */
-fun appCompat(module: String, submodule: String? = null, version: String? = null)
-  = if (submodule != null) {
-  "com.android.support.$module:$submodule:${version ?: appCompatVersion}"
-} else {
-  "com.android.support:$module:${version ?: appCompatVersion}"
-}
-
-/**
- * Builds the dependency notation for the named AppArch [module] at the given [version].
- *
- * @param module simple name of the AppArch module, for example "persistence.room".
- * @param submodule simple name of the AppArch submodule, for example "runtime".
- * @param version optional desired version, null implies [appCompatVersion].
- */
-fun appArch(module: String, submodule: String, version: String? = null)
-  = "android.arch.$module:$submodule:${version ?: appArchVersion}"
+}
\ No newline at end of file
diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro
index 10e5e5f1b..a6839415a 100644
--- a/app/proguard-rules.pro
+++ b/app/proguard-rules.pro
@@ -39,4 +39,6 @@
 # Kotlin stuff
 -dontnote kotlin.**
 # Gson
--dontnote com.google.gson.**
\ No newline at end of file
+-dontnote com.google.gson.**
+# Dagger
+-dontwarn com.google.errorprone.annotations.*
\ No newline at end of file
diff --git a/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/ChatActivity.kt b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/ChatActivity.kt
index c62795a30..6d22c4ca9 100644
--- a/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/ChatActivity.kt
+++ b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/ChatActivity.kt
@@ -22,6 +22,7 @@ import de.kuschku.quasseldroid_ng.Keys
 import de.kuschku.quasseldroid_ng.R
 import de.kuschku.quasseldroid_ng.persistence.AccountDatabase
 import de.kuschku.quasseldroid_ng.persistence.QuasselDatabase
+import de.kuschku.quasseldroid_ng.ui.settings.data.BacklogSettings
 import de.kuschku.quasseldroid_ng.ui.viewmodel.QuasselViewModel
 import de.kuschku.quasseldroid_ng.util.AndroidHandlerThread
 import de.kuschku.quasseldroid_ng.util.helper.*
@@ -54,6 +55,8 @@ class ChatActivity : ServiceBoundActivity() {
 
   private lateinit var database: QuasselDatabase
 
+  private var backlogSettings = BacklogSettings()
+
   override fun onCreate(savedInstanceState: Bundle?) {
     handler.onCreate()
     super.onCreate(savedInstanceState)
@@ -177,6 +180,21 @@ class ChatActivity : ServiceBoundActivity() {
     android.R.id.home -> {
       drawerToggle.onOptionsItemSelected(item)
     }
+    R.id.clear        -> {
+      handler.post {
+        viewModel.sessionManager { manager ->
+          viewModel.getBuffer().let { buffer ->
+            manager.backlogStorage.clearMessages(buffer)
+            manager.backlogManager?.requestBacklog(
+              bufferId = buffer,
+              last = -1,
+              limit = backlogSettings.dynamicAmount
+            )
+          }
+        }
+      }
+      true
+    }
     R.id.disconnect   -> {
       handler.post {
         getSharedPreferences(Keys.Status.NAME, Context.MODE_PRIVATE).editApply {
diff --git a/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/MessageListFragment.kt b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/MessageListFragment.kt
index 34ab9c155..1861c7a24 100644
--- a/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/MessageListFragment.kt
+++ b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/chat/MessageListFragment.kt
@@ -18,6 +18,7 @@ import de.kuschku.libquassel.protocol.MsgId
 import de.kuschku.libquassel.quassel.syncables.BufferSyncer
 import de.kuschku.quasseldroid_ng.R
 import de.kuschku.quasseldroid_ng.persistence.QuasselDatabase
+import de.kuschku.quasseldroid_ng.ui.settings.data.BacklogSettings
 import de.kuschku.quasseldroid_ng.ui.viewmodel.QuasselViewModel
 import de.kuschku.quasseldroid_ng.util.AndroidHandlerThread
 import de.kuschku.quasseldroid_ng.util.helper.*
@@ -41,6 +42,8 @@ class MessageListFragment : ServiceBoundFragment() {
 
   private var lastBuffer: BufferId? = null
 
+  private var backlogSettings = BacklogSettings()
+
   override fun onCreate(savedInstanceState: Bundle?) {
     handler.onCreate()
     super.onCreate(savedInstanceState)
@@ -84,9 +87,9 @@ class MessageListFragment : ServiceBoundFragment() {
       LivePagedListBuilder(
         database.message().findByBufferIdPaged(it),
         PagedList.Config.Builder()
-          .setPageSize(20)
-          .setPrefetchDistance(20)
-          .setInitialLoadSizeHint(20)
+          .setPageSize(backlogSettings.dynamicAmount)
+          .setPrefetchDistance(backlogSettings.dynamicAmount)
+          .setInitialLoadSizeHint(backlogSettings.dynamicAmount)
           .setEnablePlaceholders(true)
           .build()
       ).setBoundaryCallback(boundaryCallback).build()
@@ -174,7 +177,7 @@ class MessageListFragment : ServiceBoundFragment() {
           last = database.message().findFirstByBufferId(
             bufferId
           )?.messageId ?: -1,
-          limit = 20
+          limit = backlogSettings.dynamicAmount
         )
       }
     }
diff --git a/app/src/main/java/de/kuschku/quasseldroid_ng/ui/settings/data/BacklogSettings.kt b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/settings/data/BacklogSettings.kt
new file mode 100644
index 000000000..f72172969
--- /dev/null
+++ b/app/src/main/java/de/kuschku/quasseldroid_ng/ui/settings/data/BacklogSettings.kt
@@ -0,0 +1,5 @@
+package de.kuschku.quasseldroid_ng.ui.settings.data
+
+data class BacklogSettings(
+  val dynamicAmount: Int = 20
+)
\ No newline at end of file
diff --git a/app/src/main/res/menu/activity_main.xml b/app/src/main/res/menu/activity_main.xml
index 2079987d4..3c59fa43c 100644
--- a/app/src/main/res/menu/activity_main.xml
+++ b/app/src/main/res/menu/activity_main.xml
@@ -1,5 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
+  <item
+    android:id="@+id/clear"
+    android:title="Clear Backlog" />
   <item
     android:id="@+id/settings"
     android:title="Settings" />
diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts
new file mode 100644
index 000000000..d5c75922b
--- /dev/null
+++ b/buildSrc/build.gradle.kts
@@ -0,0 +1,5 @@
+import org.gradle.kotlin.dsl.`kotlin-dsl`
+
+plugins {
+  `kotlin-dsl`
+}
\ No newline at end of file
diff --git a/buildSrc/src/main/kotlin/VersionContext.kt b/buildSrc/src/main/kotlin/VersionContext.kt
new file mode 100644
index 000000000..288212239
--- /dev/null
+++ b/buildSrc/src/main/kotlin/VersionContext.kt
@@ -0,0 +1,5 @@
+data class VersionContext(val version: String)
+
+inline fun withVersion(version: String, f: VersionContext.() -> Unit) {
+  VersionContext(version).f()
+}
\ No newline at end of file
diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts
index a7cc8e972..38f2d1c3a 100644
--- a/lib/build.gradle.kts
+++ b/lib/build.gradle.kts
@@ -13,14 +13,14 @@ val appCompatVersion = "27.0.2"
 dependencies {
   implementation(kotlin("stdlib", "1.2.21"))
 
-  implementation(appCompat("support-annotations"))
+  implementation("com.android.support", "support-annotations", "27.0.2")
   implementation("org.threeten", "threetenbp", "1.3.6")
   implementation("io.reactivex.rxjava2:rxjava:2.1.9")
 
   implementation(project(":invokerannotations"))
   kapt(project(":invokergenerator"))
 
-  testImplementation("junit:junit:4.12")
+  testImplementation("junit", "junit", "4.12")
 }
 
 /*
@@ -33,12 +33,3 @@ tasks.withType(KotlinCompile::class.java) {
   }
 }
 */
-
-/**
- * Builds the dependency notation for the named AppCompat [module] at the given [version].
- *
- * @param module simple name of the AppCompat module, for example "cardview-v7".
- * @param version optional desired version, null implies [appCompatVersion].
- */
-fun appCompat(module: String, version: String? = null)
-  = "com.android.support:$module:${version ?: appCompatVersion}"
-- 
GitLab