Text Basic 2.0.2: Fixed the awful function responsible for changing the number picker values based on which button had been clicked; changed number clickers to use respective functions in QuoteStorage.kt rather than generic getInt() and setInt()

This commit is contained in:
denizk0461
2023-05-28 12:26:48 +02:00
parent 9bc1343d22
commit 942af61b9d
7 changed files with 105 additions and 241 deletions
+2 -2
View File
@@ -11,8 +11,8 @@ android {
applicationId "com.denizk0461.textbasic" applicationId "com.denizk0461.textbasic"
minSdk 23 minSdk 23
targetSdk 33 targetSdk 33
versionCode 9 versionCode 10
versionName "2.0.1" versionName "2.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
Binary file not shown.
+2 -2
View File
@@ -11,8 +11,8 @@
"type": "SINGLE", "type": "SINGLE",
"filters": [], "filters": [],
"attributes": [], "attributes": [],
"versionCode": 9, "versionCode": 10,
"versionName": "2.0.1", "versionName": "2.0.2",
"outputFile": "app-release.apk" "outputFile": "app-release.apk"
} }
], ],
@@ -1,10 +1,7 @@
package com.denizd.textbasic.db package com.denizd.textbasic.db
import android.content.Context import android.content.Context
import android.graphics.Color
import android.graphics.Typeface import android.graphics.Typeface
import com.denizd.textbasic.util.ColorTransparentUtils
import com.denizd.textbasic.R
import com.denizd.textbasic.util.SettingsPreference import com.denizd.textbasic.util.SettingsPreference
class QuoteStorage private constructor(context: Context) { class QuoteStorage private constructor(context: Context) {
@@ -69,26 +66,6 @@ class QuoteStorage private constructor(context: Context) {
prefs.edit().putInt(KEY_TEXT_SIZE, newValue).apply() prefs.edit().putInt(KEY_TEXT_SIZE, newValue).apply()
} }
fun getInt(
pref: SettingsPreference,
defaultValue: Int = 0,
): Int = prefs.getInt(pref.key, defaultValue)
fun setInt(
pref: SettingsPreference,
newValue: Int,
) {
prefs.edit().putInt(pref.key, newValue).apply()
}
fun isInvertedEnabled(): Boolean = prefs.getBoolean(KEY_INVERTED, false)
fun getTextTransparency(): Int = prefs.getInt(KEY_TEXT_TRANSPARENCY, 100)
fun setTextTransparency(newValue: Int) {
prefs.edit().putInt(KEY_TEXT_TRANSPARENCY, newValue).apply()
}
fun getBackgroundTransparency(): Int = prefs.getInt(KEY_BG_TRANSPARENCY, 100)
fun setBackgroundTransparency(newValue: Int) {
prefs.edit().putInt(KEY_BG_TRANSPARENCY, newValue).apply()
}
fun isOrderRandom(): Boolean = prefs.getBoolean(KEY_RANDOM, false) fun isOrderRandom(): Boolean = prefs.getBoolean(KEY_RANDOM, false)
fun setIsOrderRandom(newValue: Boolean) { fun setIsOrderRandom(newValue: Boolean) {
prefs.edit().putBoolean(KEY_RANDOM, newValue).apply() prefs.edit().putBoolean(KEY_RANDOM, newValue).apply()
@@ -123,7 +100,6 @@ class QuoteStorage private constructor(context: Context) {
fun setTypefaceStyle(newValue: Int) { fun setTypefaceStyle(newValue: Int) {
prefs.edit().putInt(KEY_TYPEFACE_STYLE, newValue).apply() prefs.edit().putInt(KEY_TYPEFACE_STYLE, newValue).apply()
} }
fun getOutlineSize(): Float = prefs.getFloat(KEY_OUTLINE_SIZE, 8f)
fun getOutlineSizeNew(): Int = prefs.getInt(KEY_OUTLINE_SIZE_NEW, 8) fun getOutlineSizeNew(): Int = prefs.getInt(KEY_OUTLINE_SIZE_NEW, 8)
fun getTextColour(): String = prefs.getString(SettingsPreference.TEXT_COLOUR.key, "ffffff") ?: "ffffff" fun getTextColour(): String = prefs.getString(SettingsPreference.TEXT_COLOUR.key, "ffffff") ?: "ffffff"
@@ -136,32 +112,9 @@ class QuoteStorage private constructor(context: Context) {
prefs.edit().putString(SettingsPreference.HIGHLIGHT_COLOUR.key, newValue).apply() prefs.edit().putString(SettingsPreference.HIGHLIGHT_COLOUR.key, newValue).apply()
} }
// Pair<text colour, background colour> fun getHighlightIntensity(): Int = prefs.getInt(SettingsPreference.HIGHLIGHT_INTENSITY.key, 10)
fun getColours( fun setHighlightIntensity(newValue: Int) {
isInverted: Boolean = isInvertedEnabled(), prefs.edit().putInt(SettingsPreference.HIGHLIGHT_INTENSITY.key, newValue).apply()
textTransparency: Int = getTextTransparency(),
bgTransparency: Int = getBackgroundTransparency(),
context: Context
): Pair<Int, Int> {
val colours = when (isInverted) {
true -> Pair(R.color.widget_dark, R.color.widget_light)
false -> Pair(R.color.widget_light, R.color.widget_dark)
}
return Pair(
Color.parseColor(
ColorTransparentUtils.transparentColor(
context.getColor(colours.first),
textTransparency,
)
),//Color.parseColor(ColorTransparentUtils.transparentColor(colours.first, 10)),
Color.parseColor(
ColorTransparentUtils.transparentColor(
context.getColor(colours.second),
bgTransparency,
)
)
)
} }
fun setSize(size: Pair<Int, Int>) { fun setSize(size: Pair<Int, Int>) {
@@ -173,32 +126,6 @@ class QuoteStorage private constructor(context: Context) {
fun getSize(): Pair<Int, Int> = Pair(prefs.getInt(KEY_AVG_WIDTH, 1), prefs.getInt(KEY_AVG_HEIGHT, 1)) fun getSize(): Pair<Int, Int> = Pair(prefs.getInt(KEY_AVG_WIDTH, 1), prefs.getInt(KEY_AVG_HEIGHT, 1))
fun saveSettings(
textSize: Int,
inverted: Boolean,
textTransparency: Int,
bgTransparency: Int,
random: Boolean,
widgetPosition: Int,
backgroundType: Int,
typefaceIndex: Int,
typefaceStyleIndex: Int,
outlineSize: Float,
) {
prefs.edit().apply {
putInt(KEY_TEXT_SIZE, textSize)
putBoolean(KEY_INVERTED, inverted)
putInt(KEY_TEXT_TRANSPARENCY, textTransparency)
putInt(KEY_BG_TRANSPARENCY, bgTransparency)
putBoolean(KEY_RANDOM, random)
putInt(KEY_WIDGET_POSITION, widgetPosition)
putInt(KEY_BACKGROUND_TYPE, backgroundType)
putInt(KEY_TYPEFACE, typefaceIndex)
putInt(KEY_TYPEFACE_STYLE, typefaceStyleIndex)
putFloat(KEY_OUTLINE_SIZE, outlineSize)
}.apply()
}
fun saveQuotes(quotes: Array<String>) { fun saveQuotes(quotes: Array<String>) {
prefs.edit() prefs.edit()
.putString(KEY_QUOTES, quotes.joinToString(SEPARATOR.toString())) .putString(KEY_QUOTES, quotes.joinToString(SEPARATOR.toString()))
@@ -12,7 +12,6 @@ import com.denizd.textbasic.db.QuoteStorage
import com.denizd.textbasic.util.showSnackBar import com.denizd.textbasic.util.showSnackBar
import com.denizd.textbasic.util.viewBinding import com.denizd.textbasic.util.viewBinding
class QuoteFragment : BaseFragment(R.layout.fragment_quote), QuoteAdapter.OnDeleteListener { class QuoteFragment : BaseFragment(R.layout.fragment_quote), QuoteAdapter.OnDeleteListener {
private lateinit var storage: QuoteStorage private lateinit var storage: QuoteStorage
@@ -9,7 +9,6 @@ import android.util.TypedValue
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.TextView
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.transition.TransitionManager import androidx.transition.TransitionManager
@@ -19,11 +18,9 @@ import com.denizd.textbasic.adapter.DropdownAdapter
import com.denizd.textbasic.databinding.FragmentSettingsNewBinding import com.denizd.textbasic.databinding.FragmentSettingsNewBinding
import com.denizd.textbasic.db.QuoteStorage import com.denizd.textbasic.db.QuoteStorage
import com.denizd.textbasic.sheet.TextSheet import com.denizd.textbasic.sheet.TextSheet
import com.denizd.textbasic.util.SettingsPreference
import com.denizd.textbasic.util.getConstrast import com.denizd.textbasic.util.getConstrast
import com.denizd.textbasic.util.viewBinding import com.denizd.textbasic.util.viewBinding
import com.denizd.textbasic.widget.CanvasText import com.denizd.textbasic.widget.CanvasText
import com.google.android.material.button.MaterialButton
import com.skydoves.colorpickerview.ColorEnvelope import com.skydoves.colorpickerview.ColorEnvelope
import com.skydoves.colorpickerview.ColorPickerDialog import com.skydoves.colorpickerview.ColorPickerDialog
import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener import com.skydoves.colorpickerview.listeners.ColorEnvelopeListener
@@ -82,40 +79,48 @@ class SettingsNewFragment : BaseFragment(R.layout.fragment_settings_new) {
} }
// --- text size --- // // --- text size --- //
binding.textSize.text = storage.getInt(SettingsPreference.TEXT_SIZE, 18).toString() binding.textSize.text = storage.getTextSize().toString()
binding.buttonSizeDecreaseFast.setOnClickAction( binding.buttonSizeDecreaseFast.setOnClickListener {
4, storage.setTextSize(getValueInBoundary(
256, binding.textSize.text.toString().toInt(),
isPositive = false, 4,
isFast = true, 256,
binding.textSize, -10,
SettingsPreference.TEXT_SIZE, ))
) binding.textSize.text = storage.getTextSize().toString()
binding.buttonSizeDecrease.setOnClickAction( updatePreview()
4, }
256, binding.buttonSizeDecrease.setOnClickListener {
isPositive = false, storage.setTextSize(getValueInBoundary(
isFast = false, binding.textSize.text.toString().toInt(),
binding.textSize, 4,
SettingsPreference.TEXT_SIZE, 256,
) -1,
binding.buttonSizeIncrease.setOnClickAction( ))
4, binding.textSize.text = storage.getTextSize().toString()
256, updatePreview()
isPositive = true, }
isFast = false, binding.buttonSizeIncrease.setOnClickListener {
binding.textSize, storage.setTextSize(getValueInBoundary(
SettingsPreference.TEXT_SIZE, binding.textSize.text.toString().toInt(),
) 4,
binding.buttonSizeIncreaseFast.setOnClickAction( 256,
4, 1,
256, ))
isPositive = true, binding.textSize.text = storage.getTextSize().toString()
isFast = true, updatePreview()
binding.textSize, }
SettingsPreference.TEXT_SIZE, binding.buttonSizeIncreaseFast.setOnClickListener {
) storage.setTextSize(getValueInBoundary(
binding.textSize.text.toString().toInt(),
4,
256,
10,
))
binding.textSize.text = storage.getTextSize().toString()
updatePreview()
}
// --- text colour --- // // --- text colour --- //
setTextColour(storage.getTextColour()) setTextColour(storage.getTextColour())
@@ -155,40 +160,48 @@ class SettingsNewFragment : BaseFragment(R.layout.fragment_settings_new) {
} }
// --- highlight intensity --- // // --- highlight intensity --- //
binding.textIntensity.text = storage.getInt(SettingsPreference.HIGHLIGHT_INTENSITY, 100).toString() binding.textIntensity.text = storage.getHighlightIntensity().toString()
binding.buttonIntensityDecreaseFast.setOnClickAction( binding.buttonIntensityDecreaseFast.setOnClickListener {
0, storage.setHighlightIntensity(getValueInBoundary(
100, binding.textIntensity.text.toString().toInt(),
isPositive = false, 0,
isFast = true, 100,
binding.textIntensity, -10,
SettingsPreference.HIGHLIGHT_INTENSITY, ))
) binding.textIntensity.text = storage.getHighlightIntensity().toString()
binding.buttonIntensityDecrease.setOnClickAction( updatePreview()
0, }
100, binding.buttonIntensityDecrease.setOnClickListener {
isPositive = false, storage.setHighlightIntensity(getValueInBoundary(
isFast = false, binding.textIntensity.text.toString().toInt(),
binding.textIntensity, 0,
SettingsPreference.HIGHLIGHT_INTENSITY, 100,
) -1,
binding.buttonIntensityIncrease.setOnClickAction( ))
0, binding.textIntensity.text = storage.getHighlightIntensity().toString()
100, updatePreview()
isPositive = true, }
isFast = false, binding.buttonIntensityIncrease.setOnClickListener {
binding.textIntensity, storage.setHighlightIntensity(getValueInBoundary(
SettingsPreference.HIGHLIGHT_INTENSITY, binding.textIntensity.text.toString().toInt(),
) 0,
binding.buttonIntensityIncreaseFast.setOnClickAction( 100,
0, 1,
100, ))
isPositive = true, binding.textIntensity.text = storage.getHighlightIntensity().toString()
isFast = true, updatePreview()
binding.textIntensity, }
SettingsPreference.HIGHLIGHT_INTENSITY, binding.buttonIntensityIncreaseFast.setOnClickListener {
) storage.setHighlightIntensity(getValueInBoundary(
binding.textIntensity.text.toString().toInt(),
0,
100,
10,
))
binding.textIntensity.text = storage.getHighlightIntensity().toString()
updatePreview()
}
// --- highlight colour --- // // --- highlight colour --- //
setHighlightColour(storage.getHighlightColour()) setHighlightColour(storage.getHighlightColour())
@@ -274,17 +287,14 @@ class SettingsNewFragment : BaseFragment(R.layout.fragment_settings_new) {
binding.layoutIntensity.visibility = View.GONE binding.layoutIntensity.visibility = View.GONE
R.id.toggle_button_highlight_background R.id.toggle_button_highlight_background
} }
1 -> { // stroke 1 -> { // stroke
binding.layoutIntensity.visibility = View.VISIBLE binding.layoutIntensity.visibility = View.VISIBLE
R.id.toggle_button_highlight_stroke R.id.toggle_button_highlight_stroke
} }
2 -> { // shadow 2 -> { // shadow
binding.layoutIntensity.visibility = View.VISIBLE binding.layoutIntensity.visibility = View.VISIBLE
R.id.toggle_button_highlight_shadow R.id.toggle_button_highlight_shadow
} }
else -> { // none else -> { // none
binding.layoutIntensity.visibility = View.GONE binding.layoutIntensity.visibility = View.GONE
R.id.toggle_button_highlight_none R.id.toggle_button_highlight_none
@@ -292,41 +302,22 @@ class SettingsNewFragment : BaseFragment(R.layout.fragment_settings_new) {
} }
} }
// TODO horrible, horrible, horrible function. but it works. fix this in the future. please. /**
private fun MaterialButton.setOnClickAction( * Adds a step to a numeric value while staying within bounds as defined by min and max.
min: Int, *
max: Int, * @param value value to change
isPositive: Boolean, * @param min minimum boundary
isFast: Boolean, * @param max maximum boundary
textField: TextView, * @param step step to add to the given value; may be positive or negative
pref: SettingsPreference, * @return new value with step added, or boundary if it would have been exceeded otherwise
) { */
setOnClickListener { private fun getValueInBoundary(value: Int, min: Int, max: Int, step: Int): Int = when {
var value = storage.getInt(pref) // If the new value exceeds the maximum value, return max
if (isFast) { (value + step) > max -> max
if (isPositive) { // If the new value is below the minimum value, return min
if ((value + 10) > max) { (value + step) < min -> min
value = max // Return the value with the step added
} else { else -> value + step
value += 10
}
} else {
if ((value - 10) < min) {
value = min
} else {
value -= 10
}
}
} else {
if (isPositive && (value != max)) value += 1
if (!isPositive && (value != min)) value -= 1
}
storage.setInt(pref, value)
textField.text = value.toString()
updatePreview()
}
} }
private fun setTextColour(colour: String) { private fun setTextColour(colour: String) {
@@ -164,59 +164,6 @@ class CanvasText {
return bmp return bmp
} }
// private fun calculateWidgetGravity(widgetGravity: Int, bitmap: Bitmap, boundsText: Rect, isPreview: Boolean) : Pair<Float, Float> {
// return if (isPreview) {
// Pair((bitmap.width - boundsText.width()) / 2f, (bitmap.height + boundsText.height()) / 2f)
// } else when (widgetGravity) {
// 0 -> { // top left
// Pair(0f + boundsText.left, (boundsText.height() - boundsText.top).toFloat())
// }
// 1 -> { // top center
// Pair(
// (bitmap.width - boundsText.width()) / 2f,
// (boundsText.height() - boundsText.top).toFloat()
// )
// }
// 2 -> { // top right
// Pair(
// (bitmap.width - (boundsText.right)).toFloat(),
// (boundsText.height() - boundsText.top).toFloat()
// )
// }
// 3 -> { // center left
// Pair(0f + boundsText.left, (bitmap.height + boundsText.height()) / 2f)
// }
// // 4 == else branch
// 5 -> { // center right
// Pair(
// (bitmap.width - (boundsText.right)).toFloat(),
// (bitmap.height + boundsText.height()) / 2f
// )
// }
// 6 -> { // bottom left
// Pair(0f + boundsText.left, (bitmap.height - boundsText.bottom).toFloat())
// }
// 7 -> { // bottom center
// Pair(
// (bitmap.width - boundsText.width()) / 2f,
// (bitmap.height - boundsText.bottom).toFloat()
// )
// }
// 8 -> { // bottom right
// Pair(
// (bitmap.width - (boundsText.right)).toFloat(),
// (bitmap.height - boundsText.bottom).toFloat()
// )
// }
// else -> { // center center LIKE 4
// Pair(
// (bitmap.width - boundsText.width()) / 2f,
// (bitmap.height + boundsText.height()) / 2f
// )
// }
// }
// }
private fun roundedRect( private fun roundedRect(
left: Float, top: Float, right: Float, bottom: Float, left: Float, top: Float, right: Float, bottom: Float,
roundingX: Float, roundingY: Float roundingX: Float, roundingY: Float