2018-10-25 18:52:33 +02:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < 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
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
2018-10-26 00:50:37 +02:00
import kotlinx.android.synthetic.main.fragment_settings.*
2018-11-05 20:59:41 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2019-03-19 19:51:58 +01:00
import org.mosad.seil0.projectlaogai.BuildConfig
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
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.coursesList
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-24 22:41:45 +02:00
import android.util.TypedValue
2018-10-26 00:50:37 +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-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-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-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 )
if ( outValue . string == " dark " ) {
switch _buffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryDark , activity !! . theme ) )
} else {
switch _buffet . setTextColor ( activity !! . resources . getColor ( R . color . textPrimaryLight , activity !! . theme ) )
}
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-06-24 22:41:45 +02:00
// TODO move and save
// val outValue = TypedValue()
// activity!!.theme.resolveAttribute(R.attr.themeName, outValue, true)
// if (outValue.string == "dark") {
// Aesthetic.config {
// activityTheme(R.style.AppTheme_Light)
// }
// } else {
// Aesthetic.config {
// activityTheme(R.style.AppTheme_Dark)
// }
// }
2018-10-26 00:50:37 +02:00
}
linLayoutCourse . setOnClickListener {
2019-03-17 18:38:31 +01:00
selectCourse ( context !! )
txtView _Course . text = cCourse . courseName // update txtView
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-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-03-09 22:15:53 +01:00
cColorPrimary = color
PreferencesController . save ( context !! )
}
. 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 ( )
}
cColorAccent = color
2019-02-17 15:05:03 +01:00
PreferencesController . save ( context !! )
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 {
cShowBuffet = switchBuffet . isChecked
PreferencesController . save ( context !! )
println ( switchBuffet . isChecked )
}
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 ( )
doAsync {
cCourse = coursesList [ index ] // set the course
PreferencesController . save ( context )
// update current & next weeks timetable
2019-04-06 14:13:01 +02:00
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context ) . get ( ) // blocking since we want the new data
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context ) . get ( ) // 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
uiThread {
dialog . dismiss ( )
}
}
}
. show ( )
}
2018-10-25 18:52:33 +02:00
}