ProfilePictureRecipientCell.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. protocol ProfilePictureRecipientCellDelegate: AnyObject {
  22. func valueChanged(_ cell: ProfilePictureRecipientCell)
  23. }
  24. class ProfilePictureRecipientCell: UITableViewCell {
  25. @IBOutlet weak var profilePictureRecipientLabel: UILabel!
  26. @IBOutlet weak var profilePictureRecipientSwitch: UISwitch!
  27. var identity: String? {
  28. didSet {
  29. guard let identity = identity else {
  30. profilePictureRecipientSwitch.isOn = false
  31. return
  32. }
  33. if UserSettings.shared().profilePictureContactList.contains(where: { $0 as! String == identity }) {
  34. profilePictureRecipientSwitch.isOn = true
  35. } else {
  36. profilePictureRecipientSwitch.isOn = false
  37. }
  38. }
  39. }
  40. weak var delegate : ProfilePictureRecipientCellDelegate?
  41. override func awakeFromNib() {
  42. super.awakeFromNib()
  43. profilePictureRecipientLabel.text = BundleUtil.localizedString(forKey: "profile_picture_recipient")
  44. }
  45. @IBAction func profilePictureRecipientSwitchChanged(_ sender: UIButton) {
  46. guard let identity = identity else { return }
  47. let contactIdentities = UserSettings.shared().profilePictureContactList
  48. let selectedContacts = NSMutableSet(array: contactIdentities!)
  49. if profilePictureRecipientSwitch.isOn {
  50. selectedContacts.add(identity)
  51. } else {
  52. selectedContacts.remove(identity)
  53. }
  54. UserSettings.shared().profilePictureContactList = selectedContacts.allObjects
  55. if let delegate = delegate {
  56. delegate.valueChanged(self)
  57. }
  58. }
  59. }