Initial commit

This commit is contained in:
Michatec
2025-04-27 15:07:05 +02:00
commit 2162c9fb40
157 changed files with 12179 additions and 0 deletions
@@ -0,0 +1,106 @@
/*
* CollectionViewModel.kt
* Implements the CollectionViewModel class
* A CollectionViewModel stores the collection of stations as live data
*
* This file is part of
* TRANSISTOR - Radio App for Android
*
* Copyright (c) 2015-22 - Y20K.org
* Licensed under the MIT-License
* http://opensource.org/licenses/MIT
*/
package com.michatec.radio.collection
import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.michatec.radio.Keys
import com.michatec.radio.core.Collection
import com.michatec.radio.helpers.FileHelper
import kotlinx.coroutines.launch
import java.util.*
/*
* CollectionViewModel.class
*/
class CollectionViewModel(application: Application) : AndroidViewModel(application) {
/* Define log tag */
private val TAG: String = CollectionViewModel::class.java.simpleName
/* Main class variables */
val collectionLiveData: MutableLiveData<Collection> = MutableLiveData<Collection>()
val collectionSizeLiveData: MutableLiveData<Int> = MutableLiveData<Int>()
private var modificationDateViewModel: Date = Date()
private var collectionChangedReceiver: BroadcastReceiver
/* Init constructor */
init {
// load collection
loadCollection()
// create and register collection changed receiver
collectionChangedReceiver = createCollectionChangedReceiver()
LocalBroadcastManager.getInstance(application).registerReceiver(
collectionChangedReceiver,
IntentFilter(Keys.ACTION_COLLECTION_CHANGED)
)
}
/* Overrides onCleared */
override fun onCleared() {
super.onCleared()
LocalBroadcastManager.getInstance(getApplication())
.unregisterReceiver(collectionChangedReceiver)
}
/* Creates the collectionChangedReceiver - handles Keys.ACTION_COLLECTION_CHANGED */
private fun createCollectionChangedReceiver(): BroadcastReceiver {
return object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.hasExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE)) {
val date =
Date(intent.getLongExtra(Keys.EXTRA_COLLECTION_MODIFICATION_DATE, 0L))
// check if reload is necessary
if (date.after(modificationDateViewModel)) {
Log.v(
TAG,
"CollectionViewModel - reload collection after broadcast received."
)
loadCollection()
}
}
}
}
}
/* Reads collection of radio stations from storage using GSON */
private fun loadCollection() {
Log.v(TAG, "Loading collection of stations from storage")
viewModelScope.launch {
// load collection on background thread
val collection: Collection = FileHelper.readCollectionSuspended(getApplication())
// get updated modification date
modificationDateViewModel = collection.modificationDate
// update collection view model
collectionLiveData.value = collection
// update collection sie
collectionSizeLiveData.value = collection.stations.size
}
}
}