Bimba.git

ref: 3877e1e462c33778518e8c48dbf9e53076c00a53

app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedsViewModel.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.settings.feeds

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class FeedsViewModel : ViewModel() {
	private val _settings = MutableLiveData<FeedsSettings>()
	val settings: LiveData<FeedsSettings> = _settings

	fun loadSettings(context: Context) {
		_settings.value = FeedsSettings.load(context)
	}

	fun setSettings(feedID: String, feedSettings: FeedSettings) {
		val feedsSettings = _settings.value ?: FeedsSettings(mutableMapOf())
		feedsSettings.settings[feedID] = feedSettings
		_settings.value = feedsSettings
		Log.i("Settings", "${_settings.value}")
	}

	fun setEnabled(feedID: String, enabled: Boolean) {
		val feedSettings = (_settings.value ?: FeedsSettings(mutableMapOf())).settings[feedID]
		setSettings(feedID, feedSettings?.copy(enabled = enabled) ?: FeedSettings(enabled, true))
	}
}