Skip to content

Commit 69e0fb6

Browse files
authored
๐Ÿ”€ #47 from boostcampwm-2022/feat/community_post
์ฝ”๋“œ ๋ฆฌ๋ทฐ ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ ๋ฐ ์šด๋™ ๊ธฐ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ ๋ ˆ์ด์•„์›ƒ ๋ฐ ๊ธฐ๋Šฅ ๊ตฌํ˜„
2 parents 1e92d72 + f3506ee commit 69e0fb6

18 files changed

+314
-36
lines changed

โ€Ždata/src/main/java/com/whyranoid/data/account/AccountDataSourceImpl.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import javax.inject.Inject
1616

1717
class AccountDataSourceImpl @Inject constructor(
1818
private val dataStoreDb: DataStore<Preferences>,
19-
private val fireBaseDb: FirebaseFirestore
19+
private val fireBaseDb: FirebaseFirestore,
20+
private val auth: FirebaseAuth
2021
) : AccountDataSource {
2122

22-
private val auth = FirebaseAuth.getInstance()
2323
private val currentUser = auth.currentUser
2424

2525
private object PreferenceKeys {
@@ -67,12 +67,13 @@ class AccountDataSourceImpl @Inject constructor(
6767
}
6868

6969
override suspend fun signOut(): Result<Boolean> = runCatching {
70-
signOut()
70+
auth.signOut()
7171
true
7272
}
7373

74-
override suspend fun withDrawal(): Result<Boolean> {
75-
TODO("Not yet implemented")
74+
override suspend fun withDrawal(): Result<Boolean> = runCatching {
75+
currentUser?.delete()
76+
true
7677
}
7778

7879
companion object {

โ€Ždata/src/main/java/com/whyranoid/data/account/RunningHistoryLocalDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import com.whyranoid.domain.model.RunningHistory
44
import kotlinx.coroutines.flow.Flow
55

66
interface RunningHistoryLocalDataSource {
7-
fun getRunningHistory(): Flow<List<RunningHistory>>
7+
fun getRunningHistory(): Flow<Result<List<RunningHistory>>>
88
}

โ€Ždata/src/main/java/com/whyranoid/data/account/RunningHistoryLocalDataSourceImpl.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class RunningHistoryLocalDataSourceImpl @Inject constructor(
99
private val runningHistoryDao: RunningHistoryDao
1010
) : RunningHistoryLocalDataSource {
1111

12-
override fun getRunningHistory(): Flow<List<RunningHistory>> {
12+
override fun getRunningHistory(): Flow<Result<List<RunningHistory>>> {
1313
return runningHistoryDao.getRunningHistory().map { runningHistoryList ->
14-
runningHistoryList.map { runningHistoryEntity ->
15-
runningHistoryEntity.toRunningHistory()
14+
runCatching {
15+
runningHistoryList.map { runningHistoryEntity ->
16+
runningHistoryEntity.toRunningHistory()
17+
}
1618
}
1719
}
1820
}

โ€Ždata/src/main/java/com/whyranoid/data/account/RunningHistoryRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import javax.inject.Inject
88
class RunningHistoryRepositoryImpl @Inject constructor(
99
private val runningHistoryLocalDataSource: RunningHistoryLocalDataSource
1010
) : RunningHistoryRepository {
11-
override fun getRunningHistory(): Flow<List<RunningHistory>> {
11+
override fun getRunningHistory(): Flow<Result<List<RunningHistory>>> {
1212
return runningHistoryLocalDataSource.getRunningHistory()
1313
}
1414

โ€Ždata/src/main/java/com/whyranoid/data/di/UserDataBaseModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore
55
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
66
import androidx.datastore.preferences.core.Preferences
77
import androidx.datastore.preferences.preferencesDataStoreFile
8+
import com.google.firebase.auth.FirebaseAuth
89
import dagger.Module
910
import dagger.Provides
1011
import dagger.hilt.InstallIn
@@ -26,4 +27,8 @@ object UserDataBaseModule {
2627
appContext.preferencesDataStoreFile(USER_PREFERENCES)
2728
}
2829
)
30+
31+
@Singleton
32+
@Provides
33+
fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance()
2934
}

โ€Ždomain/src/main/java/com/whyranoid/domain/repository/RunningHistoryRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow
66
interface RunningHistoryRepository {
77

88
// ์šด๋™ ๋‚ด์—ญ ๊ฐ€์ ธ์˜ค๊ธฐ - ๋กœ์ปฌ
9-
fun getRunningHistory(): Flow<List<RunningHistory>>
9+
fun getRunningHistory(): Flow<Result<List<RunningHistory>>>
1010

1111
// ๊ธ€ ์•ˆ์“ด ์šด๋™ ๋‚ด์—ญ ๊ฐ€์ ธ์˜ค๊ธฐ - ๋กœ์ปฌ
1212
suspend fun getUnpostedRunningHistory(): List<RunningHistory>

โ€Ždomain/src/main/java/com/whyranoid/domain/usecase/GetRunningHistoryUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow
66
import javax.inject.Inject
77

88
class GetRunningHistoryUseCase @Inject constructor(private val runningHistoryRepository: RunningHistoryRepository) {
9-
operator fun invoke(): Flow<List<RunningHistory>> {
9+
operator fun invoke(): Flow<Result<List<RunningHistory>>> {
1010
return runningHistoryRepository.getRunningHistory()
1111
}
1212
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.whyranoid.presentation.community
2+
3+
import com.whyranoid.presentation.R
4+
import com.whyranoid.presentation.base.BaseFragment
5+
import com.whyranoid.presentation.databinding.FragmentCreateRunningPostBinding
6+
7+
internal class CreateRunningPostFragment :
8+
BaseFragment<FragmentCreateRunningPostBinding>(R.layout.fragment_create_running_post)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.whyranoid.presentation.community
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.fragment.app.viewModels
6+
import com.whyranoid.presentation.R
7+
import com.whyranoid.presentation.base.BaseFragment
8+
import com.whyranoid.presentation.databinding.FragmentSelectRunningHistoryBinding
9+
import com.whyranoid.presentation.model.RunningHistoryUiModel
10+
import com.whyranoid.presentation.model.UiState
11+
import com.whyranoid.presentation.myrun.MyRunningHistoryAdapter
12+
import com.whyranoid.presentation.util.repeatWhenUiStarted
13+
14+
internal class SelectRunningHistoryFragment :
15+
BaseFragment<FragmentSelectRunningHistoryBinding>(R.layout.fragment_select_running_history) {
16+
17+
private val viewModel: SelectRunningHistoryViewModel by viewModels()
18+
private val runningHistoryAdapter = MyRunningHistoryAdapter()
19+
20+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
21+
super.onViewCreated(view, savedInstanceState)
22+
23+
initViews()
24+
observeState()
25+
}
26+
27+
private fun initViews() {
28+
binding.rvSelectRunningHistory.adapter = runningHistoryAdapter
29+
}
30+
31+
private fun observeState() {
32+
viewLifecycleOwner.repeatWhenUiStarted {
33+
viewModel.runningHistoryListState.collect { runningHistoryState ->
34+
when (runningHistoryState) {
35+
is UiState.UnInitialized -> {
36+
// ์ดˆ๊ธฐํ™”
37+
}
38+
is UiState.Loading -> {
39+
// ๋กœ๋”ฉ์ค‘
40+
}
41+
42+
is UiState.Success<List<RunningHistoryUiModel>> -> initRecyclerView(
43+
runningHistoryState.value
44+
)
45+
46+
is UiState.Failure -> {
47+
// ์‹คํŒจ
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
private fun initRecyclerView(runningHistoryList: List<RunningHistoryUiModel>) {
55+
runningHistoryAdapter.submitList(runningHistoryList)
56+
}
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.whyranoid.presentation.community
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import com.whyranoid.domain.usecase.GetRunningHistoryUseCase
6+
import com.whyranoid.presentation.model.RunningHistoryUiModel
7+
import com.whyranoid.presentation.model.UiState
8+
import com.whyranoid.presentation.model.toRunningHistoryUiModel
9+
import dagger.hilt.android.lifecycle.HiltViewModel
10+
import kotlinx.coroutines.flow.MutableStateFlow
11+
import kotlinx.coroutines.flow.StateFlow
12+
import kotlinx.coroutines.flow.asStateFlow
13+
import kotlinx.coroutines.launch
14+
import javax.inject.Inject
15+
16+
@HiltViewModel
17+
class SelectRunningHistoryViewModel @Inject constructor(private val getRunningHistoryUseCase: GetRunningHistoryUseCase) :
18+
ViewModel() {
19+
20+
init {
21+
getRunningHistoryList()
22+
}
23+
24+
private val _runningHistoryListState =
25+
MutableStateFlow<UiState<List<RunningHistoryUiModel>>>(UiState.UnInitialized)
26+
val runningHistoryListState: StateFlow<UiState<List<RunningHistoryUiModel>>>
27+
get() = _runningHistoryListState.asStateFlow()
28+
29+
private fun getRunningHistoryList() {
30+
viewModelScope.launch {
31+
_runningHistoryListState.value = UiState.Loading
32+
33+
getRunningHistoryUseCase().collect { runningHistoryListResult ->
34+
runningHistoryListResult.onSuccess { runningHistoryList ->
35+
_runningHistoryListState.value =
36+
UiState.Success(runningHistoryList.map { runningHistory -> runningHistory.toRunningHistoryUiModel() })
37+
}.onFailure { throwable ->
38+
_runningHistoryListState.value = UiState.Failure(throwable)
39+
}
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
ย (0)