package jp.co.example.android01.feature.settings import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import java.time.LocalDate import java.time.ZoneId import java.time.format.DateTimeFormatter import javax.inject.Inject import jp.co.example.android01.core.data.SiteUrl import jp.co.example.android01.feature.settings.repository.SiteUrlRepositoryImpl @HiltViewModel class SettingsViewModel @Inject constructor( private val siteUrlRepository: SiteUrlRepositoryImpl ) : ViewModel() { private val _siteUrls = MutableStateFlow>(emptyList()) val siteUrls: StateFlow> = _siteUrls.asStateFlow() private val _isLoading = MutableStateFlow(false) val isLoading: StateFlow = _isLoading.asStateFlow() // URLテキストボックスの状態を管理(id -> url) private val _urlFieldValues = MutableStateFlow>(emptyMap()) val urlFieldValues: StateFlow> = _urlFieldValues.asStateFlow() // 元のURLテキストボックス値を保存(キャンセル時に使用) private val _origUrlValues = MutableStateFlow>(emptyMap()) val origUrlValues: StateFlow> = _origUrlValues.asStateFlow() // 動的サイトチェックボックスの状態を管理(id -> isDynamic) private val _isDynamicValues = MutableStateFlow>(emptyMap()) val isDynamicValues: StateFlow> = _isDynamicValues.asStateFlow() // 元の動的サイトチェックボックス値を保存(キャンセル時に使用) private val _origIsDynamicValues = MutableStateFlow>(emptyMap()) val origIsDynamicValues: StateFlow> = _origIsDynamicValues.asStateFlow() init { loadAllSiteUrls() } fun loadAllSiteUrls() { viewModelScope.launch { _isLoading.value = true val urls = siteUrlRepository.getAllSiteUrls() _siteUrls.value = urls // データベースから取得したurlをURLテキストボックスの状態に設定 val urlFieldMap = urls.associate { it.id to it.url } _urlFieldValues.value = urlFieldMap // 元のURLテキストボックス値も保存 _origUrlValues.value = urlFieldMap // データベースから取得したisDynamicフラグを動的サイトチェックボックスの状態に設定 val isDynamicMap = urls.associate { it.id to it.isDynamic } _isDynamicValues.value = isDynamicMap // 元の動的サイトチェックボックス値も保存 _origIsDynamicValues.value = isDynamicMap _isLoading.value = false } } fun updateUrlFieldValue(id: String, value: String) { val currentValues = _urlFieldValues.value.toMutableMap() currentValues[id] = value _urlFieldValues.value = currentValues } fun updateIsDynamicValue(id: String, value: Boolean) { val currentValues = _isDynamicValues.value.toMutableMap() currentValues[id] = value _isDynamicValues.value = currentValues } /** * キャンセル処理:テキストボックスの値を元の値に戻す */ fun cancelChanges() { _urlFieldValues.value = _origUrlValues.value _isDynamicValues.value = _origIsDynamicValues.value } /** * 保存処理:変更があった場合のみデータベースに保存 */ fun saveChanges() { viewModelScope.launch { val currentUrlValues = _urlFieldValues.value val origUrlValues = _origUrlValues.value val currentIsDynamicValues = _isDynamicValues.value val origIsDynamicValues = _origIsDynamicValues.value // 変更があった項目のみ保存 for ((id, currentUrlValue) in currentUrlValues) { val origUrlValue = origUrlValues[id] ?: "" val currentIsDynamicValue = currentIsDynamicValues[id] ?: false val origIsDynamicValue = origIsDynamicValues[id] ?: false val _siteUrl = SiteUrl( id = id, url = currentUrlValue, isDynamic = currentIsDynamicValue, settingAt = getCurrentJSTDate(), gettingAt = "" ) // 元の値が存在せず、入力がある場合は新規追加 if (origUrlValue.isEmpty() && currentUrlValue.isNotEmpty()) { siteUrlRepository.insertSiteUrl(_siteUrl) } // 元の値が存在し、変更があった場合は更新 else if (origUrlValue.isNotEmpty() && (currentUrlValue != origUrlValue || currentIsDynamicValue != origIsDynamicValue)) { siteUrlRepository.updateSiteUrl(_siteUrl) } } // 保存後、データを再読み込みして元の値を更新 loadAllSiteUrls() } } /** * 現在のJST日付を取得(YYYY-MM-DD形式) */ private fun getCurrentJSTDate(): String { val jstZone = ZoneId.of("Asia/Tokyo") val currentDate = LocalDate.now(jstZone) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") return currentDate.format(formatter) } }