Skip to content

Commit

Permalink
- #61 Fixed old avatar displaying in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mehroozkhan committed Aug 24, 2021
1 parent e63a876 commit 088cb0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
22 changes: 11 additions & 11 deletions Snikket/Account.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -930,17 +930,17 @@
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" restorationIdentifier="PhoneAddCell" selectionStyle="default" indentationWidth="10" reuseIdentifier="PhoneAddCell" id="PkJ-5b-L9A">
<rect key="frame" x="0.0" y="383" width="600" height="49"/>
<rect key="frame" x="0.0" y="383" width="600" height="49.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="PkJ-5b-L9A" id="Kju-6c-Lhj">
<rect key="frame" x="0.0" y="0.0" width="600" height="49"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="49.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="contactAdd" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9jf-N5-mdI">
<rect key="frame" x="20" y="14.000000000000002" width="20.666666666666671" height="21.666666666666671"/>
<rect key="frame" x="20" y="13.999999999999998" width="20.666666666666671" height="22.333333333333329"/>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" text="add phone" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jAE-vq-Vfj">
<rect key="frame" x="48.666666666666686" y="14.000000000000002" width="531.33333333333326" height="20.666666666666671"/>
<rect key="frame" x="48.666666666666686" y="13.999999999999998" width="531.33333333333326" height="21.333333333333329"/>
<accessibility key="accessibilityConfiguration">
<accessibilityTraits key="traits" link="YES" staticText="YES"/>
</accessibility>
Expand All @@ -961,7 +961,7 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="EmailEditCell" rowHeight="44" id="GEd-A7-c44" customClass="VCardEditEmailTableViewCell" customModule="Snikket" customModuleProvider="target">
<rect key="frame" x="0.0" y="432" width="600" height="44"/>
<rect key="frame" x="0.0" y="432.66666793823242" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="GEd-A7-c44" id="3nQ-Uq-yP5">
<rect key="frame" x="0.0" y="0.0" width="600" height="44"/>
Expand Down Expand Up @@ -1001,7 +1001,7 @@
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="EmailAddCell" rowHeight="51.666667938232422" id="l4b-9l-fds">
<rect key="frame" x="0.0" y="476" width="600" height="51.666667938232422"/>
<rect key="frame" x="0.0" y="476.66666793823242" width="600" height="51.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="l4b-9l-fds" id="a6e-BR-Tpz">
<rect key="frame" x="0.0" y="0.0" width="600" height="51.666667938232422"/>
Expand Down Expand Up @@ -1032,17 +1032,17 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="AddressAddCell" id="mi6-7L-CB9">
<rect key="frame" x="0.0" y="527.66666793823242" width="600" height="49"/>
<rect key="frame" x="0.0" y="528.33333587646484" width="600" height="49.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="mi6-7L-CB9" id="Nkf-ag-U8r">
<rect key="frame" x="0.0" y="0.0" width="600" height="49"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="49.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="contactAdd" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cHr-1G-C3e">
<rect key="frame" x="20" y="14.000000000000002" width="20.666666666666671" height="21.666666666666671"/>
<rect key="frame" x="20" y="13.999999999999998" width="20.666666666666671" height="22.333333333333329"/>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" text="add address" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fRX-fN-yOj">
<rect key="frame" x="48.666666666666686" y="14.000000000000002" width="531.33333333333326" height="20.666666666666671"/>
<rect key="frame" x="48.666666666666686" y="13.999999999999998" width="531.33333333333326" height="21.333333333333329"/>
<accessibility key="accessibilityConfiguration">
<accessibilityTraits key="traits" link="YES" staticText="YES"/>
</accessibility>
Expand All @@ -1063,7 +1063,7 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="AddressEditCell" id="Elb-gV-k9q" customClass="VCardEditAddressTableViewCell" customModule="Snikket" customModuleProvider="target">
<rect key="frame" x="0.0" y="576.66666793823242" width="600" height="132"/>
<rect key="frame" x="0.0" y="578.00000381469727" width="600" height="132"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Elb-gV-k9q" id="b8O-NZ-4fp">
<rect key="frame" x="0.0" y="0.0" width="600" height="132"/>
Expand Down
48 changes: 21 additions & 27 deletions Snikket/vcard/VCardEditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel

