Skip to content

Commit

Permalink
init registration
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Jul 29, 2023
1 parent 34cee2a commit a65e6d3
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 36 deletions.
21 changes: 21 additions & 0 deletions app/src/main/java/com/stslex/aproselection/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
package com.stslex.aproselection.ui

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.lifecycle.lifecycleScope
import com.stslex.aproselection.core.datastore.AppDataStore
import com.stslex.aproselection.core.network.client.NetworkClient
import com.stslex.aproselection.core.ui.theme.AppTheme
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.koin.android.ext.android.inject

class MainActivity : ComponentActivity() {

val dataStore by inject<AppDataStore>()
val networkClient by inject<NetworkClient>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataStore.token
.catch {
Log.e(it.message, javaClass.simpleName, it)
}
.onEach {
if (it.isBlank()) {
networkClient.regenerateToken()
}
}
.launchIn(lifecycleScope)
setContent {
AppTheme {
InitialApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface NetworkClient {
val client: HttpClient

val apiClient: HttpClient

suspend fun regenerateToken()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.request.bearerAuth
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.URLProtocol
import io.ktor.http.appendPathSegments
import io.ktor.http.contentType
import io.ktor.http.encodedPath
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -98,13 +100,14 @@ class NetworkClientImpl(
headers.append("API_KEY", BuildConfig.API_KEY)
headers.append("DEVICE_ID", "test")
headers.append("uuid", uuid.value)
contentType(ContentType.Application.Json)
}
bearerAuth(token = token.value)
}
}

private suspend fun regenerateToken() {
val token = client
override suspend fun regenerateToken() {
val token = apiClient
.get {
url.appendPathSegments("token")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.stslex.aproselection.core.network.clients.auth.model.UserAuthResponse
import com.stslex.aproselection.core.network.clients.auth.model.UserAuthSendModel
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.appendPathSegments
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -35,9 +36,9 @@ class AuthNetworkClientImpl(
): Flow<UserAuthResponseModel> = flow {
val result = networkClient
.apiClient
.get {
.post {
url.appendPathSegments("passport/auth")
setBody(user)
setBody<UserAuthSendModel>(user)
}
.body<UserAuthResponseModel>()
emit(result)
Expand All @@ -49,9 +50,9 @@ class AuthNetworkClientImpl(
): Flow<UserAuthResponseModel> = flow {
val result = networkClient
.apiClient
.get {
.post {
url.appendPathSegments("passport/register")
setBody(user)
setBody<UserAuthSendModel>(user)
}
.body<UserAuthResponseModel>()
emit(result)
Expand Down
1 change: 1 addition & 0 deletions feature/auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
implementation(project(":core:ui"))
implementation(project(":core:datastore"))
implementation(project(":core:network"))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.stslex.aproselection.feature.auth.data.model

import com.stslex.aproselection.core.network.clients.auth.model.UserAuthResponseModel

object AuthMapper {

fun UserAuthResponseModel.toData(): UserModel = UserModel(
uuid = uuid,
username = username,
nickname = nickname
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.stslex.aproselection.feature.auth.data.model

data class UserModel(
val uuid: String,
val username: String,
val nickname: String,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.stslex.aproselection.feature.auth.data.repository

import com.stslex.aproselection.feature.auth.data.model.UserModel
import kotlinx.coroutines.flow.Flow

interface AuthRepository {

fun getHello(username: String): Flow<String>
fun auth(username: String, password: String): Flow<UserModel>

fun register(username: String, password: String): Flow<UserModel>
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
package com.stslex.aproselection.feature.auth.data.repository

import com.stslex.aproselection.core.datastore.AppDataStore
import com.stslex.aproselection.core.network.clients.auth.AuthNetworkClient
import com.stslex.aproselection.core.network.clients.auth.model.UserAuthSendModel
import com.stslex.aproselection.feature.auth.data.model.AuthMapper.toData
import com.stslex.aproselection.feature.auth.data.model.UserModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach

class AuthRepositoryImpl(
private val networkClient: AuthNetworkClient
private val networkClient: AuthNetworkClient,
private val dataSource: AppDataStore
) : AuthRepository {

override fun getHello(username: String): Flow<String> = flow {
val helloResponse = networkClient
.getHello(username = username)
.text
emit(helloResponse)
}

override fun auth(
username: String,
password: String
): Flow<UserModel> = networkClient.auth(
UserAuthSendModel(username, password)
)
.onEach { response ->
dataSource.setToken(response.token)
dataSource.setUuid(response.uuid)
}
.map { user ->
user.toData()
}

override fun register(
username: String,
password: String
): Flow<UserModel> = networkClient.register(
UserAuthSendModel(username, password)
)
.onEach { response ->
dataSource.setToken(response.token)
dataSource.setUuid(response.uuid)
}
.map { user ->
user.toData()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.stslex.aproselection.feature.auth.domain.interactor

import com.stslex.aproselection.feature.auth.data.model.UserModel
import kotlinx.coroutines.flow.Flow

interface AuthInteractor {

fun getHello(username: String): Flow<String>
fun auth(username: String, password: String): Flow<UserModel>

fun register(username: String, password: String): Flow<UserModel>
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package com.stslex.aproselection.feature.auth.domain.interactor

import com.stslex.aproselection.feature.auth.data.model.UserModel
import com.stslex.aproselection.feature.auth.data.repository.AuthRepository
import kotlinx.coroutines.flow.Flow

class AuthInteractorImpl(
private val repository: AuthRepository
) : AuthInteractor {

override fun getHello(
username: String
): Flow<String> = repository.getHello(
username = username
override fun auth(
username: String,
password: String
): Flow<UserModel> = repository.auth(
username = username,
password = password
)

override fun register(
username: String,
password: String
): Flow<UserModel> = repository.register(
username = username,
password = password
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ import com.stslex.aproselection.core.ui.navigation.NavigationScreen
fun AuthScreen(
text: String,
navigate: (NavigationScreen) -> Unit,
setUsername: (String) -> Unit,
auth: (String, String) -> Unit,
modifier: Modifier = Modifier,
) {
var inputUsername by remember {
mutableStateOf("")
}

var inputPassword by remember {
mutableStateOf("")
}

Box(
modifier = modifier
.fillMaxSize()
Expand All @@ -52,9 +56,19 @@ fun AuthScreen(
maxLines = 1,
)
Divider(Modifier.padding(16.dp))
TextField(
value = inputPassword,
onValueChange = { value ->
if (inputPassword != value) {
inputPassword = value
}
},
maxLines = 1,
)
Divider(Modifier.padding(16.dp))
ElevatedButton(
onClick = {
setUsername(inputUsername)
auth(inputUsername, inputPassword)
inputUsername = ""
}
) {
Expand All @@ -73,12 +87,12 @@ fun AuthScreen(
}
}

@Preview(device = "id:pixel_6", showSystemUi = true, showBackground = true)
@Composable
fun AuthScreenPreview() {
AuthScreen(
text = "text",
navigate = {},
setUsername = {}
)
}
//@Preview(device = "id:pixel_6", showSystemUi = true, showBackground = true)
//@Composable
//fun AuthScreenPreview() {
// AuthScreen(
// text = "text",
// navigate = {},
// setUsername = {}
// )
//}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ class AuthViewModel(
val text: StateFlow<String>
get() = _text.asStateFlow()

fun setUsername(username: String) {
fun auth(username: String, password: String) {
_text.value = "..."
interactor.getHello(username)

interactor
.register(
username = username,
password = password
)
.catch { throwable ->
_text.emit("Error: ${throwable.localizedMessage}")
handleError(throwable)
}
.onEach { receivedText ->
_text.emit(receivedText)
.onEach { userModel ->
_text.emit("Success: uuid--${userModel.uuid}")
}
.launchIn(viewModelScope)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun NavGraphBuilder.authRouter(
AuthScreen(
text = text,
navigate = navigate,
setUsername = viewModel::setUsername,
auth = viewModel::auth,
modifier = modifier
)
}
Expand Down

0 comments on commit a65e6d3

Please sign in to comment.