Skip to content
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
1 change: 1 addition & 0 deletions core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(projects.core.common)
implementation(projects.data.account)
implementation(projects.data.library)
implementation(projects.data.user)

implementation(libs.datastore.preferences)
implementation(libs.serialization.json)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.into.websoso.core.datastore.datasource.user

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.longPreferencesKey
import com.into.websoso.core.datastore.di.UserDataStore
import com.into.websoso.user.datasource.UserLocalDataSource
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.first
import javax.inject.Inject
import javax.inject.Singleton

internal class DefaultUserDataSource
@Inject
constructor(
@UserDataStore private val userDataStorage: DataStore<Preferences>,
) : UserLocalDataSource {
override suspend fun getUserId(): Long {
val preferences = userDataStorage.data.first()
return preferences[USER_ID] ?: -1L
}

override suspend fun updateUserId(userId: Long) {
userDataStorage.edit { preferences ->
preferences[USER_ID] = userId
}
}

companion object {
private val USER_ID = longPreferencesKey("USERID")
}
}

@Module
@InstallIn(SingletonComponent::class)
internal interface UserDataStorage {
@Binds
@Singleton
fun bindUserLocalDataSource(defaultUserDataSource: DefaultUserDataSource): DefaultUserDataSource
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ internal annotation class AccountDataStore
@Qualifier
@Retention(AnnotationRetention.BINARY)
internal annotation class LibraryFilterDataStore

@Qualifier
@Retention(AnnotationRetention.BINARY)
internal annotation class UserDataStore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.into.websoso.core.network.datasource.user

import com.into.websoso.core.network.datasource.user.model.UserFeedsResponseDto
import com.into.websoso.core.network.datasource.user.model.UserInfoResponseDto
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import javax.inject.Singleton

interface UserApi {
@GET("users/{userId}/feeds")
suspend fun getUserFeeds(
@Path("userId") userId: Long,
@Query("lastFeedId") lastFeedId: Long,
@Query("size") size: Int,
): UserFeedsResponseDto

@GET("users/me")
suspend fun getUserInfo(): UserInfoResponseDto
}

@Module
@InstallIn(SingletonComponent::class)
internal object UserApiModule {
@Provides
@Singleton
internal fun provideUserApi(retrofit: Retrofit): UserApi = retrofit.create(UserApi::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.into.websoso.core.network.datasource.user.model

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

@Serializable
data class UserFeedsResponseDto(
@SerialName("feeds")
val feeds: List<UserFeedResponseDto>,
@SerialName("isLoadable")
val isLoadable: Boolean,
) {
@Serializable
data class UserFeedResponseDto(
@SerialName("commentCount")
val commentCount: Int,
@SerialName("createdDate")
val createdDate: String,
@SerialName("feedContent")
val feedContent: String,
@SerialName("feedId")
val feedId: Long,
@SerialName("isLiked")
val isLiked: Boolean,
@SerialName("isModified")
val isModified: Boolean,
@SerialName("isSpoiler")
val isSpoiler: Boolean,
@SerialName("isPublic")
val isPublic: Boolean,
@SerialName("likeCount")
val likeCount: Int,
@SerialName("novelId")
val novelId: Long?,
@SerialName("novelRating")
val novelRating: Float?,
@SerialName("novelRatingCount")
val novelRatingCount: Int?,
@SerialName("relevantCategories")
val relevantCategories: List<String>,
@SerialName("title")
val title: String?,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.into.websoso.core.network.datasource.user.model

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

@Serializable
data class UserInfoResponseDto(
@SerialName("userId")
val userId: Long,
@SerialName("nickname")
val nickname: String,
@SerialName("gender")
val gender: String,
)
1 change: 1 addition & 0 deletions data/user/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions data/user/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import com.into.websoso.setNamespace

plugins {
id("websoso.android.library")
}

android {
setNamespace("data.user")
}

dependencies {
implementation(projects.core.common)
implementation(projects.core.network)
}
41 changes: 41 additions & 0 deletions data/user/src/main/java/com/into/websoso/user/UserRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.into.websoso.user

import com.into.websoso.core.network.datasource.user.UserApi
import com.into.websoso.user.datasource.UserLocalDataSource
import com.into.websoso.user.mapper.toData
import com.into.websoso.user.model.UserFeedsEntity
import com.into.websoso.user.model.UserInfoEntity
import jakarta.inject.Inject

class UserRepository
@Inject
constructor(
private val userApi: UserApi,
private val userLocalDataSource: UserLocalDataSource,
) {
suspend fun fetchUserInfo(): UserInfoEntity {
val userInfo = userApi.getUserInfo().toData()
saveUserInfo(userInfo.userId)
return userInfo
}

private suspend fun saveUserInfo(userId: Long) {
userLocalDataSource.updateUserId(userId)
}

suspend fun fetchMyActivities(
lastFeedId: Long,
size: Int,
): UserFeedsEntity {
val myUserId = fetchUserId()
return fetchUserFeeds(myUserId, lastFeedId, size)
}

suspend fun fetchUserId(): Long = userLocalDataSource.getUserId()

suspend fun fetchUserFeeds(
userId: Long,
lastFeedId: Long,
size: Int,
): UserFeedsEntity = userApi.getUserFeeds(userId, lastFeedId, size).toData()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.into.websoso.user.datasource

interface UserLocalDataSource {
suspend fun getUserId(): Long

suspend fun updateUserId(userId: Long)
}
37 changes: 37 additions & 0 deletions data/user/src/main/java/com/into/websoso/user/mapper/UserMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.into.websoso.user.mapper

import com.into.websoso.core.network.datasource.user.model.UserFeedsResponseDto
import com.into.websoso.core.network.datasource.user.model.UserInfoResponseDto
import com.into.websoso.user.model.UserFeedsEntity
import com.into.websoso.user.model.UserInfoEntity

fun UserFeedsResponseDto.toData(): UserFeedsEntity =
UserFeedsEntity(
isLoadable = this.isLoadable,
feeds = this.feeds.map { it.toData() },
)

fun UserFeedsResponseDto.UserFeedResponseDto.toData(): UserFeedsEntity.UserFeedEntity =
UserFeedsEntity.UserFeedEntity(
feedId = this.feedId,
isSpoiler = this.isSpoiler,
feedContent = this.feedContent,
createdDate = this.createdDate,
isModified = this.isModified,
isLiked = this.isLiked,
isPublic = this.isPublic,
likeCount = this.likeCount,
commentCount = this.commentCount,
novelId = this.novelId,
title = this.title,
novelRatingCount = this.novelRatingCount,
novelRating = this.novelRating,
relevantCategories = this.relevantCategories,
)

fun UserInfoResponseDto.toData(): UserInfoEntity =
UserInfoEntity(
userId = this.userId,
nickname = this.nickname,
gender = this.gender,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.into.websoso.user.model

data class UserFeedsEntity(
val isLoadable: Boolean,
val feeds: List<UserFeedEntity>,
) {
data class UserFeedEntity(
val feedId: Long,
val isSpoiler: Boolean,
val feedContent: String,
val createdDate: String,
val isModified: Boolean,
val isLiked: Boolean,
val isPublic: Boolean,
val likeCount: Int,
val commentCount: Int,
val novelId: Long?,
val title: String?,
val novelRatingCount: Int?,
val novelRating: Float?,
val relevantCategories: List<String>,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.into.websoso.user.model

data class UserInfoEntity(
val userId: Long,
val nickname: String,
val gender: String,
)
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ include(
":feature:library",
)
include(":domain:feed")
include(":data:user")
Loading