From 118bff0099b1a442bd93722d76456338737021bb Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 13 Feb 2022 09:26:26 -0500 Subject: [PATCH] Add subscribed topics to the list of suggesed topics --- .../java/io/heckel/ntfy/msg/ApiService.kt | 2 +- .../java/io/heckel/ntfy/ui/DetailAdapter.kt | 6 +-- .../java/io/heckel/ntfy/ui/ShareActivity.kt | 49 ++++++++++--------- app/src/main/res/layout/activity_share.xml | 2 +- .../main/res/layout/fragment_detail_item.xml | 18 +++---- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 42 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt index 911bcb6..7fe1276 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt @@ -63,7 +63,7 @@ class ApiService { } if (body != null) { builder - .addHeader("X-Message", message) + .addHeader("X-Message", message.replace("\n", "\\n")) .put(body) } else { builder.put(message.toRequestBody()) diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt index 7968381..ccdab8a 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailAdapter.kt @@ -68,9 +68,9 @@ class DetailAdapter(private val activity: Activity, private val repository: Repo private val tagsView: TextView = itemView.findViewById(R.id.detail_item_tags_text) private val menuButton: ImageButton = itemView.findViewById(R.id.detail_item_menu_button) private val attachmentImageView: ImageView = itemView.findViewById(R.id.detail_item_attachment_image) - private val attachmentBoxView: View = itemView.findViewById(R.id.share_content_file_box) - private val attachmentIconView: ImageView = itemView.findViewById(R.id.share_content_file_icon) - private val attachmentInfoView: TextView = itemView.findViewById(R.id.share_content_file_info) + private val attachmentBoxView: View = itemView.findViewById(R.id.detail_item_attachment_file_box) + private val attachmentIconView: ImageView = itemView.findViewById(R.id.detail_item_attachment_file_icon) + private val attachmentInfoView: TextView = itemView.findViewById(R.id.detail_item_attachment_file_info) fun bind(notification: Notification) { this.notification = notification diff --git a/app/src/main/java/io/heckel/ntfy/ui/ShareActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/ShareActivity.kt index 8dc0b30..5612f85 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/ShareActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/ShareActivity.kt @@ -15,7 +15,6 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.textfield.TextInputLayout import io.heckel.ntfy.R import io.heckel.ntfy.app.Application -import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.msg.ApiService import io.heckel.ntfy.util.* import kotlinx.coroutines.Dispatchers @@ -109,26 +108,32 @@ class ShareActivity : AppCompatActivity() { validateInput() } - // Populate "last topics" - val reversedLastTopics = repository.getLastShareTopics().reversed() - lastTopicsList.adapter = TopicAdapter(reversedLastTopics) { topicUrl -> - try { - val (baseUrl, topic) = splitTopicUrl(topicUrl) - topicText.text = topic - if (baseUrl == appBaseUrl) { - useAnotherServerCheckbox.isChecked = false - } else { - useAnotherServerCheckbox.isChecked = true - baseUrlText.setText(baseUrl) - } - } catch (e: Exception) { - Log.w(TAG, "Invalid topicUrl $topicUrl", e) - } - } - - // Add baseUrl auto-complete behavior + // Things that need the database lifecycleScope.launch(Dispatchers.IO) { - baseUrls = repository.getSubscriptions() + // Populate "suggested topics" + val subscriptions = repository.getSubscriptions() + val lastShareTopics = repository.getLastShareTopics() + val subscribedTopics = subscriptions + .map { topicUrl(it.baseUrl, it.topic) } + .subtract(lastShareTopics.toSet()) + val suggestedTopics = lastShareTopics.reversed() + subscribedTopics + lastTopicsList.adapter = TopicAdapter(suggestedTopics) { topicUrl -> + try { + val (baseUrl, topic) = splitTopicUrl(topicUrl) + topicText.text = topic + if (baseUrl == appBaseUrl) { + useAnotherServerCheckbox.isChecked = false + } else { + useAnotherServerCheckbox.isChecked = true + baseUrlText.setText(baseUrl) + } + } catch (e: Exception) { + Log.w(TAG, "Invalid topicUrl $topicUrl", e) + } + } + + // Add baseUrl auto-complete behavior + baseUrls = subscriptions .groupBy { it.baseUrl } .map { it.key } .filterNot { it == appBaseUrl } @@ -136,9 +141,9 @@ class ShareActivity : AppCompatActivity() { val activity = this@ShareActivity activity.runOnUiThread { initBaseUrlDropdown(baseUrls, baseUrlText, baseUrlLayout) - useAnotherServerCheckbox.isChecked = if (reversedLastTopics.isNotEmpty()) { + useAnotherServerCheckbox.isChecked = if (suggestedTopics.isNotEmpty()) { try { - val (baseUrl, _) = splitTopicUrl(reversedLastTopics.first()) + val (baseUrl, _) = splitTopicUrl(suggestedTopics.first()) baseUrl != appBaseUrl } catch (_: Exception) { false diff --git a/app/src/main/res/layout/activity_share.xml b/app/src/main/res/layout/activity_share.xml index 03f2f43..2bc1850 100644 --- a/app/src/main/res/layout/activity_share.xml +++ b/app/src/main/res/layout/activity_share.xml @@ -130,7 +130,7 @@ android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="3dp" - android:text="@string/share_previous_topics" + android:text="@string/share_suggested_topics" android:textAlignment="viewStart" android:textAppearance="@style/TextAppearance.AppCompat.Medium" app:layout_constraintTop_toBottomOf="@id/share_base_url_layout" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="13dp"/> diff --git a/app/src/main/res/layout/fragment_detail_item.xml b/app/src/main/res/layout/fragment_detail_item.xml index c68c9da..1a94e9d 100644 --- a/app/src/main/res/layout/fragment_detail_item.xml +++ b/app/src/main/res/layout/fragment_detail_item.xml @@ -87,13 +87,13 @@ app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="10dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="10dp" app:layout_constraintTop_toBottomOf="@id/detail_item_attachment_image" - app:layout_constraintBottom_toTopOf="@id/share_content_file_box" + app:layout_constraintBottom_toTopOf="@id/detail_item_attachment_file_box" app:layout_constraintHorizontal_bias="0.0" android:layout_marginTop="2dp" /> + app:layout_constraintTop_toTopOf="@+id/detail_item_attachment_file_icon" + app:layout_constraintBottom_toBottomOf="@+id/detail_item_attachment_file_icon"/> + app:layout_constraintTop_toBottomOf="@id/detail_item_attachment_file_box"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d32be3e..b95d6a2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -196,7 +196,7 @@ A file was shared with you Cannot read file infos: %1$s Share to - Last topics + Suggested topics Message successfully published