/** * ProjectLaogai * * Copyright 2018 * * 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 androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout 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.MainActivity import org.mosad.seil0.projectlaogai.R import org.mosad.seil0.projectlaogai.hsoparser.CourseTTLink import java.util.ArrayList /** * 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 private lateinit var courseTTLinkList: ArrayList private var mainActivity = MainActivity() 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 } /** * initialize some actions for SettingsFragment elements */ private fun initActions() { linLayoutUser.setOnClickListener { // open a new dialog } linLayoutCourse.setOnClickListener { // open a new dialog val courseList = ArrayList() courseTTLinkList = mainActivity.getCourseTTLinkList() courseTTLinkList.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 { mainActivity.updateCourse(courseTTLinkList[index]) 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.parseColor("#3F51B5"), Color.RED, Color.GREEN, Color.BLUE) MaterialDialog(context!!) .title(R.string.primary_color) .colorChooser(colors, initialSelection = Color.parseColor("#3F51B5")) { _, color -> viewPrimaryColor.setBackgroundColor(color) } .positiveButton(R.string.select) .show() } } fun setMainActivity(mainActivity: MainActivity) { this.mainActivity = mainActivity } }