Skip to content

Commit a885e0c

Browse files
committed
store tool locales when they are changed via settings
1 parent b0eedf6 commit a885e0c

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

ui/base-tool/src/main/kotlin/org/cru/godtools/base/tool/ui/settings/SettingsBottomSheetDialogFragment.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.lifecycle.lifecycleScope
1010
import androidx.lifecycle.repeatOnLifecycle
1111
import dagger.hilt.android.AndroidEntryPoint
1212
import java.util.Locale
13+
import javax.inject.Inject
1314
import kotlinx.coroutines.flow.filterNotNull
1415
import kotlinx.coroutines.flow.launchIn
1516
import kotlinx.coroutines.flow.onEach
@@ -21,6 +22,7 @@ import org.cru.godtools.base.tool.activity.MultiLanguageToolActivityDataModel
2122
import org.cru.godtools.base.tool.databinding.ToolSettingsSheetCallbacks
2223
import org.cru.godtools.base.tool.ui.shareable.ShareableImageBottomSheetDialogFragment
2324
import org.cru.godtools.base.ui.languages.LanguagesDropdownAdapter
25+
import org.cru.godtools.db.repository.ToolsRepository
2426
import org.cru.godtools.model.Language
2527
import org.cru.godtools.shared.tool.parser.model.shareable.ShareableImage
2628
import org.cru.godtools.tool.R
@@ -118,12 +120,14 @@ class SettingsBottomSheetDialogFragment :
118120
val updateActiveLocale = activeLocale.value in primaryLocales.value.orEmpty()
119121
primaryLocales.value = listOf(locale)
120122
if (updateActiveLocale) activeLocale.value = locale
123+
storeToolLocales()
121124
}
122125

123126
private fun updateParallelLanguage(locale: Locale?) = with(activityDataModel) {
124127
val updateActiveLocale = activeLocale.value in parallelLocales.value.orEmpty()
125128
parallelLocales.value = listOfNotNull(locale)
126129
if (updateActiveLocale) activeLocale.value = locale
130+
storeToolLocales()
127131
}
128132

129133
// region ToolSettingsSheetCallbacks
@@ -143,6 +147,22 @@ class SettingsBottomSheetDialogFragment :
143147
val languages = activityDataModel.primaryLocales.value
144148
activityDataModel.primaryLocales.value = activityDataModel.parallelLocales.value
145149
activityDataModel.parallelLocales.value = languages
150+
storeToolLocales()
146151
}
147152
// endregion ToolSettingsSheetCallbacks
153+
154+
@Inject
155+
internal lateinit var toolsRepository: ToolsRepository
156+
157+
private fun storeToolLocales(
158+
tool: String? = activityDataModel.toolCode.value,
159+
primaryLocale: Locale? = activityDataModel.primaryLocales.value?.firstOrNull(),
160+
parallelLocale: Locale? = activityDataModel.parallelLocales.value?.firstOrNull(),
161+
) {
162+
if (tool != null) {
163+
lifecycleScope.launch {
164+
toolsRepository.updateToolLocales(tool, primaryLocale, parallelLocale)
165+
}
166+
}
167+
}
148168
}

0 commit comments

Comments
 (0)