<commit_before>package com.github.panpf.sketch.sample.vm

import android.app.Application
import android.view.MenuItem
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.github.panpf.sketch.sample.R
import com.github.panpf.sketch.sample.appSettingsService
import com.github.panpf.sketch.sample.base.LifecycleAndroidViewModel
import com.github.panpf.sketch.sample.bean.DialogFragmentItemInfo
import com.github.panpf.sketch.sample.bean.LayoutMode
import com.github.panpf.sketch.sample.bean.MenuItemInfoGroup
import com.github.panpf.sketch.sample.bean.SwitchMenuItemInfo
import com.github.panpf.sketch.sample.ui.SettingsDialogFragment

class ListMenuViewModel(
    application1: Application,
    val showLayoutModeMenu: Boolean,
    val showPlayMenu: Boolean
) : LifecycleAndroidViewModel(application1) {

    class Factory(
        val application1: Application,
        val showLayoutModeMenu: Boolean,
        val showPlayMenu: Boolean
    ) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ListMenuViewModel(application1, showLayoutModeMenu, showPlayMenu) as T
        }
    }

    val menuList = MutableLiveData<List<MenuItemInfoGroup>>()

    init {
        menuList.postValue(assembleMenuList())
    }

    private fun assembleMenuList(): List<MenuItemInfoGroup> = buildList {
        add(MenuItemInfoGroup(buildList {
            if (showLayoutModeMenu) {
                add(SwitchMenuItemInfo(
                    values = arrayOf(
                        LayoutMode.GRID,
                        LayoutMode.STAGGERED_GRID,
                    ),
                    initValue = application1.appSettingsService.photoListLayoutMode.value!!,
                    titles = null,
                    iconResIds = arrayOf(
                        R.drawable.ic_layout_grid_staggered,
                        R.drawable.ic_layout_grid,
                    ),
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS
                ) { _, newValue ->
                    application1.appSettingsService.photoListLayoutMode.postValue(newValue)
                    menuList.postValue(menuList.value)
                })
            }

            if (showPlayMenu) {
                add(SwitchMenuItemInfo(
                    values = arrayOf(true, false),
                    initValue = application1.appSettingsService.disabledAnimatableDrawableInList.value!!,
                    titles = null,
                    iconResIds = arrayOf(
                        R.drawable.ic_pause,
                        R.drawable.ic_play,
                    ),
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS
                ) { _, newValue ->
                    application1.appSettingsService.disabledAnimatableDrawableInList.postValue(
                        newValue
                    )
                    menuList.postValue(menuList.value)
                })
            }

//            add(
//                NavMenuItemInfo(
//                    title = "Settings",
//                    iconResId = R.drawable.ic_settings,
//                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS,
//                    navDirections = NavMainDirections.actionGlobalSettingsFragment()
//                )
//            )

            add(
                DialogFragmentItemInfo(
                    title = "Settings",
                    iconResId = R.drawable.ic_settings,
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS,
                    fragment = SettingsDialogFragment()<commit_msg>fix: Fix the layout menu icon is inverted bug<commit_after>package com.github.panpf.sketch.sample.vm

import android.app.Application
import android.view.MenuItem
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.github.panpf.sketch.sample.R
import com.github.panpf.sketch.sample.appSettingsService
import com.github.panpf.sketch.sample.base.LifecycleAndroidViewModel
import com.github.panpf.sketch.sample.bean.DialogFragmentItemInfo
import com.github.panpf.sketch.sample.bean.LayoutMode
import com.github.panpf.sketch.sample.bean.MenuItemInfoGroup
import com.github.panpf.sketch.sample.bean.SwitchMenuItemInfo
import com.github.panpf.sketch.sample.ui.SettingsDialogFragment

class ListMenuViewModel(
    application1: Application,
    val showLayoutModeMenu: Boolean,
    val showPlayMenu: Boolean
) : LifecycleAndroidViewModel(application1) {

    class Factory(
        val application1: Application,
        val showLayoutModeMenu: Boolean,
        val showPlayMenu: Boolean
    ) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ListMenuViewModel(application1, showLayoutModeMenu, showPlayMenu) as T
        }
    }

    val menuList = MutableLiveData<List<MenuItemInfoGroup>>()

    init {
        menuList.postValue(assembleMenuList())
    }

    private fun assembleMenuList(): List<MenuItemInfoGroup> = buildList {
        add(MenuItemInfoGroup(buildList {
            if (showPlayMenu) {
                add(SwitchMenuItemInfo(
                    values = arrayOf(true, false),
                    initValue = application1.appSettingsService.disabledAnimatableDrawableInList.value,
                    titles = null,
                    iconResIds = arrayOf(
                        R.drawable.ic_pause,
                        R.drawable.ic_play,
                    ),
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS
                ) { _, newValue ->
                    application1.appSettingsService.disabledAnimatableDrawableInList.postValue(
                        newValue
                    )
                    menuList.postValue(menuList.value)
                })
            }

            if (showLayoutModeMenu) {
                add(SwitchMenuItemInfo(
                    values = arrayOf(
                        LayoutMode.GRID,
                        LayoutMode.STAGGERED_GRID,
                    ),
                    initValue = application1.appSettingsService.photoListLayoutMode.value,
                    titles = null,
                    iconResIds = arrayOf(
                        R.drawable.ic_layout_grid,
                        R.drawable.ic_layout_grid_staggered,
                    ),
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS
                ) { _, newValue ->
                    application1.appSettingsService.photoListLayoutMode.postValue(newValue)
                    menuList.postValue(menuList.value)
                })
            }

            add(
                DialogFragmentItemInfo(
                    title = "Settings",
                    iconResId = R.drawable.ic_settings,
                    showAsAction = MenuItem.SHOW_AS_ACTION_ALWAYS,
                    fragment = SettingsDialogFragment()
{'fraction_non_alphanumeric': 0.07259322274209556, 'fraction_numerical': 0.0022685382106904864, 'mean_word_length': 1.8103585657370518, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '4826091', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1722, 'n_words': 267}