<commit_before>/*
 * Copyright (C) 2020 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.wearable.alpha.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import com.example.android.wearable.alpha.data.WatchFaceRepository
import com.example.android.wearable.alpha.data.db.AnalogWatchFaceEntity
import com.example.android.wearable.alpha.data.db.WatchFaceArmDimensionsEntity
import com.example.android.wearable.alpha.data.db.WatchFaceColorStyleEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * View model used for [AnalogWatchFaceService].
 */
class AnalogWatchFaceViewModel(private val repository: WatchFaceRepository) {

    // Used to launch coroutines (non-blocking way to insert data).
    private val scope: CoroutineScope = MainScope()

    // [AnalogWatchFaceEntity] operations:
    // Using LiveData to cache what's returned has several benefits:
    // - We can put an observer on the data (instead of polling for changes) and only update the
    //   the UI when the data actually changes.
    // - Repository is completely separated from the UI through the ViewModel.
    val allAnalogWatchFaces: LiveData<List<AnalogWatchFaceEntity>> =
        repository.allAnalogWatchFaces.asLiveData()

    // Launches a new coroutine to insert the data in a non-blocking way
    fun insertAnalogWatchFace(analogWatchFaceEntity: AnalogWatchFaceEntity) = scope.launch {
        repository.insertAnalogWatchFace(analogWatchFaceEntity)
    }

    fun deleteAllAnalogWatchFaces() = scope.launch {
        repository.deleteAllAnalogWatchFaces()
    }

    // [WatchFaceColorStyleEntity] operations:
    val allWatchFaceColorStyles: LiveData<List<WatchFaceColorStyleEntity>> =
            repository.watchFaceColorStyle.asLiveData()

    fun insertWatchFaceColorStyle(watchFaceColorStyle: WatchFaceColorStyleEntity) = scope.launch {
        repository.insertWatchFaceColorStyle(watchFaceColorStyle)
    }

    fun deleteAllWatchFaceColorStyles() = scope.launch {
        repository.deleteAllWatchFaceColorStyles()
    }

    // [WatchFaceArmDimensionsEntity] operations:
    val allWatchFaceArmDimensions: LiveData<List<WatchFaceArmDimensionsEntity>> =
            repository.watchFaceArmDimensions.asLiveData()

    fun insertWatchFaceArmDimensions(watchFaceArmDimensionsEntity: WatchFaceArmDimensionsEntity) = scope.launch {
        repository.insertWatchFaceArmDimensions(watchFaceArmDimensionsEntity)
    }

    fun deleteAllWatchFaceArmDimensions() = scope.launch {
        repository.deleteAllWatchFaceArmDimensions()
    }
}<commit_msg>Cleans up ViewModel.<commit_after>/*
 * Copyright (C) 2020 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.wearable.alpha.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import com.example.android.wearable.alpha.data.WatchFaceRepository
import com.example.android.wearable.alpha.data.db.AnalogWatchFaceEntity
import com.example.android.wearable.alpha.data.db.WatchFaceArmDimensionsEntity
import com.example.android.wearable.alpha.data.db.WatchFaceColorStyleEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/**
 * View model used for [AnalogWatchFaceService].
 */
class AnalogWatchFaceViewModel(private val repository: WatchFaceRepository) {

    // Used to launch coroutines (non-blocking way to insert data).
    private val scope: CoroutineScope = MainScope()

    // [AnalogWatchFaceEntity] operations:
    val allAnalogWatchFaces: LiveData<List<AnalogWatchFaceEntity>> =
        repository.allAnalogWatchFaces.asLiveData()

    // Launches a new coroutine to insert the data in a non-blocking way
    fun insertAnalogWatchFace(analogWatchFaceEntity: AnalogWatchFaceEntity) = scope.launch {
        repository.insertAnalogWatchFace(analogWatchFaceEntity)
    }

    fun deleteAllAnalogWatchFaces() = scope.launch {
        repository.deleteAllAnalogWatchFaces()
    }

    // [WatchFaceColorStyleEntity] operations:
    val allWatchFaceColorStyles: LiveData<List<WatchFaceColorStyleEntity>> =
            repository.watchFaceColorStyle.asLiveData()

    fun insertWatchFaceColorStyle(watchFaceColorStyle: WatchFaceColorStyleEntity) = scope.launch {
        repository.insertWatchFaceColorStyle(watchFaceColorStyle)
    }

    fun deleteAllWatchFaceColorStyles() = scope.launch {
        repository.deleteAllWatchFaceColorStyles()
    }

    // [WatchFaceArmDimensionsEntity] operations:
    val allWatchFaceArmDimensions: LiveData<List<WatchFaceArmDimensionsEntity>> =
            repository.watchFaceArmDimensions.asLiveData()

    fun insertWatchFaceArmDimensions(watchFaceArmDimensionsEntity: WatchFaceArmDimensionsEntity) = scope.launch {
        repository.insertWatchFaceArmDimensions(watchFaceArmDimensionsEntity)
    }

    fun deleteAllWatchFaceArmDimensions() = scope.launch {
        repository.deleteAllWatchFaceArmDimensions()
    }

    fun clear() {
        scope.cancel("AnalogWatchFaceViewModel.clear() request")
    }
}
{'fraction_non_alphanumeric': 0.07478870993461968, 'fraction_numerical': 0.002551427204592569, 'mean_word_length': 4.42560553633218, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '146470', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1673, 'n_words': 427}