2018-10-25 18:52:33 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2018 < 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 .
*
* /
2018-10-30 20:41:22 +01:00
package org.mosad.seil0.projectlaogai.fragments
2018-10-25 18:52:33 +02:00
2018-10-26 00:50:37 +02:00
import android.graphics.Color
2018-10-25 18:52:33 +02:00
import android.os.Bundle
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.Fragment
2018-10-25 18:52:33 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-10-26 00:50:37 +02:00
import android.widget.LinearLayout
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
2018-11-01 02:12:43 +01:00
import org.mosad.seil0.projectlaogai.MainActivity
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2018-11-01 02:12:43 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.CourseTTLink
import java.util.ArrayList
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
private lateinit var linLayoutMainColor : LinearLayout
private lateinit var viewPrimaryColor : View
2018-11-01 02:12:43 +01:00
private lateinit var courseTTLinkList : ArrayList < CourseTTLink >
private var mainActivity = MainActivity ( )
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 )
linLayoutMainColor = view . findViewById ( R . id . linLayout _MainColor )
viewPrimaryColor = view . findViewById ( R . id . view _PrimaryColor )
initActions ( )
2018-10-25 18:52:33 +02:00
// Inflate the layout for this fragment
return view
}
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 > ( )
courseTTLinkList = mainActivity . getCourseTTLinkList ( )
courseTTLinkList . forEach { ( _ , course ) ->
courseList . add ( course )
}
2018-10-27 00:39:56 +02:00
2018-11-05 20:59:41 +01:00
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 ( )
}
}
2018-11-01 02:12:43 +01:00
2018-10-27 00:39:56 +02: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 ( )
}
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 )
2018-10-29 18:03:20 +01:00
. colorChooser ( colors , initialSelection = Color . parseColor ( " #3F51B5 " ) ) { _ , color ->
2018-10-26 00:50:37 +02:00
viewPrimaryColor . setBackgroundColor ( color )
}
. positiveButton ( R . string . select )
. show ( )
}
}
2018-11-01 02:12:43 +01:00
fun setMainActivity ( mainActivity : MainActivity ) {
this . mainActivity = mainActivity
}
2018-10-25 18:52:33 +02:00
}