<commit_before>/*
 * Copyright (c) 2020 Adventech <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.cryart.sabbathschool.lessons.ui.lessons

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cryart.sabbathschool.core.extensions.arch.SingleLiveEvent
import com.cryart.sabbathschool.core.extensions.coroutines.SchedulerProvider
import com.cryart.sabbathschool.core.extensions.prefs.SSPrefs
import com.cryart.sabbathschool.lessons.data.model.SSQuarterly
import com.cryart.sabbathschool.lessons.data.repository.QuarterliesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@HiltViewModel
class LessonsViewModel @Inject constructor(
    private val repository: QuarterliesRepository,
    private val ssPrefs: SSPrefs,
    private val schedulerProvider: SchedulerProvider
) : ViewModel() {

    private val mutableQuarterlyTypes = MutableLiveData<List<String>>()
    val quarterlyTypesLiveData: LiveData<List<String>> get() = mutableQuarterlyTypes

    private val mutableSelectedType = SingleLiveEvent<Pair<String, String?>>()
    val selectedTypeLiveData: LiveData<Pair<String, String?>> get() = mutableSelectedType

    private var lessonTypes: List<SSQuarterly> = emptyList()

    fun setQuarterlyIndex(index: String) {
        viewModelScope.launch(schedulerProvider.io) {
            repository.getQuarterlies().collect { resource ->
                if (resource.isSuccessFul) {
                    val quarterlies = resource.data ?: return@collect
                    val selected = quarterlies.find { it.index == index } ?: return@collect
                    lessonTypes = quarterlies.filter {
                        it.start_date == selected.start_date && it.end_date == selected.end_date
                    }
                    if (lessonTypes.size > 1) {
                        val names = listOf(selected.quarterly_name) + lessonTypes
                            .filterNot { it.id == selected.id }
                            .map { it.quarterly_name }
                        mutableQuarterlyTypes.postValue(names.filterNotNull())

                        val lastType = ssPrefs.getLastQuarterlyType() ?: return@collect
                        if (lastType != names.first()) {
                            quarterlyTypeSelected(lastType)
                        }
                    }
                }
            }
        }
    }

    fun quarterlyTypeSelected(type: String) {
        val index = lessonTypes.find { it.quarterly_name == type }?.index ?: return
        mutableSelectedType.postValue(Pair(index, type))
        ssPrefs.setLastQuarterlyType(type)
    }
}<commit_msg>Fix quarterly types filter<commit_after>/*
 * Copyright (c) 2020 Adventech <<EMAIL>>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.cryart.sabbathschool.lessons.ui.lessons

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cryart.sabbathschool.core.extensions.arch.SingleLiveEvent
import com.cryart.sabbathschool.core.extensions.arch.asLiveData
import com.cryart.sabbathschool.core.extensions.coroutines.SchedulerProvider
import com.cryart.sabbathschool.core.extensions.prefs.SSPrefs
import com.cryart.sabbathschool.lessons.data.model.SSQuarterly
import com.cryart.sabbathschool.lessons.data.repository.QuarterliesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class LessonsViewModel @Inject constructor(
    private val repository: QuarterliesRepository,
    private val ssPrefs: SSPrefs,
    private val schedulerProvider: SchedulerProvider
) : ViewModel() {

    private val mutableQuarterlyTypes = MutableLiveData<List<String>>()
    val quarterlyTypesLiveData: LiveData<List<String>> = mutableQuarterlyTypes.asLiveData()

    private val mutableSelectedType = SingleLiveEvent<Pair<String, String?>>()
    val selectedTypeLiveData: LiveData<Pair<String, String?>> = mutableSelectedType.asLiveData()

    private var lessonTypes: List<SSQuarterly> = emptyList()

    fun setQuarterlyIndex(index: String) = viewModelScope.launch(schedulerProvider.io) {
        repository.getQuarterlies().collect { resource ->
            val quarterlies = resource.data ?: return@collect
            val selected = quarterlies.find { it.index == index } ?: return@collect
            if (selected.group.isNullOrEmpty()) return@collect

            lessonTypes = quarterlies.filter {
                it.start_date == selected.start_date && it.end_date == selected.end_date
            }
            if (lessonTypes.isNotEmpty()) {
                val names = listOf(selected.quarterly_name) + lessonTypes
                    .filterNot { it.id == selected.id }
                    .map { it.quarterly_name }
                mutableQuarterlyTypes.postValue(names)

                val lastType = ssPrefs.getLastQuarterlyType() ?: return@collect
                if (lastType != names.first()) {
                    quarterlyTypeSelected(lastType)
                }
            }
        }
    }

    fun quarterlyTypeSelected(type: String) {
        val index = lessonTypes.find { it.quarterly_name == type }?.index ?: return
        mutableSelectedType.postValue(Pair(index, type))
        ssPrefs.setLastQuarterlyType(type)
    }
}
{'fraction_non_alphanumeric': 0.07825520833333334, 'fraction_numerical': 0.001171875, 'mean_word_length': 3.26012201885746, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5715642', 'n_tokens_mistral': 2122, 'n_tokens_neox': 1982, 'n_words': 597}