Browse Source

don't use deprecated startActivityForResult() use registerForActivityResult() instead

pull/41/head
Jannik 1 year ago
parent
commit
a3a89c6b64
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 53
      app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/AccountFragment.kt
  2. 6
      app/src/main/res/values-de-rDE/strings.xml
  3. 6
      app/src/main/res/values/strings.xml

53
app/src/main/java/org/mosad/teapod/ui/activity/main/fragments/AccountFragment.kt

@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
@ -31,8 +32,27 @@ class AccountFragment : Fragment() {
private lateinit var binding: FragmentAccountBinding
private val exportFileCode = 1111
private val importFileCode = 1122
private val getUriExport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.also { uri ->
StorageController.exportMyList(requireContext(), uri)
}
}
}
private val getUriImport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.also { uri ->
val success = StorageController.importMyList(requireContext(), uri)
if (success == 0) {
Toast.makeText(
context, getString(R.string.import_data_success),
Toast.LENGTH_SHORT
).show()
}
}
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentAccountBinding.inflate(inflater, container, false)
@ -66,30 +86,6 @@ class AccountFragment : Fragment() {
initActions()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// return if the result was not ok
if (resultCode != Activity.RESULT_OK) {
Log.e(javaClass.name, "Error while computing result. Result code is: $resultCode")
return
}
when(requestCode) {
exportFileCode -> data?.data?.also { uri ->
StorageController.exportMyList(requireContext(), uri)
}
importFileCode -> data?.data?.also { uri ->
val success = StorageController.importMyList(requireContext(), uri)
if (success == 0) {
Toast.makeText(
context, getString(R.string.import_data_success),
Toast.LENGTH_SHORT
).show()
}
}
}
}
private fun initActions() {
binding.linearAccountLogin.setOnClickListener {
showLoginDialog(true)
@ -121,7 +117,7 @@ class AccountFragment : Fragment() {
type = "text/json"
putExtra(Intent.EXTRA_TITLE, "my-list.json")
}
startActivityForResult(i, exportFileCode)
getUriExport.launch(i)
}
binding.linearImportData.setOnClickListener {
@ -129,7 +125,7 @@ class AccountFragment : Fragment() {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
}
startActivityForResult(i, importFileCode)
getUriImport.launch(i)
}
}
@ -166,4 +162,5 @@ class AccountFragment : Fragment() {
}
}
}
}

6
app/src/main/res/values-de-rDE/strings.xml

@ -48,10 +48,10 @@
<string name="theme_dark">Dunkel</string>
<string name="dev_settings">Entwickler Einstellungen</string>
<string name="export_data">Daten exportieren</string>
<string name="export_data_desc">Speichere "meine Liste" in eine Datei</string>
<string name="export_data_desc">Speichere "Meine Liste" in eine Datei</string>
<string name="import_data">Daten importieren</string>
<string name="import_data_desc">Lade "meine Liste" aus einer Datei</string>
<string name="import_data_success">"meine Liste" erfolgreich importiert</string>
<string name="import_data_desc">Lade "Meine Liste" aus einer Datei</string>
<string name="import_data_success">"Meine Liste" erfolgreich importiert</string>
<!-- about fragment -->
<string name="version">Version</string>

6
app/src/main/res/values/strings.xml

@ -59,10 +59,10 @@
<string name="theme_dark">Dark</string>
<string name="dev_settings">Developer Settings</string>
<string name="export_data">export data</string>
<string name="export_data_desc">export "my list" to a file</string>
<string name="export_data_desc">export "My list" to a file</string>
<string name="import_data">import data</string>
<string name="import_data_desc">import "my list" from a file</string>
<string name="import_data_success">imported "my list" successfully</string>
<string name="import_data_desc">import "My list" from a file</string>
<string name="import_data_success">imported "My list" successfully</string>
<!-- about fragment -->

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close