ContactFragment.kt (4649B)
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/contact/ContactFragment.kt 23 */ 24 25 package org.gnunet.gnunetmessenger.ui.contact 26 27 import android.graphics.BitmapFactory 28 import android.net.Uri 29 import android.os.Bundle 30 import android.view.LayoutInflater 31 import android.view.View 32 import android.view.ViewGroup 33 import android.widget.Toast 34 import androidx.activity.result.contract.ActivityResultContracts 35 import androidx.navigation.fragment.findNavController 36 import androidx.navigation.fragment.navArgs 37 import org.gnunet.gnunetmessenger.MainActivity 38 import org.gnunet.gnunetmessenger.R 39 import org.gnunet.gnunetmessenger.model.ChatContact 40 import org.gnunet.gnunetmessenger.service.GnunetChat 41 import org.gnunet.gnunetmessenger.ui.BaseProfileFragment 42 import org.gnunet.gnunetmessenger.util.AvatarStorageUtil 43 44 class ContactFragment : BaseProfileFragment() { 45 46 private val args: ContactFragmentArgs by navArgs() 47 private lateinit var contact: ChatContact 48 private lateinit var gnunetChat: GnunetChat 49 private var selectedAvatarUri: Uri? = null 50 51 private val avatarImageRequest = 52 registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> 53 uri?.let { 54 selectedAvatarUri = it 55 val key = gnunetChat.getContactKey(contact) 56 AvatarStorageUtil.saveAvatarFromUri(requireContext(), uri, key) 57 avatarImage.setImageURI(uri) 58 } 59 } 60 61 override fun onCreate(savedInstanceState: Bundle?) { 62 super.onCreate(savedInstanceState) 63 val activity = requireActivity() as MainActivity 64 gnunetChat = activity.getGnunetChatInstance() 65 contact = args.contact 66 } 67 68 override fun onCreateView( 69 inflater: LayoutInflater, container: ViewGroup?, 70 savedInstanceState: Bundle? 71 ): View { 72 return inflater.inflate(R.layout.fragment_profile_base, container, false) 73 } 74 75 override fun setupUI() { 76 nameEdit.visibility = View.GONE 77 saveNameButton.visibility = View.GONE 78 nameText.text = contact.name 79 80 val publicKey = gnunetChat.getContactKey(contact) 81 82 val avatar = AvatarStorageUtil.loadAvatar(requireContext(), publicKey) 83 avatarImage.setImageBitmap( 84 avatar ?: BitmapFactory.decodeResource(resources, R.drawable.ic_account_circle) 85 ) 86 87 avatarImage.setOnClickListener { 88 avatarImageRequest.launch("image/*") 89 } 90 91 publicKeyText.visibility = View.VISIBLE 92 qrCodeImage.visibility = View.GONE 93 publicKeyText.text = publicKey 94 95 shareAttributesButton.visibility = View.VISIBLE 96 blockContactButton.visibility = View.VISIBLE 97 98 /*listAttributesButton.setOnClickListener { 99 val action = ContactFragmentDirections 100 .actionContactFragmentToAttributeListFragment(contact, editable = false) 101 findNavController().navigate(action) 102 }*/ 103 104 shareAttributesButton.setOnClickListener { 105 val action = ContactFragmentDirections 106 .actionContactFragmentToShareAttributesFragment(contact) 107 findNavController().navigate(action) 108 } 109 110 blockContactButton.setOnClickListener { 111 val blocked = gnunetChat.isContactBlocked(contact) 112 gnunetChat.setContactBlocked(contact, !blocked) 113 contact.blocked = !blocked 114 val msg = if (blocked) "Unblocked" else "Blocked" 115 Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show() 116 } 117 118 shareIdentityButton.setOnClickListener { 119 val action = ContactFragmentDirections.actionContactFragmentToLobbyDisplayFragment( 120 lobbyId = publicKey, 121 lifetime = "0" 122 ) 123 findNavController().navigate(action) 124 } 125 } 126 }