Skip to content

Commit

Permalink
Merge pull request #26 from hexley21/release
Browse files Browse the repository at this point in the history
Release 1.1.2
  • Loading branch information
hexley21 authored Sep 18, 2022
2 parents 5f1441e + ab6d587 commit 03502cb
Show file tree
Hide file tree
Showing 28 changed files with 510 additions and 122 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
[![Pull Requests welcome](https://img.shields.io/badge/PRs-welcome-f7bd49.svg?style=flat-square)](https://github.com/hexley21/ArithMath/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)

</div>
<div align="center">

[<img src="https://cdn.rawgit.com/steverichey/google-play-badge-svg/master/img/en_get.svg" width="250px">](https://play.google.com/store/apps/details?id=com.hxl.arithmathics)

</div>
<details open="open">
<summary>Table of Contents</summary>

Expand All @@ -54,18 +58,18 @@ in order to improve your arithmetic skills
<summary>Screenshots</summary>
<br />

| Welcome Page | Menu Page |
| :-------------------------------------------------------------------: | :--------------------------------------------------------------------: |
| <img src="docs/images/screenshot_welcome.jpg" title="Menu" width="100%"> |<img src="docs/images/screenshot_menu.png" title="Welcome" width="100%"> |
| Welcome Page | Menu Page |
|:------------------------------------------------------------------------:|:------------------------------------------------------------------------:|
| <img src="docs/images/screenshot_welcome.png" title="Menu" width="100%"> | <img src="docs/images/screenshot_menu.png" title="Welcome" width="100%"> |


| Custom Difficulty Page | Game History Page |
| :-------------------------------------------------------------------: | :--------------------------------------------------------------------: |
| <img src="docs/images/screenshot_custom.png" title="Custom Difficulty" width="100%"> |<img src="docs/images/screenshot_history.png" title="Game History" width="100%"> |
| Custom Difficulty Page | Game History Page |
|:------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------------:|
| <img src="docs/images/screenshot_custom.png" title="Custom Difficulty" width="100%"> | <img src="docs/images/screenshot_history.png" title="Game History" width="100%"> |

| Gameplay | Dark Theme |
| :-------------------------------------------------------------------: | :--------------------------------------------------------------------: |
| <img src="docs/images/gameplay.gif" title="Gameplay" width="100%"> |<img src="docs/images/screenshot_dark.png" title="Dark Theme" width="100%"> |
| Gameplay | Dark Theme |
|:------------------------------------------------------------------:|:----------------------------------------------------------------------------:|
| <img src="docs/images/gameplay.gif" title="Gameplay" width="100%"> | <img src="docs/images/gameplay_dark.gif" title="Gameplay Dark" width="100%"> |

</details>

Expand Down
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.hxl.arithmathics"
minSdk 21
targetSdk 33
versionCode 4
versionName "1.1.1"
versionCode 5
versionName "1.1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -40,9 +40,9 @@ apply plugin: 'kotlin-kapt'
dependencies {

// Android
implementation 'androidx.core:core-ktx:1.9.0-rc01'
implementation 'androidx.appcompat:appcompat:1.6.0-beta01'
implementation 'com.google.android.material:material:1.7.0-rc01'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0-rc01'
implementation 'com.google.android.material:material:1.8.0-alpha01'
implementation 'androidx.constraintlayout:constraintlayout:2.2.0-alpha03'
implementation 'androidx.core:core-splashscreen:1.0.0'

Expand All @@ -51,8 +51,8 @@ dependencies {
kapt 'com.airbnb.android:paris-processor:2.0.1'

// View-models
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha02'

// Module implementation
implementation project(path: ':domain')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package com.hxl.arithmathics.presentation.fragment.game

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.InputType
import android.text.Selection
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2
import com.airbnb.paris.extensions.style
import com.hxl.arithmathics.R
import com.hxl.arithmathics.databinding.FragmentGameBinding
import com.hxl.arithmathics.presentation.activity.MainActivity
import com.hxl.arithmathics.presentation.fragment.results.ResultFragmentViewModel
Expand Down Expand Up @@ -51,7 +51,6 @@ class GameFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
gamePage = binding.gamePager
binding.btnAnswer.style(R.style.Default_Button)
disposable.add(
vm.readDifficulty()
.subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -91,37 +90,34 @@ class GameFragment : Fragment() {
binding.tiAnswer.setText(answerArray[position])
Selection.moveToRightEdge(binding.tiAnswer.text, binding.tiAnswer.layout)
binding.level = gamePage.currentItem + 1

if (gamePage.currentItem == answerArray.size - 1) {
binding.btnAnswer.text = resources.getString(R.string.finish)
binding.btnAnswer.style(R.style.Finish_Button)
} else {
binding.btnAnswer.text = resources.getString(R.string.continue_)
}
}
}
)
initKeyboard()
binding.attention = '/' in difEnum.operators
}
)
binding.tiAnswer.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
nextQuestion()
true
} else {
false
}
}

