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
import android.net.Uri
2020-10-08 22:20:20 +02:00
import android.os.Bundle
2020-10-12 23:26:32 +02:00
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
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
2020-10-14 20:58:42 +02:00
import com.afollestad.materialdialogs.MaterialDialog
2020-11-23 20:11:10 +01:00
import com.afollestad.materialdialogs.list.listItemsSingleChoice
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
2020-10-12 23:26:32 +02:00
import org.mosad.teapod.parser.AoDParser
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-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
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 ->
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 )
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 ,
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 ) {
2020-11-23 20:11:10 +01:00
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
2020-10-11 23:16:47 +02:00
2021-05-09 20:31:51 +02:00
binding . linearDevSettings . isVisible = Preferences . devSettings
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 {
startActivity ( Intent ( Intent . ACTION _VIEW , Uri . parse ( AoDParser . getSubscriptionUrl ( ) ) ) )
}
2020-11-25 22:35:55 +01:00
binding . linearTheme . setOnClickListener {
2020-11-23 20:11:10 +01:00
showThemeDialog ( )
}
2020-11-25 22:35:55 +01:00
binding . linearInfo . setOnClickListener {
2021-01-20 16:10:41 +01:00
activity ?. showFragment ( AboutFragment ( ) )
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
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 )
2020-10-19 19:59:53 +02:00
if ( ! AoDParser . login ( ) ) {
2020-10-12 23:26:32 +02:00
showLoginDialog ( false )
Log . w ( javaClass . name , " Login failed, please try again. " )
}
} . show {
login = EncryptedPreferences . login
password = " "
}
}
2020-11-23 20:11:10 +01:00
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 )
2020-11-23 20:11:10 +01:00
}
( activity as MainActivity ) . restart ( )
}
}
}
2021-05-27 19:50:00 +02:00
2020-10-08 22:20:20 +02:00
}