Skip to content

Commit

Permalink
Merge pull request #624 from Syn-McJ/feat/voting-cast-vote
Browse files Browse the repository at this point in the history
feat(voting): cast a vote UI
  • Loading branch information
Syn-McJ authored Nov 21, 2023
2 parents 9290ff9 + 5aa3176 commit 3732d3b
Show file tree
Hide file tree
Showing 27 changed files with 1,019 additions and 103 deletions.
30 changes: 30 additions & 0 deletions DashWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "checkmark.circle.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "checkmark.circle@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "checkmark.circle@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "plus.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "plus@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "plus@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 66 additions & 19 deletions DashWallet/Sources/Models/Voting/DAO/UsernameRequestsDAO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import SQLite
// MARK: - UsernameRequestsDAO

protocol UsernameRequestsDAO {
func create(dto: UsernameRequest)
func all(onlyWithLinks: Bool) -> [UsernameRequest]
func duplicates(onlyWithLinks: Bool) -> [UsernameRequest]
func create(dto: UsernameRequest) async
func all(onlyWithLinks: Bool) async -> [UsernameRequest]
func duplicates(onlyWithLinks: Bool) async -> [UsernameRequest]
func get(by requestId: String) -> UsernameRequest?
func update(dto: UsernameRequest)
func update(dto: UsernameRequest) async
func delete(dto: UsernameRequest)
func vote(for requestIds: [String], voteIncrement: Int) async
func deleteAll()
}