var xmppService: XmppService!;

var avatarImage: UIImage?
var displayname = ""
var account: BareJID!;
var vcard: VCard!;
Expand All @@ -37,12 +38,8 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel
xmppService = (UIApplication.shared.delegate as! AppDelegate).xmppService;
super.viewDidLoad()

// Do any additional setup after loading the view.
vcard = xmppService.dbVCardsCache.getVCard(for: account) ?? VCard();
if vcard != nil {
tableView.reloadData();
}

avatarImage = AvatarManager.instance.avatar(for: account, on: account)
self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
Expand All @@ -67,17 +64,7 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "AvatarEditCell") as! VCardAvatarEditCell;
cell.avatarView.set(name: nil, avatar: nil, orDefault: AvatarManager.instance.defaultAvatar);
if let photo = vcard.photos.first {
xmppService.dbVCardsCache.fetchPhoto(photo: photo) { (photoData) in
DispatchQueue.main.async {
if let photoData = photoData, let image = UIImage(data: photoData) {
cell.avatarView.set(name: nil, avatar: image, orDefault: AvatarManager.instance.defaultAvatar);
} else {
cell.avatarView.set(name: nil, avatar: nil, orDefault: AvatarManager.instance.defaultAvatar);
}
}
}
}
cell.avatarView.set(name: nil, avatar: avatarImage, orDefault: AvatarManager.instance.defaultAvatar)
if isUpdatingAvatar { cell.spinner.startAnimating() }
else { cell.spinner.stopAnimating() }
cell.updateCornerRadius();
Expand Down Expand Up @@ -198,16 +185,19 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel
if data.count > 72000 { // 72KB
MediaHelper.resizeTo(image: photo, targetBytes: 72000) { image in
if let image = image, let data = image.pngData() {
self.publishAvatar(data: data)
self.vcard.photos = [VCard.Photo(type: "image/png", binval: data.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)))]
self.showAvatarSpinner(show: true)
self.publishAvatar(data: data)
self.avatarImage = photo
self.tableView.reloadData()
} else {
print("failed to resize")
}
}
} else {
self.showAvatarSpinner(show: true)
self.publishAvatar(data: data)
self.vcard.photos = [VCard.Photo(type: "image/png", binval: data.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)))]
self.avatarImage = photo
self.tableView.reloadData()
}
}

Expand All @@ -229,14 +219,10 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel
question.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in

pepUserAvatarModule.publishAvatar(data: data, mimeType: "image/png", onSuccess: {
print("PEP: user avatar published");

self.showAvatarSpinner(show: false)

let avatarHash = Digest.sha1.digest(toHex: data);
let presenceModule: PresenceModule = client.modulesManager.getModule(PresenceModule.ID)!;
let x = Element(name: "x", xmlns: "vcard-temp:x:update");
x.addChild(Element(name: "photo", cdata: avatarHash));
presenceModule.setPresence(show: .online, status: nil, priority: nil, additionalElements: [x]);
self.publishAvatarHash(data: data, client: client)

}, onError: { (errorCondition, pubsubErrorCondition) in
DispatchQueue.main.async {
let alert = UIAlertController(title: "Error", message: "User avatar publication failed.\nReason: " + ((pubsubErrorCondition?.rawValue ?? errorCondition?.rawValue) ?? "unknown"), preferredStyle: .alert);
Expand Down Expand Up @@ -264,6 +250,14 @@ class VCardEditViewController: UITableViewController, UIImagePickerControllerDel
}
}
}

func publishAvatarHash(data: Data, client: XMPPClient) {
let avatarHash = Digest.sha1.digest(toHex: data)
let presenceModule: PresenceModule = client.modulesManager.getModule(PresenceModule.ID)!
let x = Element(name: "x", xmlns: "vcard-temp:x:update")
x.addChild(Element(name: "photo", cdata: avatarHash))
presenceModule.setPresence(show: .online, status: nil, priority: nil, additionalElements: [x])
}
}

// Helper function inserted by Swift 4.2 migrator.
Expand Down

0 comments on commit 088cb0e

Please sign in to comment.