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
2019-03-17 18:38:31 +01:00
import android.content.Context
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
2018-10-26 00:50:37 +02:00
import android.widget.LinearLayout
2020-02-25 15:25:45 +01:00
import android.widget.TextView
2020-08-10 14:51:40 +02:00
import androidx.appcompat.widget.SwitchCompat
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
2020-02-25 15:25:45 +01:00
import com.afollestad.materialdialogs.callbacks.onDismiss
2018-10-26 00:50:37 +02:00
import com.afollestad.materialdialogs.color.colorChooser
2018-11-05 20:59:41 +01:00
import com.afollestad.materialdialogs.customview.customView
2018-10-27 00:39:56 +02:00
import com.afollestad.materialdialogs.list.listItems
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
2018-10-26 00:50:37 +02:00
import kotlinx.android.synthetic.main.fragment_settings.*
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-10 14:31:17 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.cache.CacheController.Companion.coursesList
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences
2020-08-10 14:31:17 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.EncryptedPreferences
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cColorAccent
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cColorPrimary
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cCourse
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cShowBuffet
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 ( ) {
2018-10-26 00:50:37 +02:00
private lateinit var linLayoutUser : LinearLayout
private lateinit var linLayoutCourse : LinearLayout
2020-06-07 14:59:04 +02:00
private lateinit var linLayoutManageLessons : LinearLayout
2019-03-19 19:51:58 +01:00
private lateinit var linLayoutAbout : LinearLayout
2019-06-25 17:10:57 +02:00
private lateinit var linLayoutLicence : LinearLayout
2019-06-25 12:07:18 +02:00
private lateinit var linLayoutTheme : LinearLayout
2019-03-09 22:15:53 +01:00
private lateinit var linLayoutPrimaryColor : LinearLayout
private lateinit var linLayoutAccentColor : LinearLayout
2020-08-10 14:51:40 +02:00
private lateinit var switchBuffet : SwitchCompat
2020-02-25 15:25:45 +01:00
private lateinit var txtViewCourse : TextView
2018-10-26 00:50:37 +02:00
2020-06-07 14:59:04 +02:00
private var selectedTheme = DataTypes . Theme . Light
2018-10-25 18:52:33 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
2020-08-17 22:30:12 +02:00
return inflater . inflate ( R . layout . fragment _settings , container , false )
}
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
linLayoutUser = view . findViewById ( R . id . linLayout _User )
linLayoutCourse = view . findViewById ( R . id . linLayout _Course )
2020-06-07 14:59:04 +02:00
linLayoutManageLessons = view . findViewById ( R . id . linLayout _ManageLessons )
2019-03-19 19:51:58 +01:00
linLayoutAbout = view . findViewById ( R . id . linLayout _About )
2019-06-25 17:10:57 +02:00
linLayoutLicence = view . findViewById ( R . id . linLayout _Licence )
2019-06-25 12:07:18 +02:00
linLayoutTheme = view . findViewById ( R . id . linLayout _Theme )
2019-03-09 22:15:53 +01:00
linLayoutPrimaryColor = view . findViewById ( R . id . linLayout _PrimaryColor )
linLayoutAccentColor = view . findViewById ( R . id . linLayout _AccentColor )
2019-03-10 13:37:13 +01:00
switchBuffet = view . findViewById ( R . id . switch _buffet )
2020-08-17 22:30:12 +02:00
2020-02-25 15:25:45 +01:00
// if we call txtView_Course via KAE view binding it'll result in a NPE in the onDismissed call
txtViewCourse = view . findViewById ( R . id . txtView _Course )
2018-10-26 00:50:37 +02:00
initActions ( )
2018-11-07 14:17:33 +01:00
2020-08-15 18:13:29 +02:00
txtView _User . text = EncryptedPreferences . email . ifEmpty { resources . getString ( R . string . sample _user ) }
2019-02-17 15:05:03 +01:00
txtView _Course . text = cCourse . courseName
2019-03-19 19:51:58 +01:00
txtView _AboutDesc . text = resources . getString ( R . string . about _version , BuildConfig . VERSION _NAME , getString ( R . string . build _time ) )
2019-03-10 13:37:13 +01:00
switch _buffet . isChecked = cShowBuffet // 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 " -> {
switch _buffet . 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 " -> {
switch _buffet . 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 " -> {
switch _buffet . 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-06-07 14:59:04 +02:00
txtView _SelectedTheme . 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 ( ) {
linLayoutUser . setOnClickListener {
// 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
}
2019-08-18 22:09:33 +02:00
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
}
2018-10-26 00:50:37 +02:00
linLayoutCourse . setOnClickListener {
2020-02-25 15:25:45 +01:00
selectCourse ( context !! ) . show {
onDismiss {
txtViewCourse . text = cCourse . courseName // update txtView after the dialog is dismissed
}
}
2018-10-26 00:50:37 +02:00
}
2020-06-07 14:59:04 +02:00
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 )
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( cColorAccent )
getActionButton ( WhichButton . NEGATIVE ) . updateTextColor ( cColorAccent )
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
}
}
2019-03-19 19:51:58 +01:00
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 ( )
}
2019-06-25 17:10:57 +02:00
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 ( )
}
2019-06-25 12:07:18 +02:00
linLayoutTheme . setOnClickListener {
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
}
}
}
}
2019-03-09 22:15:53 +01:00
linLayoutPrimaryColor . setOnClickListener {
2018-10-26 00:50:37 +02:00
// open a new color chooser dialog
MaterialDialog ( context !! )
2019-03-09 22:15:53 +01:00
. colorChooser ( DataTypes ( ) . primaryColors , allowCustomArgb = true , initialSelection = cColorPrimary ) { _ , color ->
view _PrimaryColor . 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 )
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( cColorAccent )
}
2019-03-09 22:15:53 +01:00
}
linLayoutAccentColor . setOnClickListener {
// open a new color chooser dialog
MaterialDialog ( context !! )
. colorChooser ( DataTypes ( ) . accentColors , allowCustomArgb = true , initialSelection = cColorAccent ) { _ , color ->
view _AccentColor . setBackgroundColor ( color )
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 )
getActionButton ( WhichButton . POSITIVE ) . updateTextColor ( cColorAccent )
}
2018-10-26 00:50:37 +02:00
}
2019-03-09 22:15:53 +01:00
2019-03-10 13:37:13 +01:00
switchBuffet . setOnClickListener {
2020-08-13 21:01:21 +02:00
Preferences . saveShowBuffet ( context !! , switchBuffet . isChecked )
2019-03-10 13:37:13 +01:00
}
2018-10-26 00:50:37 +02:00
}
2020-02-25 15:25:45 +01:00
fun selectCourse ( context : Context ) : MaterialDialog {
2019-03-17 18:38:31 +01:00
val courseNameList = ArrayList < String > ( )
coursesList . forEach { ( _ , courseName ) ->
courseNameList . add ( courseName )
}
2020-02-25 15:25:45 +01:00
// return a new course selection dialog
return MaterialDialog ( context )
2019-03-17 18:38:31 +01:00
. title ( R . string . select _course )
2019-03-19 19:51:58 +01:00
. listItems ( items = courseNameList ) { _ , index , _ ->
2019-03-17 18:38:31 +01:00
2020-02-25 15:25:45 +01:00
val loadingDialog = MaterialDialog ( context ) . cancelable ( false )
2019-03-17 18:38:31 +01:00
. cancelOnTouchOutside ( false )
. customView ( R . layout . dialog _loading )
2020-02-25 15:25:45 +01:00
loadingDialog . show ( )
2019-03-17 18:38:31 +01:00
2020-02-04 19:58:07 +01:00
GlobalScope . launch ( Dispatchers . Default ) {
2020-08-13 21:01:21 +02:00
Preferences . saveCourse ( context , coursesList [ index ] ) // save the course
2019-03-17 18:38:31 +01:00
// update current & next weeks timetable
2020-02-04 19:58:07 +01:00
val threads = listOf (
2020-08-07 12:14:50 +02:00
CacheController . updateTimetable ( cCourse . courseName , 0 , context ) ,
CacheController . updateTimetable ( cCourse . courseName , 1 , context )
2020-02-04 19:58:07 +01:00
)
threads . joinAll ( ) // blocking since we want the new data
2019-03-17 18:38:31 +01:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2020-02-25 15:25:45 +01:00
loadingDialog . dismiss ( )
2019-03-17 18:38:31 +01:00
}
}
}
}
2018-10-25 18:52:33 +02:00
}