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

166 lines
6.0 KiB
Kotlin
Raw Normal View History

package org.mosad.teapod.ui.activity.main.fragments
2020-10-08 22:20:20 +02:00
2021-05-26 19:46:46 +02:00
import android.app.Activity
import android.content.Intent
import android.net.Uri
2020-10-08 22:20:20 +02:00
import android.os.Bundle
import android.util.Log
2020-10-08 22:20:20 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-05-26 19:46:46 +02:00
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
2020-10-08 22:20:20 +02:00
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
2020-10-14 20:58:42 +02:00
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import kotlinx.coroutines.launch
import org.mosad.teapod.BuildConfig
2020-10-08 22:20:20 +02:00
import org.mosad.teapod.R
2020-11-25 22:35:55 +01:00
import org.mosad.teapod.databinding.FragmentAccountBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.preferences.EncryptedPreferences
import org.mosad.teapod.preferences.Preferences
2021-05-26 19:46:46 +02:00
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.ui.components.LoginDialog
import org.mosad.teapod.util.DataTypes.Theme
2021-05-26 19:46:46 +02:00
import org.mosad.teapod.util.StorageController
2021-01-20 16:10:41 +01:00
import org.mosad.teapod.util.showFragment
2020-10-08 22:20:20 +02:00
class AccountFragment : Fragment() {
2020-11-25 22:35:55 +01:00
private lateinit var binding: FragmentAccountBinding
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()
}
}
}
}
2021-05-26 19:46:46 +02:00
2020-11-25 22:35:55 +01:00
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentAccountBinding.inflate(inflater, container, false)
return binding.root
2020-10-08 22:20:20 +02:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// load subscription (async) info before anything else
binding.textAccountSubscription.text = getString(R.string.account_subscription, getString(R.string.loading))
lifecycleScope.launch {
binding.textAccountSubscription.text = getString(
R.string.account_subscription,
AoDParser.getSubscriptionInfoAsync().await()
)
}
2020-11-25 22:35:55 +01:00
binding.textAccountLogin.text = EncryptedPreferences.login
binding.textInfoAboutDesc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
binding.textThemeSelected.text = when (Preferences.theme) {
Theme.DARK -> getString(R.string.theme_dark)
else -> getString(R.string.theme_light)
}
2020-11-25 22:35:55 +01:00
binding.switchSecondary.isChecked = Preferences.preferSecondary
binding.switchAutoplay.isChecked = Preferences.autoplay
binding.linearDevSettings.isVisible = Preferences.devSettings
initActions()
}
private fun initActions() {
2020-11-25 22:35:55 +01:00
binding.linearAccountLogin.setOnClickListener {
showLoginDialog(true)
}
binding.linearAccountSubscription.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(AoDParser.getSubscriptionUrl())))
}
2020-11-25 22:35:55 +01:00
binding.linearTheme.setOnClickListener {
showThemeDialog()
}
2020-11-25 22:35:55 +01:00
binding.linearInfo.setOnClickListener {
2021-01-20 16:10:41 +01:00
activity?.showFragment(AboutFragment())
}
2020-11-25 22:35:55 +01:00
binding.switchSecondary.setOnClickListener {
Preferences.savePreferSecondary(requireContext(), binding.switchSecondary.isChecked)
}
2020-11-25 22:35:55 +01:00
binding.switchAutoplay.setOnClickListener {
Preferences.saveAutoplay(requireContext(), binding.switchAutoplay.isChecked)
}
binding.linearExportData.setOnClickListener {
2021-05-26 19:46:46 +02:00
val i = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "text/json"
putExtra(Intent.EXTRA_TITLE, "my-list.json")
}
getUriExport.launch(i)
}
binding.linearImportData.setOnClickListener {
2021-05-26 19:46:46 +02:00
val i = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
}
getUriImport.launch(i)
}
2020-10-08 22:20:20 +02:00
}
private fun showLoginDialog(firstTry: Boolean) {
LoginDialog(requireContext(), firstTry).positiveButton {
EncryptedPreferences.saveCredentials(login, password, context)
2020-10-19 19:59:53 +02:00
if (!AoDParser.login()) {
showLoginDialog(false)
Log.w(javaClass.name, "Login failed, please try again.")
}
}.show {
login = EncryptedPreferences.login
password = ""
}
}
private fun showThemeDialog() {
val themes = listOf(
resources.getString(R.string.theme_light),
resources.getString(R.string.theme_dark)
)
MaterialDialog(requireContext()).show {
title(R.string.theme)
listItemsSingleChoice(items = themes, initialSelection = Preferences.theme.ordinal) { _, index, _ ->
when(index) {
0 -> Preferences.saveTheme(context, Theme.LIGHT)
1 -> Preferences.saveTheme(context, Theme.DARK)
2021-05-26 19:46:46 +02:00
else -> Preferences.saveTheme(context, Theme.DARK)
}
(activity as MainActivity).restart()
}
}
}
2020-10-08 22:20:20 +02:00
}