From 49b3898977c19c0c9baaf45552226edce4cc8e79 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 26 Oct 2021 21:44:12 -0400 Subject: [PATCH] We're getting there --- .../main/java/io/heckel/ntfy/MainActivity.kt | 28 +++---------- .../main/java/io/heckel/ntfy/TopicsAdapter.kt | 26 +++---------- .../java/io/heckel/ntfy/TopicsViewModel.kt | 7 ++-- .../java/io/heckel/ntfy/add/AddActivity.kt | 35 ++++++----------- .../{TopicsRepository.kt => Repository.kt} | 31 ++++++++------- .../main/java/io/heckel/ntfy/data/Topic.kt | 21 ++-------- .../io/heckel/ntfy/detail/DetailActivity.kt | 23 +++++------ app/src/main/res/layout/add_topic_layout.xml | 39 ++++++++++++------- app/src/main/res/layout/topic_item.xml | 4 +- app/src/main/res/values/strings.xml | 21 ++-------- 10 files changed, 91 insertions(+), 144 deletions(-) rename app/src/main/java/io/heckel/ntfy/data/{TopicsRepository.kt => Repository.kt} (75%) diff --git a/app/src/main/java/io/heckel/ntfy/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/MainActivity.kt index 96110b2..746b815 100644 --- a/app/src/main/java/io/heckel/ntfy/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/MainActivity.kt @@ -1,19 +1,3 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.heckel.ntfy import android.app.Activity @@ -32,11 +16,11 @@ import androidx.recyclerview.widget.RecyclerView import io.heckel.ntfy.add.AddTopicActivity import io.heckel.ntfy.data.Topic import io.heckel.ntfy.detail.DetailActivity -import io.heckel.ntfy.list.* import kotlin.random.Random -const val TOPIC_ID = "topic id" -const val TOPIC_URL = "url" +const val TOPIC_ID = "topic_id" +const val TOPIC_NAME = "topic_name" +const val TOPIC_BASE_URL = "base_url" class MainActivity : AppCompatActivity() { private val newTopicActivityRequestCode = 1 @@ -88,9 +72,9 @@ class MainActivity : AppCompatActivity() { if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) { intentData?.let { data -> - val topicId = Random.nextLong() - val topicUrl = data.getStringExtra(TOPIC_URL) ?: return - val topic = Topic(topicId, topicUrl) + val name = data.getStringExtra(TOPIC_NAME) ?: return + val baseUrl = data.getStringExtra(TOPIC_BASE_URL) ?: return + val topic = Topic(Random.nextLong(), name, baseUrl) topicsViewModel.add(topic) } diff --git a/app/src/main/java/io/heckel/ntfy/TopicsAdapter.kt b/app/src/main/java/io/heckel/ntfy/TopicsAdapter.kt index ab0658a..f97080f 100644 --- a/app/src/main/java/io/heckel/ntfy/TopicsAdapter.kt +++ b/app/src/main/java/io/heckel/ntfy/TopicsAdapter.kt @@ -1,20 +1,4 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.heckel.ntfy.list +package io.heckel.ntfy import android.view.LayoutInflater import android.view.View @@ -23,7 +7,6 @@ import android.widget.TextView import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView -import io.heckel.ntfy.R import io.heckel.ntfy.data.Topic class TopicsAdapter(private val onClick: (Topic) -> Unit) : @@ -43,10 +26,11 @@ class TopicsAdapter(private val onClick: (Topic) -> Unit) : } } - /* Bind topic name and image. */ fun bind(topic: Topic) { currentTopic = topic - topicTextView.text = topic.url + val shortBaseUrl = topic.baseUrl.replace("https://", "") // Leave http:// untouched + val shortName = itemView.context.getString(R.string.topic_short_name_format, shortBaseUrl, topic.name) + topicTextView.text = shortName } } @@ -71,6 +55,6 @@ object TopicDiffCallback : DiffUtil.ItemCallback() { } override fun areContentsTheSame(oldItem: Topic, newItem: Topic): Boolean { - return oldItem.id == newItem.id + return oldItem.name == newItem.name } } diff --git a/app/src/main/java/io/heckel/ntfy/TopicsViewModel.kt b/app/src/main/java/io/heckel/ntfy/TopicsViewModel.kt index b58930e..7cf58ba 100644 --- a/app/src/main/java/io/heckel/ntfy/TopicsViewModel.kt +++ b/app/src/main/java/io/heckel/ntfy/TopicsViewModel.kt @@ -4,15 +4,14 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope -import io.heckel.ntfy.data.TopicsRepository +import io.heckel.ntfy.data.Repository import io.heckel.ntfy.data.Topic -import kotlinx.coroutines.* import kotlin.collections.List data class Notification(val topic: String, val message: String) typealias NotificationListener = (notification: Notification) -> Unit -class TopicsViewModel(private val repository: TopicsRepository) : ViewModel() { +class TopicsViewModel(private val repository: Repository) : ViewModel() { fun add(topic: Topic) { repository.add(topic, viewModelScope) } @@ -39,7 +38,7 @@ class TopicsViewModelFactory() : ViewModelProvider.Factory { override fun create(modelClass: Class) = with(modelClass){ when { - isAssignableFrom(TopicsViewModel::class.java) -> TopicsViewModel(TopicsRepository.getInstance()) as T + isAssignableFrom(TopicsViewModel::class.java) -> TopicsViewModel(Repository.getInstance()) as T else -> throw IllegalArgumentException("Unknown viewModel class $modelClass") } } diff --git a/app/src/main/java/io/heckel/ntfy/add/AddActivity.kt b/app/src/main/java/io/heckel/ntfy/add/AddActivity.kt index 69c6459..8dea24b 100644 --- a/app/src/main/java/io/heckel/ntfy/add/AddActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/add/AddActivity.kt @@ -1,19 +1,3 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.heckel.ntfy.add import android.app.Activity @@ -23,10 +7,12 @@ import android.widget.Button import androidx.appcompat.app.AppCompatActivity import com.google.android.material.textfield.TextInputEditText import io.heckel.ntfy.R -import io.heckel.ntfy.TOPIC_URL +import io.heckel.ntfy.TOPIC_BASE_URL +import io.heckel.ntfy.TOPIC_NAME class AddTopicActivity : AppCompatActivity() { - private lateinit var addTopicUrl: TextInputEditText + private lateinit var topicName: TextInputEditText + private lateinit var baseUrl: TextInputEditText override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -35,8 +21,9 @@ class AddTopicActivity : AppCompatActivity() { findViewById