Expand All @@ -38,7 +39,8 @@ class UsernameRequestsDAOImpl: NSObject, UsernameRequestsDAO {

private let queue = DispatchQueue(label: "org.dash.infrastructure.queue.username-requests-dao", attributes: .concurrent)

func create(dto: UsernameRequest) {
func create(dto: UsernameRequest) async {

do {
let usernameRequest = UsernameRequest.table.insert(or: .replace,
UsernameRequest.requestId <- dto.requestId,
Expand All @@ -48,15 +50,11 @@ class UsernameRequestsDAOImpl: NSObject, UsernameRequestsDAO {
UsernameRequest.link <- dto.link,
UsernameRequest.votes <- dto.votes,
UsernameRequest.isApproved <- dto.isApproved)
try db.run(usernameRequest)

try await execute(usernameRequest)
self.cache[dto.requestId] = dto
} catch {
print(error)
}

queue.async(flags: .barrier) { [weak self] in
self?.cache[dto.requestId] = dto
}
}

func get(by requestId: String) -> UsernameRequest? {
Expand Down Expand Up @@ -91,8 +89,8 @@ class UsernameRequestsDAOImpl: NSObject, UsernameRequestsDAO {
return v
}

func update(dto: UsernameRequest) {
create(dto: dto)
func update(dto: UsernameRequest) async {
await create(dto: dto)
}

func delete(dto: UsernameRequest) {
Expand All @@ -115,8 +113,10 @@ class UsernameRequestsDAOImpl: NSObject, UsernameRequestsDAO {
static let shared = UsernameRequestsDAOImpl()
}

// MARK: - Queries

extension UsernameRequestsDAOImpl {
func all(onlyWithLinks: Bool) -> [UsernameRequest] {
func all(onlyWithLinks: Bool) async -> [UsernameRequest] {
let linksParam = onlyWithLinks ? 1 : 0
let query = """
SELECT * FROM username_requests
Expand All @@ -126,15 +126,15 @@ extension UsernameRequestsDAOImpl {
"""

do {
return try self.execute(query: query)
return try await self.execute(query: query)
} catch {
print(error)
}

return []
}

func duplicates(onlyWithLinks: Bool) -> [UsernameRequest] {
func duplicates(onlyWithLinks: Bool) async -> [UsernameRequest] {
let linksParam = onlyWithLinks ? 1 : 0
let query = """
SELECT * FROM username_requests
Expand All @@ -146,15 +146,62 @@ extension UsernameRequestsDAOImpl {
"""

do {
return try self.execute(query: query)
return try await self.execute(query: query)
} catch {
print(error)
}

return []
}

private func execute<Item: RowDecodable>(query: String) throws -> [Item] {
try db.prepare(query).prepareRowIterator().map { Item(row: $0) }
func vote(for requestIds: [String], voteIncrement: Int) async {
let idsPlaceholder = requestIds.map { _ in "?" }.joined(separator: ", ")
let query = """
UPDATE username_requests
SET isApproved = 1, votes = votes + ?
WHERE requestId IN (\(idsPlaceholder))
"""

do {
let binding: [Binding?] = [voteIncrement] + requestIds
let _: [UsernameRequest] = try await self.execute(query: query, bindings: binding)
} catch {
print(error)
}
}

}

// MARK: - async / await

extension UsernameRequestsDAOImpl {
private func execute<Item: RowDecodable>(query: String, bindings: [Binding?] = []) async throws -> [Item] {
return try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global(qos: .utility).async { [weak self] in
guard let self = self else { return continuation.resume(returning: []) }

do {
let results = try self.db.prepare(query, bindings).prepareRowIterator().map { Item(row: $0) }
continuation.resume(returning: results)
} catch {
continuation.resume(throwing: error)
}
}
}
}

private func execute(_ query: Insert) async throws {
return try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global(qos: .utility).async { [weak self] in
guard let self = self else { return continuation.resume() }

do {
try db.run(query)
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}
}
23 changes: 23 additions & 0 deletions DashWallet/Sources/Models/Voting/MasternodeKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by Andrei Ashikhmin
// Copyright © 2023 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

struct MasternodeKey: Hashable {
var key: String
var ip: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,22 @@ final class ReceiveContentView: UIStackView {
func addressButtonAction() {
feedbackGenerator.notificationOccurred(.success)
model.copyAddressToPasteboard()
dw_showInfoHUD(withText: NSLocalizedString("Copied", comment: ""))
}

@IBAction
func qrButtonAction() {
feedbackGenerator.notificationOccurred(.success)
model.copyQRImageToPasteboard()
dw_showInfoHUD(withText: NSLocalizedString("Copied", comment: ""))
}

#if DASHPAY
@objc
private func copyUsernameAction() {
feedbackGenerator.notificationOccurred(.success)
model.copyUsernameToPasteboard()
dw_showInfoHUD(withText: NSLocalizedString("Copied", comment: ""))
}
#endif

Expand Down
70 changes: 70 additions & 0 deletions DashWallet/Sources/UI/Views/CustomHUDView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Created by Andrei Ashikhmin
// Copyright © 2023 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit

@objc(DWCustomHUDView)
class CustomHUDView: UIView {
let iconImageView: UIImageView
let textLabel: UILabel

override init(frame: CGRect) {
iconImageView = UIImageView(frame: .zero)
textLabel = UILabel(frame: .zero)

super.init(frame: frame)

iconImageView.translatesAutoresizingMaskIntoConstraints = false
iconImageView.contentMode = .scaleAspectFit

textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.textColor = .white
textLabel.font = .dw_regularFont(ofSize: 14)

addSubview(iconImageView)
addSubview(textLabel)

setupConstraints()

backgroundColor = UIColor.black
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupConstraints() {
NSLayoutConstraint.activate([
iconImageView.heightAnchor.constraint(equalToConstant: 18),
iconImageView.widthAnchor.constraint(equalToConstant: 18),
iconImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
iconImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),

textLabel.leadingAnchor.constraint(equalTo: iconImageView.trailingAnchor, constant: 8),
textLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
textLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0)
])
}

@objc func setText(_ text: String) {
textLabel.text = text
}

@objc func setIconImage(_ image: UIImage) {
iconImageView.image = image
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ extension UINavigationController {
viewControllers.append(controller)
setViewControllers(viewControllers, animated: animated)
}

func popToViewController<T>(ofType type: T.Type, animated: Bool) {
if let controller = controller(by: type) {
popToViewController(controller, animated: animated)
}
}
}

extension UIViewController {
Expand Down
2 changes: 1 addition & 1 deletion DashWallet/Sources/UI/Views/UIButton+Dash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ final class GrayButton: ActivityIndicatorButton {

var container = incoming
container.foregroundColor = foregroundColor
container.font = .title3
container.font = .dw_mediumFont(ofSize: 15)
return container
}
}
Expand Down
24 changes: 18 additions & 6 deletions DashWallet/Sources/UI/Views/UIView+DWHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <objc/runtime.h>

#import "DWUIKit.h"
#import "dashwallet-Swift.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -44,13 +45,24 @@ - (void)dw_hideProgressHUD {
}

- (void)dw_showInfoHUDWithText:(NSString *)text {
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.removeFromSuperViewOnHide = YES;
hud.mode = MBProgressHUDModeText;
hud.label.font = [UIFont dw_fontForTextStyle:UIFontTextStyleCaption1];
hud.label.adjustsFontForContentSizeCategory = YES;
hud.label.numberOfLines = 0;
hud.label.text = text;
hud.mode = MBProgressHUDModeCustomView;

DWCustomHUDView *customView = [[DWCustomHUDView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), 28)];
[customView setText:text];
[customView setIconImage:[UIImage imageNamed:@"checkmark.circle.white"]];

hud.customView = customView;
hud.bezelView.layer.cornerRadius = 8.0f;
hud.bezelView.clipsToBounds = YES;
hud.bezelView.color = [UIColor colorWithWhite:0.0f alpha:0.9f];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;

CGFloat yOffset = CGRectGetHeight(self.bounds) / 2 - CGRectGetHeight(customView.bounds) - 35;
hud.offset = CGPointMake(0.0f, yOffset);
hud.userInteractionEnabled = NO;

__weak typeof(self) weakSelf = self;
hud.completionBlock = ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
Expand Down
Loading

0 comments on commit 3732d3b

Please sign in to comment.