Skip to content

Commit

Permalink
politeia: load proposal description in details page (#748)
Browse files Browse the repository at this point in the history
Proposal descriptions are now loaded from the server if the description
is outdated or not yet saved by dcrlibwallet.
  • Loading branch information
beansgum committed May 11, 2021
1 parent 6486d46 commit 6306864
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Decred Wallet/DcrlibwalletTypes/Politeia.swift
Expand Up @@ -23,6 +23,7 @@ struct Politeia: Codable {
var version: String
var publishedat: Date
var indexfile: String
var fileversion: String
var votestatus: PoliteiaVoteStatus
var voteapproved: Bool
var yesvotes: Int32
Expand All @@ -33,7 +34,7 @@ struct Politeia: Codable {
var yesPercent: Float

private enum CodingKeys : String, CodingKey {
case ID, token, category, name, state, status, timestamp, userid, username, numcomments, version, publishedat, indexfile, votestatus, voteapproved, yesvotes, novotes, eligibletickets, quorumpercentage, passpercentage
case ID, token, category, name, state, status, timestamp, userid, username, numcomments, version, publishedat, indexfile, fileversion, votestatus, voteapproved, yesvotes, novotes, eligibletickets, quorumpercentage, passpercentage
}

init(from decoder: Decoder) throws {
Expand All @@ -50,6 +51,7 @@ struct Politeia: Codable {
self.version = try! values.decode(String.self, forKey: .version)
self.publishedat = try! values.decode(Date.self, forKey: .publishedat)
self.indexfile = try! values.decode(String.self, forKey: .indexfile)
self.fileversion = try! values.decode(String.self, forKey: .fileversion)
self.status = try! values.decode(Int32.self, forKey: .status)
self.voteapproved = try! values.decode(Bool.self, forKey: .voteapproved)
self.yesvotes = try! values.decode(Int32.self, forKey: .yesvotes)
Expand Down Expand Up @@ -84,6 +86,7 @@ struct Politeia: Codable {
self.version = proposal.version
self.publishedat = Date(milliseconds: Int(proposal.publishedAt))
self.indexfile = proposal.indexFile
self.fileversion = proposal.indexFileVersion
self.status = proposal.status
self.voteapproved = proposal.voteApproved
self.yesvotes = proposal.yesVotes
Expand Down
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -312,6 +312,16 @@
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Kg1-Te-2Lj">
<rect key="frame" x="0.0" y="0.0" width="414" height="818"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="PiB-64-UOn">
<rect key="frame" x="0.0" y="200" width="414" height="33"/>
<color key="backgroundColor" red="0.95294117647058818" green="0.96078431372549022" blue="0.96470588235294119" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" animating="YES" style="medium" translatesAutoresizingMaskIntoConstraints="NO" id="Rjd-zF-bsX">
<rect key="frame" x="0.0" y="200" width="414" height="20"/>
</activityIndicatorView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="RRO-l4-9Lf">
<rect key="frame" x="0.0" y="0.0" width="414" height="200"/>
<subviews>
Expand Down Expand Up @@ -435,23 +445,19 @@
<constraint firstAttribute="bottom" secondItem="Llm-YX-JMb" secondAttribute="bottom" constant="10" id="bYv-u1-wQ4"/>
</constraints>
</view>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="PiB-64-UOn">
<rect key="frame" x="0.0" y="200" width="414" height="33"/>
<color key="backgroundColor" red="0.95294117647058818" green="0.96078431372549022" blue="0.96470588235294119" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
</subviews>
<constraints>
<constraint firstItem="iLT-bZ-oPF" firstAttribute="trailing" secondItem="RRO-l4-9Lf" secondAttribute="trailing" id="01n-43-t8X"/>
<constraint firstAttribute="bottom" secondItem="PiB-64-UOn" secondAttribute="bottom" id="AEZ-Ae-hMh"/>
<constraint firstItem="RRO-l4-9Lf" firstAttribute="leading" secondItem="iLT-bZ-oPF" secondAttribute="leading" id="ATY-o7-SbS"/>
<constraint firstItem="RRO-l4-9Lf" firstAttribute="top" secondItem="Kg1-Te-2Lj" secondAttribute="top" id="PkE-Dc-Bal"/>
<constraint firstItem="Rjd-zF-bsX" firstAttribute="top" secondItem="RRO-l4-9Lf" secondAttribute="bottom" id="Poy-wx-ijd"/>
<constraint firstItem="PiB-64-UOn" firstAttribute="leading" secondItem="Kg1-Te-2Lj" secondAttribute="leading" id="SC0-RP-CBm"/>
<constraint firstAttribute="trailing" secondItem="PiB-64-UOn" secondAttribute="trailing" id="T4E-ly-ksK"/>
<constraint firstItem="PiB-64-UOn" firstAttribute="top" secondItem="RRO-l4-9Lf" secondAttribute="bottom" id="VlK-c4-rVy"/>
<constraint firstAttribute="trailing" secondItem="Rjd-zF-bsX" secondAttribute="trailing" id="ZLl-Xg-hJ7"/>
<constraint firstItem="PiB-64-UOn" firstAttribute="width" secondItem="Kg1-Te-2Lj" secondAttribute="width" id="nbS-NE-Q5W"/>
<constraint firstItem="Rjd-zF-bsX" firstAttribute="leading" secondItem="Kg1-Te-2Lj" secondAttribute="leading" id="yIY-Fw-yqQ"/>
</constraints>
<viewLayoutGuide key="contentLayoutGuide" id="2vf-Nx-SHg"/>
<viewLayoutGuide key="frameLayoutGuide" id="iLT-bZ-oPF"/>
Expand All @@ -477,6 +483,7 @@
</view>
<navigationItem key="navigationItem" id="biV-4F-fBD"/>
<connections>
<outlet property="contentLoadingIndicator" destination="Rjd-zF-bsX" id="dVn-Cy-xaL"/>
<outlet property="contentTextView" destination="PiB-64-UOn" id="fv8-PP-uGp"/>
<outlet property="countCommentLabel" destination="MTD-9X-JzK" id="1j8-wb-6hp"/>
<outlet property="nameLabel" destination="LKv-X6-yO6" id="M3Y-sR-f6E"/>
Expand Down
Expand Up @@ -20,11 +20,14 @@ class PoliteiaDetailController: UIViewController {
@IBOutlet weak var noPercentLabel: UILabel!
@IBOutlet weak var percentView: PlainHorizontalProgressBar!
@IBOutlet weak var contentTextView: UITextView!
@IBOutlet weak var contentLoadingIndicator: UIActivityIndicatorView!

var politeia: Politeia?
var isNotificationOpen: Bool = false
var proposalId: String?

private var multiWallet = WalletLoader.shared.multiWallet!

override func viewDidLoad() {
super.viewDidLoad()
self.setup()
Expand Down Expand Up @@ -77,7 +80,7 @@ class PoliteiaDetailController: UIViewController {
func getDetailPoliteia() {
guard let idstr = self.proposalId, let id = Int(idstr) else { return }
DispatchQueue.global(qos: .userInitiated).async {
let result = WalletLoader.shared.multiWallet.politeia?.detailPoliteia(id)
let result = self.multiWallet.politeia?.detailPoliteia(id)
DispatchQueue.main.async {
if let poli = result!.0 {
self.politeia = poli
Expand All @@ -96,23 +99,26 @@ class PoliteiaDetailController: UIViewController {
self.yesPercentLabel.isHidden = true
self.noPercentLabel.isHidden = true
}

@objc func shareButtonTapped(_ sender: Any) {
guard let token = self.politeia?.token, let name = self.politeia?.name else {return}
guard let urlString = URL(string: "http://proposals.decred.org/proposals/\(token)") else {return}
guard let urlString = URL(string: "http://proposals.decred.org/record/\(token)") else {return}
let items: [Any] = [name, urlString]
let activity = UIActivityViewController(activityItems: items, applicationActivities: nil)
present(activity, animated: true)
}

@objc func openButtonTapped(_ sender: Any) {
guard let token = self.politeia?.token else {return}
let urlString = "http://proposals.decred.org/proposals/\(token)"
let urlString = "http://proposals.decred.org/record/\(token)"
if let url = URL(string: urlString) {
UIApplication.shared.open(url)
}
}

func displayData() {
guard let politeia = self.politeia else {return}

self.titleLabel.text = politeia.name
self.nameLabel.text = politeia.username
let publishAge = Int64(Date().timeIntervalSince1970) - politeia.timestamp
Expand All @@ -122,6 +128,7 @@ class PoliteiaDetailController: UIViewController {
self.versionLabel.text = String(format: LocalizedStrings.politeiaVersion, politeia.version)
self.statusLabel.text = politeia.votestatus.description
self.statusLabel.backgroundColor = Utils.politeiaColorBGStatus(politeia.votestatus)

if politeia.votestatus == .APPROVED || politeia.votestatus == .REJECT {
self.percentView.isHidden = false
self.yesPercentLabel.isHidden = false
Expand All @@ -131,10 +138,23 @@ class PoliteiaDetailController: UIViewController {
self.yesPercentLabel.text = "Yes: \(politeia.yesvotes) (\(yesPercent.round(decimals: 2))%)"
self.noPercentLabel.text = "No: \(politeia.novotes) (\(politeia.novotes > 0 ? (100 - yesPercent).round(decimals: 2) : 0)%)"
}
if let data = self.politeia?.indexfile {
let dataContent = Data(base64Encoded: data)!
let content = String(data: dataContent, encoding: .utf8)
self.contentTextView.text = content

if politeia.indexfile != "" && politeia.fileversion == politeia.version {
self.contentTextView.text = politeia.indexfile
} else {
self.contentLoadingIndicator.isHidden = false
DispatchQueue.global(qos: .userInitiated).async {
var error: NSError?
let description = self.multiWallet.politeia?.fetchProposalDescription(politeia.token, error: &error)
DispatchQueue.main.async {
self.contentLoadingIndicator.isHidden = true
if error != nil {
self.contentTextView.text = error?.localizedDescription
} else {
self.contentTextView.text = description
}
}
}
}
}
}

0 comments on commit 6306864

Please sign in to comment.