Skip to content

Commit

Permalink
Merge pull request #234 from Linfye/replace-magic-numbers
Browse files Browse the repository at this point in the history
Fix Magic Number Issues
  • Loading branch information
andrewtavis authored Oct 30, 2024
2 parents 0bf3879 + 96573b4 commit e627562
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 92 deletions.
18 changes: 13 additions & 5 deletions app/src/main/java/be/scri/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MainActivity : AppCompatActivity() {
supportActionBar?.setCustomView(R.layout.custom_action_bar_layout)
supportActionBar?.elevation = 0F
val layoutParams = supportActionBar?.customView?.layoutParams
layoutParams?.height = 1000
layoutParams?.height = DEFAULT_HEIGHT
supportActionBar?.customView?.layoutParams = layoutParams
setActionBarTitle(R.string.app_launcher_name)
val mButton = supportActionBar?.customView?.findViewById<Button>(R.id.button)
Expand Down Expand Up @@ -160,11 +160,11 @@ class MainActivity : AppCompatActivity() {
val textView = supportActionBar?.customView?.findViewById<TextView>(R.id.name)
val params = textView?.layoutParams as ViewGroup.MarginLayoutParams
if (shouldShowOnScreen) {
params.topMargin = -50
params.bottomMargin = 30
params.topMargin = ACTION_BAR_TOP_MARGIN_VISIBLE
params.bottomMargin = ACTION_BAR_BOTTOM_MARGIN_VISIBLE
} else {
params.topMargin = 50
params.bottomMargin = 0
params.topMargin = ACTION_BAR_TOP_MARGIN_HIDDEN
params.bottomMargin = ACTION_BAR_BOTTOM_MARGIN_HIDDEN
}
textView.layoutParams = params
}
Expand Down Expand Up @@ -215,4 +215,12 @@ class MainActivity : AppCompatActivity() {
val hintLayout = findViewById<View>(R.id.hint_layout)
hintLayout.visibility = View.GONE
}

companion object {
private const val DEFAULT_HEIGHT = 1000
private const val ACTION_BAR_TOP_MARGIN_VISIBLE = -50
private const val ACTION_BAR_TOP_MARGIN_HIDDEN = 50
private const val ACTION_BAR_BOTTOM_MARGIN_VISIBLE = 30
private const val ACTION_BAR_BOTTOM_MARGIN_HIDDEN = 0
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/be/scri/extensions/ContextStyling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.graphics.Color
import be.scri.R
import be.scri.helpers.DARK_GREY

// handle system default theme (Material You) specially as the color is taken from the system, not hardcoded by us
// Handle system default theme (Material You) specially as the color is taken from the system, not hardcoded by us.
fun Context.getProperTextColor() =
if (baseConfig.isUsingSystemTheme) {
resources.getColor(R.color.you_neutral_text_color, theme)
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/java/be/scri/extensions/Int.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import android.graphics.Color
import be.scri.helpers.DARK_GREY
import java.util.Random

private const val RED_COEFFICIENT = 299
private const val GREEN_COEFFICIENT = 587
private const val BLUE_COEFFICIENT = 114
private const val COEFFICIENT_SUM = 1000
private const val Y_THRESHOLD = 149

fun Int.getContrastColor(): Int {
val y = (299 * Color.red(this) + 587 * Color.green(this) + 114 * Color.blue(this)) / 1000
return if (y >= 149 && this != Color.BLACK) DARK_GREY else Color.WHITE
val y =
(
RED_COEFFICIENT * Color.red(this) +
GREEN_COEFFICIENT * Color.green(this) +
BLUE_COEFFICIENT * Color.blue(this)
) / COEFFICIENT_SUM
return if (y >= Y_THRESHOLD && this != Color.BLACK) DARK_GREY else Color.WHITE
}

fun Int.adjustAlpha(factor: Float): Int {
Expand All @@ -19,17 +30,21 @@ fun Int.adjustAlpha(factor: Float): Int {

fun ClosedRange<Int>.random() = Random().nextInt(endInclusive - start) + start

// taken from https://stackoverflow.com/a/40964456/1967672
fun Int.darkenColor(factor: Int = 8): Int {
// Taken from https://stackoverflow.com/a/40964456/1967672.
private const val HSV_COMPONENT_COUNT = 3
private const val DEFAULT_DARKEN_FACTOR = 8
private const val FACTOR_DIVIDER = 100

fun Int.darkenColor(factor: Int = DEFAULT_DARKEN_FACTOR): Int {
if (this == Color.WHITE || this == Color.BLACK) {
return this
}

val darkFactor = factor
var hsv = FloatArray(3)
var hsv = FloatArray(HSV_COMPONENT_COUNT)
Color.colorToHSV(this, hsv)
val hsl = hsv2hsl(hsv)
hsl[2] -= darkFactor / 100f
hsl[2] -= darkFactor / FACTOR_DIVIDER.toFloat()
if (hsl[2] < 0) {
hsl[2] = 0f
}
Expand All @@ -43,22 +58,24 @@ fun Int.lightenColor(factor: Int = 8): Int {
}

val lightFactor = factor
var hsv = FloatArray(3)
var hsv = FloatArray(HSV_COMPONENT_COUNT)
Color.colorToHSV(this, hsv)
val hsl = hsv2hsl(hsv)
hsl[2] += lightFactor / 100f
hsl[2] += lightFactor / FACTOR_DIVIDER.toFloat()
if (hsl[2] < 0) {
hsl[2] = 0f
}
hsv = hsl2hsv(hsl)
return Color.HSVToColor(hsv)
}

private const val LIGHTNESS_THRESHOLD = 0.5f

private fun hsl2hsv(hsl: FloatArray): FloatArray {
val hue = hsl[0]
var sat = hsl[1]
val light = hsl[2]
sat *= if (light < .5) light else 1 - light
sat *= if (light < LIGHTNESS_THRESHOLD) light else 1 - light
return floatArrayOf(hue, 2f * sat / (light + sat), light + sat)
}

Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/be/scri/fragments/LanguageSettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class LanguageSettingsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
(requireActivity() as MainActivity).setActionBarButtonFunction(3, R.string.app_settings_title)
val mainActivity = requireActivity() as MainActivity
mainActivity.setActionBarButtonFunction(
ACTION_BAR_BUTTON_INDEX,
R.string.app_settings_title,
)
val callback =
requireActivity().onBackPressedDispatcher.addCallback(this) {
viewpager.setCurrentItem(3, true)
viewpager.setCurrentItem(ACTION_BAR_BUTTON_INDEX, true)
(requireActivity() as MainActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false)
(requireActivity() as MainActivity).setActionBarVisibility(false)
}
Expand All @@ -55,8 +59,8 @@ class LanguageSettingsFragment : Fragment() {
val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = requireActivity().findViewById<ViewGroup>(R.id.fragment_container)
(requireActivity() as MainActivity).setActionBarVisibility(false)
if (viewpager.currentItem == 3) {
viewpager.setCurrentItem(3, true)
if (viewpager.currentItem == ACTION_BAR_BUTTON_INDEX) {
viewpager.setCurrentItem(ACTION_BAR_BUTTON_INDEX, true)
frameLayout.visibility = View.GONE
(requireActivity() as MainActivity).setActionBarVisibility(false)
} else {
Expand Down Expand Up @@ -86,7 +90,13 @@ class LanguageSettingsFragment : Fragment() {
(requireActivity() as MainActivity).setActionBarTitle(titleInt)
(requireActivity() as MainActivity).showFragmentContainer()
(requireActivity() as MainActivity).setActionBarButtonVisibility(true)
(requireActivity() as MainActivity).setActionBarButtonFunction(3, R.string.app_settings_title)
val mainActivity = requireActivity() as MainActivity
val actionBarButtonIndex = ACTION_BAR_BUTTON_INDEX
val titleResId = R.string.app_settings_title
mainActivity.setActionBarButtonFunction(
actionBarButtonIndex,
titleResId,
)
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
(requireActivity() as MainActivity).setActionBarButtonVisibility(false)
parentFragmentManager
Expand Down Expand Up @@ -279,4 +289,8 @@ class LanguageSettingsFragment : Fragment() {
else -> return R.string.app__global_english
}
}

companion object {
private const val ACTION_BAR_BUTTON_INDEX = 3
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/be/scri/fragments/PrivacyPolicyFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PrivacyPolicyFragment : Fragment() {

textView?.let {
val params = it.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = -50
params.topMargin = NEGATIVE_TOP_MARGIN
it.layoutParams = params
}

Expand Down Expand Up @@ -84,4 +84,8 @@ class PrivacyPolicyFragment : Fragment() {
)
return binding.root
}

companion object {
private const val NEGATIVE_TOP_MARGIN = -50
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/be/scri/helpers/AlphanumericComparator.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package be.scri.helpers

// taken from https://gist.github.com/MichaelRocks/1b94bb44c7804e999dbf31dac86955ec
// make IMG_5.jpg come before IMG_10.jpg
// Taken from https://gist.github.com/MichaelRocks/1b94bb44c7804e999dbf31dac86955ec.
// Make IMG_5.jpg come before IMG_10.jpg.
class AlphanumericComparator {
fun compare(
string1: String,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/be/scri/helpers/CommonsConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package be.scri.helpers

val DARK_GREY = 0xFF333333.toInt()

// shared preferences
// Shared preferences.
const val PREFS_KEY = "Prefs"
const val TEXT_COLOR = "text_color"
const val KEY_COLOR = "key_color"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/be/scri/helpers/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const val SHIFT_OFF = 0
const val SHIFT_ON_ONE_CHAR = 1
const val SHIFT_ON_PERMANENT = 2

// limit the count of alternative characters that show up at long pressing a key
// Limit the count of alternative characters that show up at long pressing a key.
const val MAX_KEYS_PER_MINI_ROW = 9

// shared prefs
// Shared preferences.
const val VIBRATE_ON_KEYPRESS = "vibrate_on_keypress"
const val SHOW_POPUP_ON_KEYPRESS = "show_popup_on_keypress"
const val DARK_THEME = "dark_theme"
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/be/scri/helpers/MyKeyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class MyKeyboard {
private const val TAG_KEY = "Key"
private const val EDGE_LEFT = 0x01
private const val EDGE_RIGHT = 0x02
private const val WIDTH_DIVIDER = 10
const val KEYCODE_SHIFT = -1
const val KEYCODE_MODE_CHANGE = -2
const val KEYCODE_ENTER = -4
Expand Down Expand Up @@ -302,7 +303,7 @@ class MyKeyboard {
) {
mDisplayWidth = context.resources.displayMetrics.widthPixels
mDefaultHorizontalGap = 0
mDefaultWidth = mDisplayWidth / 10
mDefaultWidth = mDisplayWidth / WIDTH_DIVIDER
mDefaultHeight = mDefaultWidth
mKeys = ArrayList()
mEnterKeyType = enterKeyType
Expand Down Expand Up @@ -455,7 +456,9 @@ class MyKeyboard {
parser: XmlResourceParser,
) {
val a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.MyKeyboard)
mDefaultWidth = getDimensionOrFraction(a, R.styleable.MyKeyboard_keyWidth, mDisplayWidth, mDisplayWidth / 10)
val keyWidthResId = R.styleable.MyKeyboard_keyWidth
val defaultWidth = mDisplayWidth / WIDTH_DIVIDER
mDefaultWidth = getDimensionOrFraction(a, keyWidthResId, mDisplayWidth, defaultWidth)
mDefaultHeight = res.getDimension(R.dimen.key_height).toInt()
mDefaultHorizontalGap = getDimensionOrFraction(a, R.styleable.MyKeyboard_horizontalGap, mDisplayWidth, 0)
a.recycle()
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/EnglishKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import be.scri.views.MyKeyboardView
class EnglishKeyboardIME : SimpleKeyboardIME("English") {
override fun getKeyboardLayoutXML(): Int = R.xml.keys_letters_english

override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/FrenchKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class FrenchKeyboardIME : SimpleKeyboardIME("French") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/GermanKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class GermanKeyboardIME : SimpleKeyboardIME("German") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/ItalianKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ItalianKeyboardIME : SimpleKeyboardIME("Italian") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class PortugueseKeyboardIME : SimpleKeyboardIME("Portuguese") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/RussianKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class RussianKeyboardIME : SimpleKeyboardIME("Russian") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/be/scri/services/SimpleKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ abstract class SimpleKeyboardIME(
MyKeyboardView.OnKeyboardActionListener {
abstract fun getKeyboardLayoutXML(): Int

abstract var shiftPermToggleSpeed: Int // how quickly do we have to doubletap shift to enable permanent caps lock
abstract val keyboardLetters: Int
abstract val keyboardSymbols: Int
abstract val keyboardSymbolShift: Int
Expand All @@ -62,12 +61,15 @@ abstract class SimpleKeyboardIME(
private var emojiBtnTablet2: Button? = null
private var emojiSpaceTablet2: View? = null
private var emojiBtnTablet3: Button? = null

// How quickly do we have to doubletap shift to enable permanent caps lock.
private val shiftPermToggleSpeed: Int = DEFAULT_SHIFT_PERM_TOGGLE_SPEED
private lateinit var dbHelper: DatabaseHelper
lateinit var emojiKeywords: HashMap<String, MutableList<String>>
var isAutoSuggestEnabled: Boolean = false
var lastWord: String? = null
var autosuggestEmojis: MutableList<String>? = null
// abstract var keyboardViewKeyboardBinding : KeyboardViewKeyboardBinding
// abstract var keyboardViewKeyboardBinding : KeyboardViewKeyboardBinding

protected var currentState: ScribeState = ScribeState.IDLE
protected lateinit var keyboardBinding: KeyboardViewKeyboardBinding
Expand Down Expand Up @@ -323,7 +325,7 @@ abstract class SimpleKeyboardIME(

fun getText(): String? {
val inputConnection = currentInputConnection ?: return null
return inputConnection.getTextBeforeCursor(20, 0)?.toString()
return inputConnection.getTextBeforeCursor(TEXT_LENGTH, 0)?.toString()
}

fun getLastWordBeforeCursor(): String? {
Expand Down Expand Up @@ -628,7 +630,7 @@ abstract class SimpleKeyboardIME(
commandBar.text = newText
}
} else {
// Handling space key logic
// Handling space key logic.
if (keyboardMode != keyboardLetters && code == MyKeyboard.KEYCODE_SPACE) {
binding?.commandBar?.text = " "
val originalText = inputConnection.getExtractedText(ExtractedTextRequest(), 0).text
Expand Down Expand Up @@ -695,4 +697,9 @@ abstract class SimpleKeyboardIME(
}
}
}

private companion object {
const val DEFAULT_SHIFT_PERM_TOGGLE_SPEED = 500
const val TEXT_LENGTH = 20
}
}
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/SpanishKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class SpanishKeyboardIME : SimpleKeyboardIME(language = "Spanish") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/be/scri/services/SwedishKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class SwedishKeyboardIME : SimpleKeyboardIME("Swedish") {
override var keyboardView: MyKeyboardView? = null
override var keyboard: MyKeyboard? = null
override var enterKeyType = IME_ACTION_NONE
override var shiftPermToggleSpeed = 500
override val keyboardLetters = 0
override val keyboardSymbols = 1
override val keyboardSymbolShift = 2
Expand Down
Loading

0 comments on commit e627562

Please sign in to comment.