2023-04-13 12:25:43 +02:00
|
|
|
package com.denizk0461.studip.fragment
|
|
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
import android.annotation.SuppressLint
|
2023-04-13 12:25:43 +02:00
|
|
|
import android.content.Intent
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2023-04-23 22:06:29 +02:00
|
|
|
import androidx.fragment.app.viewModels
|
2023-04-13 12:25:43 +02:00
|
|
|
import com.denizk0461.studip.BuildConfig
|
2023-04-24 13:08:03 +02:00
|
|
|
import com.denizk0461.studip.R
|
2023-04-16 18:19:06 +02:00
|
|
|
import com.denizk0461.studip.activity.FetcherActivity
|
2023-04-30 22:47:59 +02:00
|
|
|
import com.denizk0461.studip.activity.ImageActivity
|
|
|
|
|
import com.denizk0461.studip.data.showToast
|
2023-04-13 12:25:43 +02:00
|
|
|
import com.denizk0461.studip.databinding.FragmentSettingsBinding
|
2023-04-26 11:00:56 +02:00
|
|
|
import com.denizk0461.studip.sheet.DevCodeSheet
|
2023-04-24 13:08:03 +02:00
|
|
|
import com.denizk0461.studip.sheet.TextSheet
|
2023-04-23 22:06:29 +02:00
|
|
|
import com.denizk0461.studip.viewmodel.SettingsViewModel
|
2023-04-25 21:57:47 +02:00
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
import java.util.*
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* User-facing fragment view that is used to change app settings.
|
|
|
|
|
*/
|
2023-04-23 17:13:48 +02:00
|
|
|
class SettingsFragment : AppFragment() {
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Nullable view binding reference
|
2023-04-13 12:25:43 +02:00
|
|
|
private var _binding: FragmentSettingsBinding? = null
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Non-null reference to the view binding. This property is only valid between onCreateView and
|
|
|
|
|
* onDestroyView.
|
|
|
|
|
*/
|
2023-04-13 12:25:43 +02:00
|
|
|
private val binding get() = _binding!!
|
2023-04-23 11:57:03 +02:00
|
|
|
|
2023-04-23 22:06:29 +02:00
|
|
|
// View model reference for providing access to the database
|
|
|
|
|
private val viewModel: SettingsViewModel by viewModels()
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Click counter on the app version button
|
2023-04-30 22:47:59 +02:00
|
|
|
private var appVersionClick = 1
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// 222
|
|
|
|
|
private val mysteryLink = "https://www.youtube.com/watch?v=nhIQMCXJzLI"
|
2023-04-13 19:21:53 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Instantiate the view binding
|
2023-04-13 12:25:43 +02:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
|
_binding = FragmentSettingsBinding.inflate(inflater, container, false)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Launch the Stud.IP schedule fetcher activity
|
2023-04-16 18:19:06 +02:00
|
|
|
binding.buttonRefreshSchedule.setOnClickListener {
|
|
|
|
|
launchWebView()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 15:43:42 +02:00
|
|
|
// Set up switch for highlighting the next course in the schedule
|
|
|
|
|
binding.switchHighlight.apply {
|
|
|
|
|
isChecked = viewModel.preferenceCourseHighlighting
|
|
|
|
|
setOnCheckedChangeListener { _, newValue ->
|
|
|
|
|
viewModel.preferenceCourseHighlighting = newValue
|
2023-04-23 22:06:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 15:43:42 +02:00
|
|
|
// Set up switch for displaying allergens
|
|
|
|
|
binding.switchAllergens.apply {
|
|
|
|
|
isChecked = viewModel.preferenceAllergen
|
|
|
|
|
setOnCheckedChangeListener { _, newValue ->
|
|
|
|
|
viewModel.preferenceAllergen = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 15:38:30 +02:00
|
|
|
// Set up switch for launching the app with a specific view
|
|
|
|
|
binding.switchLaunchCanteen.apply {
|
|
|
|
|
isChecked = viewModel.preferenceLaunchCanteen
|
|
|
|
|
setOnCheckedChangeListener { _, newValue ->
|
|
|
|
|
viewModel.preferenceLaunchCanteen = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-29 22:26:26 +02:00
|
|
|
// Set up switch for colouring dietary preferences
|
|
|
|
|
binding.switchPrefColour.apply {
|
|
|
|
|
isChecked = viewModel.preferenceColour
|
|
|
|
|
setOnCheckedChangeListener { _, newValue ->
|
|
|
|
|
viewModel.preferenceColour = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 15:43:42 +02:00
|
|
|
// Set up switch for crash report opt-in
|
|
|
|
|
binding.switchCrashlytics.apply {
|
|
|
|
|
isChecked = viewModel.preferenceDataHandling
|
|
|
|
|
setOnCheckedChangeListener { _, newValue ->
|
|
|
|
|
viewModel.preferenceDataHandling = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set click listener for the data handling dialogue
|
2023-04-24 13:08:03 +02:00
|
|
|
binding.buttonDataHandling.setOnClickListener {
|
|
|
|
|
openBottomSheet(TextSheet(
|
2023-04-25 16:55:17 +02:00
|
|
|
getString(R.string.settings_data_sheet_header),
|
|
|
|
|
getString(R.string.settings_data_sheet_content),
|
2023-04-24 13:08:03 +02:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 10:21:16 +02:00
|
|
|
// Set click listener for showing licences dialogue
|
|
|
|
|
binding.buttonLicences.setOnClickListener {
|
|
|
|
|
openBottomSheet(
|
|
|
|
|
TextSheet(
|
|
|
|
|
getString(R.string.sheet_licences_header),
|
|
|
|
|
getString(R.string.sheet_licences_content),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 11:00:56 +02:00
|
|
|
// Set long click listener for licences button to open dev code sheet
|
|
|
|
|
binding.buttonLicences.setOnLongClickListener {
|
2023-04-26 18:36:32 +02:00
|
|
|
openBottomSheet(DevCodeSheet(
|
|
|
|
|
viewModel::nukeEvents,
|
|
|
|
|
viewModel::nukeOfferItems,
|
|
|
|
|
viewModel::nukeOfferCategories,
|
|
|
|
|
viewModel::nukeOfferCanteens,
|
|
|
|
|
viewModel::nukeOfferDates,
|
|
|
|
|
viewModel::nukeEverything,
|
|
|
|
|
))
|
2023-04-26 11:00:56 +02:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set click listener for the app version button
|
2023-04-13 12:25:43 +02:00
|
|
|
binding.buttonAppVersion.setOnClickListener {
|
|
|
|
|
when (appVersionClick) {
|
2023-04-30 22:47:59 +02:00
|
|
|
3 -> {
|
|
|
|
|
startActivity(Intent(context, ImageActivity::class.java).also { intent ->
|
|
|
|
|
val bundle = Bundle()
|
|
|
|
|
bundle.putString("img", "garbage")
|
|
|
|
|
intent.putExtras(bundle)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-05-01 09:35:01 +02:00
|
|
|
10 -> showToast(context, getString(R.string.settings_app_version_10))
|
|
|
|
|
15 -> showToast(context, getString(R.string.settings_app_version_15))
|
|
|
|
|
20 -> showToast(context, getString(R.string.settings_app_version_20))
|
|
|
|
|
30 -> showToast(context, getString(R.string.settings_app_version_30))
|
|
|
|
|
90 -> showToast(context, getString(R.string.settings_app_version_90))
|
|
|
|
|
121 -> showToast(context, getString(R.string.settings_app_version_121))
|
|
|
|
|
165 -> showToast(context, getString(R.string.settings_app_version_165))
|
|
|
|
|
200 -> showToast(context, getString(R.string.settings_app_version_200))
|
2023-04-30 22:47:59 +02:00
|
|
|
222 -> {
|
2023-04-23 11:57:03 +02:00
|
|
|
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(mysteryLink)))
|
2023-05-01 09:35:01 +02:00
|
|
|
appVersionClick = -1
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-01 09:35:01 +02:00
|
|
|
appVersionClick += 1
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/*
|
|
|
|
|
* Display the app's version as set in the build.gradle. Also display if the app is a
|
2023-04-25 21:57:47 +02:00
|
|
|
* development version, and if so, display build timestamp.
|
2023-04-23 11:57:03 +02:00
|
|
|
*/
|
2023-04-16 18:19:06 +02:00
|
|
|
@SuppressLint("SetTextI18n")
|
2023-04-25 21:57:47 +02:00
|
|
|
binding.appVersionText.text =
|
|
|
|
|
"${BuildConfig.VERSION_NAME}-${
|
|
|
|
|
if (BuildConfig.DEBUG)
|
2023-04-30 22:47:59 +02:00
|
|
|
"dev-[${SimpleDateFormat("yyyy-MM-dd, HH:mm:ss.SSS", Locale.GERMANY)
|
|
|
|
|
.format(Date(BuildConfig.TIMESTAMP))}]"
|
2023-04-25 21:57:47 +02:00
|
|
|
else
|
|
|
|
|
"release"
|
|
|
|
|
}"
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Invalidate the view binding
|
2023-04-13 12:25:43 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
_binding = null
|
|
|
|
|
}
|
2023-04-16 18:19:06 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Launch the Stud.IP schedule fetcher activity.
|
|
|
|
|
*/
|
2023-04-16 18:19:06 +02:00
|
|
|
private fun launchWebView() {
|
|
|
|
|
startActivity(Intent(context, FetcherActivity::class.java))
|
|
|
|
|
}
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|