Skip to content

Commit

Permalink
Update Project and Fix Location Edit Bug (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
PSchmiedmayer authored Nov 14, 2021
1 parent 9d60f51 commit ba2b1f2
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Check Swift version
run: swift --version
- name: Build and test
run: xcodebuild test -scheme Xpense -destination 'platform=iOS Simulator,OS=15.0,name=iPhone 12'
run: xcodebuild test -scheme Xpense -destination 'platform=iOS Simulator,name=iPhone 13 Pro'
macoswebservice:
name: macOS Web Service ${{ matrix.configuration }}
runs-on: macos-11
Expand Down
8 changes: 6 additions & 2 deletions App/Xpense/Account Subsystem/Edit Account/EditAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ struct EditAccount: View {
presentationMode.wrappedValue.dismiss()
}
}
}.onAppear(perform: viewModel.updateStates)
}.onAppear {
Task {
viewModel.updateStates()
}
}
.navigationTitle(viewModel.id == nil ? "Add Account" : "Edit Account")
.toolbar {
SaveButton(viewModel: viewModel) {
Expand Down Expand Up @@ -93,7 +97,7 @@ private struct DeleteButton: View {
Task {
try await viewModel.delete()

DispatchQueue.main.async {
Task(priority: .userInitiated) {
onSuccess?()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ class EditAccountViewModel: ObservableObject {
}


@MainActor
private func showDeleteProgressView(_ showDeleteProgressView: Bool) {
self.showDeleteProgressView = showDeleteProgressView
}

/// Updates the `EditViews`'s state like the name based on the `id`
@MainActor
func updateStates() {
guard let account = model?.account(id) else {
self.name = ""
Expand All @@ -65,12 +71,12 @@ class EditAccountViewModel: ObservableObject {

let account = Account(id: self.id, name: self.name, userID: userId)

self.showSaveProgressView = true
await showDeleteProgressView(true)

try await model.save(account)

self.updateStates()
self.showSaveProgressView = false
await self.updateStates()
await showDeleteProgressView(false)
}

/// Deletes the `Account` that is currently edited
Expand All @@ -79,12 +85,12 @@ class EditAccountViewModel: ObservableObject {
throw XpenseServiceError.deleteFailed(Account.self)
}

self.showDeleteProgressView = true
await showDeleteProgressView(true)

try await model.delete(account: id)

self.updateStates()
self.showDeleteProgressView = false
await self.updateStates()
await showDeleteProgressView(false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion App/Xpense/Account Subsystem/Edit Account/SaveButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct SaveButton<M: SaveButtonViewModel>: View {
Task {
try await viewModel.save()

DispatchQueue.main.async {
Task(priority: .userInitiated) {
onSuccess?()
}
}
Expand Down
16 changes: 8 additions & 8 deletions App/Xpense/Location Subsystem/EditLocationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ struct EditLocationView: View {
}
.font(.largeTitle)
.foregroundColor(.red)
}
.navigationBarTitleDisplayMode(.inline)
.onAppear {
if let coordinate = coordinate {
coordinateRegion.center = coordinate
coordinateRegion.span = LocationView.Defaults.span
}
}
}.ignoresSafeArea(edges: .bottom)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
Expand All @@ -63,6 +56,13 @@ struct EditLocationView: View {
}
}
}
.onAppear {
if let coordinate = coordinate {
coordinateRegion.center = coordinate
coordinateRegion.span = LocationView.Defaults.span
}
}
.navigationBarTitleDisplayMode(.inline)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ struct EditTransaction: View {
var body: some View {
NavigationView {
self.form
.onAppear(perform: viewModel.updateStates)
.onAppear {
Task {
viewModel.updateStates()
}
}
.navigationBarTitle(navigationTitle, displayMode: .inline)
.toolbar {
ToolbarItem(placement: .primaryAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ class EditTransactionViewModel: ObservableObject {
}


@MainActor
private func showSaveProgressView(_ showSaveProgressView: Bool) {
self.showSaveProgressView = showSaveProgressView
}

/// Updates the `EditTransaction`'s state like the name based on the `id`
@MainActor
func updateStates() {
guard let transaction = model?.transaction(id), !loaded else {
return
Expand Down Expand Up @@ -92,12 +98,12 @@ class EditTransactionViewModel: ObservableObject {
location: Coordinate(location),
account: selectedAccount)

self.showSaveProgressView = true
await showSaveProgressView(true)

try await model.save(transaction)

self.updateStates()
self.showSaveProgressView = false
await self.updateStates()
await showSaveProgressView(false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LoginViewModel: ObservableObject {
try await model?.signUp(username, password: password)
}

DispatchQueue.main.async {
Task(priority: .userInitiated) {
self.loadingInProcess = false
}
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "3bea268b223651c4ab7b7b9ad62ef9b2d4143eb6",
"version": "1.1.6"
"revision": "9b5ef28601a9c745c9cdb54d3f243e28ac830982",
"version": "2.0.1"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions Shared/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ let package = Package(
.library(name: "RESTfulXpenseModel", targets: ["RESTfulXpenseModel"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.4.4"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.1.6")
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.4.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
],
targets: [
.executableTarget(
Expand Down
12 changes: 7 additions & 5 deletions Shared/Sources/RESTfulXpenseModel/RestfulModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class RestfulModel: LocalStorageModel {
/// Creates a new `RestfulModel` and loads the current `User`s, `Account`s and `Transaction`s
public init() {
super.init()
userDidSet()
Task {
await userDidSet()
}
}

override public func userDidSet() {
super.userDidSet()
override public func userDidSet() async {
await super.userDidSet()

guard let token = user?.bearerToken else {
return
Expand Down Expand Up @@ -69,15 +71,15 @@ public class RestfulModel: LocalStorageModel {
override public func signUp(_ name: String, password: String) async throws -> User {
let user = try await sendSignUpRequest(name, password: password)
users.update(with: user)
userDidSet()
await userDidSet()
return user
}

@discardableResult
override public func login(_ name: String, password: String) async throws -> String {
let user = try await sendLoginRequest(name, password: password)
users.update(with: user)
userDidSet()
await userDidSet()

guard let token = user.tokens.first else {
throw XpenseServiceError.loginFailed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ open class LocalStorageModel: Model {
}


override open func accountsDidSet() {
super.accountsDidSet()
override open func accountsDidSet() async {
await super.accountsDidSet()
accounts.saveToFile()
}

override open func transactionsDidSet() {
super.transactionsDidSet()
override open func transactionsDidSet() async {
await super.transactionsDidSet()
transactions.saveToFile()
}

override open func userDidSet() {
super.userDidSet()
override open func userDidSet() async {
await super.userDidSet()
users.saveToFile()
}

Expand Down
56 changes: 29 additions & 27 deletions Shared/Sources/XpenseModel/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ open class Model {
/// The `Account`s of the Xpense App
public var accounts: Set<Account> {
didSet {
accountsDidSet()
Task {
await accountsDidSet()
}
}
}
/// The `Transaction`s of the Xpense App
public var transactions: Set<Transaction> {
didSet {
transactionsDidSet()
Task {
await transactionsDidSet()
}
}
}
/// A `XpenseServiceError` that should be displayed to the user in case of an error in relation with the Xpense Web Service
public var webServiceError: XpenseServiceError? {
didSet {
webServiceErrorDidSet()
Task {
await webServiceErrorDidSet()
}
}
}

Expand All @@ -53,46 +59,42 @@ open class Model {
}


#if canImport(Combine)
#if canImport(Combine) && canImport(UIKit)
/// Called when a new `User` was set to the `user` property, calls the `ObservableObject` `objectWillChange` Publisher
open func userDidSet() {
DispatchQueue.main.async {
self.objectWillChange.send()
}
@MainActor
open func userDidSet() async {
self.objectWillChange.send()
}

/// Called when a new `Account` was set to the `accounts` property, calls the `ObservableObject` `objectWillChange` Publisher
open func accountsDidSet() {
DispatchQueue.main.async {
self.objectWillChange.send()
}
@MainActor
open func accountsDidSet() async {
self.objectWillChange.send()
}

/// Called when a new `Transaction` was set to the `transactions` property, calls the `ObservableObject` `objectWillChange` Publisher
open func transactionsDidSet() {
DispatchQueue.main.async {
self.objectWillChange.send()
}
@MainActor
open func transactionsDidSet() async {
self.objectWillChange.send()
}

/// Called when a new `XpenseServiceError` was set to the `webServiceError` property, calls the `ObservableObject` `objectWillChange` Publisher
open func webServiceErrorDidSet() {
DispatchQueue.main.async {
self.objectWillChange.send()
}
@MainActor
open func webServiceErrorDidSet() async {
self.objectWillChange.send()
}
#else
/// Called when a new `User` was set to the `user` property
open func userDidSet() {}
open func userDidSet() async {}

/// Called when a new `Account` was set to the `accounts` property
open func accountsDidSet() {}
open func accountsDidSet() async {}

/// Called when a new `Transaction` was set to the `transactions` property
open func transactionsDidSet() {}
open func transactionsDidSet() async {}

/// Called when a new `XpenseServiceError` was set to the `webServiceError` property
open func webServiceErrorDidSet() {}
open func webServiceErrorDidSet() async {}
#endif

/// Get an `Account` for a specific ID
Expand Down Expand Up @@ -192,7 +194,7 @@ open class Model {
open func signUp(_ name: String, password: String) async throws -> User {
let user = User(name: name, password: password)
users.update(with: user)
userDidSet()
await userDidSet()
return user
}

Expand All @@ -214,7 +216,7 @@ open class Model {
open func createToken(for user: inout User) async -> String {
let token = user.createToken()
users.update(with: user)
userDidSet()
await userDidSet()
return token
}

Expand Down Expand Up @@ -242,6 +244,6 @@ open class Model {
}
}

#if canImport(Combine)
#if canImport(Combine) && canImport(UIKit)
extension Model: ObservableObject {}
#endif
Loading

0 comments on commit ba2b1f2

Please sign in to comment.