owenclave / app/src/main/java/io/nekohasekai/sagernet/ui/ToolsFragment.kt

github - dev - back to tree - upstream
/****************************************************************************** * Copyright (C) 2021 by nekohasekai * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * ******************************************************************************/ package io.nekohasekai.sagernet.ui import android.os.Bundle import android.view.View import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.viewpager2.adapter.FragmentStateAdapter import com.google.android.material.tabs.TabLayoutMediator import io.nekohasekai.sagernet.R import io.nekohasekai.sagernet.databinding.LayoutToolsBinding class ToolsFragment : ToolbarFragment(R.layout.layout_tools) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) toolbar.setTitle(R.string.menu_tools) ViewCompat.setOnApplyWindowInsetsListener(view.findViewById(R.id.tools_tab)) { v, insets -> val bars = insets.getInsets( WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout() ) v.updatePadding( left = bars.left, right = bars.right, ) insets } val tools = mutableListOf() tools.add(NetworkFragment()) tools.add(BackupFragment()) val binding = LayoutToolsBinding.bind(view) binding.toolsPager.adapter = ToolsAdapter(tools) TabLayoutMediator(binding.toolsTab, binding.toolsPager) { tab, position -> tab.text = when (position) { 0 -> getString(R.string.tools_network) else -> getString(R.string.backup) } tab.view.setOnLongClickListener { // clear toast true } }.attach() (requireActivity() as? MainActivity)?.onBackPressedCallback?.isEnabled = true } inner class ToolsAdapter(val tools: List) : FragmentStateAdapter(this) { override fun getItemCount() = tools.size override fun createFragment(position: Int) = tools[position] } }