commit 7e2b034dd7d4136a56272c62c79d879ed15ca5f1 Author: hmalik144 Date: Thu Feb 23 23:36:07 2023 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..8d3ccd5 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +sumtest \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..709b046 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,49 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +android { + namespace 'com.example.sumtest' + compileSdk 33 + + defaultConfig { + applicationId "com.example.sumtest" + minSdk 21 + targetSdk 33 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } + buildFeatures { + viewBinding true + } +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.9.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' + implementation 'androidx.activity:activity-ktx:1.6.1' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.4' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..b4393ac --- /dev/null +++ b/app/src/androidTest/java/com/example/sumtest/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.sumtest + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.sumtest", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2fb7b0f --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/sumtest/SumActivity.kt b/app/src/main/java/com/example/sumtest/SumActivity.kt new file mode 100644 index 0000000..a1d4e16 --- /dev/null +++ b/app/src/main/java/com/example/sumtest/SumActivity.kt @@ -0,0 +1,73 @@ +package com.example.sumtest + +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.View.GONE +import android.view.View.VISIBLE +import android.view.inputmethod.InputMethodManager +import android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS +import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import com.example.sumtest.databinding.ActivityMainBinding +import kotlinx.coroutines.launch + +class SumActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + val viewModel: SumViewModel by viewModels() + + val binding = ActivityMainBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) + + val textChangedListener : TextWatcher = object : TextWatcher { + override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { + } + + override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { + viewModel.onTextChanged() + } + + override fun afterTextChanged(p0: Editable?) { + } + } + + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + viewModel.uiState.collect { + binding.loading.visibility = if (it.loading) VISIBLE else GONE + binding.result.visibility = if (it.result != null) VISIBLE else GONE + binding.result.text = it.result + binding.error.visibility = if (it.error != null) VISIBLE else GONE + binding.error.text = it.error + if (it.result != null) { + binding.firstNumber.removeTextChangedListener(textChangedListener) + binding.firstNumber.text = null + binding.firstNumber.addTextChangedListener(textChangedListener) + + binding.secondNumber.removeTextChangedListener(textChangedListener) + binding.secondNumber.text = null + binding.secondNumber.addTextChangedListener(textChangedListener) + } + } + } + } + + binding.cta.setOnClickListener { + viewModel.onAddClicked( + binding.firstNumber.text.toString(), + binding.secondNumber.text.toString() + ) + (getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager) + .hideSoftInputFromWindow(currentFocus?.windowToken, HIDE_NOT_ALWAYS) + } + + binding.firstNumber.addTextChangedListener(textChangedListener) + binding.secondNumber.addTextChangedListener(textChangedListener) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumtest/SumViewModel.kt b/app/src/main/java/com/example/sumtest/SumViewModel.kt new file mode 100644 index 0000000..c0ac750 --- /dev/null +++ b/app/src/main/java/com/example/sumtest/SumViewModel.kt @@ -0,0 +1,108 @@ +package com.example.sumtest + +import android.os.Handler +import android.os.Looper +import androidx.lifecycle.ViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update + +data class SumUiState( + val loading: Boolean = false, + val result: String? = null, + val error: String? = null +) + +interface Result { + val message: String +} + +enum class Error(override val message: String): Result { + EmptyInput("One or more fields are empty"), + InvalidInput("Only integers are allowed"), + Overflow("Exception overflow error. NSOSStatusErrorDomain Code=-10817 \\\"(null)\\\" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0xa9298a34 dc614504 8992eb3c f65c237f ... ff5133c6 37c50886 }"), +} + +data class Success(override val message: String): Result + +class SumViewModel : ViewModel() { + + private val _uiState = MutableStateFlow(SumUiState()) + val uiState: StateFlow = _uiState.asStateFlow() + + fun onAddClicked(first: String, second: String) { + updateLoading() + Handler(Looper.getMainLooper()).postDelayed({ + when (val result = calculateSum(first, second)) { + is Success -> updateResult(result.message) + is Error -> updateError(result.message) + } + }, 1000) + } + + private fun calculateSum(first: String, second: String): Result { + if (first.isEmpty() || second.isEmpty()) return Error.EmptyInput + + val firstAsInt: Int + val secondAsInt: Int + + try { + firstAsInt = first.toInt() + } catch (exception: NumberFormatException) { + return Error.InvalidInput + } + + try { + secondAsInt = second.toInt() + } catch (exception: NumberFormatException) { + return Error.InvalidInput + } + + if (firstAsInt > 1000 || secondAsInt > 1000) { + return Error.Overflow + } + + val total = firstAsInt + secondAsInt + + return Success(total.toString()) + } + + private fun updateLoading() { + _uiState.update { currentState -> + currentState.copy( + loading = true, + result = null, + error = null + ) + } + } + + private fun updateResult(result: String?) { + _uiState.update { currentState -> + currentState.copy( + loading = false, + result = result + ) + } + } + + private fun updateError(error: String?) { + _uiState.update { currentState -> + currentState.copy( + loading = false, + error = error + ) + } + } + + fun onTextChanged() { + _uiState.update { currentState -> + currentState.copy( + loading = false, + result = null, + error = null + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..1149181 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,72 @@ + + + + + + + +