Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

17 Task TMS AN-14 #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TmsTemp">
<activity
android:name=".TransformatorActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
69 changes: 69 additions & 0 deletions app/src/main/java/com/github/krottv/tmstemp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/github/krottv/tmstemp/Student.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.krottv.tmstemp

data class Student(var name: String){}
59 changes: 59 additions & 0 deletions app/src/main/java/com/github/krottv/tmstemp/StudentViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<String>){
for (indexInArray in array.indices){
studentList[indexInArray].name = array[indexInArray]
}
}

fun getNameList(): ArrayList<String>{
val array = arrayListOf<String>()
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()
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/button_state.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#b9faf4"/>
<corners android:radius="12dp"/>
</shape>
51 changes: 51 additions & 0 deletions app/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffbaf1"
tools:context=".MainActivity">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp">

<TextView
android:id="@+id/name_of_student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="@string/name_of_student_1"
android:textColor="@color/black"
android:textSize="24sp" />

</FrameLayout>

<Button
android:id="@+id/next_button"
android:layout_marginTop="10dp"
android:layout_width="180dp"
android:layout_height="70dp"
android:background="@drawable/button_state"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_gravity="center_horizontal"
android:text="@string/next_button"/>

<Button
android:id="@+id/transform_button"
android:layout_marginTop="10dp"
android:layout_width="150dp"
android:layout_height="70dp"
android:background="@drawable/button_state"
android:textColor="@color/black"
android:textSize="17sp"
android:layout_gravity="center_horizontal"
android:text="@string/transform_button"/>

</LinearLayout>
33 changes: 33 additions & 0 deletions app/src/main/res/layout-land/activity_transformator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffbaf1"
android:orientation="vertical"
tools:context=".TransformatorActivity">

<TextView
android:id="@+id/text_transformator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="@string/name_of_student_1"
android:textColor="@color/black"
android:textSize="24sp"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"/>

<Button
android:id="@+id/transform_button"
android:layout_marginTop="10dp"
android:layout_width="150dp"
android:layout_height="70dp"
android:background="@drawable/button_state"
android:textColor="@color/black"
android:textSize="17sp"
android:layout_gravity="center_horizontal"
android:text="@string/transform_button"/>

</LinearLayout>
Loading