Skip to content

Commit

Permalink
Issues boostcampwm-2022#287 feat: setting builder를 이용하여 유동적인 화면 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
audxo112 committed Feb 18, 2023
1 parent a67c113 commit 3d135d9
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lighthouse.features.setting.ext

import com.lighthouse.features.setting.model.SettingGroup

internal fun settingGroup(block: SettingGroup.Builder.() -> Unit): SettingGroup {
val builder = SettingGroup.Builder()
builder.block()
return builder.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lighthouse.features.setting.ext

import com.lighthouse.features.setting.model.SettingItem

internal fun settingItems(block: SettingItem.Builder.() -> Unit): List<SettingItem> {
val builder = SettingItem.Builder()
builder.block()
return builder.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.lighthouse.features.setting.model

import com.lighthouse.core.android.utils.resource.UIText

internal class SettingGroup(val title: UIText?, val items: List<SettingItem>) {

class Builder {
private var title: UIText? = null
private val items = ArrayList<SettingItem>()

fun setTitle(title: UIText?) = apply {
this.title = title
}

fun addItem(item: SettingItem) = apply {
items.add(item)
}

fun addButton(menu: SettingMenu) = apply {
addItem(SettingItem.Button(menu))
}

fun addStateButton(menu: SettingMenu, state: UIText = UIText.Empty) = apply {
addItem(SettingItem.StateButton(menu, state))
}

fun addStateSwitch(menu: SettingMenu, state: Boolean = false) = apply {
addItem(SettingItem.StateSwitch(menu, state))
}

fun build(): SettingGroup {
return SettingGroup(title, items.toList())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.lighthouse.features.setting.model

import com.lighthouse.core.android.utils.resource.UIText

internal sealed class SettingItem {

data class GroupHeader(val uiText: UIText) : SettingItem()

object Divider : SettingItem()

data class Button(val menu: SettingMenu) : SettingItem()

data class StateButton(val menu: SettingMenu, val state: UIText) : SettingItem()

data class StateSwitch(val menu: SettingMenu, val state: Boolean) : SettingItem()

class Builder {
private val groups = ArrayList<SettingGroup>()

fun addGroup(group: SettingGroup) = apply {
groups.add(group)
}

fun addItem(item: SettingItem) = apply {
addGroup(
SettingGroup.Builder()
.addItem(item)
.build()
)
}

fun addButton(menu: SettingMenu) = apply {
addItem(Button(menu))
}

fun addStateButton(menu: SettingMenu, state: UIText = UIText.Empty) = apply {
addItem(StateButton(menu, state))
}

fun addStateSwitch(menu: SettingMenu, state: Boolean = false) = apply {
addItem(StateSwitch(menu, state))
}

fun build(): List<SettingItem> {
val list = ArrayList<SettingItem>()

for (group in groups) {
if (group.title != null) {
list.add(GroupHeader(group.title))
}

group.items.forEachIndexed { index, item ->
list.add(item)
if (index < group.items.lastIndex) {
list.add(Divider)
}
}
}

return list.toList()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lighthouse.features.setting.model

import com.lighthouse.core.android.utils.resource.UIText
import com.lighthouse.features.setting.R

internal enum class SettingMenu(val uiText: UIText) {

USED_GIFTICON(UIText.StringResource(R.string.used_gifticon)),
IMMINENT_NOTIFICATION(UIText.StringResource(R.string.setting_imminent_notification)),
SECURITY(UIText.StringResource(R.string.setting_security)),
LOCATION(UIText.StringResource(R.string.setting_location_permission)),
SIGN_IN(UIText.StringResource(R.string.user_sign_in)),
SIGN_OUT(UIText.StringResource(R.string.user_sign_out)),
WITHDRAWAL(UIText.StringResource(R.string.user_withdrawal)),
COFFEE(UIText.StringResource(R.string.etc_coffee)),
TERMS_OF_USE(UIText.StringResource(R.string.etc_terms_of_use)),
PERSONAL_INFO_POLICY(UIText.StringResource(R.string.etc_personal_info_policy)),
OPEN_SOURCE_LICENSE(UIText.StringResource(R.string.etc_open_source_license))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import com.lighthouse.domain.usecase.setting.GetNotificationOptionUseCase
import com.lighthouse.domain.usecase.setting.GetSecurityOptionUseCase
import com.lighthouse.domain.usecase.user.IsGuestUseCase
import com.lighthouse.features.setting.R
import com.lighthouse.features.setting.ext.settingGroup
import com.lighthouse.features.setting.ext.settingItems
import com.lighthouse.features.setting.model.SettingMenu
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
Expand All @@ -21,6 +24,35 @@ class SettingViewModel @Inject constructor(
getSecurityOptionUseCase: GetSecurityOptionUseCase
) : ViewModel() {

private val settingGroup = settingGroup {
setTitle(UIText.StringResource(R.string.subtitle_setting))
addStateSwitch(SettingMenu.IMMINENT_NOTIFICATION)
addStateButton(SettingMenu.SECURITY)
addStateButton(SettingMenu.LOCATION)
}

private val userGroup = settingGroup {
setTitle(UIText.StringResource(R.string.subtitle_user))
addStateButton(SettingMenu.SIGN_IN)
addStateButton(SettingMenu.SIGN_OUT)
addStateButton(SettingMenu.WITHDRAWAL)
}

private val etcGroup = settingGroup {
setTitle(UIText.StringResource(R.string.subtitle_etc))
addButton(SettingMenu.COFFEE)
addButton(SettingMenu.TERMS_OF_USE)
addButton(SettingMenu.PERSONAL_INFO_POLICY)
addButton(SettingMenu.OPEN_SOURCE_LICENSE)
}

private val menus = settingItems {
addButton(SettingMenu.USED_GIFTICON)
addGroup(settingGroup)
addGroup(userGroup)
addGroup(etcGroup)
}

private val isGuest = isGuestUseCase()

val isVisibleSignIn = isGuest.map {
Expand Down
9 changes: 9 additions & 0 deletions features/ui-setting/src/main/res/layout/item_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
9 changes: 9 additions & 0 deletions features/ui-setting/src/main/res/layout/item_divider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
9 changes: 9 additions & 0 deletions features/ui-setting/src/main/res/layout/item_group_header.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
9 changes: 9 additions & 0 deletions features/ui-setting/src/main/res/layout/item_state_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
9 changes: 9 additions & 0 deletions features/ui-setting/src/main/res/layout/item_state_switch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 3d135d9

Please sign in to comment.