2021-10-26 02:01:10 +13:00
|
|
|
/*
|
|
|
|
* 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.app.Activity
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import androidx.activity.viewModels
|
2021-10-26 07:24:44 +13:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-10-26 09:16:23 +13:00
|
|
|
import androidx.lifecycle.Observer
|
|
|
|
import androidx.lifecycle.asLiveData
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-10-26 02:01:10 +13:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-10-26 09:16:23 +13:00
|
|
|
import io.heckel.ntfy.R
|
2021-10-26 07:24:44 +13:00
|
|
|
import io.heckel.ntfy.add.AddTopicActivity
|
|
|
|
import io.heckel.ntfy.add.TOPIC_URL
|
2021-10-26 09:16:23 +13:00
|
|
|
import io.heckel.ntfy.data.NtfyApi
|
2021-10-26 02:01:10 +13:00
|
|
|
import io.heckel.ntfy.data.Topic
|
2021-10-26 07:24:44 +13:00
|
|
|
import io.heckel.ntfy.detail.TopicDetailActivity
|
2021-10-26 09:16:23 +13:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
const val TOPIC_ID = "topic id"
|
2021-10-26 02:01:10 +13:00
|
|
|
|
|
|
|
class TopicsListActivity : AppCompatActivity() {
|
2021-10-26 09:16:23 +13:00
|
|
|
private val api = NtfyApi(this)
|
2021-10-26 02:01:10 +13:00
|
|
|
private val newTopicActivityRequestCode = 1
|
|
|
|
private val topicsListViewModel by viewModels<TopicsListViewModel> {
|
2021-10-26 06:45:56 +13:00
|
|
|
TopicsListViewModelFactory(this)
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_main)
|
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
val adapter = TopicsAdapter { topic -> adapterOnClick(topic) }
|
2021-10-26 02:01:10 +13:00
|
|
|
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
|
2021-10-26 06:45:56 +13:00
|
|
|
recyclerView.adapter = adapter
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
topicsListViewModel.topicsLiveData.observe(this) {
|
2021-10-26 02:01:10 +13:00
|
|
|
it?.let {
|
2021-10-26 06:45:56 +13:00
|
|
|
adapter.submitList(it as MutableList<Topic>)
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
2021-10-26 06:45:56 +13:00
|
|
|
}
|
2021-10-26 02:01:10 +13:00
|
|
|
|
|
|
|
val fab: View = findViewById(R.id.fab)
|
|
|
|
fab.setOnClickListener {
|
|
|
|
fabOnClick()
|
|
|
|
}
|
2021-10-26 09:16:23 +13:00
|
|
|
|
|
|
|
val self = this
|
|
|
|
api.getEventsFlow().asLiveData(Dispatchers.IO).observe(this, Observer { event ->
|
|
|
|
// Get the Activity's lifecycleScope and launch
|
|
|
|
this.lifecycleScope.launch(Dispatchers.Main) {
|
|
|
|
// run the code again in IO context
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
println(event.data)
|
|
|
|
//Toast.makeText(self, event.data, Toast.LENGTH_SHORT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
/* Opens TopicDetailActivity when RecyclerView item is clicked. */
|
|
|
|
private fun adapterOnClick(topic: Topic) {
|
2021-10-26 02:01:10 +13:00
|
|
|
val intent = Intent(this, TopicDetailActivity()::class.java)
|
2021-10-26 06:45:56 +13:00
|
|
|
intent.putExtra(TOPIC_ID, topic.id)
|
2021-10-26 02:01:10 +13:00
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
/* Adds topic to topicList when FAB is clicked. */
|
2021-10-26 02:01:10 +13:00
|
|
|
private fun fabOnClick() {
|
|
|
|
val intent = Intent(this, AddTopicActivity::class.java)
|
|
|
|
startActivityForResult(intent, newTopicActivityRequestCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, intentData)
|
|
|
|
|
2021-10-26 06:45:56 +13:00
|
|
|
/* Inserts topic into viewModel. */
|
2021-10-26 02:01:10 +13:00
|
|
|
if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) {
|
|
|
|
intentData?.let { data ->
|
2021-10-26 07:24:44 +13:00
|
|
|
val topicName = data.getStringExtra(TOPIC_URL)
|
|
|
|
topicsListViewModel.insertTopic(topicName)
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|