From 0c60098cca1d3c0c42833787727eef008c98743b Mon Sep 17 00:00:00 2001
From: Janne Koschinski <janne@kuschku.de>
Date: Thu, 4 Apr 2019 16:42:59 +0200
Subject: [PATCH] Further work on channel creation UI

---
 .../chat/add/create/ChannelCreateFragment.kt  |  55 +++++++-
 .../ui/chat/add/create/NetworkAdapter.kt      |  88 ++++++++++++
 app/src/main/res/layout/add_create.xml        | 126 ++++++++++++++----
 app/src/main/res/values/strings_addchat.xml   |   9 ++
 4 files changed, 247 insertions(+), 31 deletions(-)
 create mode 100644 app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/NetworkAdapter.kt
 create mode 100644 app/src/main/res/values/strings_addchat.xml

diff --git a/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/ChannelCreateFragment.kt b/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/ChannelCreateFragment.kt
index 6b1075521..3422f5edb 100644
--- a/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/ChannelCreateFragment.kt
+++ b/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/ChannelCreateFragment.kt
@@ -23,17 +23,68 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.EditText
+import androidx.appcompat.widget.AppCompatSpinner
+import androidx.appcompat.widget.SwitchCompat
+import androidx.lifecycle.Observer
+import butterknife.BindView
 import butterknife.ButterKnife
+import de.kuschku.libquassel.quassel.syncables.Network
+import de.kuschku.libquassel.quassel.syncables.interfaces.INetwork
 import de.kuschku.quasseldroid.R
-import de.kuschku.quasseldroid.util.service.ServiceBoundFragment
+import de.kuschku.quasseldroid.util.helper.combineLatest
+import de.kuschku.quasseldroid.util.helper.setDependent
+import de.kuschku.quasseldroid.util.helper.toLiveData
+import de.kuschku.quasseldroid.util.ui.settings.fragment.Savable
+import de.kuschku.quasseldroid.util.ui.settings.fragment.ServiceBoundSettingsFragment
 
-class ChannelCreateFragment : ServiceBoundFragment() {
+class ChannelCreateFragment : ServiceBoundSettingsFragment(), Savable {
+  @BindView(R.id.network)
+  lateinit var network: AppCompatSpinner
+
+  @BindView(R.id.name)
+  lateinit var name: EditText
+
+  @BindView(R.id.hidden)
+  lateinit var hidden: SwitchCompat
+
+  @BindView(R.id.invite_only)
+  lateinit var inviteOnly: SwitchCompat
+
+  @BindView(R.id.password_protected)
+  lateinit var passwordProtected: SwitchCompat
+
+  @BindView(R.id.password_group)
+  lateinit var passwordGroup: ViewGroup
+
+  @BindView(R.id.password)
+  lateinit var password: EditText
 
   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                             savedInstanceState: Bundle?): View? {
     val view = inflater.inflate(R.layout.add_create, container, false)
     ButterKnife.bind(this, view)
 
+    val networkAdapter = NetworkAdapter()
+    network.adapter = networkAdapter
+
+    viewModel.networks.switchMap {
+      combineLatest(it.values.map(Network::liveNetworkInfo)).map {
+        it.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, INetwork.NetworkInfo::networkName))
+      }
+    }.toLiveData().observe(this, Observer {
+      if (it != null) {
+        networkAdapter.submitList(it)
+      }
+    })
+
+    passwordProtected.setDependent(passwordGroup)
+
     return view
   }
+
+  override fun onSave(): Boolean {
+    // TODO: Implement
+    return false
+  }
 }
