Archived
Changing fragments via bottom navigation view works well now! Also started work on SettingsFragment.kt
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.denizk0461.studip.activity
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.navigation.findNavController
|
||||
@@ -8,15 +9,23 @@ import androidx.navigation.ui.AppBarConfiguration
|
||||
import androidx.navigation.ui.navigateUp
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.data.Dependencies
|
||||
import com.denizk0461.studip.databinding.ActivityMainBinding
|
||||
import com.denizk0461.studip.db.EventRepository
|
||||
import com.denizk0461.studip.fragment.EventFragment
|
||||
import com.denizk0461.studip.fragment.FoodFragment
|
||||
import com.denizk0461.studip.fragment.SettingsFragment
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var appBarConfiguration: AppBarConfiguration
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private var currentFragment = FragmentType.SCHEDULE
|
||||
private val fragments = listOf(EventFragment(), FoodFragment(), SettingsFragment())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
@@ -27,6 +36,12 @@ class MainActivity : AppCompatActivity() {
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.contentMain.navView.setOnItemSelectedListener { item ->
|
||||
loadFragment(bottomIdToType(item.itemId))
|
||||
}
|
||||
|
||||
loadFragment(currentFragment)
|
||||
|
||||
// setSupportActionBar(binding.toolbar)
|
||||
|
||||
// val navController = findNavController(R.id.nav_host_fragment_content_main)
|
||||
@@ -41,6 +56,36 @@ class MainActivity : AppCompatActivity() {
|
||||
// }
|
||||
}
|
||||
|
||||
private fun loadFragment(type: FragmentType): Boolean {
|
||||
// val fragment = supportFragmentManager.findFragmentByTag(type.type) ?: getFragmentByType(type)
|
||||
val fragment = getFragment(type)
|
||||
|
||||
if (fragment.lifecycle.currentState != Lifecycle.State.INITIALIZED) {
|
||||
return false
|
||||
}
|
||||
|
||||
// if (supportFragmentManager.fragments.size > 1) {
|
||||
//
|
||||
// val currentFragment = supportFragmentManager.findFragmentByTag(currentFragment.type)
|
||||
// if (currentFragment != null) {
|
||||
// supportFragmentManager.beginTransaction().remove(currentFragment).commit()
|
||||
// supportFragmentManager.popBackStack()
|
||||
// }
|
||||
// }
|
||||
|
||||
// supportFragmentManager.popBackStack()
|
||||
supportFragmentManager.beginTransaction()
|
||||
.replace(R.id.nav_host_fragment_content_main, fragment, type.type)
|
||||
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
|
||||
.commit()
|
||||
|
||||
binding.contentMain.appTitleBar.text = getTitleString(type)
|
||||
|
||||
currentFragment = type
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
menuInflater.inflate(R.menu.menu_main, menu)
|
||||
@@ -62,4 +107,37 @@ class MainActivity : AppCompatActivity() {
|
||||
return navController.navigateUp(appBarConfiguration)
|
||||
|| super.onSupportNavigateUp()
|
||||
}
|
||||
|
||||
private fun getFragmentByType(type: FragmentType): Fragment {
|
||||
// Log.d("AAA!", "help")
|
||||
return when (type) {
|
||||
FragmentType.SCHEDULE -> EventFragment()
|
||||
FragmentType.FOOD -> FoodFragment() // TODO once this fragment has been implemented
|
||||
FragmentType.SETTINGS -> SettingsFragment()
|
||||
}
|
||||
}
|
||||
|
||||
private fun bottomIdToType(id: Int): FragmentType = when (id) {
|
||||
R.id.food -> FragmentType.FOOD
|
||||
R.id.settings -> FragmentType.SETTINGS
|
||||
else -> FragmentType.SCHEDULE
|
||||
}
|
||||
|
||||
private fun getFragment(type: FragmentType) = when (type) {
|
||||
FragmentType.SCHEDULE -> fragments[0]
|
||||
FragmentType.FOOD -> fragments[1]
|
||||
FragmentType.SETTINGS -> fragments[2]
|
||||
}
|
||||
|
||||
private fun getTitleString(type: FragmentType) = when (type) {
|
||||
FragmentType.SCHEDULE -> getString(R.string.title_schedule)
|
||||
FragmentType.FOOD -> getString(R.string.title_food)
|
||||
FragmentType.SETTINGS -> getString(R.string.title_settings)
|
||||
}
|
||||
|
||||
enum class FragmentType(val type: String) {
|
||||
SCHEDULE("schedule"),
|
||||
FOOD("food"),
|
||||
SETTINGS("settings"),
|
||||
}
|
||||
}
|
||||
@@ -85,10 +85,10 @@ class EventFragment : Fragment() {
|
||||
// }
|
||||
}
|
||||
|
||||
// override fun onResume() {
|
||||
// super.onResume()
|
||||
// switchToCurrentDayView()
|
||||
// }
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
switchToCurrentDayView()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
@@ -96,8 +96,8 @@ class EventFragment : Fragment() {
|
||||
}
|
||||
|
||||
private fun switchToCurrentDayView() {
|
||||
// binding.viewpager.currentItem = dayOfWeek
|
||||
binding.dayTabLayout.getTabAt(dayOfWeek)?.select()
|
||||
binding.viewPager.currentItem = dayOfWeek
|
||||
// binding.dayTabLayout.getTabAt(dayOfWeek)?.select()
|
||||
}
|
||||
|
||||
private fun launchWebview() {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.denizk0461.studip.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.denizk0461.studip.databinding.FragmentFoodBinding
|
||||
|
||||
class FoodFragment : Fragment() {
|
||||
|
||||
private var _binding: FragmentFoodBinding? = null
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
_binding = FragmentFoodBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.denizk0461.studip.fragment
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.denizk0461.studip.BuildConfig
|
||||
import com.denizk0461.studip.data.Misc
|
||||
import com.denizk0461.studip.databinding.FragmentSettingsBinding
|
||||
|
||||
class SettingsFragment : Fragment() {
|
||||
|
||||
private var _binding: FragmentSettingsBinding? = null
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
private var appVersionClick = 0
|
||||
|
||||
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)
|
||||
|
||||
val toast2 = Toast.makeText(context, "2", Toast.LENGTH_SHORT)
|
||||
val toast22 = Toast.makeText(context, "22", Toast.LENGTH_SHORT)
|
||||
val toast222 = Toast.makeText(context, "222", Toast.LENGTH_SHORT)
|
||||
|
||||
binding.buttonAppVersion.setOnClickListener {
|
||||
when (appVersionClick) {
|
||||
19 -> {
|
||||
toast2.show()
|
||||
appVersionClick += 1
|
||||
}
|
||||
20 -> {
|
||||
toast2.cancel()
|
||||
toast22.show()
|
||||
appVersionClick += 1
|
||||
}
|
||||
21 -> {
|
||||
toast22.cancel()
|
||||
toast222.show()
|
||||
appVersionClick += 1
|
||||
}
|
||||
22 -> {
|
||||
toast222.cancel()
|
||||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(Misc.mysteryLink)))
|
||||
appVersionClick = 0
|
||||
}
|
||||
else -> appVersionClick += 1
|
||||
}
|
||||
}
|
||||
|
||||
binding.appVersionText.text = BuildConfig.VERSION_NAME
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- An white rectangle ripple. -->
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="?attr/colorPrimary">
|
||||
<item
|
||||
android:id="@android:id/mask"
|
||||
android:gravity="center">
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="?attr/colorPrimary"/>
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
@@ -7,6 +7,7 @@
|
||||
android:fitsSystemWindows="true"
|
||||
tools:context=".activity.MainActivity">
|
||||
|
||||
<include layout="@layout/content_main"/>
|
||||
<include layout="@layout/content_main"
|
||||
android:id="@+id/content_main"/>
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
@@ -15,9 +15,10 @@
|
||||
app:layout_constraintEnd_toEndOf="parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/app_title_bar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:text="@string/your_schedule"
|
||||
android:text="@string/title_schedule"
|
||||
android:textSize="32sp"
|
||||
android:fontFamily="@font/lato_bolditalic"
|
||||
android:layout_marginHorizontal="16dp"
|
||||
@@ -25,7 +26,7 @@
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
<fragment
|
||||
<androidx.fragment.app.FragmentContainerView
|
||||
android:id="@+id/nav_host_fragment_content_main"
|
||||
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||
android:layout_width="0dp"
|
||||
@@ -42,6 +43,7 @@
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
app:menu="@menu/nav_view_items"
|
||||
app:labelVisibilityMode="selected"
|
||||
app:itemActiveIndicatorStyle="@style/NavigationViewItemIndicator"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".fragment.EventFragment">
|
||||
tools:context=".fragment.EventFragment"
|
||||
android:background="?android:colorBackground">
|
||||
|
||||
<com.google.android.material.tabs.TabLayout
|
||||
android:id="@+id/day_tab_layout"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?android:colorBackground">
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -0,0 +1,110 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".fragment.SettingsFragment"
|
||||
android:background="?android:colorBackground">
|
||||
|
||||
<androidx.appcompat.widget.LinearLayoutCompat
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="8dp"
|
||||
android:layout_marginVertical="4dp"
|
||||
style="@style/Widget.Material3.CardView.Outlined">
|
||||
|
||||
<androidx.appcompat.widget.LinearLayoutCompat
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="8dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:textSize="22sp"
|
||||
android:fontFamily="@font/lato_bolditalic"
|
||||
android:text="@string/settings_misc"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginHorizontal="16dp"/>
|
||||
|
||||
<com.google.android.material.divider.MaterialDivider
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="16dp"/>
|
||||
|
||||
<!-- Academic quarter -->
|
||||
<androidx.appcompat.widget.LinearLayoutCompat
|
||||
android:background="@drawable/layout_ripple"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:paddingHorizontal="16dp"
|
||||
android:paddingVertical="8dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"
|
||||
android:fontFamily="@font/lato_bolditalic"
|
||||
android:text="@string/settings_quarter_title"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"
|
||||
android:text="@string/settings_quarter_subtitle"/>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
||||
<!-- App version -->
|
||||
<androidx.appcompat.widget.LinearLayoutCompat
|
||||
android:id="@+id/button_app_version"
|
||||
android:background="@drawable/layout_ripple"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:paddingHorizontal="16dp"
|
||||
android:paddingVertical="8dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"
|
||||
android:fontFamily="@font/lato_bolditalic"
|
||||
android:text="@string/settings_app_version"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/app_version_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"/>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:text="@string/with_love"
|
||||
android:textSize="10sp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:textAlignment="center"/>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
@@ -13,7 +13,9 @@
|
||||
<string name="thursday">THURSDAY</string>
|
||||
<string name="friday">FRIDAY</string>
|
||||
|
||||
<string name="your_schedule">Your Schedule</string>
|
||||
<string name="title_schedule">Your Schedule</string>
|
||||
<string name="title_food">Canteen Plans</string>
|
||||
<string name="title_settings">App Settings</string>
|
||||
|
||||
<string name="nav_schedule">Schedule</string>
|
||||
<string name="nav_food">Food</string>
|
||||
@@ -21,6 +23,15 @@
|
||||
|
||||
<string name="toast_fetch_finished">Your schedule has been refreshed!</string>
|
||||
|
||||
<string name="settings_general">General</string>
|
||||
<string name="settings_misc">Miscellaneous</string>
|
||||
|
||||
<string name="settings_quarter_title">Show academic quarter</string>
|
||||
<string name="settings_quarter_subtitle">Where applicable</string>
|
||||
<string name="settings_app_version">App Version</string>
|
||||
|
||||
<string name="with_love">with ♡️ from Osterholz\nMade by Deniz \\__*</string>
|
||||
|
||||
<string name="lorem_ipsum">
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in scelerisque sem. Mauris
|
||||
volutpat, dolor id interdum ullamcorper, risus dolor egestas lectus, sit amet mattis purus
|
||||
|
||||
@@ -18,4 +18,9 @@
|
||||
<style name="NavigationViewItemIndicator" parent="Widget.Material3.BottomNavigationView.ActiveIndicator">
|
||||
<item name="android:color">@color/primaryDark</item>
|
||||
</style>
|
||||
|
||||
<style name="SelectableItemTheme">
|
||||
<!-- <item name="colorControlHighlight">?attr/colorPrimary</item>-->
|
||||
<item name="colorControlHighlight">@color/primaryLight</item>
|
||||
</style>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user