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

157 lines
5.7 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.Android
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.PreferencesController
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cColor
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.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 linLayoutMainColor: LinearLayout
private lateinit var viewPrimaryColor: View
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)
linLayoutMainColor = view.findViewById(R.id.linLayout_MainColor)
viewPrimaryColor = view.findViewById(R.id.view_PrimaryColor)
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()
}
linLayoutMainColor.setOnClickListener {
// open a new color chooser dialog
val colors = intArrayOf(Color.BLACK, Color.DKGRAY, Color.RED, Color.GREEN, Color.YELLOW)
MaterialDialog(context!!)
.title(R.string.primary_color)
.colorChooser(colors, allowCustomArgb = true, initialSelection = cColor) { _, color ->
viewPrimaryColor.setBackgroundColor(color)
Aesthetic.config {
colorPrimary(color)
colorPrimaryDark(color)
apply()
}
cColor = color
PreferencesController.save(context!!)
}
.positiveButton(R.string.select)
.show()
}
}
}