Skip to content

Commit

Permalink
feat: load profile screen after login
Browse files Browse the repository at this point in the history
  • Loading branch information
carl05 committed Mar 20, 2021
1 parent 8993e7b commit 7b2d75d
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 61 deletions.
15 changes: 12 additions & 3 deletions .idea/codeStyles/Project.xml

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

2 changes: 2 additions & 0 deletions app/src/main/res/navigation/nav_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
app:launchSingleTop="true"/>
<action
android:id="@+id/action_profileFragment_to_profileSignedOutFragment"
app:popUpTo="@id/profileFragment"
app:popUpToInclusive="false"
app:destination="@id/profileSignedOutFragment" />
</fragment>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ interface FightPandemicsAPI {

// Profile API calls
@GET("api/users/current")
suspend fun getCurrentUser(): IndividualProfileResponse
suspend fun getCurrentUser(): Response<IndividualProfileResponse>

@PATCH("api/users/current")
suspend fun updateCurrentUserProfile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import retrofit2.Response

interface ProfileRemoteDataSource {

suspend fun fetchCurrentUser(): IndividualProfileResponse
suspend fun fetchCurrentUser(): Response<IndividualProfileResponse>

suspend fun updateCurrentUser(patchIndividualProfileRequest: PatchIndividualProfileRequest):
Response<PatchIndividualProfileResponse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class ProfileRemoteDataSourceImpl @Inject constructor(
private val fightPandemicsAPI: FightPandemicsAPI,
) : ProfileRemoteDataSource {

override suspend fun fetchCurrentUser(): IndividualProfileResponse {
return fightPandemicsAPI.getCurrentUser()
}
override suspend fun fetchCurrentUser(): Response<IndividualProfileResponse> =
fightPandemicsAPI.getCurrentUser()

override suspend fun updateCurrentUser(patchIndividualProfileRequest: PatchIndividualProfileRequest):
Response<PatchIndividualProfileResponse> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fightpandemics.core.data.repository

import com.fightpandemics.core.data.model.login.ErrorResponse
import com.fightpandemics.core.data.model.profile.IndividualProfileResponse
import com.fightpandemics.core.data.model.profile.PatchIndividualAccountRequest
import com.fightpandemics.core.data.model.profile.PatchIndividualProfileRequest
import com.fightpandemics.core.data.prefs.PreferenceStorage
Expand All @@ -15,7 +14,6 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.flow
import java.net.HttpURLConnection
import javax.inject.Inject

Expand All @@ -27,10 +25,24 @@ class ProfileRepositoryImpl @Inject constructor(
private val profileRemoteDataSource: ProfileRemoteDataSource,
) : ProfileRepository {

override fun getIndividualUser(): Flow<Result<IndividualProfileResponse>> {
return flow {
val individualUser = profileRemoteDataSource.fetchCurrentUser()
emit(Result.Success(individualUser))
override fun getIndividualUser(): Flow<Result<*>> {
return channelFlow {
val response = profileRemoteDataSource.fetchCurrentUser()
when {
response.isSuccessful && response.code() == HttpURLConnection.HTTP_OK -> {
val profileResponse = response.body()
channel.offer(Result.Success(profileResponse))
}
response.code() == HttpURLConnection.HTTP_BAD_REQUEST -> {
val myError = response.parseErrorJsonResponse<ErrorResponse>(moshi)
channel.offer(Result.Error(Exception(myError?.message)))
}
else -> {
val myError = response.parseErrorJsonResponse<ErrorResponse>(moshi)
channel.offer(Result.Error(Exception(myError?.message)))
}
}
awaitClose { }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.fightpandemics.core.domain.repository

import com.fightpandemics.core.data.model.profile.IndividualProfileResponse
import com.fightpandemics.core.data.model.profile.PatchIndividualAccountRequest
import com.fightpandemics.core.data.model.profile.PatchIndividualProfileRequest
import com.fightpandemics.core.result.Result
import kotlinx.coroutines.flow.Flow

interface ProfileRepository {

fun getIndividualUser(): Flow<Result<IndividualProfileResponse>>
fun getIndividualUser(): Flow<Result<*>>

fun updateIndividualUserProfile(profileRequest: PatchIndividualProfileRequest): Flow<Result<*>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import javax.inject.Inject
class LoadCurrentUserUseCase @Inject constructor(
private val profileRepository: ProfileRepository,
dispatcherProvider: CoroutinesDispatcherProvider,
) : FlowUseCase<String, IndividualProfileResponse>(dispatcherProvider.default) {
) : FlowUseCase<String, Any?>(dispatcherProvider.default) {

override suspend fun execute(parameters: String?): Flow<Result<IndividualProfileResponse>> {
override suspend fun execute(parameters: String?): Flow<Result<Any?>> {
return profileRepository.getIndividualUser().map { results ->
when (results) {
is Result.Success -> results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ class ProfileFragment : Fragment() {
super.onStart()
bindListeners()
profileViewModel.getIndividualProfile()
if (!profileViewModel.isUserSignedIn()) {
findNavController().navigate(com.fightpandemics.R.id.action_profileFragment_to_profileSignedOutFragment)
return
}
}

@ExperimentalCoroutinesApi
Expand Down Expand Up @@ -98,27 +94,31 @@ class ProfileFragment : Fragment() {

@ExperimentalCoroutinesApi
private fun getIndividualProfileListener(profile: ProfileViewModel.IndividualProfileViewState) {
when {
profile.isLoading -> {
bindLoading(true)
}
profile.error == null -> {
bindLoading(false)
user_full_name.text =
profile.firstName?.capitalizeFirstLetter() + " " + profile.lastName?.capitalizeFirstLetter()
user_location.text = profile.location
bio.text = profile.bio
loadUserImage(profile, profile.imgUrl)
facebook.setOnClickListener { openWebView(profile.facebook) }
linkedin.setOnClickListener { openWebView(profile.linkedin) }
twitter.setOnClickListener { openWebView(profile.twitter) }
github.setOnClickListener { openWebView(profile.github) }
link.setOnClickListener { openWebView(profile.website) }
displayUserPosts(profile.id!!)
}
profile.error.isNotBlank() -> {
bindLoading(false)
// TODO add error treatment on get current profile
if (!profileViewModel.isUserSignedIn()) {
findNavController().navigate(com.fightpandemics.R.id.action_profileFragment_to_profileSignedOutFragment)
} else {
when {
profile.isLoading -> {
bindLoading(true)
}
profile.error == null -> {
bindLoading(false)
user_full_name.text =
profile.firstName?.capitalizeFirstLetter() + " " + profile.lastName?.capitalizeFirstLetter()
user_location.text = profile.location
bio.text = profile.bio
loadUserImage(profile, profile.imgUrl)
facebook.setOnClickListener { openWebView(profile.facebook) }
linkedin.setOnClickListener { openWebView(profile.linkedin) }
twitter.setOnClickListener { openWebView(profile.twitter) }
github.setOnClickListener { openWebView(profile.github) }
link.setOnClickListener { openWebView(profile.website) }
displayUserPosts(profile.id!!)
}
profile.error.isNotBlank() -> {
bindLoading(false)
// TODO add error treatment on get current profile
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,29 @@ class ProfileViewModel @Inject constructor(
when (it) {
is Result.Success -> {
Timber.i("Debug: Result was a success, ${it.data}")
currentProfile = it.data
val data = it.data as IndividualProfileResponse
currentProfile = data
individualProfile.value?.isLoading = false
individualProfile.value = IndividualProfileViewState(
isLoading = false,
email = it.data.email,
id = it.data.id,
firstName = it.data.firstName.capitalizeFirstLetter(),
lastName = it.data.lastName.capitalizeFirstLetter(),
imgUrl = it.data.photo,
location = it.data.location.city.capitalizeFirstLetter() + " , " +
it.data.location.state.capitalizeFirstLetter() + " , " +
it.data.location.country.capitalizeFirstLetter(),
bio = it.data.about,
facebook = it.data.urls.facebook,
instagram = it.data.urls.instagram,
linkedin = it.data.urls.linkedin,
twitter = it.data.urls.twitter,
github = it.data.urls.github,
website = it.data.urls.website,
email = data.email,
id = data.id,
firstName = data.firstName.capitalizeFirstLetter(),
lastName = data.lastName.capitalizeFirstLetter(),
imgUrl = data.photo,
location = data.location.city.capitalizeFirstLetter() + " , " +
data.location.state.capitalizeFirstLetter() + " , " +
data.location.country.capitalizeFirstLetter(),
bio = data.about,
facebook = data.urls.facebook,
instagram = data.urls.instagram,
linkedin = data.urls.linkedin,
twitter = data.urls.twitter,
github = data.urls.github,
website = data.urls.website,
error = null,
objectives = it.data.objectives,
needs = it.data.needs
objectives = data.objectives,
needs = data.needs
)
}
is Result.Error -> {
Expand Down

0 comments on commit 7b2d75d

Please sign in to comment.