Skip to content

Commit f709038

Browse files
committed
✨ 인증글 작성하기 위한 운동 기록 선택 페이지 레이아웃 및 운동기록 불러오기 구현
1 parent 765621d commit f709038

File tree

11 files changed

+235
-7
lines changed

11 files changed

+235
-7
lines changed

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

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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
5+
<data>
6+
7+
</data>
8+
9+
<androidx.constraintlayout.widget.ConstraintLayout
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent">
12+
13+
<com.google.android.material.appbar.AppBarLayout
14+
android:id="@+id/app_bar"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
app:layout_constraintTop_toTopOf="parent">
18+
19+
<com.google.android.material.appbar.MaterialToolbar
20+
android:id="@+id/top_app_bar"
21+
style="@style/Widget.MaterialComponents.Toolbar.Primary"
22+
android:layout_width="match_parent"
23+
android:layout_height="?attr/actionBarSize"
24+
app:title="@string/community_create_running_post_title" />
25+
26+
</com.google.android.material.appbar.AppBarLayout>
27+
28+
<TextView
29+
android:id="@+id/tv_label_running_history"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:text="@string/community_label_running_history"
33+
app:layout_constraintTop_toBottomOf="@id/app_bar"
34+
app:layout_constraintStart_toStartOf="parent"
35+
android:layout_marginTop="20dp"
36+
android:layout_marginStart="16dp"
37+
/>
38+
39+
<include
40+
android:id="@+id/item_view_running_history"
41+
layout="@layout/item_running_history"
42+
android:layout_width="0dp"
43+
android:layout_height="wrap_content"
44+
android:layout_marginTop="12dp"
45+
app:layout_constraintStart_toStartOf="parent"
46+
app:layout_constraintEnd_toEndOf="parent"
47+
app:layout_constraintTop_toBottomOf="@id/tv_label_running_history"/>
48+
49+
<EditText
50+
android:id="@+id/et_running_post_content"
51+
android:layout_width="0dp"
52+
android:layout_height="300dp"
53+
android:layout_marginHorizontal="12dp"
54+
android:inputType="textMultiLine"
55+
app:layout_constraintStart_toStartOf="parent"
56+
app:layout_constraintEnd_toEndOf="parent"
57+
app:layout_constraintTop_toBottomOf="@id/item_view_running_history"/>
58+
59+
</androidx.constraintlayout.widget.ConstraintLayout>
60+
</layout>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<data>
7+
8+
</data>
9+
10+
<androidx.constraintlayout.widget.ConstraintLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent">
13+
14+
<com.google.android.material.appbar.AppBarLayout
15+
android:id="@+id/app_bar"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
app:layout_constraintTop_toTopOf="parent">
19+
20+
<com.google.android.material.appbar.MaterialToolbar
21+
android:id="@+id/top_app_bar"
22+
style="@style/Widget.MaterialComponents.Toolbar.Primary"
23+
android:layout_width="match_parent"
24+
android:layout_height="?attr/actionBarSize"
25+
app:title="@string/community_select_running_history_title" />
26+
27+
</com.google.android.material.appbar.AppBarLayout>
28+
29+
<TextView
30+
android:id="@+id/tv_select_running_history_helper"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_marginTop="12dp"
34+
android:text="@string/community_select_running_history_helper_text"
35+
app:layout_constraintEnd_toEndOf="parent"
36+
app:layout_constraintStart_toStartOf="parent"
37+
app:layout_constraintTop_toBottomOf="@id/app_bar" />
38+
39+
<androidx.recyclerview.widget.RecyclerView
40+
android:id="@+id/rv_select_running_history"
41+
android:layout_width="0dp"
42+
android:layout_height="0dp"
43+
android:layout_marginHorizontal="12dp"
44+
android:layout_marginTop="12dp"
45+
android:orientation="vertical"
46+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
47+
app:layout_constraintBottom_toBottomOf="parent"
48+
app:layout_constraintEnd_toEndOf="parent"
49+
app:layout_constraintStart_toStartOf="parent"
50+
app:layout_constraintTop_toBottomOf="@id/tv_select_running_history_helper"
51+
tools:listitem="@layout/item_running_history" />
52+
53+
</androidx.constraintlayout.widget.ConstraintLayout>
54+
</layout>

0 commit comments

Comments
 (0)