binding.btnAnswer.setOnClickListener { nextQuestion() }
binding.btnEnd.setOnClickListener { endGame() }
}
private fun initKeyboard() {
val editText: EditText = binding.tiAnswer
val keyboard: Keyboard = binding.keyboard

editText.setRawInputType(InputType.TYPE_CLASS_TEXT)
editText.setTextIsSelectable(true)
editText.showSoftInputOnFocus = false

keyboard.setConnection(editText.onCreateInputConnection(EditorInfo()), ::nextQuestion)
}

private fun nextQuestion() {
answerArray[gamePage.currentItem] = binding.tiAnswer.text.toString()
if (gamePage.currentItem == answerArray.size - 1) {
endGame()
}
gamePage.setCurrentItem(gamePage.currentItem + 1, true)
binding.attention = false
}

private fun startTimer() {
Expand Down Expand Up @@ -149,6 +145,8 @@ class GameFragment : Fragment() {
Timer().scheduleAtFixedRate(timerTask, 0, 1000)
}



private fun endGame() {
val resultVm: ResultFragmentViewModel by activityViewModels()
resultVm.answers = answerArray
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.hxl.arithmathics.presentation.fragment.game

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.InputConnection
import android.widget.Button
import android.widget.LinearLayout
import com.hxl.arithmathics.R
import com.hxl.arithmathics.databinding.KeyboardBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class Keyboard(context: Context, attrs: AttributeSet) :
LinearLayout(context, attrs, 0), View.OnClickListener {
private var binding: KeyboardBinding

private var buttonBackSpace: Button
private var buttonAnswer: Button
private var buttonOne: Button
private var buttonTwo: Button
private var buttonThree: Button
private var buttonFour: Button
private var buttonFive: Button
private var buttonSix: Button
private var buttonSeven: Button
private var buttonEight: Button
private var buttonNine: Button
private var buttonZero: Button
private var buttonDot: Button
private var buttonNegative: Button

private val keyValues = SparseArray<String>()
private lateinit var inputConnection: InputConnection
private lateinit var answer: () ->Unit

init {
val layoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
binding = KeyboardBinding.inflate(layoutInflater, this, true)
buttonBackSpace = binding.btnBackspace
buttonAnswer = binding.btnAnswer
buttonOne = binding.btnOne
buttonTwo = binding.btnTwo
buttonThree = binding.btnThree
buttonFour = binding.btnFour
buttonFive = binding.btnFive
buttonSix = binding.btnSix
buttonSeven = binding.btnSeven
buttonEight = binding.btnEight
buttonNine = binding.btnNine
buttonZero = binding.btnZero
buttonDot = binding.btnDot
buttonNegative = binding.btnNegative

buttonBackSpace.setOnClickListener(this)
buttonAnswer.setOnClickListener(this)
buttonOne.setOnClickListener(this)
buttonTwo.setOnClickListener(this)
buttonThree.setOnClickListener(this)
buttonFour.setOnClickListener(this)
buttonFive.setOnClickListener(this)
buttonSix.setOnClickListener(this)
buttonSeven.setOnClickListener(this)
buttonEight.setOnClickListener(this)
buttonNine.setOnClickListener(this)
buttonZero.setOnClickListener(this)
buttonDot.setOnClickListener(this)
buttonNegative.setOnClickListener(this)

keyValues.put(buttonOne.id, getStringResource(R.string.btn_one))
keyValues.put(buttonTwo.id, getStringResource(R.string.btn_two))
keyValues.put(buttonThree.id, getStringResource(R.string.btn_three))
keyValues.put(buttonFour.id, getStringResource(R.string.btn_four))
keyValues.put(buttonFive.id, getStringResource(R.string.btn_five))
keyValues.put(buttonSix.id, getStringResource(R.string.btn_six))
keyValues.put(buttonSeven.id, getStringResource(R.string.btn_seven))
keyValues.put(buttonEight.id, getStringResource(R.string.btn_eight))
keyValues.put(buttonNine.id, getStringResource(R.string.btn_nine))
keyValues.put(buttonZero.id, getStringResource(R.string.btn_zero))
keyValues.put(buttonNegative.id, getStringResource(R.string.btn_negative))
keyValues.put(buttonDot.id, getStringResource(R.string.btn_dot))

}

override fun onClick(v: View) {
when (v.id) {
buttonAnswer.id -> answer()
buttonBackSpace.id -> onBackspace()
else -> onElse(v.id)
}
}

private fun onBackspace() {
if (TextUtils.isEmpty(inputConnection.getSelectedText(0))) {
inputConnection.deleteSurroundingText(1, 0)
}
else {
commitText("", 1)
}
}

private fun onElse(id: Int) {
commitText(keyValues[id])
}

private fun getStringResource(id: Int): String {
return context.resources.getString(id)
}

private fun commitText(text: String, position: Int = 1) {
inputConnection.commitText(text, position)
}

fun setConnection(ic: InputConnection?, unit: () -> Unit) {
inputConnection = ic!!
answer = unit
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class GameResultFormatter {
fun getDifficultyColor(difficulty: Int, context: Context): Int {
val typedValue = TypedValue()
when (difficulty) {
in 0..200 -> context.theme.resolveAttribute(com.google.android.material.R.attr.colorTertiaryContainer, typedValue, true)
in 200..1000 -> context.theme.resolveAttribute(com.google.android.material.R.attr.colorPrimaryContainer, typedValue, true)
in 0..50 -> context.theme.resolveAttribute(com.google.android.material.R.attr.colorTertiaryContainer, typedValue, true)
in 50..200 -> context.theme.resolveAttribute(com.google.android.material.R.attr.colorPrimaryContainer, typedValue, true)
else -> context.theme.resolveAttribute(com.google.android.material.R.attr.colorErrorContainer, typedValue, true)
}
return typedValue.data
}

fun getDifficultyText(difficulty: Int, resources: Resources): String {
return when (difficulty) {
in 0..100 -> resources.getString(R.string.beginner)
in 100..200 -> resources.getString(R.string.easy)
in 200..350 -> resources.getString(R.string.medium)
in 350..1000 -> resources.getString(R.string.hard)
in 1000..3000 -> resources.getString(R.string.expert)
in 3000..7000 -> resources.getString(R.string.extreme)
in 7000..10000 -> resources.getString(R.string.impossible)
in 0..30 -> resources.getString(R.string.beginner)
in 30..50 -> resources.getString(R.string.easy)
in 50..100 -> resources.getString(R.string.medium)
in 100..200 -> resources.getString(R.string.hard)
in 200..400 -> resources.getString(R.string.expert)
in 400..500 -> resources.getString(R.string.extreme)
in 500..600 -> resources.getString(R.string.impossible)
else -> resources.getString(R.string.calculator)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.schedulers.Schedulers
import javax.inject.Inject
import kotlin.math.log
import kotlin.math.pow
import kotlin.math.roundToInt

@HiltViewModel
Expand All @@ -25,6 +27,8 @@ class ResultFragmentViewModel @Inject constructor(
lateinit var questions: Array<Question>
lateinit var answers: Array<String>
private val disposable = CompositeDisposable()
private val operators: HashMap<Char, Int> = hashMapOf('+' to 1, '-' to 2, '×' to 3, '÷' to 4)
private val numbers = arrayOf('0', '1', '2' , '3' ,'4', '5', '6', '7', '8', '9')
var corrects: Int = 0
var time: Int = 0

Expand All @@ -50,13 +54,52 @@ class ResultFragmentViewModel @Inject constructor(
private fun calculateDifficulty(operations: Int): Int {
var difficulty = 0.0
for (i in questions) {
difficulty += Question.getDifficulty(i, operations)
difficulty += getDifficulty(i, operations)
}
return difficulty.roundToInt()
return difficulty.roundToInt() / questions.size
}

private fun getDifficulty(question: Question, operations: Int): Double {
var answerDifficulty = log(question.answer.replace(',', '.').toDouble().pow(2.0), 2.0)
if (answerDifficulty < 0) { answerDifficulty = 5.0 }
else if (answerDifficulty == 0.0) { answerDifficulty = 3.0 }

val operationsDifficulty = questionToNumbers(question.question, operations).sum() / operations
var operatorDifficulty = 0

val decimalDifficulty = if(question.answer.replace(',', '.').toDouble() % 1.0 != 0.0) { 0 } else { 20 }

val operatorsCount: Array<Int> = Array(4) { 0 }
for ((key, value) in operators) {
operatorsCount[value-1] = question.question.count { it == key }
}
for (i in 0..3) {
operatorDifficulty += operatorsCount[i] * (i/2 + 1)
}
return answerDifficulty + operationsDifficulty + operatorDifficulty + decimalDifficulty
}

private fun questionToNumbers(question: String, operations: Int): Array<Int> {
val numbersArray: Array<Int> = Array(operations + 1) { -1 }
var number = ""
var count = 0
for (i in question.replace(" ", "")) {
if (i in numbers) {
number += i
}
else {
numbersArray[count] = (number.toInt())
count++
number = ""
}
}
numbersArray[count] = (number.toInt())
return numbersArray
}

override fun onCleared() {
super.onCleared()
disposable.clear()
}
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_check_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
Loading

0 comments on commit 03502cb

Please sign in to comment.