mirror of
https://github.com/rt-bishop/Look4Sat.git
synced 2026-10-02 03:15:37 +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
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.findNavController
|
||||
@@ -39,19 +38,22 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@AndroidEntryPoint
|
||||
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 entriesAdapter: EntriesAdapter? = null
|
||||
private val viewModel: SharedViewModel by activityViewModels()
|
||||
private val pickFileReqCode = 100
|
||||
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
binding = FragmentEntriesBinding.bind(view)
|
||||
setupComponents()
|
||||
observeEntries()
|
||||
}
|
||||
|
||||
|
||||
private fun setupComponents() {
|
||||
entriesAdapter = EntriesAdapter()
|
||||
binding?.apply {
|
||||
@@ -62,7 +64,7 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
||||
addItemDecoration(RecyclerDivider(R.drawable.rec_divider_light))
|
||||
}
|
||||
importWeb.setOnClickListener { showImportFromWebDialog() }
|
||||
importFile.setOnClickListener { showImportFromFileDialog() }
|
||||
importFile.setOnClickListener { filePicker.launch("*/*") }
|
||||
selectAll.setOnClickListener { entriesAdapter?.selectAll() }
|
||||
entriesFab.setOnClickListener { navigateToPasses() }
|
||||
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() {
|
||||
binding?.apply {
|
||||
entriesError.visibility = View.INVISIBLE
|
||||
@@ -122,7 +112,7 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
||||
entriesProgress.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun setError() {
|
||||
binding?.apply {
|
||||
entriesProgress.visibility = View.INVISIBLE
|
||||
@@ -130,7 +120,11 @@ class EntriesFragment : Fragment(R.layout.fragment_entries) {
|
||||
entriesError.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun showImportFromWebDialog() {
|
||||
findNavController().navigate(R.id.action_entries_to_dialog_sources)
|
||||
}
|
||||
|
||||
private fun navigateToPasses() {
|
||||
binding?.searchBar?.clearFocus()
|
||||
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() {
|
||||
entriesAdapter = null
|
||||
binding = null
|
||||
|
||||
Reference in new issue
Block a user