deleted unwanted codes
This commit is contained in:
@@ -44,22 +44,10 @@ class DashboardActivity : AppCompatActivity() {
|
||||
setupActionBarWithNavController(navController, appBarConfiguration)
|
||||
navView.setupWithNavController(navController)
|
||||
|
||||
// Handle navigation item clicks
|
||||
navView.setNavigationItemSelectedListener { menuItem ->
|
||||
when (menuItem.itemId) {
|
||||
R.id.nav_settings -> {
|
||||
// Open the settings fragment
|
||||
openSettings()
|
||||
drawerLayout.closeDrawers() // Close the drawer after selecting an item
|
||||
true
|
||||
}
|
||||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
menuInflater.inflate(R.menu.dashboard, menu)
|
||||
return true
|
||||
}
|
||||
@@ -69,14 +57,6 @@ class DashboardActivity : AppCompatActivity() {
|
||||
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
|
||||
}
|
||||
|
||||
// Add this function to open the settings fragment
|
||||
private fun openSettings() {
|
||||
supportFragmentManager.beginTransaction()
|
||||
.replace(R.id.nav_host_fragment_content_dashboard, SlideshowSettingsFragment())
|
||||
.addToBackStack(null)
|
||||
.commit()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
|
||||
@@ -1,40 +1,42 @@
|
||||
package com.example.hpostesting.presentation.dashboard
|
||||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.SharedPreferences
|
||||
import android.content.res.Configuration
|
||||
import android.os.Bundle
|
||||
import android.preference.PreferenceManager
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import `in`.sminnovations.hpostesting.R
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.example.hpostesting.presentation.dashboard.ui.slideshow.SlideshowViewModel
|
||||
|
||||
import `in`.sminnovations.hpostesting.databinding.FragmentSlideshowBinding
|
||||
import java.util.Locale
|
||||
|
||||
class SlideshowFragment : Fragment() {
|
||||
private var _binding: FragmentSlideshowBinding? = null
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
savedInstanceState: Bundle?,
|
||||
): View {
|
||||
val root = inflater.inflate(R.layout.fragment_slideshow, container, false)
|
||||
val slideshowViewModel =
|
||||
ViewModelProvider(this).get(SlideshowViewModel::class.java)
|
||||
|
||||
// Find the FrameLayout and add the PreferenceFragmentCompat dynamically
|
||||
val fragmentContainer = root.findViewById<FrameLayout>(R.id.fragment_container)
|
||||
val settingsFragment = SlideshowSettingsFragment()
|
||||
childFragmentManager.beginTransaction()
|
||||
.replace(fragmentContainer.id, settingsFragment)
|
||||
.commit()
|
||||
_binding = FragmentSlideshowBinding.inflate(inflater, container, false)
|
||||
val root: View = binding.root
|
||||
|
||||
// Handle language selection logic if needed
|
||||
val pInfo = requireActivity().packageManager.getPackageInfo(
|
||||
requireActivity().packageName, 0
|
||||
)
|
||||
val version = pInfo.versionName
|
||||
binding.textSlideshow.text = version
|
||||
|
||||
// val textView: TextView = binding.textSlideshow
|
||||
// slideshowViewModel.text.observe(viewLifecycleOwner) {
|
||||
// textView.text = it
|
||||
// }
|
||||
return root
|
||||
}
|
||||
|
||||
@@ -42,92 +44,4 @@ class SlideshowFragment : Fragment() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
|
||||
class SlideshowSettingsFragment : PreferenceFragmentCompat() {
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.settings, rootKey)
|
||||
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener { _, key ->
|
||||
if (key == "language_preference") {
|
||||
val selectedLanguage = sharedPreferences.getString(key, "en")
|
||||
if (selectedLanguage != null) {
|
||||
setLocale(selectedLanguage)
|
||||
}
|
||||
restartActivity()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setLocale(languageCode: String) {
|
||||
val newContext = LanguageHelper.wrap(requireContext(), languageCode)
|
||||
LanguagePreference.setLanguage(newContext, languageCode)
|
||||
|
||||
// Set the locale for the entire app, not just the context
|
||||
val resources = newContext.resources
|
||||
val configuration = resources.configuration
|
||||
configuration.setLocale(Locale(languageCode))
|
||||
resources.updateConfiguration(configuration, resources.displayMetrics)
|
||||
|
||||
requireActivity().recreate() // Restart the activity to apply language changes
|
||||
}
|
||||
|
||||
private fun restartActivity() {
|
||||
requireActivity().recreate()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
sharedPreferences.unregisterOnSharedPreferenceChangeListener { _, _ -> }
|
||||
}
|
||||
}
|
||||
|
||||
class LanguageHelper(base: Context) : ContextWrapper(base) {
|
||||
|
||||
companion object {
|
||||
fun wrap(context: Context, language: String): ContextWrapper {
|
||||
var newContext = context
|
||||
|
||||
val config = Configuration(context.resources.configuration)
|
||||
val locale = Locale(language)
|
||||
Locale.setDefault(locale)
|
||||
config.setLocale(locale)
|
||||
|
||||
newContext = context.createConfigurationContext(config)
|
||||
|
||||
return LanguageHelper(newContext)
|
||||
}
|
||||
|
||||
fun updateBaseContextLocale(context: Context, language: String): Context {
|
||||
val config = Configuration(context.resources.configuration)
|
||||
val locale = Locale(language)
|
||||
Locale.setDefault(locale)
|
||||
config.setLocale(locale)
|
||||
|
||||
return context.createConfigurationContext(config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LanguagePreference(context: Context) {
|
||||
|
||||
private val preferences: SharedPreferences = context.getSharedPreferences("LanguagePrefs", Context.MODE_PRIVATE)
|
||||
|
||||
companion object {
|
||||
private const val SELECTED_LANGUAGE = "selectedLanguage"
|
||||
|
||||
fun getLanguage(context: Context): String? {
|
||||
val preferences = context.getSharedPreferences("LanguagePrefs", Context.MODE_PRIVATE)
|
||||
return preferences.getString(SELECTED_LANGUAGE, null)
|
||||
}
|
||||
|
||||
fun setLanguage(context: Context, language: String) {
|
||||
val editor = context.getSharedPreferences("LanguagePrefs", Context.MODE_PRIVATE).edit()
|
||||
editor.putString(SELECTED_LANGUAGE, language)
|
||||
editor.apply()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user