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.google.android.material.textfield.TextInputEditText
const val TOPIC_NAME = "name"
const val TOPIC_DESCRIPTION = "description"
const val TOPIC_URL = "url"
class AddTopicActivity : AppCompatActivity() {
private lateinit var addTopicName: TextInputEditText
private lateinit var addTopicDescription: TextInputEditText
private lateinit var addTopicUrl: TextInputEditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.add_topic_layout)
findViewById<Button>(R.id.done_button).setOnClickListener {
findViewById<Button>(R.id.subscribe_button).setOnClickListener {
addTopic()
}
addTopicName = findViewById(R.id.add_topic_name)
addTopicDescription = findViewById(R.id.add_topic_description)
addTopicUrl = findViewById(R.id.add_topic_url)
addTopicUrl.setText("https://ntfy.sh/")
}
/* 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() {
val resultIntent = Intent()
if (addTopicName.text.isNullOrEmpty() || addTopicDescription.text.isNullOrEmpty()) {
if (addTopicUrl.text.isNullOrEmpty()) {
setResult(Activity.RESULT_CANCELED, resultIntent)
} else {
val name = addTopicName.text.toString()
val description = addTopicDescription.text.toString()
resultIntent.putExtra(TOPIC_NAME, name)
resultIntent.putExtra(TOPIC_DESCRIPTION, description)
val url = addTopicUrl.text.toString()
resultIntent.putExtra(TOPIC_URL, url)
setResult(Activity.RESULT_OK, resultIntent)
}
finish()

View file

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

View file

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

View file

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

View file

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

View file

@ -38,33 +38,17 @@
android:paddingBottom="16dp">
<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_height="wrap_content" />
</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
android:id="@+id/done_button"
android:id="@+id/subscribe_button"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/done_button_text" />
android:text="@string/subscribe_button_text" />
</LinearLayout>

View file

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

View file

@ -18,19 +18,9 @@
<string name="app_name">Ntfy</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_finder">Topic finder</string>
<string name="topic_description_edit_text">Topic Description</string>
<string name="topic_name_edit_text">Topic Name</string>
<string name="done_button_text">Done</string>
<string name="topic_name_edit_text">Topic URL</string>
<string name="subscribe_button_text">Subscribe</string>
<string name="fab_content_description">fab</string>
<string name="remove_topic">Unsubscribe</string>