Step 5: Split to tab view

This commit is contained in:
Francisco Gindre 2021-02-24 20:16:23 -03:00
parent c1e9216bf4
commit 614d3ea5e8
7 changed files with 254 additions and 47 deletions

View File

@ -206,3 +206,57 @@ func nuke() {
If you diff this commit you will see that there are a lot of changes and other files. Think of it as a cooking show with some pre-arrangements made for the sake of brevity. We encourage you to look at those changes!
## Tag: `step-5-split-to-tab-view`
We are going to change the HomeScreen into a TabView
so we will move its contents to the `SellView`, other tab is going to be the `ReceivedTransactions` and a Settings tab where we will be moving the nuking button for now
````
struct HomeScreen: View {
@Environment(\.zcashEnvironment) var zcash: ZcashEnvironment
@EnvironmentObject var model: ZcashPoSModel
@State var alertType: AlertType? = nil
var body: some View {
TabView {
SellScreen()
.tabItem {
Label("Sell", systemImage: "shield")
}
ReceivedTransactions()
.tabItem {
Label("History", systemImage: "square.and.pencil")
}
SettingsScreen()
.tabItem {
Label("Settings", systemImage: "list.dash")
}
}
.navigationBarHidden(false)
.onAppear() {
_zECCWalletNavigationBarLookTweaks()
do {
guard let ivk = model.viewingKey, let bday = model.birthday else {
throw ZcashPoSModel.PoSError.unableToRetrieveCredentials
}
try self.zcash.synchronizer.initializer.initialize(viewingKeys: [ivk], walletBirthday: bday)
try self.zcash.synchronizer.start()
} catch {
self.alertType = AlertType.errorAlert(error)
}
}
.alert(item: $alertType) { (type) -> Alert in
type.buildAlert()
}
}
}
````

View File

@ -10,6 +10,9 @@
0D08DE6A25E6C29700E08533 /* sapling-output.params in Resources */ = {isa = PBXBuildFile; fileRef = 0D08DE6825E6C29600E08533 /* sapling-output.params */; };
0D08DE6B25E6C29700E08533 /* sapling-spend.params in Resources */ = {isa = PBXBuildFile; fileRef = 0D08DE6925E6C29700E08533 /* sapling-spend.params */; };
0D08DE7025E6CA6800E08533 /* AlertType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D08DE6F25E6CA6800E08533 /* AlertType.swift */; };
0D08DE7325E6FE4B00E08533 /* SellScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D08DE7225E6FE4B00E08533 /* SellScreen.swift */; };
0D08DE7625E7077F00E08533 /* ReceivedTransactions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D08DE7525E7077F00E08533 /* ReceivedTransactions.swift */; };
0D08DEAA25E70D8400E08533 /* SettingsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D08DEA925E70D8400E08533 /* SettingsScreen.swift */; };
0D475B2125E3ED600067978E /* CombineSynchronizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D475B2025E3ED600067978E /* CombineSynchronizer.swift */; };
0D475B2725E3EEE10067978E /* DetailModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D475B2625E3EEE10067978E /* DetailModel.swift */; };
0D475B2A25E3F04C0067978E /* String+Zcash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D475B2925E3F04C0067978E /* String+Zcash.swift */; };
@ -41,6 +44,9 @@
0D08DE6825E6C29600E08533 /* sapling-output.params */ = {isa = PBXFileReference; lastKnownFileType = file; path = "sapling-output.params"; sourceTree = "<group>"; };
0D08DE6925E6C29700E08533 /* sapling-spend.params */ = {isa = PBXFileReference; lastKnownFileType = file; path = "sapling-spend.params"; sourceTree = "<group>"; };
0D08DE6F25E6CA6800E08533 /* AlertType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertType.swift; sourceTree = "<group>"; };
0D08DE7225E6FE4B00E08533 /* SellScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SellScreen.swift; sourceTree = "<group>"; };
0D08DE7525E7077F00E08533 /* ReceivedTransactions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReceivedTransactions.swift; sourceTree = "<group>"; };
0D08DEA925E70D8400E08533 /* SettingsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScreen.swift; sourceTree = "<group>"; };
0D475B2025E3ED600067978E /* CombineSynchronizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombineSynchronizer.swift; sourceTree = "<group>"; };
0D475B2625E3EEE10067978E /* DetailModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailModel.swift; sourceTree = "<group>"; };
0D475B2925E3F04C0067978E /* String+Zcash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Zcash.swift"; sourceTree = "<group>"; };
@ -107,6 +113,9 @@
children = (
0D5050A925DFEFAC00E7A697 /* ImportViewingKey.swift */,
0DC0819625E53E690028532F /* HomeScreen.swift */,
0D08DE7225E6FE4B00E08533 /* SellScreen.swift */,
0D08DE7525E7077F00E08533 /* ReceivedTransactions.swift */,
0D08DEA925E70D8400E08533 /* SettingsScreen.swift */,
);
path = Views;
sourceTree = "<group>";
@ -325,6 +334,7 @@
0D475B3325E3F4910067978E /* ZcashEnvironment.swift in Sources */,
0D50519A25E0591B00E7A697 /* ZcashBackground.swift in Sources */,
0D5051B625E076BA00E7A697 /* KeyboardResponder.swift in Sources */,
0D08DE7625E7077F00E08533 /* ReceivedTransactions.swift in Sources */,
0D5051C425E079DE00E7A697 /* ZcashButton.swift in Sources */,
0D5051AE25E066E300E7A697 /* ZcashLogo.swift in Sources */,
0D50519D25E0596B00E7A697 /* ZcashTextField.swift in Sources */,
@ -336,6 +346,8 @@
0D5051A225E0599800E7A697 /* Baseline.swift in Sources */,
0D475B2725E3EEE10067978E /* DetailModel.swift in Sources */,
0D5050A825DFEFAC00E7A697 /* accept_zcash_pocApp.swift in Sources */,
0D08DEAA25E70D8400E08533 /* SettingsScreen.swift in Sources */,
0D08DE7325E6FE4B00E08533 /* SellScreen.swift in Sources */,
0D475B2D25E3F2E50067978E /* BalanceUtils.swift in Sources */,
0D475B2A25E3F04C0067978E /* String+Zcash.swift in Sources */,
0D475B2125E3ED600067978E /* CombineSynchronizer.swift in Sources */,

