2020-10-15 19:01:37 +02:00
package org.mosad.teapod.ui.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-13 21:27:05 +02:00
import de.psdev.licensesdialog.LicensesDialog
2020-10-08 22:20:20 +02:00
import kotlinx.android.synthetic.main.fragment_account.*
2020-10-11 23:16:47 +02:00
import org.mosad.teapod.BuildConfig
2020-11-23 20:11:10 +01:00
import org.mosad.teapod.MainActivity
2020-10-08 22:20:20 +02:00
import org.mosad.teapod.R
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
2020-10-08 22:20:20 +02:00
class AccountFragment : Fragment ( ) {
2020-11-23 20:11:10 +01:00
2020-10-08 22:20:20 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
return inflater . inflate ( R . layout . fragment _account , container , false )
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2020-10-11 23:16:47 +02:00
text _account _login . text = EncryptedPreferences . login
text _info _about _desc . text = getString ( R . string . info _about _desc , BuildConfig . VERSION _NAME , getString ( R . string . build _time ) )
2020-11-23 20:11:10 +01:00
text _theme _selected . text = when ( Preferences . theme ) {
Theme . DARK -> getString ( R . string . theme _dark )
else -> getString ( R . string . theme _light )
}
2020-10-23 11:28:47 +02:00
switch _secondary . isChecked = Preferences . preferSecondary
2020-11-15 13:39:33 +01:00
switch _autoplay . isChecked = Preferences . autoplay
2020-10-11 23:16:47 +02:00
initActions ( )
}
private fun initActions ( ) {
linear _account _login . setOnClickListener {
2020-10-12 23:26:32 +02:00
showLoginDialog ( true )
2020-10-11 23:16:47 +02:00
}
2020-11-23 20:11:10 +01:00
linear _theme . setOnClickListener {
showThemeDialog ( )
}
2020-10-11 23:16:47 +02:00
linear _about . setOnClickListener {
2020-10-14 20:58:42 +02:00
MaterialDialog ( requireContext ( ) )
. title ( R . string . info _about )
. message ( R . string . info _about _dialog )
2020-10-13 21:27:05 +02:00
. show ( )
}
text _licenses . setOnClickListener {
2020-11-23 09:53:44 +01:00
2020-11-23 20:11:10 +01:00
val dialogCss = when ( Preferences . theme ) {
Theme . DARK -> R . string . license _dialog _style _dark
2020-11-23 09:53:44 +01:00
else -> R . string . license _dialog _style _light
}
2020-11-23 20:11:10 +01:00
val themeId = when ( Preferences . theme ) {
Theme . DARK -> R . style . LicensesDialogTheme _Dark
2020-11-23 09:53:44 +01:00
else -> R . style . AppTheme _Light
}
2020-10-13 21:27:05 +02:00
LicensesDialog . Builder ( requireContext ( ) )
. setNotices ( R . raw . notices )
. setTitle ( R . string . licenses )
. setIncludeOwnLicense ( true )
2020-11-23 09:53:44 +01:00
. setThemeResourceId ( themeId )
. setNoticesCssStyle ( dialogCss )
2020-10-13 21:27:05 +02:00
. build ( )
2020-10-11 23:16:47 +02:00
. show ( )
}
2020-10-23 11:28:47 +02:00
switch _secondary . setOnClickListener {
Preferences . savePreferSecondary ( requireContext ( ) , switch _secondary . isChecked )
}
2020-11-15 13:39:33 +01:00
switch _autoplay . setOnClickListener {
Preferences . saveAutoplay ( requireContext ( ) , switch _autoplay . isChecked )
}
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
}