Remove more flower stuff
|
@ -24,38 +24,38 @@ 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 FLOWER_NAME = "name"
|
const val TOPIC_NAME = "name"
|
||||||
const val FLOWER_DESCRIPTION = "description"
|
const val TOPIC_DESCRIPTION = "description"
|
||||||
|
|
||||||
class AddTopicActivity : AppCompatActivity() {
|
class AddTopicActivity : AppCompatActivity() {
|
||||||
private lateinit var addFlowerName: TextInputEditText
|
private lateinit var addTopicName: TextInputEditText
|
||||||
private lateinit var addFlowerDescription: 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_flower_layout)
|
setContentView(R.layout.add_topic_layout)
|
||||||
|
|
||||||
findViewById<Button>(R.id.done_button).setOnClickListener {
|
findViewById<Button>(R.id.done_button).setOnClickListener {
|
||||||
addFlower()
|
addTopic()
|
||||||
}
|
}
|
||||||
addFlowerName = findViewById(R.id.add_flower_name)
|
addTopicName = findViewById(R.id.add_topic_name)
|
||||||
addFlowerDescription = findViewById(R.id.add_flower_description)
|
addTopicDescription = findViewById(R.id.add_topic_description)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The onClick action for the done button. Closes the activity and returns the new flower name
|
/* The onClick action for the done button. Closes the activity and returns the new topic name
|
||||||
and description as part of the intent. If the name or description are missing, the result is set
|
and description as part of the intent. If the name or description are missing, the result is set
|
||||||
to cancelled. */
|
to cancelled. */
|
||||||
|
|
||||||
private fun addFlower() {
|
private fun addTopic() {
|
||||||
val resultIntent = Intent()
|
val resultIntent = Intent()
|
||||||
|
|
||||||
if (addFlowerName.text.isNullOrEmpty() || addFlowerDescription.text.isNullOrEmpty()) {
|
if (addTopicName.text.isNullOrEmpty() || addTopicDescription.text.isNullOrEmpty()) {
|
||||||
setResult(Activity.RESULT_CANCELED, resultIntent)
|
setResult(Activity.RESULT_CANCELED, resultIntent)
|
||||||
} else {
|
} else {
|
||||||
val name = addFlowerName.text.toString()
|
val name = addTopicName.text.toString()
|
||||||
val description = addFlowerDescription.text.toString()
|
val description = addTopicDescription.text.toString()
|
||||||
resultIntent.putExtra(FLOWER_NAME, name)
|
resultIntent.putExtra(TOPIC_NAME, name)
|
||||||
resultIntent.putExtra(FLOWER_DESCRIPTION, description)
|
resultIntent.putExtra(TOPIC_DESCRIPTION, description)
|
||||||
setResult(Activity.RESULT_OK, resultIntent)
|
setResult(Activity.RESULT_OK, resultIntent)
|
||||||
}
|
}
|
||||||
finish()
|
finish()
|
||||||
|
|
|
@ -20,49 +20,43 @@ import android.content.res.Resources
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
|
||||||
/* Handles operations on flowersLiveData and holds details about it. */
|
/* Handles operations on topicsLiveData and holds details about it. */
|
||||||
class DataSource(resources: Resources) {
|
class DataSource(resources: Resources) {
|
||||||
private val initialFlowerList = flowerList(resources)
|
private val initialTopicList = topicList(resources)
|
||||||
private val flowersLiveData = MutableLiveData(initialFlowerList)
|
private val topicsLiveData = MutableLiveData(initialTopicList)
|
||||||
|
|
||||||
/* Adds flower to liveData and posts value. */
|
/* Adds topic to liveData and posts value. */
|
||||||
fun addFlower(flower: Topic) {
|
fun addTopic(topic: Topic) {
|
||||||
val currentList = flowersLiveData.value
|
val currentList = topicsLiveData.value
|
||||||
if (currentList == null) {
|
if (currentList == null) {
|
||||||
flowersLiveData.postValue(listOf(flower))
|
topicsLiveData.postValue(listOf(topic))
|
||||||
} else {
|
} else {
|
||||||
val updatedList = currentList.toMutableList()
|
val updatedList = currentList.toMutableList()
|
||||||
updatedList.add(0, flower)
|
updatedList.add(0, topic)
|
||||||
flowersLiveData.postValue(updatedList)
|
topicsLiveData.postValue(updatedList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Removes flower from liveData and posts value. */
|
/* Removes topic from liveData and posts value. */
|
||||||
fun removeFlower(flower: Topic) {
|
fun removeTopic(topic: Topic) {
|
||||||
val currentList = flowersLiveData.value
|
val currentList = topicsLiveData.value
|
||||||
if (currentList != null) {
|
if (currentList != null) {
|
||||||
val updatedList = currentList.toMutableList()
|
val updatedList = currentList.toMutableList()
|
||||||
updatedList.remove(flower)
|
updatedList.remove(topic)
|
||||||
flowersLiveData.postValue(updatedList)
|
topicsLiveData.postValue(updatedList)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns flower given an ID. */
|
/* Returns topic given an ID. */
|
||||||
fun getFlowerForId(id: Long): Topic? {
|
fun getTopicForId(id: Long): Topic? {
|
||||||
flowersLiveData.value?.let { flowers ->
|
topicsLiveData.value?.let { topics ->
|
||||||
return flowers.firstOrNull{ it.id == id}
|
return topics.firstOrNull{ it.id == id}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFlowerList(): LiveData<List<Topic>> {
|
fun getTopicList(): LiveData<List<Topic>> {
|
||||||
return flowersLiveData
|
return topicsLiveData
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns a random flower asset for flowers that are added. */
|
|
||||||
fun getRandomFlowerImageAsset(): Int? {
|
|
||||||
val randomNumber = (initialFlowerList.indices).random()
|
|
||||||
return initialFlowerList[randomNumber].image
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -20,8 +20,6 @@ import androidx.annotation.DrawableRes
|
||||||
|
|
||||||
data class Topic(
|
data class Topic(
|
||||||
val id: Long,
|
val id: Long,
|
||||||
val name: String,
|
val url: String,
|
||||||
@DrawableRes
|
|
||||||
val image: Int?,
|
|
||||||
val description: String
|
val description: String
|
||||||
)
|
)
|
||||||
|
|
|
@ -19,74 +19,18 @@ package io.heckel.ntfy.data
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import com.heckel.ntfy.R
|
import com.heckel.ntfy.R
|
||||||
|
|
||||||
/* Returns initial list of flowers. */
|
/* Returns initial list of topics. */
|
||||||
fun flowerList(resources: Resources): List<Topic> {
|
fun topicList(resources: Resources): List<Topic> {
|
||||||
return listOf(
|
return listOf(
|
||||||
Topic(
|
Topic(
|
||||||
id = 1,
|
id = 1,
|
||||||
name = resources.getString(R.string.flower1_name),
|
url = resources.getString(R.string.topic1_name),
|
||||||
image = R.drawable.rose,
|
description = resources.getString(R.string.topic1_description)
|
||||||
description = resources.getString(R.string.flower1_description)
|
|
||||||
),
|
),
|
||||||
Topic(
|
Topic(
|
||||||
id = 2,
|
id = 2,
|
||||||
name = resources.getString(R.string.flower2_name),
|
url = resources.getString(R.string.topic2_name),
|
||||||
image = R.drawable.freesia,
|
description = resources.getString(R.string.topic2_description)
|
||||||
description = resources.getString(R.string.flower2_description)
|
|
||||||
),
|
),
|
||||||
Topic(
|
|
||||||
id = 3,
|
|
||||||
name = resources.getString(R.string.flower3_name),
|
|
||||||
image = R.drawable.lily,
|
|
||||||
description = resources.getString(R.string.flower3_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 4,
|
|
||||||
name = resources.getString(R.string.flower4_name),
|
|
||||||
image = R.drawable.sunflower,
|
|
||||||
description = resources.getString(R.string.flower4_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 5,
|
|
||||||
name = resources.getString(R.string.flower5_name),
|
|
||||||
image = R.drawable.peony,
|
|
||||||
description = resources.getString(R.string.flower5_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 6,
|
|
||||||
name = resources.getString(R.string.flower6_name),
|
|
||||||
image = R.drawable.daisy,
|
|
||||||
description = resources.getString(R.string.flower6_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 7,
|
|
||||||
name = resources.getString(R.string.flower7_name),
|
|
||||||
image = R.drawable.lilac,
|
|
||||||
description = resources.getString(R.string.flower7_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 8,
|
|
||||||
name = resources.getString(R.string.flower8_name),
|
|
||||||
image = R.drawable.marigold,
|
|
||||||
description = resources.getString(R.string.flower8_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 9,
|
|
||||||
name = resources.getString(R.string.flower9_name),
|
|
||||||
image = R.drawable.poppy,
|
|
||||||
description = resources.getString(R.string.flower9_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 10,
|
|
||||||
name = resources.getString(R.string.flower10_name),
|
|
||||||
image = R.drawable.daffodil,
|
|
||||||
description = resources.getString(R.string.flower10_description)
|
|
||||||
),
|
|
||||||
Topic(
|
|
||||||
id = 11,
|
|
||||||
name = resources.getString(R.string.flower11_name),
|
|
||||||
image = R.drawable.dahlia,
|
|
||||||
description = resources.getString(R.string.flower11_description)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,46 +23,40 @@ import android.widget.TextView
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.heckel.ntfy.R
|
import com.heckel.ntfy.R
|
||||||
import io.heckel.ntfy.list.FLOWER_ID
|
import io.heckel.ntfy.list.TOPIC_ID
|
||||||
|
|
||||||
class TopicDetailActivity : AppCompatActivity() {
|
class TopicDetailActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private val flowerDetailViewModel by viewModels<FlowerDetailViewModel> {
|
private val topicDetailViewModel by viewModels<TopicDetailViewModel> {
|
||||||
FlowerDetailViewModelFactory(this)
|
TopicDetailViewModelFactory(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.flower_detail_activity)
|
setContentView(R.layout.topic_detail_activity)
|
||||||
|
|
||||||
var currentFlowerId: Long? = null
|
var currentTopicId: Long? = null
|
||||||
|
|
||||||
/* Connect variables to UI elements. */
|
/* Connect variables to UI elements. */
|
||||||
val flowerName: TextView = findViewById(R.id.flower_detail_name)
|
val topicName: TextView = findViewById(R.id.topic_detail_name)
|
||||||
val flowerImage: ImageView = findViewById(R.id.flower_detail_image)
|
val topicDescription: TextView = findViewById(R.id.topic_detail_url)
|
||||||
val flowerDescription: TextView = findViewById(R.id.flower_detail_description)
|
val removeTopicButton: Button = findViewById(R.id.remove_button)
|
||||||
val removeFlowerButton: Button = findViewById(R.id.remove_button)
|
|
||||||
|
|
||||||
val bundle: Bundle? = intent.extras
|
val bundle: Bundle? = intent.extras
|
||||||
if (bundle != null) {
|
if (bundle != null) {
|
||||||
currentFlowerId = bundle.getLong(FLOWER_ID)
|
currentTopicId = bundle.getLong(TOPIC_ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If currentFlowerId is not null, get corresponding flower and set name, image and
|
/* If currentTopicId is not null, get corresponding topic and set name, image and
|
||||||
description */
|
description */
|
||||||
currentFlowerId?.let {
|
currentTopicId?.let {
|
||||||
val currentFlower = flowerDetailViewModel.getFlowerForId(it)
|
val currentTopic = topicDetailViewModel.getTopicForId(it)
|
||||||
flowerName.text = currentFlower?.name
|
topicName.text = currentTopic?.url
|
||||||
if (currentFlower?.image == null) {
|
topicDescription.text = currentTopic?.description
|
||||||
flowerImage.setImageResource(R.drawable.rose)
|
|
||||||
} else {
|
|
||||||
flowerImage.setImageResource(currentFlower.image)
|
|
||||||
}
|
|
||||||
flowerDescription.text = currentFlower?.description
|
|
||||||
|
|
||||||
removeFlowerButton.setOnClickListener {
|
removeTopicButton.setOnClickListener {
|
||||||
if (currentFlower != null) {
|
if (currentTopic != null) {
|
||||||
flowerDetailViewModel.removeFlower(currentFlower)
|
topicDetailViewModel.removeTopic(currentTopic)
|
||||||
}
|
}
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,25 +22,25 @@ import androidx.lifecycle.ViewModelProvider
|
||||||
import io.heckel.ntfy.data.DataSource
|
import io.heckel.ntfy.data.DataSource
|
||||||
import io.heckel.ntfy.data.Topic
|
import io.heckel.ntfy.data.Topic
|
||||||
|
|
||||||
class FlowerDetailViewModel(private val datasource: DataSource) : ViewModel() {
|
class TopicDetailViewModel(private val datasource: DataSource) : ViewModel() {
|
||||||
|
|
||||||
/* Queries datasource to returns a flower that corresponds to an id. */
|
/* Queries datasource to returns a topic that corresponds to an id. */
|
||||||
fun getFlowerForId(id: Long) : Topic? {
|
fun getTopicForId(id: Long) : Topic? {
|
||||||
return datasource.getFlowerForId(id)
|
return datasource.getTopicForId(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Queries datasource to remove a flower. */
|
/* Queries datasource to remove a topic. */
|
||||||
fun removeFlower(flower: Topic) {
|
fun removeTopic(topic: Topic) {
|
||||||
datasource.removeFlower(flower)
|
datasource.removeTopic(topic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FlowerDetailViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
|
class TopicDetailViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
|
||||||
|
|
||||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||||
if (modelClass.isAssignableFrom(FlowerDetailViewModel::class.java)) {
|
if (modelClass.isAssignableFrom(TopicDetailViewModel::class.java)) {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
return FlowerDetailViewModel(
|
return TopicDetailViewModel(
|
||||||
datasource = DataSource.getDataSource(context.resources)
|
datasource = DataSource.getDataSource(context.resources)
|
||||||
) as T
|
) as T
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,62 +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.list
|
|
||||||
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import com.heckel.ntfy.R
|
|
||||||
|
|
||||||
/* A list always displaying one element: the number of flowers. */
|
|
||||||
|
|
||||||
class HeaderAdapter: RecyclerView.Adapter<HeaderAdapter.HeaderViewHolder>() {
|
|
||||||
private var flowerCount: Int = 0
|
|
||||||
|
|
||||||
/* ViewHolder for displaying header. */
|
|
||||||
class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view){
|
|
||||||
private val flowerNumberTextView: TextView = itemView.findViewById(R.id.flower_number_text)
|
|
||||||
|
|
||||||
fun bind(flowerCount: Int) {
|
|
||||||
flowerNumberTextView.text = flowerCount.toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Inflates view and returns HeaderViewHolder. */
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder {
|
|
||||||
val view = LayoutInflater.from(parent.context)
|
|
||||||
.inflate(R.layout.header_item, parent, false)
|
|
||||||
return HeaderViewHolder(view)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Binds number of flowers to the header. */
|
|
||||||
override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) {
|
|
||||||
holder.bind(flowerCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Returns number of items, since there is only one item in the header return one */
|
|
||||||
override fun getItemCount(): Int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Updates header to display number of flowers when a flower is added or subtracted. */
|
|
||||||
fun updateFlowerCount(updatedFlowerCount: Int) {
|
|
||||||
flowerCount = updatedFlowerCount
|
|
||||||
notifyDataSetChanged()
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -27,53 +27,46 @@ import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.heckel.ntfy.R
|
import com.heckel.ntfy.R
|
||||||
import io.heckel.ntfy.data.Topic
|
import io.heckel.ntfy.data.Topic
|
||||||
|
|
||||||
class FlowersAdapter(private val onClick: (Topic) -> Unit) :
|
class TopicsAdapter(private val onClick: (Topic) -> Unit) :
|
||||||
ListAdapter<Topic, FlowersAdapter.FlowerViewHolder>(FlowerDiffCallback) {
|
ListAdapter<Topic, TopicsAdapter.TopicViewHolder>(TopicDiffCallback) {
|
||||||
|
|
||||||
/* ViewHolder for Flower, takes in the inflated view and the onClick behavior. */
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
||||||
class FlowerViewHolder(itemView: View, val onClick: (Topic) -> Unit) :
|
class TopicViewHolder(itemView: View, val onClick: (Topic) -> Unit) :
|
||||||
RecyclerView.ViewHolder(itemView) {
|
RecyclerView.ViewHolder(itemView) {
|
||||||
private val flowerTextView: TextView = itemView.findViewById(R.id.flower_text)
|
private val topicTextView: TextView = itemView.findViewById(R.id.topic_text)
|
||||||
private val flowerImageView: ImageView = itemView.findViewById(R.id.flower_image)
|
private var currentTopic: Topic? = null
|
||||||
private var currentFlower: Topic? = null
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
itemView.setOnClickListener {
|
itemView.setOnClickListener {
|
||||||
currentFlower?.let {
|
currentTopic?.let {
|
||||||
onClick(it)
|
onClick(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bind flower name and image. */
|
/* Bind topic name and image. */
|
||||||
fun bind(flower: Topic) {
|
fun bind(topic: Topic) {
|
||||||
currentFlower = flower
|
currentTopic = topic
|
||||||
|
topicTextView.text = topic.url
|
||||||
flowerTextView.text = flower.name
|
|
||||||
if (flower.image != null) {
|
|
||||||
flowerImageView.setImageResource(flower.image)
|
|
||||||
} else {
|
|
||||||
flowerImageView.setImageResource(R.drawable.rose)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Creates and inflates view and return FlowerViewHolder. */
|
/* Creates and inflates view and return TopicViewHolder. */
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FlowerViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicViewHolder {
|
||||||
val view = LayoutInflater.from(parent.context)
|
val view = LayoutInflater.from(parent.context)
|
||||||
.inflate(R.layout.flower_item, parent, false)
|
.inflate(R.layout.topic_item, parent, false)
|
||||||
return FlowerViewHolder(view, onClick)
|
return TopicViewHolder(view, onClick)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Gets current flower and uses it to bind view. */
|
/* Gets current topic and uses it to bind view. */
|
||||||
override fun onBindViewHolder(holder: FlowerViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: TopicViewHolder, position: Int) {
|
||||||
val flower = getItem(position)
|
val topic = getItem(position)
|
||||||
holder.bind(flower)
|
holder.bind(topic)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object FlowerDiffCallback : DiffUtil.ItemCallback<Topic>() {
|
object TopicDiffCallback : DiffUtil.ItemCallback<Topic>() {
|
||||||
override fun areItemsTheSame(oldItem: Topic, newItem: Topic): Boolean {
|
override fun areItemsTheSame(oldItem: Topic, newItem: Topic): Boolean {
|
||||||
return oldItem == newItem
|
return oldItem == newItem
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,37 +27,31 @@ import androidx.recyclerview.widget.RecyclerView
|
||||||
import io.heckel.ntfy.add.AddTopicActivity
|
import io.heckel.ntfy.add.AddTopicActivity
|
||||||
import io.heckel.ntfy.detail.TopicDetailActivity
|
import io.heckel.ntfy.detail.TopicDetailActivity
|
||||||
import com.heckel.ntfy.R
|
import com.heckel.ntfy.R
|
||||||
import io.heckel.ntfy.add.FLOWER_DESCRIPTION
|
import io.heckel.ntfy.add.TOPIC_DESCRIPTION
|
||||||
import io.heckel.ntfy.add.FLOWER_NAME
|
import io.heckel.ntfy.add.TOPIC_NAME
|
||||||
import io.heckel.ntfy.data.Topic
|
import io.heckel.ntfy.data.Topic
|
||||||
|
|
||||||
const val FLOWER_ID = "flower id"
|
const val TOPIC_ID = "topic id"
|
||||||
|
|
||||||
class TopicsListActivity : AppCompatActivity() {
|
class TopicsListActivity : AppCompatActivity() {
|
||||||
private val newTopicActivityRequestCode = 1
|
private val newTopicActivityRequestCode = 1
|
||||||
private val topicsListViewModel by viewModels<TopicsListViewModel> {
|
private val topicsListViewModel by viewModels<TopicsListViewModel> {
|
||||||
FlowersListViewModelFactory(this)
|
TopicsListViewModelFactory(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
/* Instantiates headerAdapter and flowersAdapter. Both adapters are added to concatAdapter.
|
val adapter = TopicsAdapter { topic -> adapterOnClick(topic) }
|
||||||
which displays the contents sequentially */
|
|
||||||
val headerAdapter = HeaderAdapter()
|
|
||||||
val topicsAdapter = FlowersAdapter { flower -> adapterOnClick(flower) }
|
|
||||||
val concatAdapter = ConcatAdapter(headerAdapter, topicsAdapter)
|
|
||||||
|
|
||||||
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
|
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
|
||||||
recyclerView.adapter = concatAdapter
|
recyclerView.adapter = adapter
|
||||||
|
|
||||||
topicsListViewModel.topicsLiveData.observe(this, {
|
topicsListViewModel.topicsLiveData.observe(this) {
|
||||||
it?.let {
|
it?.let {
|
||||||
topicsAdapter.submitList(it as MutableList<Topic>)
|
adapter.submitList(it as MutableList<Topic>)
|
||||||
headerAdapter.updateFlowerCount(it.size)
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
val fab: View = findViewById(R.id.fab)
|
val fab: View = findViewById(R.id.fab)
|
||||||
fab.setOnClickListener {
|
fab.setOnClickListener {
|
||||||
|
@ -65,14 +59,14 @@ class TopicsListActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Opens FlowerDetailActivity when RecyclerView item is clicked. */
|
/* Opens TopicDetailActivity when RecyclerView item is clicked. */
|
||||||
private fun adapterOnClick(flower: Topic) {
|
private fun adapterOnClick(topic: Topic) {
|
||||||
val intent = Intent(this, TopicDetailActivity()::class.java)
|
val intent = Intent(this, TopicDetailActivity()::class.java)
|
||||||
intent.putExtra(FLOWER_ID, flower.id)
|
intent.putExtra(TOPIC_ID, topic.id)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Adds flower to flowerList when FAB is clicked. */
|
/* Adds topic to topicList when FAB is clicked. */
|
||||||
private fun fabOnClick() {
|
private fun fabOnClick() {
|
||||||
val intent = Intent(this, AddTopicActivity::class.java)
|
val intent = Intent(this, AddTopicActivity::class.java)
|
||||||
startActivityForResult(intent, newTopicActivityRequestCode)
|
startActivityForResult(intent, newTopicActivityRequestCode)
|
||||||
|
@ -81,13 +75,13 @@ class TopicsListActivity : AppCompatActivity() {
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, intentData)
|
super.onActivityResult(requestCode, resultCode, intentData)
|
||||||
|
|
||||||
/* Inserts flower 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 flowerName = data.getStringExtra(FLOWER_NAME)
|
val topicName = data.getStringExtra(TOPIC_NAME)
|
||||||
val flowerDescription = data.getStringExtra(FLOWER_DESCRIPTION)
|
val topicDescription = data.getStringExtra(TOPIC_DESCRIPTION)
|
||||||
|
|
||||||
topicsListViewModel.insertFlower(flowerName, flowerDescription)
|
topicsListViewModel.insertTopic(topicName, topicDescription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,27 +25,25 @@ import kotlin.random.Random
|
||||||
|
|
||||||
class TopicsListViewModel(val dataSource: DataSource) : ViewModel() {
|
class TopicsListViewModel(val dataSource: DataSource) : ViewModel() {
|
||||||
|
|
||||||
val topicsLiveData = dataSource.getFlowerList()
|
val topicsLiveData = dataSource.getTopicList()
|
||||||
|
|
||||||
/* If the name and description are present, create new Flower and add it to the datasource */
|
/* If the name and description are present, create new Topic and add it to the datasource */
|
||||||
fun insertFlower(flowerName: String?, flowerDescription: String?) {
|
fun insertTopic(topicName: String?, topicDescription: String?) {
|
||||||
if (flowerName == null || flowerDescription == null) {
|
if (topicName == null || topicDescription == null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val image = dataSource.getRandomFlowerImageAsset()
|
val newTopic = Topic(
|
||||||
val newFlower = Topic(
|
|
||||||
Random.nextLong(),
|
Random.nextLong(),
|
||||||
flowerName,
|
topicName,
|
||||||
image,
|
topicDescription
|
||||||
flowerDescription
|
|
||||||
)
|
)
|
||||||
|
|
||||||
dataSource.addFlower(newFlower)
|
dataSource.addTopic(newTopic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FlowersListViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
|
class TopicsListViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
|
||||||
|
|
||||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||||
if (modelClass.isAssignableFrom(TopicsListViewModel::class.java)) {
|
if (modelClass.isAssignableFrom(TopicsListViewModel::class.java)) {
|
||||||
|
|
Before Width: | Height: | Size: 361 KiB |
Before Width: | Height: | Size: 410 KiB |
Before Width: | Height: | Size: 739 KiB |
Before Width: | Height: | Size: 348 KiB |
Before Width: | Height: | Size: 823 KiB |
Before Width: | Height: | Size: 2.1 MiB |
Before Width: | Height: | Size: 647 KiB |
Before Width: | Height: | Size: 884 KiB |
Before Width: | Height: | Size: 614 KiB |
Before Width: | Height: | Size: 880 KiB |
Before Width: | Height: | Size: 961 KiB |
Before Width: | Height: | Size: 566 KiB |
|
@ -33,12 +33,12 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
android:hint="@string/flower_name_edit_text"
|
android:hint="@string/topic_name_edit_text"
|
||||||
android:paddingTop="16dp"
|
android:paddingTop="16dp"
|
||||||
android:paddingBottom="16dp">
|
android:paddingBottom="16dp">
|
||||||
|
|
||||||
<com.google.android.material.textfield.TextInputEditText
|
<com.google.android.material.textfield.TextInputEditText
|
||||||
android:id="@+id/add_flower_name"
|
android:id="@+id/add_topic_name"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
@ -48,12 +48,12 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
android:hint="@string/flower_description_edit_text"
|
android:hint="@string/topic_description_edit_text"
|
||||||
android:paddingTop="16dp"
|
android:paddingTop="16dp"
|
||||||
android:paddingBottom="16dp">
|
android:paddingBottom="16dp">
|
||||||
|
|
||||||
<com.google.android.material.textfield.TextInputEditText
|
<com.google.android.material.textfield.TextInputEditText
|
||||||
android:id="@+id/add_flower_description"
|
android:id="@+id/add_topic_description"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:inputType="textMultiLine" />
|
android:inputType="textMultiLine" />
|
|
@ -1,42 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!--
|
|
||||||
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.
|
|
||||||
-->
|
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content">
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/flower_image"
|
|
||||||
android:layout_width="48dp"
|
|
||||||
android:layout_height="48dp"
|
|
||||||
android:layout_margin="8dp"
|
|
||||||
android:contentDescription="@string/flower_image_content_description"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintEnd_toStartOf="@+id/flower_text"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/flower_text"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_margin="8dp"
|
|
||||||
android:text="@string/flower1_name"
|
|
||||||
android:textAppearance="?attr/textAppearanceHeadline5"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintStart_toEndOf="@+id/flower_image"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
|
@ -16,29 +16,4 @@
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content"/>
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/header_text"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:text="@string/flower_finder"
|
|
||||||
android:textAppearance="?attr/textAppearanceHeadline3" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/flower_number_text"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:textAppearance="?attr/textAppearanceHeadline6" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/flower_text"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:text="@string/flower_string"
|
|
||||||
android:textAppearance="?attr/textAppearanceHeadline6" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
|
@ -19,29 +19,21 @@
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/flower_detail_name"
|
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="@string/flower1_name"
|
android:text="@string/topic1_name"
|
||||||
android:textAlignment="center"
|
android:textAlignment="center"
|
||||||
android:textAppearance="?attr/textAppearanceHeadline3" />
|
android:textAppearance="?attr/textAppearanceHeadline3" />
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/flower_detail_image"
|
|
||||||
android:layout_width="320dp"
|
|
||||||
android:layout_height="320dp"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:contentDescription="@string/flower_image_content_description"
|
|
||||||
app:srcCompat="@drawable/rose" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/flower_detail_description"
|
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/flower1_description" />
|
android:text="@string/topic1_description" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/remove_button"
|
android:id="@+id/remove_button"
|
31
app/src/main/res/layout/topic_item.xml
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="80dp"
|
||||||
|
android:orientation="vertical">
|
||||||
|
<TextView
|
||||||
|
android:text="https://ntfy.sh/example"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" android:id="@+id/topic_text"
|
||||||
|
android:layout_marginTop="16dp" android:layout_marginLeft="16dp"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
|
||||||
|
<TextView
|
||||||
|
android:text="Subscribed, 0 messages"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" android:id="@+id/topic_status"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:layout_marginLeft="16dp"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
|
@ -15,47 +15,23 @@
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">ntfy.sh</string>
|
<string name="app_name">Ntfy</string>
|
||||||
<string name="add_topic">Add Topic</string>
|
<string name="add_topic">Add Topic</string>
|
||||||
|
|
||||||
<string name="flower1_name">Rose</string>
|
<string name="topic1_name">Rose</string>
|
||||||
<string name="flower2_name">Freesia</string>
|
<string name="topic2_name">Freesia</string>
|
||||||
<string name="flower3_name">Lily</string>
|
|
||||||
<string name="flower4_name">Sunflower</string>
|
|
||||||
<string name="flower5_name">Peony</string>
|
|
||||||
<string name="flower6_name">Daisy</string>
|
|
||||||
<string name="flower7_name">Lilac</string>
|
|
||||||
<string name="flower8_name">Marigold</string>
|
|
||||||
<string name="flower9_name">Poppy</string>
|
|
||||||
<string name="flower10_name">Daffodil</string>
|
|
||||||
<string name="flower11_name">Dahlia</string>
|
|
||||||
<string name="flower12_name">Tulip</string>
|
|
||||||
|
|
||||||
<string name="flower1_description">Rose comes from the Latin word Rosa. There are over 100
|
<string name="topic1_description">Rose comes from the Latin word Rosa. There are over 100
|
||||||
species of the rose. </string>
|
species of the rose. </string>
|
||||||
<string name="flower2_description">Freesias bloom during spring and are native to
|
<string name="topic2_description">Freesias bloom during spring and are native to
|
||||||
Africa.</string>
|
Africa.</string>
|
||||||
<string name="flower3_description">Lilies have the longest in vase lifespan of any cut
|
|
||||||
bloom.</string>
|
|
||||||
<string name="flower4_description">Mature Sunflowers face east and are native too the United
|
|
||||||
States.</string>
|
|
||||||
<string name="flower5_description">Peony plants can live to be 100 years old.</string>
|
|
||||||
<string name="flower6_description">Daisies are cousins with Sunflowers.</string>
|
|
||||||
<string name="flower7_description">Lilacs belong to the olive family.</string>
|
|
||||||
<string name="flower8_description">Marigolds come in orange, reed, maroon and yellow.</string>
|
|
||||||
<string name="flower9_description">Poppies can be over 3 feet tall.</string>
|
|
||||||
<string name="flower10_description">Daffodils are referred to as Lent Lilies in England</string>
|
|
||||||
<string name="flower11_description">Dahlia is named after the Swedish botanist Anders
|
|
||||||
Dahl</string>
|
|
||||||
<string name="flower12_description">There are over 150 species of tulips.</string>
|
|
||||||
|
|
||||||
<string name="flower_string">Flowers</string>
|
<string name="topic_string">Topics</string>
|
||||||
<string name="flower_finder">Flower finder</string>
|
<string name="topic_finder">Topic finder</string>
|
||||||
<string name="flower_description_edit_text">Flower Description</string>
|
<string name="topic_description_edit_text">Topic Description</string>
|
||||||
<string name="flower_name_edit_text">Flower Name</string>
|
<string name="topic_name_edit_text">Topic Name</string>
|
||||||
<string name="done_button_text">Done</string>
|
<string name="done_button_text">Done</string>
|
||||||
<string name="fab_content_description">fab</string>
|
<string name="fab_content_description">fab</string>
|
||||||
<string name="flower_image_content_description">Image of flower</string>
|
|
||||||
<string name="remove_topic">Unsubscribe</string>
|
<string name="remove_topic">Unsubscribe</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -16,10 +16,13 @@
|
||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<!-- Base application theme. -->
|
<!-- Base application theme. -->
|
||||||
|
<!-- No action bar, https://stackoverflow.com/a/36236700 -->
|
||||||
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
|
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
|
||||||
<item name="colorPrimary">@color/colorPrimary</item>
|
<item name="colorPrimary">@color/colorPrimary</item>
|
||||||
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
|
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
|
||||||
<item name="colorSecondary">@color/colorAccent</item>
|
<item name="colorSecondary">@color/colorAccent</item>
|
||||||
|
<item name="windowActionBar">false</item>
|
||||||
|
<item name="windowNoTitle">true</item>
|
||||||
|
<!--<item name="android:windowFullscreen">true</item>-->
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|