Skip to content

Commit

Permalink
implement: simple LoginPage, LoginViewModel, LoginUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
2018JunyoungLim authored and 2018JunyoungLim committed Oct 11, 2023
1 parent f1ee4bd commit 5c46289
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 4 deletions.
3 changes: 0 additions & 3 deletions android/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions android/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.goliath.emojihub.usecases

import com.goliath.emojihub.models.User
import com.goliath.emojihub.models.UserDto
import com.goliath.emojihub.viewmodels.Resource

class LoginUseCase {
fun login(username: String, password: String): Resource<User>? {
val dummyUser = User(UserDto(
userId = "1",
likedEmojiList = arrayOf(),
createdEmojiList = arrayOf()
))
return Resource.Success(dummyUser);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.goliath.emojihub.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.goliath.emojihub.models.User
import com.goliath.emojihub.usecases.LoginUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(
private val loginUseCase: LoginUseCase
) {

private val _loginState = MutableLiveData<Resource<User>>()
val loginState: LiveData<Resource<User>> = _loginState

fun onLoginClicked(username: String, password: String) {
_loginState.value = loginUseCase.login(username, password) // Hand
}
}

sealed class Resource<out T> {
object Loading : Resource<Nothing>()
open class Success<out T>(val data: T) : Resource<T>()
open class Error(val exception: Throwable) : Resource<Nothing>()
}
67 changes: 67 additions & 0 deletions android/app/src/main/java/com/goliath/emojihub/views/LoginPage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.goliath.emojihub.views

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview(showBackground = true)
@Composable
fun PreviewLoginView() {
LoginPage(onLoginClicked = { _, _ -> },
onCreateAccountClicked = {})
}

@Composable
fun LoginPage(
onLoginClicked: (String, String) -> Unit,
onCreateAccountClicked: () -> Unit
) {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }

// Your UI components here
Column(
modifier = Modifier
.fillMaxSize()
.padding(all = 16.dp)
) {
Text("EmojiHub")
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = username,
onValueChange = { username = it },
label = { Text("Username") }
)
Spacer(modifier = Modifier.height(16.dp))
TextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") }
)
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = { onLoginClicked(username, password) }
) {
Text("로그인")
}
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = { onCreateAccountClicked() }
) {
Text("계정 생성")
}
}
}

0 comments on commit 5c46289

Please sign in to comment.