AttributeListFragment.kt (3663B)
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/account/AttributeListFragment.kt 23 */ 24 25 package org.gnunet.gnunetmessenger.ui.account 26 27 import android.os.Bundle 28 import androidx.fragment.app.Fragment 29 import org.gnunet.gnunetmessenger.ui.adapters.AttributeAdapter 30 import android.view.LayoutInflater 31 import android.view.View 32 import android.view.ViewGroup 33 import androidx.navigation.fragment.navArgs 34 import androidx.recyclerview.widget.LinearLayoutManager 35 import org.gnunet.gnunetmessenger.MainActivity 36 import org.gnunet.gnunetmessenger.databinding.FragmentAttributeListBinding 37 38 39 class AttributeListFragment : Fragment() { 40 41 private val args: AttributeListFragmentArgs by navArgs() 42 private lateinit var binding: FragmentAttributeListBinding 43 private val attributes = mutableListOf<Pair<String, String>>() 44 private lateinit var adapter: AttributeAdapter 45 46 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { 47 val activity = activity as MainActivity 48 val gnunetChat = activity.getGnunetChatInstance() 49 val handle = activity.getChatHandle() 50 val contact = args.contact 51 val editable = args.editable 52 53 binding = FragmentAttributeListBinding.inflate(inflater, container, false) 54 55 56 adapter = AttributeAdapter(attributes) 57 if (editable) 58 gnunetChat.getAttributes(handle) { key, value -> 59 attributes.add(key to value) 60 adapter.notifyItemInserted(attributes.size - 1) 61 } 62 else 63 gnunetChat.getContactAttributes(contact!!) { key, value -> 64 attributes.add(key to value) 65 adapter.notifyItemInserted(attributes.size - 1) 66 } 67 binding.attributeList.layoutManager = LinearLayoutManager(requireContext()) 68 binding.attributeList.adapter = adapter 69 70 if (editable) { 71 binding.editKey.visibility = View.VISIBLE 72 binding.editValue.visibility = View.VISIBLE 73 binding.btnAddAttribute.visibility = View.VISIBLE 74 75 binding.btnAddAttribute.setOnClickListener { 76 val key = binding.editKey.text.toString() 77 val value = binding.editValue.text.toString() 78 if (key.isNotBlank()) { 79 attributes.add(Pair(key, value)) 80 adapter.notifyItemInserted(attributes.size - 1) 81 gnunetChat.setAttribute(handle, key, value) 82 binding.editKey.text.clear() 83 binding.editValue.text.clear() 84 } 85 } 86 } else { 87 binding.editKey.visibility = View.GONE 88 binding.editValue.visibility = View.GONE 89 binding.btnAddAttribute.visibility = View.GONE 90 } 91 92 return binding.root 93 } 94 }