2021-02-06 19:02:12 +01:00
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
2021-04-17 20:59:37 +02:00
import android.content.Intent
2020-10-08 22:20:20 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-05-27 19:50:00 +02:00
import androidx.activity.result.contract.ActivityResultContracts
2021-05-09 20:31:51 +02:00
import androidx.core.view.isVisible
2020-10-08 22:20:20 +02:00
import androidx.fragment.app.Fragment
2021-06-06 17:54:19 +02:00
import androidx.lifecycle.lifecycleScope
2022-02-05 20:02:33 +01:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
2021-04-17 20:59:37 +02:00
import kotlinx.coroutines.launch
2020-10-11 23:16:47 +02:00
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
2022-02-05 20:02:33 +01:00
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.parser.crunchyroll.Profile
import org.mosad.teapod.parser.crunchyroll.supportedLocals
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.preferences.EncryptedPreferences
2020-10-23 11:28:47 +02:00
import org.mosad.teapod.preferences.Preferences
2021-05-26 19:46:46 +02:00
import org.mosad.teapod.ui.activity.main.MainActivity
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.ui.components.LoginDialog
2020-11-23 20:11:10 +01:00
import org.mosad.teapod.util.DataTypes.Theme
2021-01-20 16:10:41 +01:00
import org.mosad.teapod.util.showFragment
2022-02-05 20:02:33 +01:00
import java.util.*
2020-10-08 22:20:20 +02:00
class AccountFragment : Fragment ( ) {
2020-11-25 22:35:55 +01:00
private lateinit var binding : FragmentAccountBinding
2022-02-05 20:02:33 +01:00
private val profile : Deferred < Profile > = lifecycleScope . async {
Crunchyroll . profile ( )
}
2020-11-23 20:11:10 +01:00
2021-05-27 19:50:00 +02:00
private val getUriExport = registerForActivityResult ( ActivityResultContracts . StartActivityForResult ( ) ) { result ->
if ( result . resultCode == Activity . RESULT _OK ) {
result . data ?. data ?. also { uri ->
2022-01-03 14:10:41 +01:00
//StorageController.exportMyList(requireContext(), uri)
2021-05-27 19:50:00 +02:00
}
}
}
private val getUriImport = registerForActivityResult ( ActivityResultContracts . StartActivityForResult ( ) ) { result ->
if ( result . resultCode == Activity . RESULT _OK ) {
result . data ?. data ?. also { uri ->
2022-01-03 14:10:41 +01:00
// val success = StorageController.importMyList(requireContext(), uri)
// if (success == 0) {
// Toast.makeText(
// context, getString(R.string.import_data_success),
// Toast.LENGTH_SHORT
// ).show()
// }
2021-05-27 19:50:00 +02:00
}
}
}
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 )
2022-02-05 20:02:33 +01:00
binding . textAccountLogin . text = EncryptedPreferences . login
// TODO reimplement for cr, if possible (maybe account status would be better? (premium))
2021-04-17 20:59:37 +02:00
// load subscription (async) info before anything else
binding . textAccountSubscription . text = getString ( R . string . account _subscription , getString ( R . string . loading ) )
2021-06-06 17:54:19 +02:00
lifecycleScope . launch {
2021-04-17 20:59:37 +02:00
binding . textAccountSubscription . text = getString (
R . string . account _subscription ,
2021-12-27 21:14:35 +01:00
" TODO "
2021-04-17 20:59:37 +02:00
)
}
2022-02-05 20:02:33 +01:00
// add preferred subtitles
lifecycleScope . launch {
binding . textSettingsContentLanguageDesc . text = Locale . forLanguageTag (
profile . await ( ) . preferredContentSubtitleLanguage
) . displayLanguage
}
binding . switchSecondary . isChecked = Preferences . preferSecondary
binding . switchAutoplay . isChecked = Preferences . autoplay
2020-11-25 22:35:55 +01:00
binding . textThemeSelected . text = when ( Preferences . theme ) {
2020-11-23 20:11:10 +01:00
Theme . DARK -> getString ( R . string . theme _dark )
else -> getString ( R . string . theme _light )
}
2021-05-09 20:31:51 +02:00
binding . linearDevSettings . isVisible = Preferences . devSettings
2022-02-05 20:02:33 +01:00
binding . textInfoAboutDesc . text = getString ( R . string . info _about _desc , BuildConfig . VERSION _NAME , getString ( R . string . build _time ) )
2020-10-11 23:16:47 +02:00
initActions ( )
}
private fun initActions ( ) {
2020-11-25 22:35:55 +01:00
binding . linearAccountLogin . setOnClickListener {
2020-10-12 23:26:32 +02:00
showLoginDialog ( true )
2020-10-11 23:16:47 +02:00
}
2021-04-17 20:59:37 +02:00
binding . linearAccountSubscription . setOnClickListener {
2021-12-27 21:14:35 +01:00
// TODO
//startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(AoDParser.getSubscriptionUrl())))
2021-04-17 20:59:37 +02:00
}
2022-02-05 20:02:33 +01:00
binding . linearSettingsContentLanguage . setOnClickListener {
showContentLanguageSelection ( )
2020-10-11 23:16:47 +02:00
}
2020-10-23 11:28:47 +02:00
2020-11-25 22:35:55 +01:00
binding . switchSecondary . setOnClickListener {
Preferences . savePreferSecondary ( requireContext ( ) , binding . switchSecondary . isChecked )
2020-10-23 11:28:47 +02:00
}
2020-11-15 13:39:33 +01:00
2020-11-25 22:35:55 +01:00
binding . switchAutoplay . setOnClickListener {
Preferences . saveAutoplay ( requireContext ( ) , binding . switchAutoplay . isChecked )
2020-11-15 13:39:33 +01:00
}
2021-05-09 20:31:51 +02:00
2022-02-05 20:02:33 +01:00
binding . linearTheme . setOnClickListener {
showThemeDialog ( )
}
binding . linearInfo . setOnClickListener {
activity ?. showFragment ( AboutFragment ( ) )
}
2021-05-09 20:31:51 +02:00
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 " )
}
2021-05-27 19:50:00 +02:00
getUriExport . launch ( i )
2021-05-09 20:31:51 +02:00
}
binding . linearImportData . setOnClickListener {
2021-05-26 19:46:46 +02:00
val i = Intent ( Intent . ACTION _OPEN _DOCUMENT ) . apply {
addCategory ( Intent . CATEGORY _OPENABLE )
type = " */* "
}
2021-05-27 19:50:00 +02:00
getUriImport . launch ( i )
2021-05-09 20:31:51 +02:00
}
2020-10-08 22:20:20 +02:00
}
2020-10-12 23:26:32 +02:00
private fun showLoginDialog ( firstTry : Boolean ) {
LoginDialog ( requireContext ( ) , firstTry ) . positiveButton {
EncryptedPreferences . saveCredentials ( login , password , context )
2021-12-27 21:14:35 +01:00
// TODO
// if (!AoDParser.login()) {
// showLoginDialog(false)
// Log.w(javaClass.name, "Login failed, please try again.")
// }
2020-10-12 23:26:32 +02:00
} . show {
login = EncryptedPreferences . login
password = " "
}
}
2020-11-23 20:11:10 +01:00
2022-02-05 20:02:33 +01:00
private fun showContentLanguageSelection ( ) {
val items = supportedLocals . map {
if ( it . displayLanguage . isNotEmpty ( ) && it . displayCountry . isNotEmpty ( ) ) {
" ${it.displayLanguage} ( ${it.displayCountry} ) "
} else if ( it . displayCountry . isNotEmpty ( ) ) {
it . displayLanguage
} else {
getString ( R . string . settings _content _language _none )
}
} . toTypedArray ( )
MaterialAlertDialogBuilder ( requireContext ( ) )
. setTitle ( R . string . settings _content _language )
. setSingleChoiceItems ( items , 0 ) { _ , which ->
// TODO
}
. show ( )
}
2020-11-23 20:11:10 +01:00
private fun showThemeDialog ( ) {
2022-02-05 20:02:33 +01:00
val items = arrayOf (
2020-11-23 20:11:10 +01:00
resources . getString ( R . string . theme _light ) ,
resources . getString ( R . string . theme _dark )
)
2022-02-05 20:02:33 +01:00
MaterialAlertDialogBuilder ( requireContext ( ) )
. setTitle ( R . string . settings _content _language )
. setSingleChoiceItems ( items , Preferences . theme . ordinal ) { _ , which ->
when ( which ) {
0 -> Preferences . saveTheme ( requireContext ( ) , Theme . LIGHT )
1 -> Preferences . saveTheme ( requireContext ( ) , Theme . DARK )
else -> Preferences . saveTheme ( requireContext ( ) , Theme . DARK )
2020-11-23 20:11:10 +01:00
}
( activity as MainActivity ) . restart ( )
}
2022-02-05 20:02:33 +01:00
. show ( )
2020-11-23 20:11:10 +01:00
}
2021-05-27 19:50:00 +02:00
2020-10-08 22:20:20 +02:00
}