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/100 main page repository #169

Merged
merged 8 commits into from
Jul 2, 2024
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 @@ -37,6 +37,9 @@ class MainActivity : AppCompatActivity() {
Timber.tag("MainActivity").w("Warning log")
Timber.tag("MainActivity").e("Error log")

viewModel.getUserInfoMore()
viewModel.getUserAnalytics()

binding = ActivityMainBinding.inflate(layoutInflater).also { setContentView(it.root) }
setStatusBarColor()
val navHostFragment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import app.cashadvisor.authorization.data.dto.CredentialsDto
import app.cashadvisor.authorization.domain.api.AccountInformationInteractor
import app.cashadvisor.authorization.domain.api.CredentialsRepository
import app.cashadvisor.authorization.domain.models.states.AccountInformation
import app.cashadvisor.common.domain.Resource
import app.cashadvisor.common.ui.BaseViewModel
import app.cashadvisor.common.utill.extensions.logDebugMessage
import app.cashadvisor.profile.domain.api.ProfileInfoInteractor
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -16,7 +19,8 @@ import javax.inject.Inject
@HiltViewModel
class MainViewModel @Inject constructor(
private val interactor: AccountInformationInteractor,
private val storage: CredentialsRepository
private val storage: CredentialsRepository,
private val profileInfoInteractor: ProfileInfoInteractor
) : BaseViewModel(){

private val _state = MutableStateFlow<AccountInformation>(AccountInformation.NotAuthorized)
Expand All @@ -36,6 +40,31 @@ class MainViewModel @Inject constructor(
}
}
}
fun getUserInfoMore(){
viewModelScope.launch {
when(val result = profileInfoInteractor.getUserInfoMore()) {
is Resource.Error -> {
logDebugMessage(result.error.message)
}
is Resource.Success -> {
logDebugMessage(result.data.toString())
}
}

}
}
fun getUserAnalytics(){
viewModelScope.launch {
when(val result = profileInfoInteractor.getUserAnalytics()){
is Resource.Error -> {
logDebugMessage(result.error.message)
}
is Resource.Success -> {
logDebugMessage(result.data.toString())
}
}
}
}

