2018-10-25 18:52:33 +02:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2018-10-25 18:52:33 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2018-10-30 20:41:22 +01:00
package org.mosad.seil0.projectlaogai.fragments
2018-10-25 18:52:33 +02:00
import android.os.Bundle
2019-06-25 17:10:57 +02:00
import android.util.TypedValue
2018-10-25 18:52:33 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-02-17 15:05:03 +01:00
import androidx.fragment.app.Fragment
2018-12-14 15:02:19 +01:00
import com.afollestad.aesthetic.Aesthetic
2018-10-26 00:50:37 +02:00
import com.afollestad.materialdialogs.MaterialDialog
2020-06-07 14:59:04 +02:00
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.getActionButton
2018-10-26 00:50:37 +02:00
import com.afollestad.materialdialogs.color.colorChooser
2020-06-07 14:59:04 +02:00
import com.afollestad.materialdialogs.list.listItemsMultiChoice
2019-06-25 17:10:57 +02:00
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import de.psdev.licensesdialog.LicensesDialog
2020-02-04 19:58:07 +01:00
import kotlinx.coroutines.*
2019-03-19 19:51:58 +01:00
import org.mosad.seil0.projectlaogai.BuildConfig
2019-06-25 17:10:57 +02:00
import org.mosad.seil0.projectlaogai.R
2020-08-31 23:03:58 +02:00
import org.mosad.seil0.projectlaogai.controller.GradesController
2020-08-10 14:31:17 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.cache.CacheController.Companion.coursesList
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
2020-09-08 18:33:00 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences
2020-12-06 18:00:28 +01:00
import org.mosad.seil0.projectlaogai.databinding.FragmentSettingsBinding
2020-09-08 18:33:00 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.CourseSelectionDialog
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.LoadingDialog
2020-08-11 17:09:46 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.LoginDialog
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.DataTypes
2019-02-17 15:05:03 +01:00
import java.util.*
2019-06-25 17:10:57 +02:00
2018-10-25 18:52:33 +02:00
/ * *
* The settings controller class
* contains all needed parts to display and the settings screen
* /
class SettingsFragment : Fragment ( ) {
2020-12-06 18:00:28 +01:00
private lateinit var binding : FragmentSettingsBinding
2018-10-26 00:50:37 +02:00
2020-06-07 14:59:04 +02:00
private var selectedTheme = DataTypes . Theme . Light
2020-12-06 18:00:28 +01:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentSettingsBinding . inflate ( inflater , container , false )
return binding . root
2020-08-17 22:30:12 +02:00
}
2018-10-25 18:52:33 +02:00
2020-08-17 22:30:12 +02:00
/ * *
* initialize the settings gui
* /
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2018-10-25 18:52:33 +02:00
2018-10-26 00:50:37 +02:00
initActions ( )
2020-12-06 18:00:28 +01:00
binding . textUser . text = EncryptedPreferences . email . ifEmpty { resources . getString ( R . string . sample _user ) }
binding . textCourse . text = Preferences . course . courseName
binding . textAboutDesc . text = resources . getString ( R . string . about _version , BuildConfig . VERSION _NAME , getString ( R . string . build _time ) )
binding . textGradesSyncDesc . text = if ( Preferences . gradesSyncInterval == 0 ) {
2020-08-31 23:03:58 +02:00
resources . getString ( R . string . grades _sync _desc _never )
} else {
resources . getString ( R . string . grades _sync _desc , Preferences . gradesSyncInterval )
}
2020-12-06 18:00:28 +01:00
binding . switchBuffet . isChecked = Preferences . showBuffet // init switch
2019-06-24 22:41:45 +02:00
val outValue = TypedValue ( )
activity !! . theme . resolveAttribute ( R . attr . themeName , outValue , true )
2019-06-25 12:07:18 +02:00
when ( outValue . string ) {
" light " -> {
2020-12-06 18:00:28 +01:00
binding . switchBuffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryLight , activity !! . theme ) )
2020-06-07 14:59:04 +02:00
selectedTheme = DataTypes . Theme . Light
selectedTheme . string = resources . getString ( R . string . themeLight )
2019-06-25 12:07:18 +02:00
}
" dark " -> {
2020-12-06 18:00:28 +01:00
binding . switchBuffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryDark , activity !! . theme ) )
2020-06-07 14:59:04 +02:00
selectedTheme = DataTypes . Theme . Dark
selectedTheme . string = resources . getString ( R . string . themeDark )
2019-06-25 12:07:18 +02:00
}
" black " -> {
2020-12-06 18:00:28 +01:00
binding . switchBuffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryDark , activity !! . theme ) )
2020-06-07 14:59:04 +02:00
selectedTheme = DataTypes . Theme . Black
selectedTheme . string = resources . getString ( R . string . themeBlack )
2019-06-25 12:07:18 +02:00
}
2019-06-24 22:41:45 +02:00
}
2020-12-06 18:00:28 +01:00
binding . textThemeSelected . text = selectedTheme . string
2018-11-07 14:17:33 +01:00
}
2018-10-26 00:50:37 +02:00
/ * *
* initialize some actions for SettingsFragment elements
* /
private fun initActions ( ) {
2020-12-06 18:00:28 +01:00
binding . linLayoutUser . setOnClickListener {
2018-10-26 00:50:37 +02:00
// open a new dialog
2020-08-11 17:09:46 +02:00
LoginDialog ( context !! )
. positiveButton {
2020-08-13 21:01:21 +02:00
EncryptedPreferences . saveCredentials ( email , password , context )
2020-08-11 17:09:46 +02:00
}
. show {
2020-08-13 21:01:21 +02:00
email = EncryptedPreferences . email
2020-08-12 11:00:22 +02:00
password = " "
2020-08-11 17:09:46 +02:00
}
2018-10-26 00:50:37 +02:00
}
2020-12-06 18:00:28 +01:00
binding . linLayoutUser . setOnLongClickListener {
2020-08-13 21:01:21 +02:00
Preferences . oGiants = true // enable easter egg
2019-08-18 22:09:33 +02:00
return @setOnLongClickListener true
}
2020-12-06 18:00:28 +01:00
binding . linLayoutCourse . setOnClickListener {
2020-09-08 18:33:00 +02:00
CourseSelectionDialog ( context !! ) . show {
list = coursesList . map { it . courseName }
listItems {
val loadingDialog = LoadingDialog ( context )
loadingDialog . show ( )
GlobalScope . launch ( Dispatchers . Default ) {
Preferences . saveCourse ( context , coursesList [ selectedIndex ] ) // save the course
// update current & next weeks timetable
val threads = listOf (
CacheController . updateTimetable ( Preferences . course . courseName , 0 , context ) ,
CacheController . updateTimetable ( Preferences . course . courseName , 1 , context )
)
threads . joinAll ( ) // blocking since we want the new data
withContext ( Dispatchers . Main ) {
loadingDialog . dismiss ( )
}
}
}
2020-02-25 15:25:45 +01:00
onDismiss {
2020-12-06 18:00:28 +01:00
binding . textCourse . text = Preferences . course . courseName // update txtView after the dialog is dismissed
2020-02-25 15:25:45 +01:00
}
}
2018-10-26 00:50:37 +02:00
}
2020-12-06 18:00:28 +01:00
binding . linLayoutManageLessons . setOnClickListener {
2020-06-11 21:41:05 +02:00
val lessons = ArrayList < String > ( )
2020-06-12 00:51:50 +02:00
TimetableController . subjectMap . forEach { pair ->
2020-06-11 21:41:05 +02:00
pair . value . forEach {
lessons . add ( " ${pair.key} - $it " )
}
}
2020-06-07 14:59:04 +02:00
MaterialDialog ( context !! ) . show {
title ( R . string . manage _lessons )
2020-06-11 21:41:05 +02:00
positiveButton ( R . string . delete )
2020-06-07 14:59:04 +02:00
negativeButton ( R . string . cancel )
2020-09-02 15:36:03 +02:00
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( Preferences . colorAccent )
getActionButton ( WhichButton . NEGATIVE ) . updateTextColor ( Preferences . colorAccent )
2020-06-11 21:41:05 +02:00
2020-08-07 12:14:50 +02:00
listItemsMultiChoice ( items = lessons ) { _ , _ , items ->
2020-06-11 21:41:05 +02:00
items . forEach {
val list = it . split ( " - " )
2020-08-10 14:31:17 +02:00
TimetableController . removeSubject ( list [ 0 ] , list [ 1 ] , context )
2020-06-11 21:41:05 +02:00
}
}
2020-06-07 14:59:04 +02:00
}
}
2020-12-06 18:00:28 +01:00
binding . linLayoutAbout . setOnClickListener {
2018-10-26 00:50:37 +02:00
// open a new info dialog
MaterialDialog ( context !! )
2019-03-19 19:51:58 +01:00
. title ( R . string . about _dialog _heading )
. message ( R . string . about _dialog _text )
2018-10-26 00:50:37 +02:00
. show ( )
}
2020-12-06 18:00:28 +01:00
binding . linLayoutLicence . setOnClickListener {
2019-08-25 22:19:36 +02:00
// do the theme magic, as the lib's theme support is broken
val outValue = TypedValue ( )
context !! . theme . resolveAttribute ( R . attr . themeName , outValue , true )
val dialogCss = when ( outValue . string ) {
" light " -> R . string . license _dialog _style _light
else -> R . string . license _dialog _style _dark
}
val themeId = when ( outValue . string ) {
" light " -> R . style . AppTheme _Light
else -> R . style . LicensesDialogTheme _Dark
}
2019-06-25 17:10:57 +02:00
// open a new license dialog
LicensesDialog . Builder ( context !! )
. setNotices ( R . raw . notices )
. setTitle ( R . string . licenses )
. setIncludeOwnLicense ( true )
2019-08-25 22:19:36 +02:00
. setThemeResourceId ( themeId )
. setNoticesCssStyle ( dialogCss )
2019-06-25 17:10:57 +02:00
. build ( )
. show ( )
}
2020-12-06 18:00:28 +01:00
binding . linLayoutTheme . setOnClickListener {
2019-06-25 12:07:18 +02:00
val themes = listOf (
resources . getString ( R . string . themeLight ) ,
resources . getString ( R . string . themeDark ) ,
resources . getString ( R . string . themeBlack )
)
2020-06-07 14:59:04 +02:00
MaterialDialog ( context !! ) . show {
title ( R . string . theme )
listItemsSingleChoice ( items = themes , initialSelection = selectedTheme . ordinal ) { _ , index , _ ->
2019-06-25 12:07:18 +02:00
Aesthetic . config {
when ( index ) {
0 -> activityTheme ( R . style . AppTheme _Light )
1 -> activityTheme ( R . style . AppTheme _Dark )
2 -> activityTheme ( R . style . AppTheme _Black )
else -> activityTheme ( R . style . AppTheme _Light )
}
2019-12-13 14:54:51 +01:00
apply ( )
2019-06-25 12:07:18 +02:00
}
}
}
}
2020-12-06 18:00:28 +01:00
binding . linLayoutPrimaryColor . setOnClickListener {
2018-10-26 00:50:37 +02:00
// open a new color chooser dialog
MaterialDialog ( context !! )
2020-09-02 15:36:03 +02:00
. colorChooser ( DataTypes ( ) . primaryColors , allowCustomArgb = true , initialSelection = Preferences . colorPrimary ) { _ , color ->
2020-12-06 18:00:28 +01:00
binding . viewPrimaryColor . setBackgroundColor ( color )
2018-12-14 15:02:19 +01:00
Aesthetic . config {
colorPrimary ( color )
colorPrimaryDark ( color )
apply ( )
}
2020-08-13 21:01:21 +02:00
Preferences . saveColorPrimary ( context !! , color )
2019-03-09 22:15:53 +01:00
}
2020-06-07 14:59:04 +02:00
. show {
title ( R . string . primary _color )
positiveButton ( R . string . select )
2020-09-02 15:36:03 +02:00
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( Preferences . colorAccent )
2020-06-07 14:59:04 +02:00
}
2019-03-09 22:15:53 +01:00
}
2020-12-06 18:00:28 +01:00
binding . linLayoutAccentColor . setOnClickListener {
2019-03-09 22:15:53 +01:00
// open a new color chooser dialog
MaterialDialog ( context !! )
2020-09-02 15:36:03 +02:00
. colorChooser ( DataTypes ( ) . accentColors , allowCustomArgb = true , initialSelection = Preferences . colorAccent ) { _ , color ->
2020-12-06 18:00:28 +01:00
binding . viewAccentColor . setBackgroundColor ( color )
2019-03-09 22:15:53 +01:00
Aesthetic . config {
colorAccent ( color )
apply ( )
}
2020-08-13 21:01:21 +02:00
Preferences . saveColorAccent ( context !! , color )
2018-10-26 00:50:37 +02:00
}
2020-06-07 14:59:04 +02:00
. show {
title ( R . string . accent _color )
positiveButton ( R . string . select )
2020-09-02 15:36:03 +02:00
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( Preferences . colorAccent )
2020-06-07 14:59:04 +02:00
}
2018-10-26 00:50:37 +02:00
}
2019-03-09 22:15:53 +01:00
2020-12-06 18:00:28 +01:00
binding . linLayoutGradesSync . setOnClickListener {
2020-08-31 23:03:58 +02:00
val items = resources . getStringArray ( R . array . syncInterval ) . toList ( )
val initial = when ( Preferences . gradesSyncInterval ) {
1 -> 1
3 -> 2
6 -> 3
12 -> 4
else -> 0
}
MaterialDialog ( context !! )
. title ( R . string . grades _sync )
. listItemsSingleChoice ( items = items , initialSelection = initial ) { _ , index , _ ->
val interval = when ( index ) {
1 -> 1
2 -> 3
3 -> 6
4 -> 12
else -> 0
}
Preferences . saveGradesSync ( context !! , interval )
GradesController . updateBackgroundSync ( context !! )
2020-12-06 18:00:28 +01:00
binding . textGradesSyncDesc . text = if ( Preferences . gradesSyncInterval == 0 ) {
2020-08-31 23:03:58 +02:00
resources . getString ( R . string . grades _sync _desc _never )
} else {
resources . getString ( R . string . grades _sync _desc , Preferences . gradesSyncInterval )
}
} . show ( )
}
2020-12-06 18:00:28 +01:00
binding . switchBuffet . setOnClickListener {
Preferences . saveShowBuffet ( context !! , binding . switchBuffet . isChecked )
2019-03-10 13:37:13 +01:00
}
2018-10-26 00:50:37 +02:00
}
2018-10-25 18:52:33 +02:00
}