Skip to content

Commit

Permalink
Merge pull request #34 from mplekunov/Version_In_Progress
Browse files Browse the repository at this point in the history
HotFix: Version 2.1
  • Loading branch information
mplekunov authored May 25, 2022
2 parents 72f6f2f + 653a6e9 commit a8fd4df
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 49 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ android {
jvmTarget = '1.8'
}
buildToolsVersion '32.1.0 rc1'
flavorDimensions 'CalculatorApp'
productFlavors {
CalculatorApp {
dimension 'CalculatorApp'
versionCode 2
versionName '2.1'
}
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ abstract class Clickable(
protected val buttons: Buttons,
protected val viewModel: CalculatorViewModel,
protected val liveInput: MutableLiveData<SpannableStringBuilder>,
protected val index: Int
protected var index: Int
) : ClickableSpan() {
protected val spannable get() = liveInput.value ?: SpannableStringBuilder()


open var highlightedColor: Int = ResourcesCompat.getColor(context.resources, R.color.highlighted_text, context.theme)
open var defaultTextColor: Int = ResourcesCompat.getColor(context.resources, R.color.default_text, context.theme)

Expand Down Expand Up @@ -92,7 +91,7 @@ abstract class Clickable(

protected abstract fun bindToEditableToken()

protected fun applyColorToSpan(@ColorInt color: Int, start: Int = 0, end: Int = liveInput.value?.length!!) {
protected open fun applyColorToSpan(@ColorInt color: Int, start: Int = 0, end: Int = liveInput.value?.length!!) {
spannable.highlight(color, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}

Expand All @@ -115,7 +114,7 @@ abstract class Clickable(
btn.isClickable = isClickable
}

private fun resetSpannableFocus() {
protected open fun resetSpannableFocus() {
applyColorToSpan(defaultTextColor)
setButtonsAsClickable()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.calculator.model.input.expandedEditing

import android.app.Activity
import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.DynamicDrawableSpan
import android.text.style.ImageSpan
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.lifecycle.MutableLiveData
import com.example.calculator.R
import com.example.calculator.model.function.FunctionKind
import com.example.calculator.model.input.defaultEditing.Clickable
import com.example.calculator.model.number.NumberKind
import com.example.calculator.model.operator.OperatorKind
import com.example.calculator.model.token.Token
import com.example.calculator.model.token.TokenTypes
import com.example.calculator.model.wrapper.Buttons
import com.example.calculator.parser.FunctionParser
import com.example.calculator.parser.NumberParser
import com.example.calculator.parser.OperatorParser
import com.example.calculator.viewmodel.CalculatorViewModel

abstract class ExpandedClickable(
context: Context,
buttons: Buttons,
viewModel: CalculatorViewModel,
liveInput: MutableLiveData<SpannableStringBuilder>,
index: Int
) : Clickable(context, buttons, viewModel, liveInput, index) {
private val token get() = viewModel.inputAsTokens[index]

override fun resetSpannableFocus() {
super.resetSpannableFocus()

val originalIndex = index

for (i in viewModel.inputAsTokens.indices) {
index = i
if (token.type == TokenTypes.Function)
setDrawableSpan(defaultTextColor)
}

index = originalIndex
}

override fun bindToEditableToken() {}

override fun applyColorToSpan(color: Int, start: Int, end: Int) {
super.applyColorToSpan(color, start, end)

if (token.type == TokenTypes.Function)
setDrawableSpan(color)
}

protected fun setDrawableSpan(color: Int = defaultTextColor, start: Int = newStart, end: Int = newEnd) {
val drawable = when(token.type) {
TokenTypes.Function -> buttons.functions[FunctionParser.parse<FunctionKind>(token)]!!.drawable.constantState!!.newDrawable().mutate()
TokenTypes.Number -> buttons.numbers[NumberParser.parse<NumberKind>(token)]!!.drawable.constantState?.newDrawable()!!.mutate()
TokenTypes.Operator -> buttons.operators[OperatorParser.parse<OperatorKind>(token)]!!.drawable.constantState!!.newDrawable().mutate()
}

drawable.setTint(color)
val size: Int = (context as Activity).findViewById<TextView>(R.id.input).textSize.toInt()
drawable.setBounds(0, 0, size - 15, size - 20)

if (FunctionParser.parse<FunctionKind>(token) == FunctionKind.LOG)
drawable.setBounds(0, 0, size + 45, size)

spannable.setSpan(
ImageSpan(
drawable,
DynamicDrawableSpan.ALIGN_CENTER
), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)

liveInput.value = spannable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.text.style.DynamicDrawableSpan
import android.text.style.ImageSpan
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import com.example.calculator.R
Expand All @@ -30,28 +31,24 @@ class ExpandedClickableFunction(
viewModel: CalculatorViewModel,
liveInput: MutableLiveData<SpannableStringBuilder>,
index: Int
) : Clickable(context, buttons, viewModel, liveInput, index) {
) : ExpandedClickable(context, buttons, viewModel, liveInput, index) {

override lateinit var oldString: String

override val what
get() = ExpandedClickableFunction(context, buttons, viewModel, liveInput, index)

override fun onClick(view: View) {
super.onClick(view)
}

override fun bindToEditableToken() {
buttons.operators.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false) }
buttons.numbers.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false) }

setButtonState(buttons.clear, disabledButtonColor, false)
setButtonState(buttons.clearAll, disabledButtonColor, false)

setButtonState(buttons.functions[FunctionKind.PERCENTAGE], disabledButtonColor, false)
setButtonState(buttons.functions[FunctionKind.SQUARED], disabledButtonColor, false)
setButtonState(buttons.functions[FunctionKind.FACTORIAL], disabledButtonColor, false)

setButtonState(buttons.clear, disabledButtonColor, false)
setButtonState(buttons.clearAll, disabledButtonColor, false)

buttons.functions.forEach { (button, function) ->
button.setOnClickListener {
buttons.functions
Expand All @@ -70,42 +67,10 @@ class ExpandedClickableFunction(

if (viewModel.set(function, index)) {
replaceSpan(viewModel.formattedInput[index])
setDrawableSpan()
applyColorToSpan(highlightedColor, newStart, newEnd)
}
}
}
}


override fun replaceSpan(newString: String) {
setDrawableSpan(newString)
}

private fun setDrawableSpan(newString: String) {
spannable.replace(oldStart, oldEnd, newString)

val token = viewModel.inputAsTokens[index]

val drawable = when(token.type) {
TokenTypes.Function -> buttons.functions[FunctionParser.parse<FunctionKind>(token)]!!.drawable.constantState!!.newDrawable().mutate()
TokenTypes.Number -> buttons.numbers[NumberParser.parse<NumberKind>(token)]!!.drawable.constantState?.newDrawable()!!.mutate()
TokenTypes.Operator -> buttons.operators[OperatorParser.parse<OperatorKind>(token)]!!.drawable.constantState!!.newDrawable().mutate()
}

drawable.setTint(ContextCompat.getColor(context, R.color.white))
val size: Int = (context as Activity).findViewById<TextView>(R.id.input).textSize.toInt()
drawable.setBounds(0, 0, size - 15, size - 20)

if (FunctionParser.parse<FunctionKind>(token) == FunctionKind.LOG)
drawable.setBounds(0, 0, size + 45, size)

spannable.setSpan(
ImageSpan(
drawable,
DynamicDrawableSpan.ALIGN_CENTER
), newStart, newEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)

spannable.setSpan(newStart, newEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ class ExpandedClickableNumber(
viewModel: CalculatorViewModel,
liveInput: MutableLiveData<SpannableStringBuilder>,
index: Int
) : ClickableNumber(context, buttons, viewModel, liveInput, index) {
) : ExpandedClickable(context, buttons, viewModel, liveInput, index) {
override lateinit var oldString: String

override val what
get() = ExpandedClickableNumber(context, buttons, viewModel, liveInput, index)

override fun bindToEditableToken() {
super.bindToEditableToken()
buttons.operators.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false)}
buttons.functions.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false) }

buttons.clear.setOnClickListener {
oldString = viewModel.formattedInput[index]

if (viewModel.delete(index)) {
replaceSpan(viewModel.formattedInput[index])
applyColorToSpan(highlightedColor, newStart, newEnd)
}
}

buttons.clearAll.setOnClickListener {
oldString = viewModel.formattedInput[index]

if (viewModel.deleteAll(index)) {
replaceSpan(viewModel.formattedInput[index])
applyColorToSpan(highlightedColor, newStart, newEnd)
}
}

buttons.numbers.forEach { (button, number) ->
button.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ExpandedClickableOperator(
viewModel: CalculatorViewModel,
liveInput: MutableLiveData<SpannableStringBuilder>,
index: Int
) : ClickableOperator(context, buttons, viewModel, liveInput, index) {
) : ExpandedClickable(context, buttons, viewModel, liveInput, index) {
override lateinit var oldString: String

override val what
get() = ExpandedClickableOperator(context, buttons, viewModel, liveInput, index)

Expand All @@ -29,7 +31,11 @@ class ExpandedClickableOperator(
}

override fun bindToEditableToken() {
super.bindToEditableToken()
buttons.functions.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false) }
buttons.numbers.forEach { (button, _) -> setButtonState(button, disabledButtonColor, false) }

setButtonState(buttons.clear, disabledButtonColor, false)
setButtonState(buttons.clearAll, disabledButtonColor, false)

buttons.operators.forEach { (button, operator) ->
if (operator == OperatorKind.LEFT_BRACKET || operator == OperatorKind.RIGHT_BRACKET)
Expand Down

0 comments on commit a8fd4df

Please sign in to comment.