Skip to content

Commit

Permalink
Merge pull request #222 from growabeard/fix-toomanyfunctions-fragments
Browse files Browse the repository at this point in the history
Fix TooManyFunctions in Fragments and MainActivity.
  • Loading branch information
andrewtavis authored Oct 25, 2024
2 parents f2286b6 + a1d3f43 commit 04bfee0
Show file tree
Hide file tree
Showing 17 changed files with 415 additions and 399 deletions.
67 changes: 23 additions & 44 deletions app/src/main/java/be/scri/activities/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package be.scri.activities

import android.app.UiModeManager
import android.content.Context
import android.os.Bundle
import android.util.Log
Expand All @@ -14,6 +13,7 @@ import androidx.viewpager2.widget.ViewPager2
import be.scri.R
import be.scri.adapters.ViewPagerAdapter
import be.scri.databinding.ActivityMainBinding
import be.scri.helpers.PreferencesHelper
import be.scri.services.EnglishKeyboardIME
import com.google.android.material.bottomnavigation.BottomNavigationView

Expand All @@ -36,7 +36,7 @@ class MainActivity : AppCompatActivity() {
setActionBarTitle(R.string.app_launcher_name)
val mButton = supportActionBar?.customView?.findViewById<Button>(R.id.button)
val mImage = getDrawable(R.drawable.chevron)
applyUserDarkModePreference(this)
AppCompatDelegate.setDefaultNightMode(PreferencesHelper.getUserDarkModePreference(this))
mButton?.setCompoundDrawablesWithIntrinsicBounds(mImage, null, null, null)
mButton?.compoundDrawablePadding = 2
mButton?.visibility = View.GONE
Expand Down Expand Up @@ -64,22 +64,22 @@ class MainActivity : AppCompatActivity() {
0 -> {
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_launcher_name)
setActionBarButtonInvisible()
unsetActionBarLayoutMargin()
setActionBarButtonVisibility(false)
setActionBarVisibility(false)
}

1 -> {
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_settings_title)
setActionBarButtonInvisible()
unsetActionBarLayoutMargin()
setActionBarButtonVisibility(false)
setActionBarVisibility(false)
}

2 -> {
binding.fragmentContainer.visibility = View.GONE
setActionBarButtonInvisible()
setActionBarButtonVisibility(false)
setActionBarTitle(R.string.app_about_title)
unsetActionBarLayoutMargin()
setActionBarVisibility(false)
}

else -> {
Expand Down Expand Up @@ -129,12 +129,12 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.customView?.findViewById<TextView>(R.id.name)?.text = getString(title)
}

fun setActionBarButtonVisible() {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.VISIBLE
}

fun setActionBarButtonInvisible() {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.GONE
fun setActionBarButtonVisibility(visible: Boolean) {
if (visible) {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.VISIBLE
} else {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.GONE
}
}

fun setActionBarButtonFunction(
Expand All @@ -150,46 +150,25 @@ class MainActivity : AppCompatActivity() {
viewpager.setCurrentItem(page, true)
}
frameLayout.visibility = View.GONE
unsetActionBarLayoutMargin()
setActionBarVisibility(false)
setActionBarTitle(title)
button.visibility = View.GONE
}
}

fun setActionBarLayoutMargin() {
val textView = supportActionBar?.customView?.findViewById<TextView>(R.id.name)
val params = textView?.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = -50
params.bottomMargin = 30
textView.layoutParams = params
}

fun unsetActionBarLayoutMargin() {
fun setActionBarVisibility(shouldShowOnScreen: Boolean) {
val textView = supportActionBar?.customView?.findViewById<TextView>(R.id.name)
val params = textView?.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = 50
params.bottomMargin = 0
if (shouldShowOnScreen) {
params.topMargin = -50
params.bottomMargin = 30
} else {
params.topMargin = 50
params.bottomMargin = 0
}
textView.layoutParams = params
}

private fun applyUserDarkModePreference(context: Context) {
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
val isSystemDarkTheme = isDarkMode(context)
val isUserDarkMode = sharedPref.getBoolean("dark_mode", isSystemDarkTheme)
AppCompatDelegate.setDefaultNightMode(
if (isUserDarkMode) {
AppCompatDelegate.MODE_NIGHT_YES
} else {
AppCompatDelegate.MODE_NIGHT_NO
},
)
}

fun isDarkMode(context: Context): Boolean {
val uiModeManager = context.getSystemService(UI_MODE_SERVICE) as UiModeManager
return uiModeManager.nightMode == UiModeManager.MODE_NIGHT_YES
}

fun showHint(
sharedPrefsKey: String,
hintMessageResId: Int,
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/be/scri/extensions/RecyclerView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package be.scri.extensions

import CustomDividerItemDecoration
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.recyclerview.widget.RecyclerView
import be.scri.R

fun RecyclerView.addCustomItemDecoration(context: android.content.Context) {
val itemDecoration =
CustomDividerItemDecoration(
drawable = getDrawable(context, R.drawable.rv_divider)!!,
width = 1,
marginLeft = 50,
marginRight = 50,
)
addItemDecoration(itemDecoration)
}
Loading

0 comments on commit 04bfee0

Please sign in to comment.