fun saveCredentials(accessToken: String, refreshToken: String) {
viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app.cashadvisor.profile.data.api
import app.cashadvisor.profile.data.dto.request.UpdateUserNameRequest
import app.cashadvisor.profile.data.dto.response.ConfirmUpdateNameResponse
import app.cashadvisor.profile.data.dto.response.ConfirmUpdatePicResponse
import app.cashadvisor.profile.data.dto.response.ProfileAnalyticsResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoMoreResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoResponse
import okhttp3.MultipartBody
import retrofit2.http.Body
Expand Down Expand Up @@ -32,4 +34,14 @@ interface ProfileInfoApiService {
@Part part: MultipartBody.Part
): ConfirmUpdatePicResponse

@GET("profile/more/get")
suspend fun getUserInfoMore(
@Header("Authorization") accessToken: String
):ProfileInfoMoreResponse

@GET("profile/analytics/get")
suspend fun getUserAnalytics(
@Header("Authorization") accessToken: String
):ProfileAnalyticsResponse

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import app.cashadvisor.profile.data.dto.request.UpdateProfilePicRequest
import app.cashadvisor.profile.data.dto.request.UpdateUserNameRequest
import app.cashadvisor.profile.data.dto.response.ConfirmUpdateNameResponse
import app.cashadvisor.profile.data.dto.response.ConfirmUpdatePicResponse
import app.cashadvisor.profile.data.dto.response.ProfileAnalyticsResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoMoreResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoResponse

interface ProfileInfoRemoteDataSource {
Expand All @@ -19,5 +21,8 @@ interface ProfileInfoRemoteDataSource {
dto: UpdateProfilePicRequest,
accessToken: String
): ConfirmUpdatePicResponse
suspend fun getUserInfoMore(accessToken: String):ProfileInfoMoreResponse

suspend fun getUserAnalytics(accessToken: String):ProfileAnalyticsResponse

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package app.cashadvisor.profile.data.dto.response

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

@Serializable
data class ProfileAnalyticsResponse(
@SerialName("status_code") val statusCode: Int,
val message: String,
@SerialName("response_currency") val responseCurrency: String,
@SerialName("analytics") val userAnalyticsDto: UserAnalyticsDto
)

data class ProfileAnalyticsDto(
val statusCode: Int,
val message: String,
val responseCurrency: String,
val userAnalyticsDto: UserAnalyticsDto
)

@Serializable
data class UserAnalyticsDto(
@SerialName("income") val incomeDto: List<IncomeDto>?,
@SerialName("expense") val expenseDto: List<ExpenseDto>?,
@SerialName("wealth_fund") val wealthFundDto: List<WealthFundDto>?
)

@Serializable
data class IncomeDto(
val amount: Double,
@SerialName("category_id") val categoryId: String,
val date: String,
val id: String,
val planned: Boolean,
@SerialName("user_id") val userId: String,
@SerialName("bank_account") val bankAccount: String,
val sender: String,
val currency: String
)

@Serializable
data class ExpenseDto(
val amount: Double,
@SerialName("category_id") val categoryId: String,
val date: String,
val id: String,
val planned: Boolean,
@SerialName("user_id") val userId: String,
@SerialName("bank_account") val bankAccount: String,
@SerialName("sent_to") val sentTo: String,
val currency: String
)

@Serializable
data class WealthFundDto(
val amount: Double,
val date: String,
val id: String,
val planned: Boolean,
@SerialName("user_id") val userId: String,
@SerialName("bank_account") val bankAccount: String,
val currency: String,
@SerialName("category_id") val categoryId: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package app.cashadvisor.profile.data.dto.response

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

@Serializable
data class ProfileInfoMoreResponse(
@SerialName("status_code") val statusCode: Int,
val message: String,
@SerialName("more") val userInfoMoreDto: UserInfoMoreDto,

)

@Serializable
data class UserInfoMoreDto(
@SerialName("app") val appDto: AppDto,
@SerialName("settings") val settingsDto: SettingsDto
)

@Serializable
data class AppDto(
@SerialName("category_settings") val categorySettingsDto: CategorySettingsDto,
@SerialName("connected_accounts") val connectedAccountsDto: List<ConnectedAccountDto>?
)

@Serializable
data class SettingsDto(
@SerialName("subscriptions") val subscriptionsDto: SubscriptionsDto
)

@Serializable
data class CategorySettingsDto(
@SerialName("expense_categories") val expenseCategoriesDto: List<ExpenseCategoryDto>?,
@SerialName("income_categories") val incomeCategoriesDto: List<IncomeCategoryDto>?,
@SerialName("investment_category") val investmentCategoriesDto: List<InvestmentCategoryDto>?
)

@Serializable
data class ConnectedAccountDto(
val id: String,
@SerialName("user_id") val userId: String,
@SerialName("bank_id") val bankId: String,
@SerialName("account_number") val accountNumber: String,
@SerialName("account_type") val accountType: String
)

@Serializable
data class ExpenseCategoryDto(
val id: String,
val name: String,
val icon: String,
@SerialName("is_constant") val isConstant: Boolean,
@SerialName("user_id") val userId: String
)

@Serializable
data class IncomeCategoryDto(
val id: String,
val icon: String,
val name: String,
@SerialName("is_constant") val isConstant: Boolean,
@SerialName("user_id") val userId: String
)

@Serializable
data class InvestmentCategoryDto(
val id: String,
val name: String,
val icon: String,
@SerialName("is_constant") val isConstant: Boolean,
@SerialName("user_id") val userId: String
)

@Serializable
data class SubscriptionsDto(
val id: String,
@SerialName("user_id") val userId: String,
@SerialName("start_date") val startDate: String,
@SerialName("end_date") val endDate: String,
@SerialName("is_active") val isActive: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import app.cashadvisor.profile.data.dto.request.UpdateProfilePicRequest
import app.cashadvisor.profile.data.dto.request.UpdateUserNameRequest
import app.cashadvisor.profile.data.dto.response.ConfirmUpdateNameResponse
import app.cashadvisor.profile.data.dto.response.ConfirmUpdatePicResponse
import app.cashadvisor.profile.data.dto.response.ProfileAnalyticsResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoMoreResponse
import app.cashadvisor.profile.data.dto.response.ProfileInfoResponse
import app.cashadvisor.profile.data.mapper.NetworkToProfileExceptionMapper
import okhttp3.MediaType.Companion.toMediaTypeOrNull
Expand Down Expand Up @@ -56,4 +58,22 @@ class ProfileInfoRemoteDataSourceImpl @Inject constructor(
}
}

override suspend fun getUserInfoMore(accessToken: String): ProfileInfoMoreResponse {
return try {
val response = profileInfoApiService.getUserInfoMore(accessToken = accessToken)
response
} catch (exception: NetworkException) {
throw networkToProfileExceptionMapper.handleExceptionGetMoreProfile(exception)
}
}

override suspend fun getUserAnalytics(accessToken: String): ProfileAnalyticsResponse {
return try {
val response = profileInfoApiService.getUserAnalytics(accessToken)
response
} catch (exception: NetworkException) {
throw networkToProfileExceptionMapper.handleExceptionAnalyticsProfile(exception)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import app.cashadvisor.profile.data.api.ProfileInfoStorage
import app.cashadvisor.profile.data.dto.UserInfoDto
import app.cashadvisor.profile.data.dto.request.UpdateProfilePicRequest
import app.cashadvisor.profile.data.dto.request.UpdateUserNameRequest
import app.cashadvisor.profile.data.mapper.ProfileAnalyticsDataMapper
import app.cashadvisor.profile.data.mapper.ProfileAnalyticsDomainMapper
import app.cashadvisor.profile.data.mapper.ProfileInfoMapper
import app.cashadvisor.profile.data.mapper.UserInfoMoreDomainMapper
import app.cashadvisor.profile.domain.api.ProfileInfoRepository
import app.cashadvisor.profile.domain.model.ProfileAnalytics
import app.cashadvisor.profile.domain.model.UserInfoMore
import app.cashadvisor.profile.domain.model.UserProfileInfo
import dagger.hilt.android.qualifiers.ApplicationContext
import java.io.File
Expand All @@ -26,9 +31,16 @@ class ProfileInfoRepositoryImpl @Inject constructor(
private val remoteDataSource: ProfileInfoRemoteDataSource,
private val credentialsRepository: CredentialsRepository,
private val mapper: ProfileInfoMapper,
private val analyticsDataMapper: ProfileAnalyticsDataMapper,
private val userInfoMoreMapper: UserInfoMoreDomainMapper,
private val profileAnalyticsDomainMapper: ProfileAnalyticsDomainMapper,
private val profileExceptionToErrorMapper: BaseExceptionToErrorMapper
) : ProfileInfoRepository {


private var userAnalytics: ProfileAnalytics? = null
private var userInfoMore: UserInfoMore? = null

private suspend fun getAccessToken(): String {
return credentialsRepository.getCredentials()?.accessToken ?: ""
}
Expand Down Expand Up @@ -99,6 +111,41 @@ class ProfileInfoRepositoryImpl @Inject constructor(
}
}

override suspend fun getUserInfoMore(): Resource<UserInfoMore> {


return try {
val response = userInfoMoreMapper.toUserInfoMore(
remoteDataSource.getUserInfoMore(getAccessToken()).userInfoMoreDto
)
writeUserInfoMoreInStorage(response)
Resource.Success(response)
} catch (exception: Exception) {
Resource.Error(
profileExceptionToErrorMapper.handleException(exception)
)
}
}

override suspend fun getUserAnalytics(): Resource<ProfileAnalytics> {


return try {
val response = profileAnalyticsDomainMapper.toProfileAnalytics(
analyticsDataMapper.toProfileAnalyticsDto(
remoteDataSource.getUserAnalytics(getAccessToken())
)
)
writeUserAnalyticsInStorage(response)
Resource.Success(response)

} catch (exception: Exception) {
Resource.Error(
profileExceptionToErrorMapper.handleException(exception)
)
}
}


private fun createTemporaryFile(inputStream: InputStream?): File {
val file = File.createTempFile("temp_image", null, context.cacheDir)
Expand All @@ -113,6 +160,14 @@ class ProfileInfoRepositoryImpl @Inject constructor(
return file
}

private fun writeUserInfoMoreInStorage(userInfoMore: UserInfoMore) {
this.userInfoMore = userInfoMore
}

private fun writeUserAnalyticsInStorage(userAnalytics: ProfileAnalytics) {
this.userAnalytics = userAnalytics
}

companion object {
private const val BLANK_PROFILE_NAME = "Мы тебя не знаем..."
private const val BLANK_PROFILE_SURNAME = "..."
Expand Down
Loading
Loading