package com.base.appzxhy.ui.appuninstall

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.base.appzxhy.bean.AppInfoBean
import com.base.appzxhy.ui.appuninstall.AppInfoUtils.getAppInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class AppUninstallViewModel : ViewModel() {

    val appList = arrayListOf<AppInfoBean>()

    fun initData(context: Context, callBack: () -> Unit) = viewModelScope.launch(Dispatchers.IO) {
        val list = getAppInfo(context)
        appList.addAll(list)
        launch(Dispatchers.Main) {
            callBack.invoke()
        }
    }

    var sortMode = -1

    //下从大到小,下从新-旧
    private fun sortList() {
        when (this.sortMode) {
            0 -> {
                  appList.sortByDescending { it.storageSize }
            }

            1 -> {
                 appList.sortBy { it.storageSize }
            }

            2 -> {
                appList.sortByDescending { it.installTime }
            }

            3 -> {
                appList.sortBy { it.installTime }
            }
        }
    }

    fun changeSort(callBack: () -> Unit) = viewModelScope.launch(Dispatchers.IO) {
        sortList()
        launch(Dispatchers.Main) {
            callBack.invoke()
        }
    }


}