2019-09-01 11:54:12 +02:00
|
|
|
package com.denizd.substitutionplan.fragments
|
2019-05-26 11:35:36 +02:00
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import android.content.res.Configuration
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.os.Handler
|
|
|
|
|
import android.preference.PreferenceManager
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.widget.LinearLayout
|
|
|
|
|
import android.widget.TextView
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.lifecycle.ViewModelProviders
|
|
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.*
|
|
|
|
|
import com.denizd.substitutionplan.adapters.CardAdapter
|
|
|
|
|
import com.denizd.substitutionplan.database.SubstViewModel
|
|
|
|
|
import com.denizd.substitutionplan.models.Subst
|
|
|
|
|
import com.denizd.substitutionplan.data.DataFetcher
|
2019-05-26 11:35:36 +02:00
|
|
|
import kotlin.collections.ArrayList
|
|
|
|
|
|
2019-09-01 11:54:12 +02:00
|
|
|
internal open class PlanFragment : Fragment(R.layout.plan) {
|
2019-07-22 19:10:25 +02:00
|
|
|
lateinit var recyclerView: RecyclerView
|
|
|
|
|
lateinit var mAdapter: CardAdapter
|
2019-05-26 11:35:36 +02:00
|
|
|
private lateinit var layoutManager: GridLayoutManager
|
2019-08-31 16:12:36 +02:00
|
|
|
var planCardList = ArrayList<Subst>()
|
2019-07-22 19:10:25 +02:00
|
|
|
lateinit var substViewModel: SubstViewModel
|
2019-08-31 16:56:00 +02:00
|
|
|
private lateinit var mContext: Context
|
2019-07-22 19:10:25 +02:00
|
|
|
lateinit var prefs: SharedPreferences
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-07-22 19:10:25 +02:00
|
|
|
lateinit var smileydown: TextView
|
|
|
|
|
lateinit var smileydowntext: TextView
|
|
|
|
|
lateinit var linearsmiley: LinearLayout
|
|
|
|
|
var persPlanEmpty: Boolean = true
|
|
|
|
|
val handler = Handler()
|
2019-05-26 11:35:36 +02:00
|
|
|
|
|
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
|
super.onAttach(context)
|
|
|
|
|
mContext = context
|
|
|
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
|
|
|
|
super.onConfigurationChanged(newConfig)
|
2019-08-31 16:56:00 +02:00
|
|
|
layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
|
2019-05-26 11:35:36 +02:00
|
|
|
recyclerView.layoutManager = layoutManager
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
|
|
|
|
|
|
|
|
|
|
recyclerView = view.findViewById(R.id.linearRecycler)
|
|
|
|
|
recyclerView.hasFixedSize()
|
2019-08-31 16:56:00 +02:00
|
|
|
layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
|
2019-05-26 11:35:36 +02:00
|
|
|
recyclerView.layoutManager = layoutManager
|
|
|
|
|
|
|
|
|
|
mAdapter = CardAdapter(planCardList)
|
|
|
|
|
recyclerView.adapter = mAdapter
|
|
|
|
|
|
2019-08-31 16:12:36 +02:00
|
|
|
if (prefs.getInt("firstTimeOpening", 0) == 1) {
|
2019-09-01 11:54:12 +02:00
|
|
|
startFetch(view, pullToRefresh, true)
|
|
|
|
|
prefs.edit().putInt("firstTimeOpening", 2).apply()
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prefs.getBoolean("autoRefresh", false)) {
|
2019-09-01 11:54:12 +02:00
|
|
|
startFetch(view, pullToRefresh, false)
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
2019-05-26 18:40:02 +02:00
|
|
|
substViewModel = ViewModelProviders.of(this).get(SubstViewModel::class.java)
|
2019-05-26 11:35:36 +02:00
|
|
|
|
|
|
|
|
pullToRefresh.setOnRefreshListener {
|
2019-09-01 11:54:12 +02:00
|
|
|
startFetch(view, pullToRefresh, false)
|
2019-08-31 16:56:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 11:54:12 +02:00
|
|
|
private fun startFetch(view: View, pullToRefresh: SwipeRefreshLayout, refreshMenu: Boolean) {
|
|
|
|
|
pullToRefresh.isRefreshing = true
|
|
|
|
|
DataFetcher(
|
|
|
|
|
isPlan = true,
|
|
|
|
|
isMenu = refreshMenu,
|
|
|
|
|
isJobService = false,
|
|
|
|
|
context = mContext,
|
|
|
|
|
application = activity!!.application,
|
|
|
|
|
parentView = view.rootView
|
|
|
|
|
).execute()
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 16:56:00 +02:00
|
|
|
private fun getGridColumnCount(config: Configuration): Int {
|
|
|
|
|
val tabletSize = resources.getBoolean(R.bool.isTablet)
|
|
|
|
|
return if (tabletSize) {
|
|
|
|
|
when (config.orientation) {
|
|
|
|
|
Configuration.ORIENTATION_PORTRAIT -> 2
|
|
|
|
|
else -> 3
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
when (config.orientation) {
|
|
|
|
|
Configuration.ORIENTATION_PORTRAIT -> 1
|
|
|
|
|
else -> 2
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|