mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-03 19:27:24 +00:00
Switched to using the new Activity Result API
This commit is contained in:
1 parent
bfeeff5372
commit
386c087946
1 file changed
+16
-28
@@ -19,10 +19,9 @@
|
|||||||
|
|
||||||
package com.rtbishop.look4sat.ui.mainScreen
|
package com.rtbishop.look4sat.ui.mainScreen
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
@@ -39,19 +38,22 @@ import dagger.hilt.android.AndroidEntryPoint
|
|||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
||||||
|
|
||||||
|
private val viewModel: SharedViewModel by activityViewModels()
|
||||||
|
private val filePicker =
|
||||||
|
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
|
||||||
|
viewModel.updateEntriesFromFile(uri)
|
||||||
|
}
|
||||||
private var binding: FragmentEntriesBinding? = null
|
private var binding: FragmentEntriesBinding? = null
|
||||||
private var entriesAdapter: EntriesAdapter? = null
|
private var entriesAdapter: EntriesAdapter? = null
|
||||||
private val viewModel: SharedViewModel by activityViewModels()
|
|
||||||
private val pickFileReqCode = 100
|
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
binding = FragmentEntriesBinding.bind(view)
|
binding = FragmentEntriesBinding.bind(view)
|
||||||
setupComponents()
|
setupComponents()
|
||||||
observeEntries()
|
observeEntries()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupComponents() {
|
private fun setupComponents() {
|
||||||
entriesAdapter = EntriesAdapter()
|
entriesAdapter = EntriesAdapter()
|
||||||
binding?.apply {
|
binding?.apply {
|
||||||
@@ -62,7 +64,7 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
|||||||
addItemDecoration(RecyclerDivider(R.drawable.rec_divider_light))
|
addItemDecoration(RecyclerDivider(R.drawable.rec_divider_light))
|
||||||
}
|
}
|
||||||
importWeb.setOnClickListener { showImportFromWebDialog() }
|
importWeb.setOnClickListener { showImportFromWebDialog() }
|
||||||
importFile.setOnClickListener { showImportFromFileDialog() }
|
importFile.setOnClickListener { filePicker.launch("*/*") }
|
||||||
selectAll.setOnClickListener { entriesAdapter?.selectAll() }
|
selectAll.setOnClickListener { entriesAdapter?.selectAll() }
|
||||||
entriesFab.setOnClickListener { navigateToPasses() }
|
entriesFab.setOnClickListener { navigateToPasses() }
|
||||||
searchBar.setOnQueryTextListener(entriesAdapter)
|
searchBar.setOnQueryTextListener(entriesAdapter)
|
||||||
@@ -95,18 +97,6 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showImportFromWebDialog() {
|
|
||||||
findNavController().navigate(R.id.action_entries_to_dialog_sources)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun showImportFromFileDialog() {
|
|
||||||
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
|
||||||
type = "*/*"
|
|
||||||
startActivityForResult(this, pickFileReqCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setLoaded() {
|
private fun setLoaded() {
|
||||||
binding?.apply {
|
binding?.apply {
|
||||||
entriesError.visibility = View.INVISIBLE
|
entriesError.visibility = View.INVISIBLE
|
||||||
@@ -122,7 +112,7 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
|||||||
entriesProgress.visibility = View.VISIBLE
|
entriesProgress.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setError() {
|
private fun setError() {
|
||||||
binding?.apply {
|
binding?.apply {
|
||||||
entriesProgress.visibility = View.INVISIBLE
|
entriesProgress.visibility = View.INVISIBLE
|
||||||
@@ -130,7 +120,11 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
|||||||
entriesError.visibility = View.VISIBLE
|
entriesError.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun showImportFromWebDialog() {
|
||||||
|
findNavController().navigate(R.id.action_entries_to_dialog_sources)
|
||||||
|
}
|
||||||
|
|
||||||
private fun navigateToPasses() {
|
private fun navigateToPasses() {
|
||||||
binding?.searchBar?.clearFocus()
|
binding?.searchBar?.clearFocus()
|
||||||
entriesAdapter?.let {
|
entriesAdapter?.let {
|
||||||
@@ -142,12 +136,6 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
||||||
if (requestCode == pickFileReqCode && resultCode == AppCompatActivity.RESULT_OK) {
|
|
||||||
data?.data?.also { uri -> viewModel.updateEntriesFromFile(uri) }
|
|
||||||
} else super.onActivityResult(requestCode, resultCode, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
entriesAdapter = null
|
entriesAdapter = null
|
||||||
binding = null
|
binding = null
|
||||||
|
|||||||
Reference in new issue
Block a user