SettingsFragment.kt (3844B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2025 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author t3sserakt 22 * @file GNUnetMessenger/app/src/main/java/org/gnunet/gnunetmessenger/ui/SettingsFragment.kt 23 */ 24 25 package org.gnunet.gnunetmessenger.ui 26 27 import android.os.Bundle 28 import android.view.LayoutInflater 29 import android.view.View 30 import android.view.ViewGroup 31 import android.widget.CompoundButton 32 import androidx.fragment.app.Fragment 33 import androidx.fragment.app.activityViewModels 34 import androidx.recyclerview.widget.LinearLayoutManager 35 import org.gnunet.gnunetmessenger.MainActivity 36 import org.gnunet.gnunetmessenger.databinding.FragmentSettingsBinding 37 import org.gnunet.gnunetmessenger.model.ChatContact 38 import org.gnunet.gnunetmessenger.model.ChatSummary 39 import org.gnunet.gnunetmessenger.service.GnunetChat 40 import org.gnunet.gnunetmessenger.ui.adapters.BlockedContactAdapter 41 import org.gnunet.gnunetmessenger.viewmodel.ChatOverviewViewModel 42 43 class SettingsFragment : Fragment() { 44 45 private var _binding: FragmentSettingsBinding? = null 46 private val binding get() = _binding!! 47 48 private lateinit var gnunetChat: GnunetChat 49 private lateinit var adapter: BlockedContactAdapter 50 51 override fun onCreateView( 52 inflater: LayoutInflater, container: ViewGroup?, 53 savedInstanceState: Bundle? 54 ): View { 55 _binding = FragmentSettingsBinding.inflate(inflater, container, false) 56 gnunetChat = (requireActivity() as MainActivity).getGnunetChatInstance() 57 return binding.root 58 } 59 60 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 61 val prefs = requireContext().getSharedPreferences("settings", 0) 62 val viewModel: ChatOverviewViewModel by activityViewModels() 63 64 binding.switchNotifications.isChecked = prefs.getBoolean("notifications", true) 65 binding.switchReceipts.isChecked = prefs.getBoolean("receipts", true) 66 67 binding.switchNotifications.setOnCheckedChangeListener { _: CompoundButton, isChecked: Boolean -> 68 prefs.edit().putBoolean("notifications", isChecked).apply() 69 } 70 71 binding.switchReceipts.setOnCheckedChangeListener { _: CompoundButton, isChecked: Boolean -> 72 prefs.edit().putBoolean("receipts", isChecked).apply() 73 } 74 75 val blockedContacts = getBlockedContacts(viewModel) // returns List<ChatContact> 76 adapter = BlockedContactAdapter(blockedContacts) 77 78 binding.blockedContactsList.layoutManager = LinearLayoutManager(requireContext()) 79 binding.blockedContactsList.adapter = adapter 80 } 81 82 private fun getBlockedContacts(viewModel: ChatOverviewViewModel): List<ChatContact> { 83 val blockedContacts = mutableListOf<ChatContact>() 84 viewModel.chats.value?.forEach { chatSummary: ChatSummary -> 85 if (!chatSummary.chatContext.isGroup && 86 !chatSummary.chatContext.isPlatform && 87 chatSummary.contact!!.blocked) 88 blockedContacts.add(chatSummary.contact) 89 } 90 91 return blockedContacts 92 } 93 94 override fun onDestroyView() { 95 super.onDestroyView() 96 _binding = null 97 } 98 }