2023-04-08 11:41:02 +02:00
|
|
|
package com.denizk0461.studip.activity
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
|
import androidx.core.view.WindowCompat
|
2023-04-21 15:42:21 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
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
|
2023-04-08 17:30:05 +02:00
|
|
|
import com.denizk0461.studip.data.Dependencies
|
2023-04-08 11:41:02 +02:00
|
|
|
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-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-08 11:41:02 +02:00
|
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
|
|
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-21 15:42:21 +02:00
|
|
|
/*
|
|
|
|
|
* ID of the bottom navigation view button that launched the currently active fragment; set on
|
|
|
|
|
* activity launch to the fragment that will be first shown
|
|
|
|
|
*/
|
|
|
|
|
private var currentFragment: Int = R.id.plan
|
|
|
|
|
// All fragments are instantiated at app launch
|
|
|
|
|
private val fragments: List<Fragment> =
|
|
|
|
|
listOf(EventFragment(), CanteenFragment(), SettingsFragment())
|
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-23 11:57:03 +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-21 15:42:21 +02:00
|
|
|
/*
|
|
|
|
|
* Set up navigation view bar to launch fragments.
|
|
|
|
|
* TODO: why do fragments launch slowly when quickly clicking between them?
|
|
|
|
|
*/
|
2023-04-13 12:25:43 +02:00
|
|
|
binding.contentMain.navView.setOnItemSelectedListener { item ->
|
2023-04-21 15:42:21 +02:00
|
|
|
loadFragment(item.itemId)
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param id the ID of the bottom navigation view button that was clicked
|
|
|
|
|
* @return true on success, false if another fragment is currently instantiating
|
|
|
|
|
*/
|
|
|
|
|
private fun loadFragment(id: Int): Boolean {
|
|
|
|
|
// Get fragment to be loaded
|
|
|
|
|
val fragment = getFragment(id)
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Check if another fragment exists and is not fully initialised
|
2023-04-13 12:25:43 +02:00
|
|
|
if (fragment.lifecycle.currentState != Lifecycle.State.INITIALIZED) {
|
2023-04-21 15:42:21 +02:00
|
|
|
// Return false to avoid interrupting the fragment's lifecycle
|
2023-04-13 12:25:43 +02:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Prepare and commit a transaction between the current and the next fragment
|
2023-04-13 12:25:43 +02:00
|
|
|
supportFragmentManager.beginTransaction()
|
2023-04-21 15:42:21 +02:00
|
|
|
.replace(R.id.nav_host_fragment_content_main, fragment, id.toString())
|
2023-04-25 15:38:30 +02:00
|
|
|
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
|
|
|
|
|
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
2023-04-13 12:25:43 +02:00
|
|
|
.commit()
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Set text of the app bar accordingly
|
2023-04-25 15:38:30 +02:00
|
|
|
// binding.contentMain.appTitleBar.text = getTitleString(id)
|
2023-04-21 15:42:21 +02:00
|
|
|
// Note down the new current fragment
|
|
|
|
|
currentFragment = id
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Transaction successful
|
2023-04-13 12:25:43 +02:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieve fragment by the ID of the bottom navigation view's button.
|
|
|
|
|
*
|
|
|
|
|
* @param id the ID of the bottom navigation button
|
|
|
|
|
* @return the corresponding fragment
|
|
|
|
|
*/
|
|
|
|
|
private fun getFragment(id: Int) = when (id) {
|
|
|
|
|
R.id.food -> fragments[1] // CanteenFragment.kt
|
|
|
|
|
R.id.settings -> fragments[2] // SettingsFragment.kt
|
|
|
|
|
else -> fragments[0] // on unknown value or R.id.plan, launch EventFragment.kt
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|
2023-04-13 12:25:43 +02:00
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
|
|
|
|
* Retrieve app bar text by the ID of the bottom navigation view's button.
|
|
|
|
|
*
|
|
|
|
|
* @param id the ID of the bottom navigation button
|
|
|
|
|
* @return the corresponding fragment's title
|
|
|
|
|
*/
|
|
|
|
|
private fun getTitleString(id: Int) = when (id) {
|
|
|
|
|
R.id.food -> getString(R.string.title_food)
|
|
|
|
|
R.id.settings -> getString(R.string.title_settings)
|
|
|
|
|
else -> getString(R.string.title_schedule) // on unknown value or R.id.plan, get text for EventFragment.kt
|
2023-04-13 12:25:43 +02:00
|
|
|
}
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|