2021-01-16 00:16:47 +01:00
package org.mosad.teapod.activity.main.fragments
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
import androidx.fragment.app.Fragment
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
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.BuildConfig
2021-01-16 00:16:47 +01:00
import org.mosad.teapod.activity.main.MainActivity
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
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
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
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 )
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
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
}
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
}
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 )
else -> Preferences . saveTheme ( context , Theme . LIGHT )
}
( activity as MainActivity ) . restart ( )
}
}
}
2020-10-08 22:20:20 +02:00
}