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
2019-03-10 13:37:13 +01:00
import android.widget.Switch
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
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
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
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.coursesList
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.controller.PreferencesController
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorAccent
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
2019-03-09 22:15:53 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.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
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
2019-03-10 13:37:13 +01:00
private lateinit var switchBuffet : Switch
2018-10-26 00:50:37 +02:00
2018-10-25 18:52:33 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _settings , container , false )
2018-10-26 00:50:37 +02:00
linLayoutUser = view . findViewById ( R . id . linLayout _User )
linLayoutCourse = view . findViewById ( R . id . linLayout _Course )
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 )
2018-10-26 00:50:37 +02:00
initActions ( )
2018-10-25 18:52:33 +02:00
// Inflate the layout for this fragment
return view
}
2018-11-07 14:17:33 +01:00
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2019-06-25 12:07:18 +02:00
// initialize the settings gui
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 ) )
txtView _SelectedTheme . text = resources . getString ( R . string . themeLight )
}
" dark " -> {
switch _buffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryDark , activity !! . theme ) )
txtView _SelectedTheme . text = resources . getString ( R . string . themeDark )
}
" black " -> {
switch _buffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryDark , activity !! . theme ) )
txtView _SelectedTheme . text = resources . getString ( R . string . themeBlack )
}
2019-06-24 22:41:45 +02:00
}
2019-06-25 12:07:18 +02:00
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
}
2019-08-18 22:09:33 +02:00
linLayoutUser . setOnLongClickListener {
PreferencesController . oGiants = true // enable easter egg
return @setOnLongClickListener true
}
2018-10-26 00:50:37 +02:00
linLayoutCourse . setOnClickListener {
2019-03-17 18:38:31 +01:00
selectCourse ( context !! )
2018-10-26 00:50:37 +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-02-23 20:09:27 +01:00
MaterialDialog ( context !! )
. title ( R . string . theme )
. show {
2019-06-25 12:07:18 +02:00
listItemsSingleChoice ( items = themes ) { _ , index , _ ->
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 !! )
. title ( R . string . primary _color )
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 ( )
}
2019-01-03 01:45:28 +01:00
2019-12-13 14:54:51 +01:00
PreferencesController . saveColorPrimary ( context !! , color )
2019-03-09 22:15:53 +01:00
}
. positiveButton ( R . string . select )
. show ( )
}
linLayoutAccentColor . setOnClickListener {
// open a new color chooser dialog
MaterialDialog ( context !! )
. title ( R . string . accent _color )
. colorChooser ( DataTypes ( ) . accentColors , allowCustomArgb = true , initialSelection = cColorAccent ) { _ , color ->
view _AccentColor . setBackgroundColor ( color )
Aesthetic . config {
colorAccent ( color )
apply ( )
}
2019-12-13 14:54:51 +01:00
PreferencesController . saveColorAccent ( context !! , color )
2018-10-26 00:50:37 +02:00
}
. positiveButton ( R . string . select )
. show ( )
}
2019-03-09 22:15:53 +01:00
2019-03-10 13:37:13 +01:00
switchBuffet . setOnClickListener {
2019-12-13 14:54:51 +01:00
PreferencesController . saveShowBuffet ( context !! , switchBuffet . isChecked )
2019-03-10 13:37:13 +01:00
}
2018-10-26 00:50:37 +02:00
}
2019-03-17 18:38:31 +01:00
fun selectCourse ( context : Context ) {
val courseNameList = ArrayList < String > ( )
coursesList . forEach { ( _ , courseName ) ->
courseNameList . add ( courseName )
}
// open a new dialog
MaterialDialog ( context )
. 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
val dialog = MaterialDialog ( context ) . cancelable ( false )
. cancelOnTouchOutside ( false )
. customView ( R . layout . dialog _loading )
dialog . show ( )
2020-02-04 19:58:07 +01:00
GlobalScope . launch ( Dispatchers . Default ) {
2019-12-13 14:54:51 +01:00
PreferencesController . 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 (
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context ) ,
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context )
)
threads . joinAll ( ) // blocking since we want the new data
2019-03-17 18:38:31 +01:00
2019-04-06 14:13:01 +02:00
CacheController . readTimetable ( cCourse . courseName , 0 , context )
CacheController . readTimetable ( cCourse . courseName , 1 , context )
2019-03-17 18:38:31 +01:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2019-03-17 18:38:31 +01:00
dialog . dismiss ( )
2019-09-23 22:44:03 +02:00
txtView _Course . text = cCourse . courseName // update txtView
2019-03-17 18:38:31 +01:00
}
}
}
. show ( )
}
2018-10-25 18:52:33 +02:00
}