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

[FEATURE] #30 Profile 기본 유저 데이터 연동 #34

Merged
merged 4 commits into from
Sep 28, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
Expand All @@ -31,6 +32,7 @@ abstract class RepositoryModule {
repoRepository: RepoRepositoryImpl
) : RepoRepository

@Singleton
@Binds
abstract fun bindUserRepository(
userRepository: UserRepositoryImpl
Expand Down
31 changes: 20 additions & 11 deletions data/src/main/java/com/eshc/data/repository/UserRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userDataSource: UserDataSource
) : UserRepository{
) : UserRepository {
private var user: User? = null

override fun getUser(): Single<Result<User>> {
return try {
userDataSource.getUser()
.map {
Result.success(it.getOrThrow())
}
.onErrorReturn {
Result.failure(it.cause ?: Throwable())
}
} catch (e : Exception) {
Single.create {
try {
return if (user == null)
userDataSource.getUser()
.map {
it.getOrThrow().run {
user = this
Result.success(this)
}
}
.onErrorReturn {
Result.failure(it.cause ?: Throwable())
}
else Single.just (
Result.success(user ?: User())
)
} catch (e: Exception) {
return Single.create {
Result.failure<String>(e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.eshc.feature.profile

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.eshc.feature.profile.databinding.ActivityProfileBinding
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -11,12 +12,15 @@ class ProfileActivity : AppCompatActivity() {
private var _binding : ActivityProfileBinding? = null
private val binding get() = _binding

private val viewModel : ProfileViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = DataBindingUtil.setContentView(
this,R.layout.activity_profile
)
binding?.lifecycleOwner = this

binding?.viewModel = viewModel
viewModel.getUser()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.eshc.feature.profile

import com.eshc.domain.model.User

data class ProfileUiState(
val user : UserModel = User().toUserModel()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.eshc.feature.profile

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.eshc.domain.usecase.user.GetUserProfileUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel @Inject constructor(
private val getUserProfileUseCase: GetUserProfileUseCase
) : ViewModel() {

private val _uiState = MutableLiveData(ProfileUiState())
val uiState : LiveData<ProfileUiState>
get() = _uiState

fun getUser(){
getUserProfileUseCase()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { result, error ->
if(result.isSuccess){
result.getOrThrow().let {
_uiState.value = uiState.value?.copy(
user = it.toUserModel()
)
}
}
else {

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.eshc.feature.profile

import com.eshc.domain.model.User

data class UserModel(
val id : Long,
val login : String,
val avatarUrl : String,
val name : String,
val blog : String,
val location : String,
val email : String,
val bio : String,
val publicRepos : Int,
val followers : Int,
val following : Int,
)

fun User.toUserModel() : UserModel {
return UserModel(
id, login, avatarUrl, name, blog, location, email, bio, publicRepos, followers, following
)
}
15 changes: 13 additions & 2 deletions feature-profile/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="viewModel"
type="com.eshc.feature.profile.ProfileViewModel" />

</data>

Expand Down Expand Up @@ -46,6 +48,7 @@
android:layout_height="@dimen/size_80"
android:layout_marginStart="@dimen/margin_24"
android:layout_marginTop="@dimen/margin_24"
app:iconImage="@{viewModel.uiState.user.avatarUrl}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand All @@ -57,6 +60,7 @@
android:layout_marginTop="@dimen/margin_26"
android:textColor="@color/white"
android:textSize="@dimen/text_size_18"
android:text="@{viewModel.uiState.user.login}"
app:layout_constraintStart_toEndOf="@id/iv_profile"
app:layout_constraintTop_toTopOf="parent" />

Expand All @@ -67,6 +71,7 @@
android:layout_marginStart="@dimen/margin_16"
android:textColor="@color/gray"
android:textSize="@dimen/text_size_16"
android:text="@{viewModel.uiState.user.name}"
app:layout_constraintStart_toEndOf="@id/iv_profile"
app:layout_constraintTop_toBottomOf="@id/tv_login" />

Expand All @@ -83,6 +88,7 @@
android:paddingEnd="@dimen/padding_8"
android:paddingBottom="@dimen/padding_4"
android:textColor="@color/gray"
android:text="@{viewModel.uiState.user.bio}"
app:layout_constraintStart_toEndOf="@id/iv_profile"
app:layout_constraintTop_toBottomOf="@id/tv_name" />

Expand All @@ -106,6 +112,7 @@
android:gravity="center_vertical"
android:textColor="@color/white"
android:textSize="@dimen/text_size_14"
android:text="@{viewModel.uiState.user.location}"
app:drawableStartCompat="@drawable/ic_location"
app:drawableTint="@color/gray"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -121,6 +128,7 @@
android:gravity="center_vertical"
android:textColor="@color/white"
android:textSize="@dimen/text_size_14"
android:text="@{viewModel.uiState.user.blog}"
app:drawableStartCompat="@drawable/ic_link"
app:drawableTint="@color/gray"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -136,6 +144,7 @@
android:gravity="center_vertical"
android:textColor="@color/white"
android:textSize="@dimen/text_size_14"
android:text="@{viewModel.uiState.user.email}"
app:drawableStartCompat="@drawable/ic_mail"
app:drawableTint="@color/gray"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -152,13 +161,14 @@
android:gravity="center_vertical"
android:textColor="@color/white"
android:textSize="@dimen/text_size_14"
android:text="@{@string/profile_follow(viewModel.uiState.user.followers,viewModel.uiState.user.following)}"
app:drawableStartCompat="@drawable/ic_user"
app:drawableTint="@color/gray"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_email" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_repo_start"
android:id="@+id/cl_repo_star"
android:layout_width="match_parent"
android:layout_height="@dimen/size_128"
android:layout_marginHorizontal="@dimen/margin_24"
Expand Down Expand Up @@ -226,6 +236,7 @@
android:gravity="center_vertical"
android:textColor="@color/white"
android:textSize="@dimen/text_size_14"
android:text="@{String.valueOf(viewModel.uiState.user.publicRepos)}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/ib_repository" />

Expand Down
2 changes: 2 additions & 0 deletions feature-profile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="profile_repository">Repositories</string>
<string name="profile_starred">Starred</string>

<string name="profile_follow">%d Followers • %d Following</string>
</resources>