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
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-02-17 15:05:03 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController
2019-03-09 22:15:53 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColorAccent
2019-02-17 15:05:03 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourseTTLinkList
2019-03-10 13:37:13 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cShowBuffet
2019-02-17 15:05:03 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableCurrentWeek
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableNextWeek
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2019-03-09 22:15:53 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
2019-02-17 15:05:03 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.TimeTableParser
import java.util.*
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
private lateinit var linLayoutInfo : 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 )
linLayoutInfo = view . findViewById ( R . id . linLayout _Info )
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-10 13:37:13 +01:00
switch _buffet . isChecked = cShowBuffet // init switch
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
}
linLayoutCourse . setOnClickListener {
// open a new dialog
2018-11-01 02:12:43 +01:00
val courseList = ArrayList < String > ( )
2019-02-17 15:05:03 +01:00
cCourseTTLinkList . forEach { ( _ , course ) ->
2018-11-01 02:12:43 +01:00
courseList . add ( course )
}
2018-10-27 00:39:56 +02:00
2019-02-17 15:05:03 +01:00
MaterialDialog ( context !! ) . listItems ( items = courseList ) { _ , index , text ->
2018-11-05 20:59:41 +01:00
txtView _Course . text = text // update txtView
val dialog = MaterialDialog ( context !! ) . cancelable ( false )
. cancelOnTouchOutside ( false )
. customView ( R . layout . dialog _loading )
dialog . show ( )
doAsync {
2019-02-17 15:05:03 +01:00
cCourse = cCourseTTLinkList [ index ] // set the course
PreferencesController . save ( context !! )
// update current & next weeks timetable
cTimeTableCurrentWeek = TimeTableParser ( ) . getTimeTable ( cCourse . courseLink )
cTimeTableNextWeek = TimeTableParser ( ) . getTimeTable ( cCourse . courseLink . replace ( " week=0 " , " week=1 " ) )
2018-11-05 20:59:41 +01:00
uiThread {
dialog . dismiss ( )
}
}
2018-11-01 02:12:43 +01:00
2018-10-27 00:39:56 +02:00
}
2019-02-17 15:05:03 +01:00
. show ( )
2018-10-26 00:50:37 +02:00
}
linLayoutInfo . setOnClickListener {
// open a new info dialog
MaterialDialog ( context !! )
. title ( R . string . about )
. message ( R . string . about _text )
. 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 ->
//viewAccentColor.setBackgroundColor(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
}
2018-10-25 18:52:33 +02:00
}