195 lines
7.3 KiB
Kotlin
195 lines
7.3 KiB
Kotlin
/**
|
|
* ProjectLaogai
|
|
*
|
|
* Copyright 2019 <seil0@mosad.xyz>
|
|
*
|
|
* 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.
|
|
*
|
|
*/
|
|
|
|
package org.mosad.seil0.projectlaogai.fragments
|
|
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.LinearLayout
|
|
import android.widget.Switch
|
|
import androidx.fragment.app.Fragment
|
|
import com.afollestad.aesthetic.Aesthetic
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
|
import com.afollestad.materialdialogs.color.colorChooser
|
|
import com.afollestad.materialdialogs.customview.customView
|
|
import com.afollestad.materialdialogs.list.listItems
|
|
import kotlinx.android.synthetic.main.fragment_settings.*
|
|
import org.jetbrains.anko.doAsync
|
|
import org.jetbrains.anko.uiThread
|
|
import org.mosad.seil0.projectlaogai.BuildConfig
|
|
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.R
|
|
import org.mosad.seil0.projectlaogai.controller.CacheController
|
|
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.coursesList
|
|
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
|
|
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
|
|
import java.util.*
|
|
|
|
/**
|
|
* The settings controller class
|
|
* contains all needed parts to display and the settings screen
|
|
*/
|
|
class SettingsFragment : Fragment() {
|
|
|
|
private lateinit var linLayoutUser: LinearLayout
|
|
private lateinit var linLayoutCourse: LinearLayout
|
|
private lateinit var linLayoutAbout: LinearLayout
|
|
private lateinit var linLayoutPrimaryColor: LinearLayout
|
|
private lateinit var linLayoutAccentColor: LinearLayout
|
|
private lateinit var switchBuffet: Switch
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
val view: View = inflater.inflate(R.layout.fragment_settings, container, false)
|
|
|
|
linLayoutUser = view.findViewById(R.id.linLayout_User)
|
|
linLayoutCourse = view.findViewById(R.id.linLayout_Course)
|
|
linLayoutAbout = view.findViewById(R.id.linLayout_About)
|
|
linLayoutPrimaryColor = view.findViewById(R.id.linLayout_PrimaryColor)
|
|
linLayoutAccentColor = view.findViewById(R.id.linLayout_AccentColor)
|
|
switchBuffet = view.findViewById(R.id.switch_buffet)
|
|
|
|
initActions()
|
|
|
|
// Inflate the layout for this fragment
|
|
return view
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
txtView_Course.text = cCourse.courseName
|
|
txtView_AboutDesc.text = resources.getString(R.string.about_version, BuildConfig.VERSION_NAME, getString(R.string.build_time))
|
|
switch_buffet.isChecked = cShowBuffet // init switch
|
|
}
|
|
|
|
/**
|
|
* initialize some actions for SettingsFragment elements
|
|
*/
|
|
private fun initActions() {
|
|
linLayoutUser.setOnClickListener {
|
|
// open a new dialog
|
|
}
|
|
|
|
linLayoutCourse.setOnClickListener {
|
|
selectCourse(context!!)
|
|
txtView_Course.text = cCourse.courseName // update txtView
|
|
}
|
|
|
|
linLayoutAbout.setOnClickListener {
|
|
// open a new info dialog
|
|
MaterialDialog(context!!)
|
|
.title(R.string.about_dialog_heading)
|
|
.message(R.string.about_dialog_text)
|
|
.show()
|
|
}
|
|
|
|
linLayoutPrimaryColor.setOnClickListener {
|
|
// open a new color chooser dialog
|
|
MaterialDialog(context!!)
|
|
.title(R.string.primary_color)
|
|
.colorChooser(DataTypes().primaryColors, allowCustomArgb = true, initialSelection = cColorPrimary) { _, color ->
|
|
view_PrimaryColor.setBackgroundColor(color)
|
|
Aesthetic.config {
|
|
colorPrimary(color)
|
|
colorPrimaryDark(color)
|
|
apply()
|
|
}
|
|
|
|
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
|
|
PreferencesController.save(context!!)
|
|
}
|
|
.positiveButton(R.string.select)
|
|
.show()
|
|
}
|
|
|
|
switchBuffet.setOnClickListener {
|
|
cShowBuffet = switchBuffet.isChecked
|
|
PreferencesController.save(context!!)
|
|
println(switchBuffet.isChecked)
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
.listItems(items = courseNameList) { _, index, _ ->
|
|
|
|
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
|
|
TCoRAPIController(context).getTimetable(cCourse.courseName, 0)
|
|
TCoRAPIController(context).getTimetable(cCourse.courseName, 1)
|
|
|
|
CacheController(context).readTimetable(cCourse.courseName, 0)
|
|
CacheController(context).readTimetable(cCourse.courseName, 1)
|
|
|
|
uiThread {
|
|
dialog.dismiss()
|
|
}
|
|
}
|
|
|
|
}
|
|
.show()
|
|
}
|
|
|
|
}
|