2023-04-08 11:41:02 +02:00
|
|
|
package com.denizk0461.studip.activity
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import androidx.core.view.WindowCompat
|
2023-04-28 15:14:50 +02:00
|
|
|
import androidx.fragment.app.FragmentActivity
|
2023-04-13 12:25:43 +02:00
|
|
|
import androidx.fragment.app.FragmentTransaction
|
|
|
|
|
import androidx.lifecycle.Lifecycle
|
2023-04-08 11:41:02 +02:00
|
|
|
import com.denizk0461.studip.R
|
|
|
|
|
import com.denizk0461.studip.databinding.ActivityMainBinding
|
2023-04-23 11:57:03 +02:00
|
|
|
import com.denizk0461.studip.db.AppRepository
|
2023-04-13 12:25:43 +02:00
|
|
|
import com.denizk0461.studip.fragment.EventFragment
|
2023-04-16 18:19:06 +02:00
|
|
|
import com.denizk0461.studip.fragment.CanteenFragment
|
2023-04-13 12:25:43 +02:00
|
|
|
import com.denizk0461.studip.fragment.SettingsFragment
|
2023-04-27 15:15:29 +02:00
|
|
|
import com.denizk0461.studip.model.SettingsPreferences
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
|
|
|
|
* Main activity that handles all common fragments. This is opened on app launch.
|
|
|
|
|
*/
|
2023-04-28 15:14:50 +02:00
|
|
|
class MainActivity : FragmentActivity() {
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// View binding
|
2023-04-08 11:41:02 +02:00
|
|
|
private lateinit var binding: ActivityMainBinding
|
2023-04-26 19:49:29 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/*
|
2023-04-26 19:49:29 +02:00
|
|
|
* Tag for the currently active fragment; set on activity launch to the fragment that will be
|
|
|
|
|
* shown first.
|
2023-04-21 15:42:21 +02:00
|
|
|
*/
|
2023-04-26 19:49:29 +02:00
|
|
|
private var currentFragment: String = "event"
|
2023-04-08 11:41:02 +02:00
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/*
|
|
|
|
|
* Instantiate repository object that is accessed by the fragments' view models to retrieve
|
|
|
|
|
* data.
|
|
|
|
|
*/
|
2023-04-28 15:14:50 +02:00
|
|
|
// Dependencies.repo = AppRepository(application)
|
2023-04-08 17:30:05 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Inflate view binding and bind to this activity
|
2023-04-08 11:41:02 +02:00
|
|
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
|
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
2023-04-27 15:15:29 +02:00
|
|
|
// Open the fragment that the user specified to open on app start
|
|
|
|
|
if (
|
2023-04-28 15:14:50 +02:00
|
|
|
AppRepository.getRepositoryInstance(application)
|
|
|
|
|
.getBooleanPreference(SettingsPreferences.LAUNCH_CANTEEN_ON_START)
|
2023-04-27 15:15:29 +02:00
|
|
|
) {
|
|
|
|
|
// Open canteen fragment
|
|
|
|
|
binding.contentMain.navView.selectedItemId = R.id.food
|
|
|
|
|
currentFragment = "canteen"
|
|
|
|
|
} else {
|
|
|
|
|
// Open event fragment
|
|
|
|
|
binding.contentMain.navView.selectedItemId = R.id.plan
|
|
|
|
|
currentFragment = "event"
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 19:49:29 +02:00
|
|
|
// Set up navigation view bar to launch fragments
|
2023-04-13 12:25:43 +02:00
|
|
|
binding.contentMain.navView.setOnItemSelectedListener { item ->
|
2023-04-26 19:49:29 +02:00
|
|
|
// Launch fragment based on the item that has been clicked
|
|
|
|
|
loadFragment(when (item.itemId) {
|
|
|
|
|
R.id.food -> "canteen"
|
|
|
|
|
R.id.settings -> "settings"
|
|
|
|
|
else -> "event"
|
|
|
|
|
})
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Launch the fragment defined in currentFragment
|
2023-04-13 12:25:43 +02:00
|
|
|
loadFragment(currentFragment)
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
|
|
|
|
* Loads a given fragment.
|
|
|
|
|
*
|
2023-04-26 19:49:29 +02:00
|
|
|
* @param type tag for the fragment that is to be opened
|
2023-04-21 15:42:21 +02:00
|
|
|
* @return true on success, false if another fragment is currently instantiating
|
|
|
|
|
*/
|
2023-04-26 19:49:29 +02:00
|
|
|
private fun loadFragment(type: String): Boolean {
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-26 19:49:29 +02:00
|
|
|
// Find the fragment that is to be opened, or create it if it hasn't been instantiated yet
|
|
|
|
|
val fragment = supportFragmentManager.findFragmentByTag(type) ?: getFragment(type)
|
|
|
|
|
|
|
|
|
|
// Don't interrupt the current fragment's lifecycle if it isn't fully initialised
|
2023-04-13 12:25:43 +02:00
|
|
|
if (fragment.lifecycle.currentState != Lifecycle.State.INITIALIZED) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 19:49:29 +02:00
|
|
|
// Start the transaction to show the new fragment
|
2023-04-13 12:25:43 +02:00
|
|
|
supportFragmentManager.beginTransaction()
|
2023-04-26 19:49:29 +02:00
|
|
|
.replace(R.id.nav_host_fragment_content_main, fragment, type)
|
2023-04-25 15:38:30 +02:00
|
|
|
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
|
2023-04-13 12:25:43 +02:00
|
|
|
.commit()
|
|
|
|
|
|
2023-04-26 19:49:29 +02:00
|
|
|
// Success
|
2023-04-13 12:25:43 +02:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
2023-04-26 19:49:29 +02:00
|
|
|
* Retrieve fragment by its tag.
|
2023-04-21 15:42:21 +02:00
|
|
|
*
|
2023-04-26 19:49:29 +02:00
|
|
|
* @param type tag for the corresponding fragment
|
2023-04-21 15:42:21 +02:00
|
|
|
* @return the corresponding fragment
|
|
|
|
|
*/
|
2023-04-26 19:49:29 +02:00
|
|
|
private fun getFragment(type: String) = when (type) {
|
|
|
|
|
"canteen" -> CanteenFragment() // CanteenFragment.kt
|
|
|
|
|
"settings" -> SettingsFragment() // SettingsFragment.kt
|
|
|
|
|
else -> EventFragment() // on unknown value or "event", launch EventFragment.kt
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|