View File

@ -36,6 +36,24 @@ class ZcashEnvironment {
func initialize(viewingKey: String, birthday: BlockHeight) throws {
try self.synchronizer.initializer.initialize(viewingKeys: [viewingKey], walletBirthday: birthday)
}
func nuke() throws {
do {
try FileManager.default.removeItem(at: try Self.dataDbURL())
} catch {
logger.error("could not nuke wallet: \(error)")
}
do {
try FileManager.default.removeItem(at: try Self.cacheDbURL())
} catch {
logger.error("could not nuke wallet: \(error)")
}
do {
try FileManager.default.removeItem(at: try Self.pendingDbURL())
} catch {
logger.error("could not nuke wallet: \(error)")
}
}
}
fileprivate struct ZcashEnvironmentKey: EnvironmentKey {

View File

@ -8,59 +8,30 @@
import SwiftUI
import ZcashLightClientKit
struct HomeScreen: View {
enum Status {
case ready
case syncing
case offline
}
@EnvironmentObject var model: ZcashPoSModel
@Environment(\.zcashEnvironment) var zcash: ZcashEnvironment
@EnvironmentObject var model: ZcashPoSModel
@State var alertType: AlertType? = nil
@State var status: Status = .offline
@State var progress: Int = 0
@State var height: BlockHeight = ZcashSDK.SAPLING_ACTIVATION_HEIGHT
var body: some View {
ZStack {
ZcashBackground()
VStack(alignment: .center, spacing: 20) {
ZcashLogo()
switch status {
case .offline:
Text("Offline").foregroundColor(.white)
case .ready:
Text("Ready! Yay!").foregroundColor(.white)
case .syncing:
Text("Syncing \(progress)% Block: \(height)").foregroundColor(.white)
}
TabView {
SellScreen()
.tabItem {
Label("Sell", systemImage: "shield")
}
ReceivedTransactions()
.tabItem {
Label("History", systemImage: "square.and.pencil")
}
Button(action: {
zcash.synchronizer.stop()
model.nuke()
}) {
Text("Stop And Nuke")
.foregroundColor(.red)
.font(.title3)
}
SettingsScreen()
.tabItem {
Label("Settings", systemImage: "list.dash")
}
}
.onReceive(zcash.synchronizer.progress) { (p) in
self.progress = Int(p * 100)
}
.onReceive(zcash.synchronizer.syncBlockHeight) { (h) in
self.height = h
}
}.onReceive(zcash.synchronizer.status) { (s) in
switch s {
case .disconnected, .stopped:
self.status = .offline
case .synced:
self.status = .ready
case .syncing:
self.status = .syncing
}
}
.navigationBarTitle(Text("Zcash PoS"), displayMode: .inline)
.navigationBarHidden(false)
.onAppear() {
_zECCWalletNavigationBarLookTweaks()

View File

@ -0,0 +1,24 @@
//
// ReceivedTransactions.swift
// accept-zcash-poc
//
// Created by Francisco Gindre on 2/24/21.
//
import SwiftUI
struct ReceivedTransactions: View {
var body: some View {
ZStack {
ZcashBackground()
Text("This is where our transactions will be")
.foregroundColor(.white)
}
}
}
struct ReceivedTransactions_Previews: PreviewProvider {
static var previews: some View {
ReceivedTransactions()
}
}

View File

@ -0,0 +1,87 @@
//
// SellScreen.swift
// accept-zcash-poc
//
// Created by Francisco Gindre on 2/24/21.
//
import SwiftUI
import ZcashLightClientKit
struct SellScreen: View {
enum Status {
case ready
case syncing
case offline
}
@EnvironmentObject var model: ZcashPoSModel
@Environment(\.zcashEnvironment) var zcash: ZcashEnvironment
@State var alertType: AlertType? = nil
@State var status: Status = .offline
@State var progress: Int = 0
@State var height: BlockHeight = ZcashSDK.SAPLING_ACTIVATION_HEIGHT
var body: some View {
ZStack {
ZcashBackground()
VStack(alignment: .center, spacing: 20) {
ZcashLogo()
switch status {
case .offline:
Button(action: {
start()
}) {
Text("Offline - Tap to Start").foregroundColor(.white)
}
case .ready:
Text("Ready! Yay!").foregroundColor(.white)
case .syncing:
Text("Syncing \(progress)% Block: \(height)").foregroundColor(.white)
}
}
.onReceive(zcash.synchronizer.progress) { (p) in
self.progress = Int(p * 100)
}
.onReceive(zcash.synchronizer.syncBlockHeight) { (h) in
self.height = h
}
}.onReceive(zcash.synchronizer.status) { (s) in
switch s {
case .disconnected, .stopped:
self.status = .offline
case .synced:
self.status = .ready
case .syncing:
self.status = .syncing
}
}
.navigationBarTitle(Text("Zcash PoS"), displayMode: .inline)
.navigationBarHidden(false)
.onAppear() {
_zECCWalletNavigationBarLookTweaks()
start()
}
.alert(item: $alertType) { (type) -> Alert in
type.buildAlert()
}
}
func start() {
do {
guard let ivk = model.viewingKey, let bday = model.birthday else {
throw ZcashPoSModel.PoSError.unableToRetrieveCredentials
}
try self.zcash.synchronizer.initializer.initialize(viewingKeys: [ivk], walletBirthday: bday)
try self.zcash.synchronizer.start()
} catch {
self.alertType = AlertType.errorAlert(error)
}
}
}
struct SellScreen_Previews: PreviewProvider {
static var previews: some View {
SellScreen()
}
}

View File

@ -0,0 +1,41 @@
//
// SettingsScreen.swift
// accept-zcash-poc
//
// Created by Francisco Gindre on 2/24/21.
//
import SwiftUI
struct SettingsScreen: View {
@EnvironmentObject var model: ZcashPoSModel
@Environment(\.zcashEnvironment) var zcash: ZcashEnvironment
var body: some View {
ZStack {
ZcashBackground()
VStack {
ZcashLogo()
Text("This is where the settings would be")
.foregroundColor(.white)
Button(action: {
zcash.synchronizer.stop()
model.nuke()
try! zcash.nuke()
model.navigation = .importViewingKey
}) {
Text("Stop And Nuke")
.foregroundColor(.red)
.font(.title3)
}
}
}
}
}
struct SettingsScreen_Previews: PreviewProvider {
static var previews: some View {
SettingsScreen()
}
}