Skip to content

init application #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
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
26 changes: 15 additions & 11 deletions app/src/main/kotlin/com/stslex/compiler_app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.stslex.compiler_app
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.stslex.compiler_app.UserToastUtil.sendToastOfUserChanges
import com.stslex.compiler_app.app.R
import com.stslex.compiler_app.model.UserModel
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -36,25 +36,29 @@ class MainActivity : ComponentActivity() {
findViewById<Button>(R.id.usernameChangeButton).setOnClickListener {
logger.log(Level.INFO, "usernameChangeButton clicked")
val randomInt = Random.nextInt()
viewModel.setName("John $randomInt")
viewModel.setName("Name $randomInt")
}
findViewById<Button>(R.id.secondNameChangeButton).setOnClickListener {
logger.log(Level.INFO, "secondNameChangeButton clicked")
val randomInt = Random.nextInt()
viewModel.setSecondName("SecondName $randomInt")
}
}

private fun setUI(user: UserModel) {
logger.log(Level.INFO, "Setting UI with user: $user")
sendToastOfUserChanges(user)
findViewById<TextView>(R.id.usernameFieldTextView).text = user.name
setName(user.name)
setSecondName(user.secondName)
}

private fun sendToastOfUserChanges(userModel: UserModel) {
val textMsg = userModel.getChangesValue(user)
Toast.makeText(this, textMsg, Toast.LENGTH_SHORT).show()
user = userModel
private fun setName(name: String) {
logger.log(Level.INFO, "setName: $name")
findViewById<TextView>(R.id.usernameFieldTextView).text = name
}

companion object {

private var user: UserModel? = null
private fun setSecondName(name: String) {
logger.log(Level.INFO, "setSecondName: $name")
findViewById<TextView>(R.id.secondNameFieldTextView).text = name
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,12 @@ class MainActivityViewModel : ViewModel() {
val userInfo: StateFlow<UserModel>
get() = _userInfo.asStateFlow()

fun setIsLiked(isLiked: Boolean) {
_userInfo.value = _userInfo.value.copy(isLiked = isLiked)
}

fun setLikes(likes: Int) {
_userInfo.value = _userInfo.value.copy(likes = likes)
}

fun setSubscriptions(subscriptions: Int) {
_userInfo.value = _userInfo.value.copy(subscriptions = subscriptions)
}

fun setIsSubscribed(isSubscribed: Boolean) {
_userInfo.value = _userInfo.value.copy(isSubscribed = isSubscribed)
}

fun setName(name: String) {
_userInfo.value = _userInfo.value.copy(name = name)
}

fun setAvatarUrl(avatarUrl: String) {
_userInfo.value = _userInfo.value.copy(avatarUrl = avatarUrl)
fun setSecondName(secondName: String) {
_userInfo.value = _userInfo.value.copy(secondName = secondName)
}

fun setEmail(email: String) {
_userInfo.value = _userInfo.value.copy(email = email)
}
}
18 changes: 18 additions & 0 deletions app/src/main/kotlin/com/stslex/compiler_app/UserToastUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.stslex.compiler_app

import android.content.Context
import android.widget.Toast
import com.stslex.compiler_app.model.UserModel

object UserToastUtil {

private var user: UserModel? = null

fun Context.sendToastOfUserChanges(
userModel: UserModel
) {
val textMsg = userModel.getChangesValue(user)
Toast.makeText(this, textMsg, Toast.LENGTH_SHORT).show()
user = userModel
}
}
23 changes: 3 additions & 20 deletions app/src/main/kotlin/com/stslex/compiler_app/model/UserModel.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.stslex.compiler_app.model

data class UserModel(
val uuid: String,
val name: String,
val avatarUrl: String,
val email: String,
val isLiked: Boolean,
val likes: Int,
val isSubscribed: Boolean,
val subscriptions: Int,
val secondName: String,
) {

fun getChangesValue(
Expand All @@ -18,12 +12,7 @@ data class UserModel(
} else {
val changes = mutableListOf<String>()
if (user.name != name) changes.add("name")
if (user.avatarUrl != avatarUrl) changes.add("avatarUrl")
if (user.email != email) changes.add("email")
if (user.isLiked != isLiked) changes.add("isLiked")
if (user.likes != likes) changes.add("likes")
if (user.isSubscribed != isSubscribed) changes.add("isSubscribed")
if (user.subscriptions != subscriptions) changes.add("subscriptions")
if (user.secondName != secondName) changes.add("secondName")
when {
changes.isEmpty() -> "No changes"
changes.size == 7 -> "User full change"
Expand All @@ -34,14 +23,8 @@ data class UserModel(
companion object {

val defaultMock = UserModel(
uuid = "uuid",
name = "Test User",
avatarUrl = "https://images.unsplash.com/photo-1488161628813-04466f872be2?q=80&w=3376&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
email = "email",
isLiked = false,
likes = 0,
isSubscribed = false,
subscriptions = 0,
secondName = "Test SecondName",
)
}
}
29 changes: 29 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,33 @@
android:text="change username"
app:layout_constraintStart_toStartOf="@id/usernameTitleTextView"
app:layout_constraintTop_toBottomOf="@id/usernameFieldTextView" />

<TextView
android:id="@+id/secondNameTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:text="SecondName: "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/usernameChangeButton" />

<TextView
android:id="@+id/secondNameFieldTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="@id/secondNameTitleTextView"
app:layout_constraintStart_toEndOf="@id/secondNameTitleTextView"
app:layout_constraintTop_toTopOf="@id/secondNameTitleTextView" />

<Button
android:id="@+id/secondNameChangeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change secondName"
app:layout_constraintStart_toStartOf="@id/secondNameTitleTextView"
app:layout_constraintTop_toBottomOf="@id/secondNameFieldTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>