Skip to content

Commit

Permalink
Merge pull request #236 from woowacourse-teams/AN/feature/214-creates…
Browse files Browse the repository at this point in the history
…tudy-inflate

[스터디 개설 뷰] 스터디 개설 뷰 리펙터링
  • Loading branch information
inseonyun authored Aug 9, 2023
2 parents c62b486 + 1d8bb6a commit eba7e10
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class CreateStudyActivity :

private fun initActionBar() {
setSupportActionBar(binding.tbCreateStudy)
supportActionBar?.setHomeActionContentDescription(R.string.toolbar_back_text)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_back)
}
Expand Down Expand Up @@ -121,6 +122,9 @@ class CreateStudyActivity :
}

companion object {
fun getIntent(context: Context): Intent = Intent(context, CreateStudyActivity::class.java)
fun getIntent(context: Context): Intent =
Intent(context, CreateStudyActivity::class.java).also {
it.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.created.team201.presentation.createStudy.model.CreateStudyUiModel
import com.created.team201.presentation.createStudy.model.PeriodUiModel
import com.created.team201.util.NonNullLiveData
import com.created.team201.util.NonNullMutableLiveData
import com.created.team201.util.addSourceList
import kotlinx.coroutines.launch

class CreateStudyViewModel(
Expand Down Expand Up @@ -62,15 +63,7 @@ class CreateStudyViewModel(
val studyState: LiveData<State>
get() = _studyState

val study: CreateStudyUiModel
get() = CreateStudyUiModel(
name.value,
peopleCount.value,
startDate.value,
period.value,
cycle.value,
introduction.value,
)
private var isOpenStudy: Boolean = false

fun setName(name: String) {
_name.value = name.replace("\n", "")
Expand All @@ -97,14 +90,25 @@ class CreateStudyViewModel(
_period.value = period
}

fun createStudy(study: CreateStudyUiModel) {
fun createStudy() {
if (isOpenStudy) return
isOpenStudy = true
viewModelScope.launch {
createStudyRepository.createStudy(study.toDomain())
.onSuccess {
_studyState.value = State.Success(it)
}.onFailure {
_studyState.value = State.FAIL
}
CreateStudyUiModel(
name.value.trim(),
peopleCount.value,
startDate.value,
period.value,
cycle.value,
introduction.value.trim(),
).apply {
createStudyRepository.createStudy(this.toDomain())
.onSuccess {
_studyState.value = State.Success(it)
}.onFailure {
_studyState.value = State.FAIL
}
}
}
}

Expand All @@ -118,27 +122,20 @@ class CreateStudyViewModel(
}

private fun isInitializeCreateStudyInformation(): Boolean =
name.value.isNotEmpty() && peopleCount.value.isNotZero() && startDate.value.isNotEmpty() && period.value.isNotZero() && cycle.value.date.isNotZero() && introduction.value.isNotEmpty()
name.value.isNotBlankAndEmpty() && peopleCount.value.isNotZero() && startDate.value.isNotEmpty() && period.value.isNotZero() && cycle.value.date.isNotZero() && introduction.value.isNotBlankAndEmpty()

private fun CreateStudyUiModel.toDomain(): CreateStudy =
CreateStudy(name, peopleCount, startDate, period, cycle.toDomain(), introduction)

private fun PeriodUiModel.toDomain(): Period = Period(date, unit)

private fun String.isNotBlankAndEmpty(): Boolean = isNotBlank().and(isNotEmpty())

private fun String.isNotEmpty(): Boolean = isEmpty().not()

private fun Int.isNotZero(): Boolean = this != 0
private fun String.isNotBlank(): Boolean = isBlank().not()

private fun <T> MediatorLiveData<T>.addSourceList(
vararg liveDataArgument: LiveData<*>,
onChanged: () -> T,
) {
liveDataArgument.forEach {
this.addSource(it) {
value = onChanged()
}
}
}
private fun Int.isNotZero(): Boolean = this != 0

companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.created.team201.util

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData

fun <T> MediatorLiveData<T>.addSourceList(
vararg liveDataArgument: LiveData<*>,
onChanged: () -> T,
) {
liveDataArgument.forEach {
this.addSource(it) {
value = onChanged()
}
}
}

2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/activity_create_study.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
android:layout_marginBottom="28dp"
android:background="@drawable/bg_btn_color_and_radius_10dp_disabled"
android:enabled="@{viewModel.isEnableCreateStudy}"
android:onClick="@{() -> viewModel.createStudy(viewModel.study)}"
android:onClick="@{() -> viewModel.createStudy()}"
android:paddingVertical="14dp"
android:text="@string/createStudy_button_created"
android:textAlignment="center"
Expand Down
17 changes: 15 additions & 2 deletions android/app/src/main/res/layout/fragment_cycle_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_cycle_bottom_sheet_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="@string/bottomSheetFragment_cycle_title_text"
android:textAlignment="center"
android:textAppearance="@style/picker_sb16"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/mp_cycle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_cycle" />

<com.created.team201.presentation.createStudy.custom.MultiPicker
android:id="@+id/mp_cycle"
changeListener="@{changeListener}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_cycle"
app:layout_constraintTop_toBottomOf="@id/tv_cycle_bottom_sheet_title"
app:leftMaxValue="14"
app:leftMinValue="1"
app:leftValue="@{viewModel.cycle.date}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_people_count_bottom_sheet_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="@string/bottomSheetFragment_peopleCount_title_text"
android:textAlignment="center"
android:textAppearance="@style/picker_sb16"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/sp_people_count"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_people_count" />

<com.created.team201.presentation.createStudy.custom.SinglePicker
android:id="@+id/sp_people_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_people_count"
app:layout_constraintTop_toBottomOf="@id/tv_people_count_bottom_sheet_title"
app:maxValue="8"
app:minValue="2"
app:singlePickerTitle="@string/single_picker_text_people_count"
Expand Down
16 changes: 15 additions & 1 deletion android/app/src/main/res/layout/fragment_period_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_period_bottom_sheet_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="@string/bottomSheetFragment_period_title_text"
android:textAlignment="center"
android:textAppearance="@style/picker_sb16"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/sp_period"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_period" />

<com.created.team201.presentation.createStudy.custom.SinglePicker
android:id="@+id/sp_period"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_period"
app:layout_constraintTop_toBottomOf="@id/tv_period_bottom_sheet_title"
app:maxValue="60"
app:minValue="1"
app:singlePickerTitle="@string/single_picker_text_period"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_start_date_bottom_sheet_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="20dp"
android:text="@string/bottomSheetFragment_startDate_title_text"
android:textAlignment="center"
android:textAppearance="@style/picker_sb16"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@id/calendar_create_study_start_date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_start_date" />

<com.created.team201.presentation.createStudy.custom.Calendar
android:id="@+id/calendar_create_study_start_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_create_study_start_date"
app:layout_constraintTop_toBottomOf="@id/tv_start_date_bottom_sheet_title"
app:maximumMonthRange="3" />


Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
<string name="createStudy_toast_fail">스터디 개설에 실패했습니다</string>

<!-- 스터디 개설 - 바텀 다이얼로그 뷰 -->
<string name="bottomSheetFragment_peopleCount_title_text">인원 수</string>
<string name="bottomSheetFragment_startDate_title_text">예상 시작일</string>
<string name="bottomSheetFragment_cycle_title_text">스터디 주기</string>
<string name="bottomSheetFragment_period_title_text">스터디 기간</string>
<string name="bottomSheetFragment_button_name_cancel">취소</string>
<string name="bottomSheetFragment_button_name_save">저장</string>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.created.team201.presentation.createStudy

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.created.domain.model.CreateStudy
import com.created.domain.model.Period
import com.created.domain.repository.CreateStudyRepository
import com.created.team201.presentation.createStudy.CreateStudyViewModel.State.Success
import com.created.team201.presentation.createStudy.model.CreateStudyUiModel
import com.created.team201.presentation.createStudy.model.PeriodUiModel
import com.created.team201.presentation.util.getOrAwaitValue
import io.mockk.coEvery
import io.mockk.mockk
Expand Down Expand Up @@ -45,9 +41,18 @@ class CreateStudyViewModelTest {
// given
val studyId = 1L
coEvery { repository.createStudy(CreateStudyFixture.study) } returns Result.success(studyId)
viewModel.setName(CreateStudyFixture.study.name)
viewModel.setPeopleCount(CreateStudyFixture.study.peopleCount)
viewModel.setStartDate(CreateStudyFixture.study.startDate)
viewModel.setPeriod(CreateStudyFixture.study.period)
viewModel.setCycle(
CreateStudyFixture.study.cycle.date,
CreateStudyFixture.study.cycle.unit.type
)
viewModel.setIntroduction(CreateStudyFixture.study.introduction)

// when
viewModel.createStudy(CreateStudyFixture.study.toUiModel())
viewModel.createStudy()

// then
viewModel.studyState.getOrAwaitValue()
Expand All @@ -70,10 +75,4 @@ class CreateStudyViewModelTest {
viewModel.isEnableCreateStudy.getOrAwaitValue()
assertEquals(true, viewModel.isEnableCreateStudy.value)
}

private fun CreateStudy.toUiModel(): CreateStudyUiModel =
CreateStudyUiModel(name, peopleCount, startDate, period, cycle.toUiModel(), introduction)

private fun Period.toUiModel(): PeriodUiModel =
PeriodUiModel(date, unit)
}

0 comments on commit eba7e10

Please sign in to comment.