Repository: skyline-emu/skyline
File: app/src/main/java/emu/skyline/MainActivity.kt (Kotlin)

Code before:
                            add(AppItem(entry))
                        }
                    }
                }
            }
        } else {
            val gameList = mutableListOf<AppEntry>()
            appEntries?.let { entries ->
                val formats = formatFilter?.let { listOf(it) } ?: formatOrder
                for (format in formats) {
                    entries[format]?.let {
                        it.forEach { entry -> gameList.add(entry) }
                    }
                }
            }
            for (entry in sortGameList(gameList.toList())) {
                add(AppItem(entry))
            }
        }
    }

    private fun sortGameList(gameList : List<AppEntry>) : MutableList<AppEntry> {
        val sortedApps : MutableList<AppEntry> = mutableListOf<AppEntry>()
        gameList.forEach { entry -> sortedApps.add(entry) }
        when (preferenceSettings.sortAppsBy) {
            1 -> sortedApps.sortByDescending { it.name }
            else -> sortedApps.sortBy { it.name }
        }
        return sortedApps
    }

    private fun handleState(state : MainState) = when (state) {
        MainState.Loading -> {
            binding.refreshIcon.apply { animate().rotation(rotation - 180f) }
            binding.swipeRefreshLayout.isRefreshing = true
        }

        is MainState.Loaded -> {
            binding.swipeRefreshLayout.isRefreshing = false

            appEntries = state.items
            populateAdapter()
        }

        is MainState.Error -> Snackbar.make(findViewById(android.R.id.content), getString(R.string.error) + ": ${state.ex.localizedMessage}", Snackbar.LENGTH_SHORT).show()
    }

    private fun selectStartGame(appItem : AppItem) {
        if (binding.swipeRefreshLayout.isRefreshing) return

        if (preferenceSettings.selectAction) {

Review comment (suggestion):
Avoid using magic numbers, instead create an `enum class` with the possible sorting methods and use those in the `when` block.

Code after:
                val formats = formatFilter?.let { listOf(it) } ?: formatOrder
                for (format in formats) {
                    entries[format]?.let {
                        add(HeaderItem(format.name))
                        for (entry in sortGameList(it)) {
                            add(AppItem(entry))
                        }
                    }
                }
            }
        } else {
            val gameList = mutableListOf<AppEntry>()
            appEntries?.let { entries ->
                val formats = formatFilter?.let { listOf(it) } ?: formatOrder
                for (format in formats) {
                    entries[format]?.let {
                        it.forEach { entry -> gameList.add(entry) }
                    }
                }
            }
            for (entry in sortGameList(gameList.toList())) {
                add(AppItem(entry))
            }
        }
    }

    private fun sortGameList(gameList : List<AppEntry>) : List<AppEntry> {
        val sortedApps : MutableList<AppEntry> = mutableListOf<AppEntry>()
        gameList.forEach { entry -> sortedApps.add(entry) }
        when (preferenceSettings.sortAppsBy) {
            SortingOrder.AlphabeticalAsc.ordinal -> sortedApps.sortBy { it.name }
            SortingOrder.AlphabeticalDesc.ordinal -> sortedApps.sortByDescending { it.name }
        }
        return sortedApps
    }

    private fun handleState(state : MainState) = when (state) {
        MainState.Loading -> {
            binding.refreshIcon.apply { animate().rotation(rotation - 180f) }
            binding.swipeRefreshLayout.isRefreshing = true
        }

        is MainState.Loaded -> {
            binding.swipeRefreshLayout.isRefreshing = false

            appEntries = state.items
            populateAdapter()
        }

        is MainState.Error -> Snackbar.make(findViewById(android.R.id.content), getString(R.string.error) + ": ${state.ex.localizedMessage}", Snackbar.LENGTH_SHORT).show()
    }