Skip to content
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

4주차 세미나 과제 #9

Merged
merged 17 commits into from
May 19, 2023
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
11 changes: 11 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10'
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

android {
namespace 'org.android.go.sopt'
compileSdk 33
Expand All @@ -20,6 +23,9 @@ android {
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField "String", "BASE_URL", properties['base.url']
buildConfigField "String", "REQRES_URL", properties['reqres.url']
}

buildTypes {
Expand Down Expand Up @@ -81,6 +87,11 @@ dependencies {
implementation "com.google.code.gson:gson:2.10.1"
implementation "com.squareup.retrofit2:retrofit:2.9.0"

// okhttp
implementation "com.squareup.okhttp3:okhttp"
implementation "com.squareup.okhttp3:okhttp-bom:4.10.0"
implementation "com.squareup.okhttp3:logging-interceptor"

// coil
implementation "io.coil-kt:coil:2.3.0"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.android.go.sopt.data.entity
package org.android.go.sopt.data.entity.local

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -10,6 +10,7 @@ data class MockRepoDto(
val allowForking: Boolean,
@SerialName("archive_url")
val archiveUrl: String,
@SerialName("archived")
val archived: Boolean,
@SerialName("assignees_url")
val assigneesUrl: String,
Expand Down Expand Up @@ -37,13 +38,17 @@ data class MockRepoDto(
val defaultBranch: String,
@SerialName("deployments_url")
val deploymentsUrl: String,
@SerialName("description")
val description: String,
@SerialName("disabled")
val disabled: Boolean,
@SerialName("downloads_url")
val downloadsUrl: String,
@SerialName("events_url")
val eventsUrl: String,
@SerialName("fork")
val fork: Boolean,
@SerialName("forks")
val forks: Int,
@SerialName("forks_count")
val forksCount: Int,
Expand Down Expand Up @@ -71,11 +76,13 @@ data class MockRepoDto(
val hasProjects: Boolean,
@SerialName("has_wiki")
val hasWiki: Boolean,
@SerialName("homepage")
val homepage: String,
@SerialName("hooks_url")
val hooksUrl: String,
@SerialName("html_url")
val htmlUrl: String,
@SerialName("id")
val id: Int,
@SerialName("is_template")
val isTemplate: Boolean,
Expand All @@ -89,16 +96,19 @@ data class MockRepoDto(
val keysUrl: String,
@SerialName("labels_url")
val labelsUrl: String,
@SerialName("language")
val language: String?,
@SerialName("languages_url")
val languagesUrl: String,
@SerialName("license")
val license: String?,
@SerialName("merges_url")
val mergesUrl: String,
@SerialName("milestones_url")
val milestonesUrl: String,
@SerialName("mirror_url")
val mirrorUrl: String?,
@SerialName("name")
val name: String,
@SerialName("node_id")
val nodeId: String,
Expand All @@ -108,6 +118,7 @@ data class MockRepoDto(
val openIssues: Int,
@SerialName("open_issues_count")
val openIssuesCount: Int,
@SerialName("owner")
val owner: Owner,
@SerialName("private")
val isPrivate: Boolean,
Expand All @@ -117,6 +128,7 @@ data class MockRepoDto(
val pushedAt: String,
@SerialName("releases_url")
val releasesUrl: String,
@SerialName("size")
val size: Int,
@SerialName("ssh_url")
val sshUrl: String,
Expand All @@ -136,13 +148,17 @@ data class MockRepoDto(
val tagsUrl: String,
@SerialName("teams_url")
val teamsUrl: String,
@SerialName("topics")
val topics: List<String>,
@SerialName("trees_url")
val treesUrl: String,
@SerialName("updated_at")
val updatedAt: String,
@SerialName("url")
val url: String,
@SerialName("visibility")
val visibility: String,
@SerialName("watchers")
val watchers: Int,
@SerialName("watchers_count")
val watchersCount: Int,
Expand All @@ -165,7 +181,9 @@ data class MockRepoDto(
val gravatarId: String,
@SerialName("html_url")
val htmlUrl: String,
@SerialName("id")
val id: Int,
@SerialName("login")
val login: String,
@SerialName("node_id")
val nodeId: String,
Expand All @@ -181,7 +199,9 @@ data class MockRepoDto(
val starredUrl: String,
@SerialName("subscriptions_url")
val subscriptionsUrl: String,
@SerialName("type")
val type: String,
@SerialName("url")
val url: String,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.android.go.sopt.data.entity.remote.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RequestPostSignInDto(
@SerialName("id")
val id: String,
@SerialName("password")
val password: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.android.go.sopt.data.entity.remote.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RequestPostSignUpDto(
@SerialName("id")
val id: String,
@SerialName("password")
val password: String,
@SerialName("name")
val name: String,
@SerialName("skill")
val skill: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.android.go.sopt.data.entity.remote.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.android.go.sopt.domain.model.Follower

@Serializable
data class ResponseGetFollowerListDto(
@SerialName("page")
val page: Int,
@SerialName("per_page")
val perPage: Int,
@SerialName("total")
val total: Int,
@SerialName("total_pages")
val totalPages: Int,
@SerialName("data")
val data: List<Follower>,
@SerialName("support")
val support: Support,
) {
@Serializable
data class Follower(
@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,
)

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

fun toFollower() = data.map { follower ->
Follower(
id = follower.id,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DiffUtill에서 아이템을 비교할 때 혹시 명확한 식별을 위해서 id도 가져오는건가용? (뭔가 나의 말이 이상한데......)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

areItemsTheSame 에서 id 값을 비교해주고 싶어서 가져왔습니다!

name = "${follower.firstName} ${follower.lastName}",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이렇게도 할 수 있구낭..!

profile = follower.avatar,
email = follower.email,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.android.go.sopt.data.entity.remote.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponsePostSignInDto(
@SerialName("id")
val id: String,
@SerialName("name")
val name: String,
@SerialName("skill")
val skill: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.android.go.sopt.data.entity.remote.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponsePostSignUpDto(
@SerialName("name")
val name: String,
@SerialName("skill")
val skill: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.android.go.sopt.data.entity.remote.response.wrapper

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class BaseResponse<T>(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우와..! 이거 정말 짱이네용

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 이거 쇽샥해도 되나요 ? ㅋㅋ

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배웠습니다 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 이거 쇽샥해도 되나요 ? ㅋㅋ

저희 다 오픈소스잖아요 🤭

@SerialName("status")
val status: Int,
@SerialName("message")
val message: String,
@SerialName("data")
val data: T? = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오오 Response에서 data의 타입만 달라지니까 제네릭으로 만들 수 있군요! 한수 배워갑니다👍👍

)
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package org.android.go.sopt.data.repository

import org.android.go.sopt.data.source.LocalPrefDataSource
import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto
import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto
import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto
import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto
import org.android.go.sopt.data.source.local.SharedPrefDataSource
import org.android.go.sopt.data.source.remote.AuthDataSource
import org.android.go.sopt.domain.model.User
import org.android.go.sopt.domain.repository.AuthRepository
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val localPrefDataSource: LocalPrefDataSource,
private val authDataSource: AuthDataSource,
private val sharedPrefDataSource: SharedPrefDataSource,
) : AuthRepository {
override suspend fun postSignup(requestPostSignUpDto: RequestPostSignUpDto): Result<ResponsePostSignUpDto?> =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result 타입을 사용한 이유가 궁금합니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좀 더 간결하게 이벤트를 처리하기 위해 runCatching을 활용해서 반환 타입을 Result로 지정해주었습니당

runCatching {
authDataSource.postSignup(requestPostSignUpDto).data
}

override suspend fun postSignin(requestPostSignInDto: RequestPostSignInDto): Result<ResponsePostSignInDto?> =
runCatching {
authDataSource.postSignIn(requestPostSignInDto).data
}

override fun setAutoLogin(isAutoLogin: Boolean) {
localPrefDataSource.isAutoLogin = isAutoLogin
sharedPrefDataSource.isAutoLogin = isAutoLogin
}

override fun getAutoLogin(): Boolean = localPrefDataSource.isAutoLogin
override fun getAutoLogin(): Boolean = sharedPrefDataSource.isAutoLogin

override fun setSignedUpUser(user: User) {
localPrefDataSource.signedUpUser = user
sharedPrefDataSource.signedUpUser = user
}

override fun getSignedUpUser(): User? = localPrefDataSource.signedUpUser
override fun getSignedUpUser(): User? = sharedPrefDataSource.signedUpUser

override fun clearLocalPref() {
localPrefDataSource.clearLocalPref()
sharedPrefDataSource.clearLocalPref()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.android.go.sopt.data.repository

import org.android.go.sopt.data.source.remote.FollowerDataSource
import org.android.go.sopt.domain.model.Follower
import org.android.go.sopt.domain.repository.FollowerRepository
import javax.inject.Inject

class FollowerRepositoryImpl @Inject constructor(
private val followerDataSource: FollowerDataSource,
) : FollowerRepository {
override suspend fun getFollowerList(page: Int): Result<List<Follower>> =
runCatching {
followerDataSource.getFollowerList(page).toFollower()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 처음부터 api url 에 page 숫자를 명시해준게 아닌, 인자로 받아서 여러 페이지 접근할 수 있도록 해주셨군요 짱이십니다 👍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

22

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.android.go.sopt.data.repository

import org.android.go.sopt.data.source.RepoDataSource
import org.android.go.sopt.data.source.local.RepoDataSource
import org.android.go.sopt.domain.model.Repo
import org.android.go.sopt.domain.repository.RepoRepository
import javax.inject.Inject
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/android/go/sopt/data/service/AuthService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.android.go.sopt.data.service

import org.android.go.sopt.data.entity.remote.request.RequestPostSignInDto
import org.android.go.sopt.data.entity.remote.request.RequestPostSignUpDto
import org.android.go.sopt.data.entity.remote.response.ResponsePostSignInDto
import org.android.go.sopt.data.entity.remote.response.ResponsePostSignUpDto
import org.android.go.sopt.data.entity.remote.response.wrapper.BaseResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthService {
@POST("sign-up")
suspend fun postSignUp(
@Body requestPostSignUpDto: RequestPostSignUpDto,
): BaseResponse<ResponsePostSignUpDto>

@POST("sign-in")
suspend fun postSignIn(
@Body requestPostSignInDto: RequestPostSignInDto,
): BaseResponse<ResponsePostSignInDto>
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 함수명이랑 Dto 클래스명에 어떤 HTTP 메서드를 사용하는지 명시해주니까 더 좋은 거 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.android.go.sopt.data.service

import org.android.go.sopt.data.entity.remote.response.ResponseGetFollowerListDto
import retrofit2.http.GET
import retrofit2.http.Query

interface FollowerService {
@GET("users")
suspend fun getFollowerList(
@Query("page") page: Int,
): ResponseGetFollowerListDto
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.android.go.sopt.data.source
package org.android.go.sopt.data.source.local

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.android.go.sopt.data.entity.MockRepoDto
import org.android.go.sopt.data.entity.local.MockRepoDto
import org.android.go.sopt.util.AssetLoader
import javax.inject.Inject

Expand Down
Loading