Skip to content

Commit

Permalink
Merge pull request #515 from woowacourse-teams/AN/feature/441-studyth…
Browse files Browse the repository at this point in the history
…read-remote

[스레드뷰] 서버 연결 및 부가 기능 추가
  • Loading branch information
RightHennessy authored Oct 17, 2023
2 parents b8e401c + b6c2da1 commit dcb017d
Show file tree
Hide file tree
Showing 48 changed files with 928 additions and 274 deletions.
4 changes: 3 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<application
android:name=".application.Team201App"
Expand All @@ -23,7 +24,8 @@
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".presentation.studyThread.ThreadActivity"
android:exported="false" />
android:exported="true"
android:screenOrientation="portrait" />
<activity
android:name=".presentation.report.ReportActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.created.domain.repository.SettingRepository
import com.created.domain.repository.SplashRepository
import com.created.domain.repository.StudyDetailRepository
import com.created.domain.repository.StudyListRepository
import com.created.domain.repository.ThreadRepository
import com.created.domain.repository.UpdateStudyRepository
import com.created.team201.data.repository.DefaultAuthRepository
import com.created.team201.data.repository.DefaultCreateStudyRepository
Expand All @@ -25,6 +26,7 @@ import com.created.team201.data.repository.DefaultSettingRepository
import com.created.team201.data.repository.DefaultSplashRepository
import com.created.team201.data.repository.DefaultStudyDetailRepository
import com.created.team201.data.repository.DefaultStudyListRepository
import com.created.team201.data.repository.DefaultThreadRepository
import com.created.team201.data.repository.DefaultUpdateStudyRepository
import dagger.Binds
import dagger.Module
Expand Down Expand Up @@ -87,4 +89,8 @@ interface RepositoryModule {
@Binds
@Singleton
fun bindDefaultUpdateStudyRepository(defaultUpdateStudyRepository: DefaultUpdateStudyRepository): UpdateStudyRepository

@Binds
@Singleton
fun bindDefaultThreadRepository(defaultThreadRepository: DefaultThreadRepository): ThreadRepository
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.created.team201.data.remote.api.ProfileService
import com.created.team201.data.remote.api.ReportService
import com.created.team201.data.remote.api.SettingService
import com.created.team201.data.remote.api.StudyDetailService
import com.created.team201.data.remote.api.ThreadService
import com.created.team201.data.remote.api.UpdateStudyService
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -84,4 +85,9 @@ object ServiceModule {
@Provides
fun provideSettingService(@AuthRetrofit retrofit: Retrofit): SettingService =
retrofit.create(SettingService::class.java)

@Singleton
@Provides
fun provideThreadService(@AuthRetrofit retrofit: Retrofit): ThreadService =
retrofit.create(ThreadService::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fun HomeStudyResponseDto.toUserStudy(): UserStudy =
isMaster = isMaster,
studyId = studyId,
studyName = studyName,
mustDo = mustDo,
mustDo = mustDo ?: "",
leftDays = leftDays,
grassSeedsCount = grassSeedsCount,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.created.team201.data.mapper

import com.created.domain.model.Feeds
import com.created.domain.model.MustDo
import com.created.domain.model.MustDoCertification
import com.created.team201.data.remote.response.FeedsResponseDto
import com.created.team201.data.remote.response.MustDoCertificationResponseDto

fun MustDoCertificationResponseDto.toDomain(): MustDoCertification = MustDoCertification(
studyName = studyName,
upcomingRound = upcomingRound.toUpcomingRound(),
me = me.toMustDo(),
others = others.map { it.toMustDo() }

)

fun MustDoCertificationResponseDto.UpcomingRound.toUpcomingRound(): MustDoCertification.UpcomingRound =
MustDoCertification.UpcomingRound(
id = id, weekNumber = weekNumber

)

fun MustDoCertificationResponseDto.User.toMustDo(): MustDo = MustDo(
id = id,
isCertified = isCertified,
nickname = nickname,
profileImageUrl = profileImageUrl
)

fun FeedsResponseDto.toFeeds(): Feeds = Feeds(
author = this.author.toAuthor(),
content = content,
createdAt = createdAt,
id = id.toLong(),
imageUrl = imageUrl ?: ""
// 서버 널 확인
)

fun FeedsResponseDto.Author.toAuthor(): Feeds.Author = Feeds.Author(
id = id.toLong(), nickname = nickname, profileImageUrl = profileImageUrl
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.created.team201.data.remote.api

import com.created.team201.data.remote.request.FeedRequestDto
import com.created.team201.data.remote.response.FeedsResponseDto
import com.created.team201.data.remote.response.MustDoCertificationResponseDto
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

interface ThreadService {

@GET("v1/studies/{id}/certifications")
suspend fun getMustDoCertification(
@Path("id") studyId: Long,
): MustDoCertificationResponseDto

@POST("v1/studies/{id}/feeds")
suspend fun postFeeds(
@Path("id") studyId: Long,
@Body content: FeedRequestDto
): Response<Unit>

@GET("v1/studies/{id}/feeds")
suspend fun getFeeds(
@Path("id") studyId: Long,
): List<FeedsResponseDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.created.team201.data.remote.request

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

@Serializable
data class FeedRequestDto(
@SerialName("content")
val content: String,
@SerialName("imageUrl")
val imageUrl: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.created.team201.data.remote.response

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

@Serializable
data class FeedsResponseDto(
@SerialName("id")
val id: Int,
@SerialName("author")
val author: Author,
@SerialName("imageUrl")
val imageUrl: String?,
@SerialName("content")
val content: String,
@SerialName("createdAt")
val createdAt: String
) {
@Serializable
data class Author(
@SerialName("id")
val id: Int,
@SerialName("nickname")
val nickname: String,
@SerialName("profileImageUrl")
val profileImageUrl: String
)
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import kotlinx.serialization.Serializable
@Serializable
data class HomeStudyResponseDto(
@SerialName("id")
val studyId: Int,
val studyId: Long,
@SerialName("name")
val studyName: String,
@SerialName("todoContent")
val mustDo: String,
val mustDo: String?,
@SerialName("leftDays")
val leftDays: Int,
@SerialName("grassCount")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.created.team201.data.remote.response

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

@Serializable
data class MustDoCertificationResponseDto(
@SerialName("studyName")
val studyName: String,
@SerialName("upcomingRound")
val upcomingRound: UpcomingRound,
@SerialName("me")
val me: User,
@SerialName("others")
val others: List<User>
) {
@Serializable
data class User(
@SerialName("id")
val id: Int,
@SerialName("nickname")
val nickname: String,
@SerialName("profileImageUrl")
val profileImageUrl: String,
@SerialName("isCertified")
val isCertified: Boolean
)

@Serializable
data class UpcomingRound(
@SerialName("id")
val id: Int,
@SerialName("weekNumber")
val weekNumber: Int,
)
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class DefaultSplashRepository @Inject constructor() : SplashRepository {
.get()
documentTask.addOnSuccessListener { documentSnapshot ->
CoroutineScope(Dispatchers.IO).launch {
onSuccess.invoke(documentSnapshot.toObject<AppUpdateInformation>()?.run {
copy(message = message.replace("\\n", "\n"))
} ?: DEFAULT_APP_INFORMATION)
onSuccess.invoke(
documentSnapshot.toObject<AppUpdateInformation>()?.run {
copy(message = message.replace("\\n", "\n"))
} ?: DEFAULT_APP_INFORMATION,
)
}
}
documentTask.addOnFailureListener { exception ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.created.team201.data.repository

import com.created.domain.model.Feeds
import com.created.domain.model.MustDoCertification
import com.created.domain.repository.ThreadRepository
import com.created.team201.data.mapper.toDomain
import com.created.team201.data.mapper.toFeeds
import com.created.team201.data.remote.api.ThreadService
import com.created.team201.data.remote.request.FeedRequestDto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class DefaultThreadRepository @Inject constructor(
private val threadService: ThreadService
) : ThreadRepository {

override suspend fun getMustDoCertification(studyId: Long): MustDoCertification {
val mustDoCertification = threadService.getMustDoCertification(studyId = studyId)

return mustDoCertification.toDomain()
}

override fun getFeeds(studyId: Long): Flow<List<Feeds>> = flow {
while (true) {
val feeds = threadService.getFeeds(studyId).map { it.toFeeds() }

emit(feeds)
delay(1000)
}
}.flowOn(Dispatchers.IO)

override suspend fun updateFeeds(studyId: Long, content: String, image: String?) {
threadService.postFeeds(
studyId,
FeedRequestDto(
content = content, imageUrl = image
)
)
}

override fun postMustDoCertification(studyId: Long) {
TODO("Not yet implemented")
}

override fun getStudyInfo(studyId: Long) {
TODO("Not yet implemented")
}

override fun getMustDo(studyId: Long) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.created.team201.databinding.FragmentHomeBinding
import com.created.team201.presentation.common.BindingFragment
import com.created.team201.presentation.home.HomeViewModel.UserStudyState.Joined
import com.created.team201.presentation.home.adapter.HomeAdapter
import kotlinx.coroutines.flow.Flow
import com.created.team201.presentation.studyThread.ThreadActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -30,12 +29,17 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
collectUiState()
}

override fun onResume() {
super.onResume()
homeViewModel.updateUserStudy()
}

private fun setupViewModel() {
binding.lifecycleOwner = viewLifecycleOwner
binding.vm = homeViewModel
}

private fun navigateToThreadActivity(studyId: Int) {
private fun navigateToThreadActivity(studyId: Long) {
startActivity(ThreadActivity.getIntent(requireContext(), studyId))
}

Expand All @@ -51,5 +55,6 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home

private fun setupAdapter() {
binding.rvHome.adapter = homeAdapter
binding.rvHome.setHasFixedSize(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ class HomeViewModel @Inject constructor(
private val _userStudyUiState: MutableStateFlow<UserStudyState> = MutableStateFlow(Idle)
val userStudyUiState: StateFlow<UserStudyState> get() = _userStudyUiState

init {
updateUserStudy()
}

private fun updateUserStudy() {
fun updateUserStudy() {
viewModelScope.launch {
runCatching {
homeRepository.getUserStudies()
Expand All @@ -40,10 +36,7 @@ class HomeViewModel @Inject constructor(
}

sealed interface UserStudyState {
data class Joined(
val userStudies: List<UserStudy>
) : UserStudyState

data class Joined(val userStudies: List<UserStudy>) : UserStudyState
object Nothing : UserStudyState
object Idle : UserStudyState
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.created.domain.model.UserStudy


class HomeAdapter(
private val onClick: (studyId: Int) -> Unit
private val onClick: (studyId: Long) -> Unit
) : ListAdapter<UserStudy, HomeViewHolder>(diffCallBack) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
Expand Down
Loading

0 comments on commit dcb017d

Please sign in to comment.