ProjectLaogai/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/SettingsFragment.kt

176 lines
6.5 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.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
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.PreferencesController
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColorAccent
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cCourseTTLinkList
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableCurrentWeek
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableNextWeek
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.TimeTableParser
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 linLayoutInfo: LinearLayout
private lateinit var linLayoutPrimaryColor: LinearLayout
private lateinit var linLayoutAccentColor: LinearLayout
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)
linLayoutInfo = view.findViewById(R.id.linLayout_Info)
linLayoutPrimaryColor = view.findViewById(R.id.linLayout_PrimaryColor)
linLayoutAccentColor = view.findViewById(R.id.linLayout_AccentColor)
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
}
/**
* initialize some actions for SettingsFragment elements
*/
private fun initActions() {
linLayoutUser.setOnClickListener {
// open a new dialog
}
linLayoutCourse.setOnClickListener {
// open a new dialog
val courseList = ArrayList<String>()
cCourseTTLinkList.forEach { (_, course) ->
courseList.add(course)
}
MaterialDialog(context!!).listItems(items = courseList) { _, index, text ->
txtView_Course.text = text // update txtView
val dialog = MaterialDialog(context!!).cancelable(false)
.cancelOnTouchOutside(false)
.customView(R.layout.dialog_loading)
dialog.show()
doAsync {
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"))
uiThread {
dialog.dismiss()
}
}
}
.show()
}
linLayoutInfo.setOnClickListener {
// open a new info dialog
MaterialDialog(context!!)
.title(R.string.about)
.message(R.string.about_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 ->
//viewAccentColor.setBackgroundColor(color)
view_AccentColor.setBackgroundColor(color)
Aesthetic.config {
colorAccent(color)
apply()
}
cColorAccent = color
PreferencesController.save(context!!)
}
.positiveButton(R.string.select)
.show()
}
}
}