diff --git a/app/build.gradle b/app/build.gradle index 93d31ce..a3d319c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a551aff..5fd99f2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.TmsTemp"> + diff --git a/app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt b/app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt index 8952f64..0030754 100644 --- a/app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt +++ b/app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt @@ -1,11 +1,80 @@ package com.github.krottv.tmstemp +import android.app.Activity +import android.content.Intent +import androidx.lifecycle.ViewModelProvider import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.widget.Button +import android.widget.TextView +import androidx.lifecycle.ViewModelProviders + +private const val KEY_CURRENT_INDEX = "Current index value" +private const val KEY_NAME = "Name values" +private const val REQUEST_CODE_TRANSFORM = 0 class MainActivity : AppCompatActivity() { + + private val studentViewModel: StudentViewModel by lazy { + ViewModelProviders.of(this).get(StudentViewModel::class.java) + } + + private lateinit var textView: TextView + private lateinit var nextButton: Button + private lateinit var transformButton: Button + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + val currentIndex = savedInstanceState?.getInt(KEY_CURRENT_INDEX, 0) ?: 0 + studentViewModel.setIndex(currentIndex) + val studentNameList = savedInstanceState?.getStringArrayList(KEY_NAME) + if (studentNameList != null){ + studentViewModel.setNameList(studentNameList) + } + + textView = findViewById(R.id.name_of_student) + nextButton = findViewById(R.id.next_button) + transformButton = findViewById(R.id.transform_button) + + nextButton.setOnClickListener { + studentViewModel.implementCI() + updateScreenText() + } + transformButton.setOnClickListener { + val name = studentViewModel.getName() + val intent = TransformatorActivity.newIntent(this@MainActivity, name) + startActivityForResult(intent, REQUEST_CODE_TRANSFORM) + } + + updateScreenText() + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putInt(KEY_CURRENT_INDEX, studentViewModel.getIndex()) + savedInstanceState.putStringArrayList(KEY_NAME, studentViewModel.getNameList()) + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (resultCode != Activity.RESULT_OK){ + return + } + + if (requestCode == REQUEST_CODE_TRANSFORM){ + val requestedBool = data?.getBooleanExtra(EXTRA_IS_NAME_TRANSFORMED, false) ?: false + if (requestedBool){ + studentViewModel.setName() + updateScreenText() + } + } + } + + private fun updateScreenText(){ + val textResId = studentViewModel.getName() + textView.setText(textResId) } } \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/Student.kt b/app/src/main/java/com/github/krottv/tmstemp/Student.kt new file mode 100644 index 0000000..158c187 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/Student.kt @@ -0,0 +1,3 @@ +package com.github.krottv.tmstemp + +data class Student(var name: String){} \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/StudentViewModel.kt b/app/src/main/java/com/github/krottv/tmstemp/StudentViewModel.kt new file mode 100644 index 0000000..a1c4f51 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/StudentViewModel.kt @@ -0,0 +1,59 @@ +package com.github.krottv.tmstemp + +import androidx.lifecycle.ViewModel + +class StudentViewModel: ViewModel() { + + private var currentIndex = 0 + private var transformMessage = "Transformed_" + + private val studentList = listOf( + Student("John"), + Student("Mike"), + Student("Brad"), + Student("Nick"), + Student("Bob"), + Student("Tim"), + Student("Corey"), + Student("Eric"), + Student("Hanna"), + Student("Jim")) + + fun getName(): String{ + return studentList[currentIndex].name + } + + fun setNameList(array: ArrayList){ + for (indexInArray in array.indices){ + studentList[indexInArray].name = array[indexInArray] + } + } + + fun getNameList(): ArrayList{ + val array = arrayListOf() + for (index in studentList.indices){ + array.add(studentList[index].name) + } + return array + } + + fun implementCI(){ + currentIndex = (currentIndex + 1) % studentList.size + } + + fun getIndex(): Int{ + return currentIndex + } + + fun setIndex(value: Int){ + currentIndex = value + } + + private fun transformName(){ + studentList[currentIndex].name = transformMessage + studentList[currentIndex].name + } + + fun setName(){ + transformName() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/TransformatorActivity.kt b/app/src/main/java/com/github/krottv/tmstemp/TransformatorActivity.kt new file mode 100644 index 0000000..afde048 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/TransformatorActivity.kt @@ -0,0 +1,91 @@ +package com.github.krottv.tmstemp + +import android.app.Activity +import android.content.Context +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.Button +import android.widget.TextView +import android.widget.Toast +import android.widget.Toast.LENGTH_SHORT +import androidx.lifecycle.ViewModelProviders +import kotlin.properties.Delegates + +private const val EXTRA_NAME = "Name of student" +private const val KEY_IS_TRANSFORMED = "Is name transformed key" +private const val KEY_NAME = "Current name" +const val EXTRA_IS_NAME_TRANSFORMED = "Is name transformed extra" + +class TransformatorActivity : AppCompatActivity() { + + private val transformatorViewModel by lazy { + ViewModelProviders.of(this@TransformatorActivity).get(TransformatorViewModel::class.java) + } + + private lateinit var textView: TextView + private lateinit var transformButton: Button + private var transformMessage = "Transformed_" + private val regEx = "^(Transformed)\\_(?!Transformed_).*" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_transformator) + + val isTransformed = savedInstanceState?.getBoolean(KEY_IS_TRANSFORMED, false) ?: false + transformatorViewModel.setIsTransformed(isTransformed) + val currentName = savedInstanceState?.getString(KEY_NAME, "Null") ?: "Null" + transformatorViewModel.setText(currentName) + + val textName = intent.getStringExtra(EXTRA_NAME) + if (textName != null) { + transformatorViewModel.setText(textName) + } + transformButton = findViewById(R.id.transform_button) + textView = findViewById(R.id.text_transformator) + + transformButton.setOnClickListener { + transformText() + updateScreen() + } + + updateScreen() + } + + override fun onSaveInstanceState(savedInstanceState: Bundle) { + super.onSaveInstanceState(savedInstanceState) + savedInstanceState.putString(KEY_NAME, transformatorViewModel.getText()) + savedInstanceState.putBoolean(KEY_IS_TRANSFORMED, transformatorViewModel.getIsTransformed()) + } + + companion object{ + fun newIntent(context: Context, name: String): Intent{ + return Intent(context, TransformatorActivity::class.java).apply { + putExtra(EXTRA_NAME, name) + } + } + } + + private fun sendResult(){ + val data = Intent().apply { + putExtra(EXTRA_IS_NAME_TRANSFORMED, transformatorViewModel.getIsTransformed()) + } + + setResult(Activity.RESULT_OK, data) + } + + private fun updateScreen(){ + textView.setText(transformatorViewModel.getText()) + sendResult() + } + + private fun transformText(){ + if (!(transformatorViewModel.getText().matches(regEx.toRegex()))) { + transformatorViewModel.setText(transformMessage + transformatorViewModel.getText()) + transformatorViewModel.setIsTransformed(true) + } else { + val toastMessage = R.string.already_transformed_toast + Toast.makeText(this, toastMessage, LENGTH_SHORT).show() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/krottv/tmstemp/TransformatorViewModel.kt b/app/src/main/java/com/github/krottv/tmstemp/TransformatorViewModel.kt new file mode 100644 index 0000000..68925c7 --- /dev/null +++ b/app/src/main/java/com/github/krottv/tmstemp/TransformatorViewModel.kt @@ -0,0 +1,28 @@ +package com.github.krottv.tmstemp + +import androidx.lifecycle.ViewModel + +class TransformatorViewModel: ViewModel() { + + private var text: String = "" + private var isTransformed = false + private val regEx: Regex = "^(Transformed)\\_.*".toRegex() + + fun getText(): String{ + return text + } + + fun setText(value: String){ + if (!text.matches(regEx)) { + text = value + } + } + + fun getIsTransformed(): Boolean{ + return isTransformed + } + + fun setIsTransformed(value: Boolean){ + isTransformed = value + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/button_state.xml b/app/src/main/res/drawable/button_state.xml new file mode 100644 index 0000000..02e97d9 --- /dev/null +++ b/app/src/main/res/drawable/button_state.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml new file mode 100644 index 0000000..fbe6b36 --- /dev/null +++ b/app/src/main/res/layout-land/activity_main.xml @@ -0,0 +1,51 @@ + + + + + + + + + +