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
|
2019-09-24 09:32:25 +02:00
|
|
|
import androidx.lifecycle.LiveData
|
2019-05-26 11:35:36 +02:00
|
|
|
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.*
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.adapters.SubstitutionAdapter
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.database.SubstViewModel
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.models.Substitution
|
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-09-24 09:32:25 +02:00
|
|
|
internal lateinit var recyclerView: RecyclerView
|
|
|
|
|
internal lateinit var mAdapter: SubstitutionAdapter
|
|
|
|
|
internal var planCardList = ArrayList<Substitution>()
|
|
|
|
|
private lateinit var substViewModel: SubstViewModel
|
2019-08-31 16:56:00 +02:00
|
|
|
private lateinit var mContext: Context
|
2019-09-24 09:32:25 +02:00
|
|
|
internal lateinit var prefs: SharedPreferences
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-09-24 09:32:25 +02:00
|
|
|
internal lateinit var personalPlanEmptyEmoticon: TextView
|
|
|
|
|
internal lateinit var personalPlanEmptyText: TextView
|
|
|
|
|
internal lateinit var personalPlanEmptyLayout: LinearLayout
|
|
|
|
|
internal var isPersonalPlanEmpty: Boolean = true
|
|
|
|
|
internal val handler = Handler()
|
|
|
|
|
internal var substitutionPlan: LiveData<List<Substitution>>? = null
|
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-09-18 11:12:10 +02:00
|
|
|
recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(newConfig))
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2019-09-18 11:12:10 +02:00
|
|
|
substViewModel = ViewModelProviders.of(this).get(SubstViewModel::class.java)
|
2019-09-24 09:32:25 +02:00
|
|
|
substitutionPlan = if (prefs.getBoolean("app_specific_sorting", true)) {
|
|
|
|
|
substViewModel.allSubstitutionsSorted
|
|
|
|
|
} else {
|
|
|
|
|
substViewModel.allSubstitutionsOriginal
|
|
|
|
|
}
|
2019-05-26 11:35:36 +02:00
|
|
|
|
2019-09-18 11:12:10 +02:00
|
|
|
val pullToRefresh = view.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh)
|
2019-05-26 11:35:36 +02:00
|
|
|
recyclerView = view.findViewById(R.id.linearRecycler)
|
|
|
|
|
recyclerView.hasFixedSize()
|
2019-09-18 11:12:10 +02:00
|
|
|
recyclerView.layoutManager = GridLayoutManager(mContext, getGridColumnCount(resources.configuration))
|
2019-09-23 17:23:59 +02:00
|
|
|
mAdapter = SubstitutionAdapter(planCardList, prefs)
|
2019-05-26 11:35:36 +02:00
|
|
|
recyclerView.adapter = mAdapter
|
|
|
|
|
|
2019-08-31 16:12:36 +02:00
|
|
|
if (prefs.getInt("firstTimeOpening", 0) == 1) {
|
2019-09-18 11:12:10 +02:00
|
|
|
substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = true)
|
2019-09-01 11:54:12 +02:00
|
|
|
prefs.edit().putInt("firstTimeOpening", 2).apply()
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prefs.getBoolean("autoRefresh", false)) {
|
2019-09-18 11:12:10 +02:00
|
|
|
substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = false)
|
2019-05-26 11:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pullToRefresh.setOnRefreshListener {
|
2019-09-18 11:12:10 +02:00
|
|
|
substViewModel.refresh(swipeRefreshLayout = pullToRefresh, rootView = view.rootView, refreshMenu = false)
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|