Skip to content

Commit

Permalink
Merge pull request #615 from Syn-McJ/feature/voting-list
Browse files Browse the repository at this point in the history
feat(voting): list of username requests
  • Loading branch information
Syn-McJ authored Nov 1, 2023
2 parents 90c15ae + f79a151 commit d6e0908
Show file tree
Hide file tree
Showing 66 changed files with 17,265 additions and 603 deletions.
2 changes: 1 addition & 1 deletion DashSyncCurrentCommit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23f06b9dd7ba1c5998bbf5a37e06af207d7cb997
8e0e0029fff15707bae4dd8512840ccef4e85c83
68 changes: 60 additions & 8 deletions DashWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions DashWallet/Resources/AppAssets.xcassets/Voting/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "link.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"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" : "filters.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"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.
3 changes: 3 additions & 0 deletions DashWallet/Sources/Application/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ final class App {
func cleanUp() {
TxUserInfoDAOImpl.shared.deleteAll()
AddressUserInfoDAOImpl.shared.deleteAll()
#if DASHPAY
UsernameRequestsDAOImpl.shared.deleteAll()
#endif
Coinbase.shared.reset()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE username_requests (
requestId TEXT PRIMARY KEY,
username TEXT NOT NULL,
createdAt INTEGER NOT NULL,
identity TEXT NOT NULL,
link TEXT,
votes INTEGER NOT NULL,
isApproved INTEGER NOT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SQLite
import SQLiteMigrationManager

struct SeedDB: Migration {
var version: Int64 = 20220713105051
var version: Int64 = 20231023152234

func migrateDatabase(_ db: Connection) throws { }
}
138 changes: 138 additions & 0 deletions DashWallet/Sources/Models/Voting/DAO/UsernameRequestsDAO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// 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
import SQLite

// MARK: - UsernameRequestsDAO

protocol UsernameRequestsDAO {
func create(dto: UsernameRequest)
func all() -> [UsernameRequest]
func get(by requestId: String) -> UsernameRequest?
func update(dto: UsernameRequest)
func delete(dto: UsernameRequest)
func deleteAll()
}

// MARK: - UsernameRequestsDAOImpl

class UsernameRequestsDAOImpl: NSObject, UsernameRequestsDAO {
private var db: Connection { DatabaseConnection.shared.db }
private var cache: [String: UsernameRequest] = [:]

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

func create(dto: UsernameRequest) {
do {
let usernameRequest = UsernameRequest.table.insert(or: .replace,
UsernameRequest.requestId <- dto.requestId,
UsernameRequest.username <- dto.username,
UsernameRequest.createdAt <- dto.createdAt,
UsernameRequest.identity <- dto.identity,
UsernameRequest.link <- dto.link,
UsernameRequest.votes <- dto.votes,
UsernameRequest.isApproved <- dto.isApproved)
try db.run(usernameRequest)

} catch {
print(error)
}

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

func all() -> [UsernameRequest] {
let statement = UsernameRequest.table
var userInfos: [UsernameRequest] = []

do {
for requestRow in try db.prepare(statement) {
let userInfo = UsernameRequest(row: requestRow)
userInfos.append(userInfo)
}
} catch {
print(error)
}

return userInfos
}

func get(by requestId: String) -> UsernameRequest? {
if let cached = cachedValue(by: requestId) {
return cached
}

let statement = UsernameRequest.table.filter(UsernameRequest.requestId == requestId)

do {
for row in try db.prepare(statement) {
let userInfo = UsernameRequest(row: row)
queue.async(flags: .barrier) { [weak self] in
self?.cache[requestId] = userInfo
}
return userInfo
}
} catch {
print(error)
}

return nil
}

private func cachedValue(by key: String) -> UsernameRequest? {
var v: UsernameRequest?

queue.sync {
v = cache[key]
}

return v
}

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

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

func deleteAll() {
do {
try db.run(UsernameRequest.table.delete())
queue.async(flags: .barrier) { [weak self] in
self?.cache = [:]
}
} catch {
print(error)
}
}

static let shared = UsernameRequestsDAOImpl()
}

extension UsernameRequestsDAOImpl {
func dictionaryOfAllItems() -> [String: UsernameRequest] {
_ = all()
return cache
}
}
63 changes: 63 additions & 0 deletions DashWallet/Sources/Models/Voting/UsernameRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// 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
import SQLite

// MARK: - UsernameRequest

struct UsernameRequest {
var requestId: String
var username: String
var createdAt: Int64
var identity: String
var link: String?
var votes: Int
var isApproved: Bool

init(requestId: String, username: String, createdAt: Int64, identity: String, link: String?, votes: Int, isApproved: Bool) {
self.requestId = requestId
self.username = username
self.createdAt = createdAt
self.identity = identity
self.link = link
self.votes = votes
self.isApproved = isApproved
}

init(row: Row) {
self.requestId = row[UsernameRequest.requestId]
self.username = row[UsernameRequest.username]
self.createdAt = row[UsernameRequest.createdAt]
self.identity = row[UsernameRequest.identity]
self.link = row[UsernameRequest.link]
self.votes = row[UsernameRequest.votes]
self.isApproved = row[UsernameRequest.isApproved]
}
}

extension UsernameRequest {
static var table: Table { Table("username_requests") }

static var requestId: Expression<String> { Expression<String>("requestId") }
static var username: Expression<String> { Expression<String>("username") }
static var createdAt: Expression<Int64> { Expression<Int64>("createdAt") }
static var identity: Expression<String> { Expression<String>("identity") }
static var votes: Expression<Int> { Expression<Int>("votes") }
static var isApproved: Expression<Bool> { Expression<Bool>("isApproved") }
static var link: Expression<String?> { .init("link") }
}
35 changes: 35 additions & 0 deletions DashWallet/Sources/Models/Voting/Voting+UserDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// 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

private let kIsVotingInfoShown = "votingInfoShownKey"

// MARK: - VotingPrefs

class VotingPrefs {
public static let shared: VotingPrefs = .init()

private var _infoShown: Bool? = nil
var infoShown: Bool {
get { _infoShown ?? UserDefaults.standard.bool(forKey: kIsVotingInfoShown) }
set(value) {
_infoShown = value
UserDefaults.standard.set(value, forKey: kIsVotingInfoShown)
}
}
}
Loading

0 comments on commit d6e0908

Please sign in to comment.