This commit is contained in:
Philipp Heckel 2021-10-25 14:24:44 -04:00
parent ad6e340fb7
commit 5f2f62c8f0
10 changed files with 37 additions and 115 deletions

View file

@ -24,22 +24,20 @@ import androidx.appcompat.app.AppCompatActivity
import com.heckel.ntfy.R import com.heckel.ntfy.R
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
const val TOPIC_NAME = "name" const val TOPIC_URL = "url"
const val TOPIC_DESCRIPTION = "description"
class AddTopicActivity : AppCompatActivity() { class AddTopicActivity : AppCompatActivity() {
private lateinit var addTopicName: TextInputEditText private lateinit var addTopicUrl: TextInputEditText
private lateinit var addTopicDescription: TextInputEditText
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.add_topic_layout) setContentView(R.layout.add_topic_layout)
findViewById<Button>(R.id.done_button).setOnClickListener { findViewById<Button>(R.id.subscribe_button).setOnClickListener {
addTopic() addTopic()
} }
addTopicName = findViewById(R.id.add_topic_name) addTopicUrl = findViewById(R.id.add_topic_url)
addTopicDescription = findViewById(R.id.add_topic_description) addTopicUrl.setText("https://ntfy.sh/")
} }
/* The onClick action for the done button. Closes the activity and returns the new topic name /* The onClick action for the done button. Closes the activity and returns the new topic name
@ -49,13 +47,11 @@ class AddTopicActivity : AppCompatActivity() {
private fun addTopic() { private fun addTopic() {
val resultIntent = Intent() val resultIntent = Intent()
if (addTopicName.text.isNullOrEmpty() || addTopicDescription.text.isNullOrEmpty()) { if (addTopicUrl.text.isNullOrEmpty()) {
setResult(Activity.RESULT_CANCELED, resultIntent) setResult(Activity.RESULT_CANCELED, resultIntent)
} else { } else {
val name = addTopicName.text.toString() val url = addTopicUrl.text.toString()
val description = addTopicDescription.text.toString() resultIntent.putExtra(TOPIC_URL, url)
resultIntent.putExtra(TOPIC_NAME, name)
resultIntent.putExtra(TOPIC_DESCRIPTION, description)
setResult(Activity.RESULT_OK, resultIntent) setResult(Activity.RESULT_OK, resultIntent)
} }
finish() finish()

View file

@ -22,7 +22,7 @@ import androidx.lifecycle.MutableLiveData
/* Handles operations on topicsLiveData and holds details about it. */ /* Handles operations on topicsLiveData and holds details about it. */
class DataSource(resources: Resources) { class DataSource(resources: Resources) {
private val initialTopicList = topicList(resources) private val initialTopicList: List<Topic> = mutableListOf()
private val topicsLiveData = MutableLiveData(initialTopicList) private val topicsLiveData = MutableLiveData(initialTopicList)
/* Adds topic to liveData and posts value. */ /* Adds topic to liveData and posts value. */

View file

@ -16,10 +16,7 @@
package io.heckel.ntfy.data package io.heckel.ntfy.data
import androidx.annotation.DrawableRes
data class Topic( data class Topic(
val id: Long, val id: Long,
val url: String, val url: String,
val description: String
) )

View file

@ -1,36 +0,0 @@
/*
* 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.data
import android.content.res.Resources
import com.heckel.ntfy.R
/* Returns initial list of topics. */
fun topicList(resources: Resources): List<Topic> {
return listOf(
Topic(
id = 1,
url = resources.getString(R.string.topic1_name),
description = resources.getString(R.string.topic1_description)
),
Topic(
id = 2,
url = resources.getString(R.string.topic2_name),
description = resources.getString(R.string.topic2_description)
),
)
}

View file

@ -18,7 +18,6 @@ package io.heckel.ntfy.detail
import android.os.Bundle import android.os.Bundle
import android.widget.Button import android.widget.Button
import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -38,8 +37,7 @@ class TopicDetailActivity : AppCompatActivity() {
var currentTopicId: Long? = null var currentTopicId: Long? = null
/* Connect variables to UI elements. */ /* Connect variables to UI elements. */
val topicName: TextView = findViewById(R.id.topic_detail_name) val topicUrl: TextView = findViewById(R.id.topic_detail_url)
val topicDescription: TextView = findViewById(R.id.topic_detail_url)
val removeTopicButton: Button = findViewById(R.id.remove_button) val removeTopicButton: Button = findViewById(R.id.remove_button)
val bundle: Bundle? = intent.extras val bundle: Bundle? = intent.extras
@ -51,8 +49,7 @@ class TopicDetailActivity : AppCompatActivity() {
description */ description */
currentTopicId?.let { currentTopicId?.let {
val currentTopic = topicDetailViewModel.getTopicForId(it) val currentTopic = topicDetailViewModel.getTopicForId(it)
topicName.text = currentTopic?.url topicUrl.text = currentTopic?.url
topicDescription.text = currentTopic?.description
removeTopicButton.setOnClickListener { removeTopicButton.setOnClickListener {
if (currentTopic != null) { if (currentTopic != null) {

View file

@ -18,18 +18,16 @@ package io.heckel.ntfy.list
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.recyclerview.widget.ConcatAdapter import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.heckel.ntfy.add.AddTopicActivity
import io.heckel.ntfy.detail.TopicDetailActivity
import com.heckel.ntfy.R import com.heckel.ntfy.R
import io.heckel.ntfy.add.TOPIC_DESCRIPTION import io.heckel.ntfy.add.AddTopicActivity
import io.heckel.ntfy.add.TOPIC_NAME import io.heckel.ntfy.add.TOPIC_URL
import io.heckel.ntfy.data.Topic import io.heckel.ntfy.data.Topic
import io.heckel.ntfy.detail.TopicDetailActivity
const val TOPIC_ID = "topic id" const val TOPIC_ID = "topic id"
@ -78,10 +76,8 @@ class TopicsListActivity : AppCompatActivity() {
/* Inserts topic into viewModel. */ /* Inserts topic into viewModel. */
if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) { if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) {
intentData?.let { data -> intentData?.let { data ->
val topicName = data.getStringExtra(TOPIC_NAME) val topicName = data.getStringExtra(TOPIC_URL)
val topicDescription = data.getStringExtra(TOPIC_DESCRIPTION) topicsListViewModel.insertTopic(topicName)
topicsListViewModel.insertTopic(topicName, topicDescription)
} }
} }
} }

View file

@ -28,15 +28,14 @@ class TopicsListViewModel(val dataSource: DataSource) : ViewModel() {
val topicsLiveData = dataSource.getTopicList() val topicsLiveData = dataSource.getTopicList()
/* If the name and description are present, create new Topic and add it to the datasource */ /* If the name and description are present, create new Topic and add it to the datasource */
fun insertTopic(topicName: String?, topicDescription: String?) { fun insertTopic(topicUrl: String?) {
if (topicName == null || topicDescription == null) { if (topicUrl == null) {
return return
} }
val newTopic = Topic( val newTopic = Topic(
Random.nextLong(), Random.nextLong(),
topicName, topicUrl
topicDescription
) )
dataSource.addTopic(newTopic) dataSource.addTopic(newTopic)

View file

@ -38,33 +38,17 @@
android:paddingBottom="16dp"> android:paddingBottom="16dp">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/add_topic_name" android:id="@+id/add_topic_url"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@string/topic_description_edit_text"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/add_topic_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" />
</com.google.android.material.textfield.TextInputLayout>
<Button <Button
android:id="@+id/done_button" android:id="@+id/subscribe_button"
style="?attr/materialButtonOutlinedStyle" style="?attr/materialButtonOutlinedStyle"
android:layout_width="240dp" android:layout_width="240dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:text="@string/done_button_text" /> android:text="@string/subscribe_button_text" />
</LinearLayout> </LinearLayout>

View file

@ -19,21 +19,20 @@
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
android:id="@+id/topic_detail_name" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:paddingTop="24dp"
android:paddingTop="24dp" android:paddingBottom="24dp"
android:paddingBottom="24dp" android:text="Delete topic"
android:text="@string/topic1_name" android:textAlignment="center"
android:textAlignment="center" android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
android:textAppearance="?attr/textAppearanceHeadline3" />
<TextView <TextView
android:id="@+id/topic_detail_url" android:id="@+id/topic_detail_url"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="16dp" android:padding="16dp"
android:text="@string/topic1_description" /> android:text="https://ntfy.sh/..."/>
<Button <Button
android:id="@+id/remove_button" android:id="@+id/remove_button"

View file

@ -18,19 +18,9 @@
<string name="app_name">Ntfy</string> <string name="app_name">Ntfy</string>
<string name="add_topic">Add Topic</string> <string name="add_topic">Add Topic</string>
<string name="topic1_name">Rose</string>
<string name="topic2_name">Freesia</string>
<string name="topic1_description">Rose comes from the Latin word Rosa. There are over 100
species of the rose. </string>
<string name="topic2_description">Freesias bloom during spring and are native to
Africa.</string>
<string name="topic_string">Topics</string> <string name="topic_string">Topics</string>
<string name="topic_finder">Topic finder</string> <string name="topic_name_edit_text">Topic URL</string>
<string name="topic_description_edit_text">Topic Description</string> <string name="subscribe_button_text">Subscribe</string>
<string name="topic_name_edit_text">Topic Name</string>
<string name="done_button_text">Done</string>
<string name="fab_content_description">fab</string> <string name="fab_content_description">fab</string>
<string name="remove_topic">Unsubscribe</string> <string name="remove_topic">Unsubscribe</string>