Skip to content

Commit

Permalink
[feat] #18 서버 통신 + hilt 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyobeen-Park committed Jul 3, 2024
1 parent 60827e8 commit b8eb673
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 18 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ dependencies {
implementation("com.squareup.okhttp3:logging-interceptor")

// hilt
implementation("com.google.dagger:hilt-android:2.48.1")
kapt("com.google.dagger:hilt-android-compiler:2.48.1")
kapt("androidx.hilt:hilt-compiler:1.0.0")
implementation 'com.google.dagger:hilt-android:2.51'
kapt 'com.google.dagger:hilt-compiler:2.51.1'
implementation 'androidx.hilt:hilt-navigation-compose:1.2.0'

implementation("io.coil-kt:coil-compose:2.4.0")
}
12 changes: 6 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".NowSoptApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -17,6 +17,11 @@
tools:targetApi="31">
<activity
android:name=".presentation.ui.login.LoginActivity"
android:exported="false"
android:label="@string/app_name"
android:theme="@style/Theme.NOWSOPTAndroid"></activity>
<activity
android:name=".presentation.ui.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.NOWSOPTAndroid">
Expand All @@ -26,11 +31,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".presentation.ui.main.MainActivity"
android:exported="false"
android:label="@string/app_name"
android:theme="@style/Theme.NOWSOPTAndroid"></activity>
<activity
android:name=".presentation.ui.signup.SignupActivity"
android:exported="false"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/sopt/now/compose/data/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFact
import com.sopt.now.compose.BuildConfig.AUTH_BASE_URL
import com.sopt.now.compose.BuildConfig.USER_API_URL
import com.sopt.now.compose.data.service.AuthService
import com.sopt.now.compose.data.service.FriendService
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit
Expand All @@ -20,5 +21,5 @@ object ApiFactory {

object ServicePool {
val authService = ApiFactory.create<AuthService, String>(AUTH_BASE_URL)
val userService = ApiFactory.create<AuthService, String>(USER_API_URL)
val friendService = ApiFactory.create<FriendService, String>(USER_API_URL)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sopt.now.compose.data.datasource

import com.sopt.now.compose.data.model.response.ResponseFriendsDto

interface FriendRemoteDataSource {
suspend fun getFriendsList(page: Int): ResponseFriendsDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sopt.now.compose.data.datasourceImpl

import com.sopt.now.compose.data.datasource.FriendRemoteDataSource
import com.sopt.now.compose.data.model.response.ResponseFriendsDto
import com.sopt.now.compose.data.service.FriendService
import javax.inject.Inject

class FriendRemoteDatasourceImpl @Inject constructor(
private val friendService: FriendService
) : FriendRemoteDataSource {
override suspend fun getFriendsList(page: Int): ResponseFriendsDto =
friendService.getFriendsList(page = page)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sopt.now.compose.data.model.response

import com.sopt.now.compose.domain.model.Friend
import com.sopt.now.compose.domain.model.FriendsList
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponseFriendsDto(
@SerialName("page")
val page: Int = 2,
@SerialName("per_page")
val perPage: Int,
@SerialName("total")
val total: Int,
@SerialName("total_pages")
val totalPages: Int,
@SerialName("data")
val data: List<ResponseFriend>,
@SerialName("support")
val support: ResponseFriendsListSupportDto,
) {
@Serializable
data class ResponseFriend(
@SerialName("id")
val id: Int,
@SerialName("email")
val email: String,
@SerialName("first_name")
val firstName: String,
@SerialName("last_name")
val lastName: String,
@SerialName("avatar")
val avatar: String,
) {
fun toFriendEntity(): Friend =
Friend(
id = this.id,
email = this.email,
firstName = this.firstName,
lastName = this.lastName,
avatar = this.avatar
)
}

@Serializable
data class ResponseFriendsListSupportDto(
@SerialName("url")
val url: String,
@SerialName("text")
val text: String,
)

fun toFriendsList(): FriendsList =
FriendsList(
friendsList = this.data.map { it.toFriendEntity() }
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.sopt.now.compose.data.model.request.RequestSignupDto
import com.sopt.now.compose.domain.model.User
import com.sopt.now.compose.domain.repository.AuthRepository

class AuthRepositoryImpl (
private val authRemoteDataSource: AuthRemoteDataSource,
class AuthRepositoryImpl(
val authRemoteDataSource: AuthRemoteDataSource
) : AuthRepository {
override suspend fun signUp(requestSignupDto: RequestSignupDto): Result<Unit> =
runCatching {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopt.now.compose.data.repository

import com.sopt.now.compose.data.datasource.FriendRemoteDataSource
import com.sopt.now.compose.domain.model.FriendsList
import com.sopt.now.compose.domain.repository.FriendRepository
import javax.inject.Inject

class FriendsRepositoryImpl @Inject constructor(
private val friendRemoteDataSource: FriendRemoteDataSource,
) : FriendRepository {
override suspend fun getFriendsList(page: Int): Result<FriendsList> =
runCatching {
friendRemoteDataSource.getFriendsList(page = page).toFriendsList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sopt.now.compose.data.service

import com.sopt.now.compose.data.model.response.ResponseFriendsDto
import retrofit2.http.GET
import retrofit2.http.Query

interface FriendService {
@GET("api/users")
suspend fun getFriendsList(
@Query("page") page: Int,
): ResponseFriendsDto
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/sopt/now/compose/domain/model/Friend.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.now.compose.domain.model

data class Friend(
val id: Int,
val email: String,
val firstName: String,
val lastName: String,
val avatar: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sopt.now.compose.domain.model

data class FriendsList(
val friendsList: List<Friend>
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.sopt.now.compose.domain.repository
import com.sopt.now.compose.data.model.request.RequestLoginDto
import com.sopt.now.compose.data.model.request.RequestSignupDto
import com.sopt.now.compose.domain.model.User
import dagger.Binds


interface AuthRepository {
@Binds

suspend fun signUp(requestSignupDto: RequestSignupDto): Result<Unit>


suspend fun login(requestLoginDto: RequestLoginDto): Result<Int?>

suspend fun getUserInfo(memberId: String): Result<User>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sopt.now.compose.domain.repository

import com.sopt.now.compose.domain.model.FriendsList

interface FriendRepository {
suspend fun getFriendsList(page: Int): Result<FriendsList>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.sopt.now.compose.presentation.ui.login.LoginActivity
import com.sopt.now.compose.theme.NOWSOPTAndroidTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SignupActivity : ComponentActivity() {
val signupViewModel = SignupViewModel(AuthRepositoryImpl(AuthRemoteDatasourceImpl()))
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import kotlinx.coroutines.launch
import java.util.regex.Pattern
import javax.inject.Inject

@HiltViewModel
class SignupViewModel @Inject constructor(
class SignupViewModel(
private val authRepository: AuthRepository
) : ViewModel() {
private var phone_pattern = "^01([0|1|6|7|8|9])-([0-9]{4})-([0-9]{4})$"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ plugins {
id 'com.android.application' version '8.3.1' apply false
id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
id 'com.google.dagger.hilt.android' version '2.48.1' apply false
id 'com.google.dagger.hilt.android' version '2.51.1' apply false
}

0 comments on commit b8eb673

Please sign in to comment.