diff --git a/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/NetworkAdapter.kt b/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/NetworkAdapter.kt
new file mode 100644
index 000000000..f1f5976cb
--- /dev/null
+++ b/app/src/main/java/de/kuschku/quasseldroid/ui/chat/add/create/NetworkAdapter.kt
@@ -0,0 +1,88 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2019 Janne Koschinski
+ * Copyright (c) 2019 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package de.kuschku.quasseldroid.ui.chat.add.create
+
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.TextView
+import androidx.appcompat.widget.ThemedSpinnerAdapter
+import androidx.recyclerview.widget.RecyclerView
+import butterknife.BindView
+import butterknife.ButterKnife
+import de.kuschku.libquassel.protocol.NetworkId
+import de.kuschku.libquassel.quassel.syncables.interfaces.INetwork
+import de.kuschku.quasseldroid.R
+import de.kuschku.quasseldroid.util.ui.ContextThemeWrapper
+import de.kuschku.quasseldroid.util.ui.RecyclerSpinnerAdapter
+
+class NetworkAdapter : RecyclerSpinnerAdapter<NetworkAdapter.NetworkViewHolder>(),
+                       ThemedSpinnerAdapter {
+  var data = listOf<INetwork.NetworkInfo>()
+
+  fun submitList(list: List<INetwork.NetworkInfo>) {
+    data = list
+    notifyDataSetChanged()
+  }
+
+  override fun isEmpty() = data.isEmpty()
+
+  override fun onBindViewHolder(holder: NetworkViewHolder, position: Int) =
+    holder.bind(getItem(position))
+
+  override fun onCreateViewHolder(parent: ViewGroup, dropDown: Boolean)
+    : NetworkViewHolder {
+    val inflater = LayoutInflater.from(
+      if (dropDown)
+        ContextThemeWrapper(parent.context, dropDownViewTheme)
+      else
+        parent.context
+    )
+    val view = inflater.inflate(R.layout.widget_spinner_item_material, parent, false)
+    return NetworkViewHolder(view)
+  }
+
+  fun indexOf(id: NetworkId): Int? {
+    for ((key, item) in data.withIndex()) {
+      if (item.networkId == id) {
+        return key
+      }
+    }
+    return null
+  }
+
+  override fun getItem(position: Int): INetwork.NetworkInfo = data[position]
+  override fun getItemId(position: Int) = getItem(position).networkId.id.toLong()
+  override fun hasStableIds() = true
+  override fun getCount() = data.size
+  class NetworkViewHolder(itemView: View) :
+    RecyclerView.ViewHolder(itemView) {
+    @BindView(android.R.id.text1)
+    lateinit var text: TextView
+
+    init {
+      ButterKnife.bind(this, itemView)
+    }
+
+    fun bind(network: INetwork.NetworkInfo) {
+      text.text = network.networkName
+    }
+  }
+}
diff --git a/app/src/main/res/layout/add_create.xml b/app/src/main/res/layout/add_create.xml
index 4aa14e116..94c0c9fd3 100644
--- a/app/src/main/res/layout/add_create.xml
+++ b/app/src/main/res/layout/add_create.xml
@@ -1,34 +1,102 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
+<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+  xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
-  android:layout_height="match_parent"
-  android:orientation="vertical"
-  android:padding="16dp">
-
-  <de.kuschku.ui.spinner.MaterialSpinnerLayout
-    style="@style/Widget.CustomSpinnerLayout"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:hint="@string/label_network">
-
-    <androidx.appcompat.widget.AppCompatSpinner
-      android:id="@+id/network"
-      style="@style/Widget.MaterialSpinner"
+  android:layout_height="match_parent">
+
+  <LinearLayout style="@style/Widget.CoreSettings.Wrapper">
+
+    <de.kuschku.ui.spinner.MaterialSpinnerLayout
+      style="@style/Widget.CustomSpinnerLayout"
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content"
+      android:hint="@string/label_network">
+
+      <androidx.appcompat.widget.AppCompatSpinner
+        android:id="@+id/network"
+        style="@style/Widget.MaterialSpinner"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        tools:listitem="@layout/widget_spinner_item_material" />
+    </de.kuschku.ui.spinner.MaterialSpinnerLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+      style="@style/Widget.CustomTextInput"
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content"
+      android:hint="@string/label_channel_name">
+
+      <com.google.android.material.textfield.TextInputEditText
+        android:id="@+id/name"
+        style="@style/Widget.CoreSettings.EditText"
+        tools:text="#trees" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <LinearLayout
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content"
+      android:minHeight="48dp">
+
+      <androidx.appcompat.widget.AppCompatImageView
+        style="@style/Widget.CoreSettings.PrimaryItemIcon"
+        app:srcCompat="@drawable/ic_eye_off" />
+
+      <androidx.appcompat.widget.SwitchCompat
+        android:id="@+id/hidden"
+        style="@style/Widget.CoreSettings.PrimaryItemSwitch"
+        android:text="@string/addchat_channel_hidden" />
+    </LinearLayout>
+
+    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
-      tools:listitem="@layout/widget_spinner_item_material" />
-  </de.kuschku.ui.spinner.MaterialSpinnerLayout>
-
-  <com.google.android.material.textfield.TextInputLayout
-    style="@style/Widget.CustomTextInput"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:hint="@string/settings_aliasitem_name">
-
-    <com.google.android.material.textfield.TextInputEditText
-      android:id="@+id/name"
-      style="@style/Widget.CoreSettings.EditText"
-      tools:text="back" />
-  </com.google.android.material.textfield.TextInputLayout>
-</LinearLayout>
+      android:minHeight="48dp">
+
+      <androidx.appcompat.widget.AppCompatImageView
+        style="@style/Widget.CoreSettings.PrimaryItemIcon"
+        app:srcCompat="@drawable/ic_account_card" />
+
+      <androidx.appcompat.widget.SwitchCompat
+        android:id="@+id/invite_only"
+        style="@style/Widget.CoreSettings.PrimaryItemSwitch"
+        android:text="@string/addchat_channel_invite_only" />
+    </LinearLayout>
+
+    <LinearLayout
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content"
+      android:minHeight="48dp">
+
+      <androidx.appcompat.widget.AppCompatImageView
+        style="@style/Widget.CoreSettings.PrimaryItemIcon"
+        app:srcCompat="@drawable/ic_key_variant" />
+
+      <androidx.appcompat.widget.SwitchCompat
+        android:id="@+id/password_protected"
+        style="@style/Widget.CoreSettings.PrimaryItemSwitch"
+        android:text="@string/addchat_channel_password_protected" />
+    </LinearLayout>
+
+    <LinearLayout
+      android:id="@+id/password_group"
+      style="@style/Widget.CoreSettings.DependentGroup"
+      tools:visibility="visible">
+
+      <com.google.android.material.textfield.TextInputLayout
+        style="@style/Widget.CustomTextInput"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:hint="@string/addchat_channel_password"
+        app:passwordToggleEnabled="true">
+
+        <com.google.android.material.textfield.TextInputEditText
+          android:id="@+id/password"
+          style="@style/Widget.CoreSettings.EditText"
+          android:inputType="textPassword"
+          tools:text="NickServ" />
+      </com.google.android.material.textfield.TextInputLayout>
+    </LinearLayout>
+  </LinearLayout>
+</androidx.core.widget.NestedScrollView>
diff --git a/app/src/main/res/values/strings_addchat.xml b/app/src/main/res/values/strings_addchat.xml
new file mode 100644
index 000000000..74cdbb64d
--- /dev/null
+++ b/app/src/main/res/values/strings_addchat.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+  <string name="addchat_channel_hidden">Hidden</string>
+
+  <string name="addchat_channel_invite_only">Invite Only</string>
+
+  <string name="addchat_channel_password_protected">Password Protected</string>
+  <string name="addchat_channel_password">Password</string>
+</resources>
-- 
GitLab