Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating To GRDBQuery 0.9 #152

Merged
merged 11 commits into from
Sep 12, 2024
1 change: 1 addition & 0 deletions Gem/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct GemApp: App {
WalletCoordinator(
db: db
)
.databaseContext(.readWrite { db.dbQueue })
.navigationBarTitleDisplayMode(.inline)
.tint(Colors.black)
}
Expand Down
6 changes: 3 additions & 3 deletions Gem/Asset/AssetScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ struct AssetScene: View {
self.wallet = wallet
self.input = input
_isPresentingAssetSelectType = isPresentingAssetSelectType
_assetData = Query(constant: input.assetRequest, in: \.db.dbQueue)
_transactions = Query(constant: input.transactionsRequest, in: \.db.dbQueue)
_banners = Query(constant: input.bannersRequest, in: \.db.dbQueue)
_assetData = Query(constant: input.assetRequest)
_transactions = Query(constant: input.transactionsRequest)
_banners = Query(constant: input.bannersRequest)
}

var body: some View {
Expand Down
5 changes: 2 additions & 3 deletions Gem/Assets/Scenes/SelectAssetScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Style
import Keystore

struct SelectAssetScene: View {
@Environment(\.db) private var DB
@Environment(\.keystore) private var keystore
@Environment(\.walletsService) private var walletsService
@Environment(\.nodeService) private var nodeService
Expand Down Expand Up @@ -41,8 +40,8 @@ struct SelectAssetScene: View {
} set: { new in
model.filterModel.assetsRequest = new
}
_assets = Query(request, in: \.db.dbQueue)
_assetInfo = Query(model.assetsInfoRequest, in: \.db.dbQueue)
_assets = Query(request)
_assetInfo = Query(model.assetsInfoRequest)
}

var body: some View {
Expand Down
2 changes: 1 addition & 1 deletion Gem/Charts/Scenes/ChartScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ChartScene: View {
model: ChartsViewModel
) {
_model = StateObject(wrappedValue: model)
_assetData = Query(constant: model.assetRequest, in: \.db.dbQueue)
_assetData = Query(constant: model.assetRequest)
}

var body: some View {
Expand Down
8 changes: 2 additions & 6 deletions Gem/Connections/Scenes/ConnectionsScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import QRScanner
import Style

struct ConnectionsScene: View {

@Environment(\.db) private var DB
@Environment(\.keystore) private var keystore
@State private var isPresentingScanner: Bool = false
@State private var isPresentingErrorMessage: String?
Expand All @@ -28,11 +26,9 @@ struct ConnectionsScene: View {

let model: ConnectionsViewModel

init(
model: ConnectionsViewModel
) {
init(model: ConnectionsViewModel) {
self.model = model
_connections = Query(ConnectionsRequest(), in: \.db.dbQueue)
_connections = Query(constant: model.request)
}

var body: some View {
Expand Down
7 changes: 6 additions & 1 deletion Gem/Connections/ViewModels/ConnectionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import Foundation
import Primitives
import Store

struct ConnectionsViewModel {
let service: ConnectionsService


var request: ConnectionsRequest {
ConnectionsRequest()
}

func addConnectionURI(uri: String, wallet: Wallet) async throws {
try await service.addConnectionURI(uri: uri, wallet: wallet)
}
Expand Down
1 change: 0 additions & 1 deletion Gem/Core/Coordinator/WalletCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ struct WalletCoordinator: View {
} message: {
Text(Localized.UpdateApp.description(updateAvailableAlertSheetMessage ?? ""))
}
.environment(\.db, db)
.environment(\.nodeService, nodeService)
.environment(\.keystore, keystore)
.environment(\.walletService, walletService)
Expand Down
2 changes: 1 addition & 1 deletion Gem/Core/Views/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct MainTabView: View {
navigationStateManager: Binding<NavigationStateManagable>
) {
self.model = model
_transactions = Query(constant: model.transactionsCountRequest, in: \.db.dbQueue)
_transactions = Query(constant: model.transactionsCountRequest)
_navigationStateManager = navigationStateManager
}

Expand Down
3 changes: 1 addition & 2 deletions Gem/Stake/Scenes/StakeScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Components
import Style

struct StakeScene: View {
@Environment(\.db) private var DB
@Environment(\.keystore) private var keystore
@Environment(\.nodeService) private var nodeService
@Environment(\.walletsService) private var walletsService
Expand All @@ -27,7 +26,7 @@ struct StakeScene: View {
model: StakeViewModel
) {
_model = State(initialValue: model)
_delegations = Query(model.request, in: \.db.dbQueue)
_delegations = Query(model.request)
}

var body: some View {
Expand Down
17 changes: 11 additions & 6 deletions Gem/Swap/Scenes/SwapScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ import Keystore
struct SwapScene: View {
@Environment(\.nodeService) private var nodeService

@Query<AssetRequest> var fromAsset: AssetData
@Query<AssetRequest> var toAsset: AssetData
@Query<TransactionsRequest> var tokenApprovals: [TransactionExtended]
@Query<AssetRequest>
var fromAsset: AssetData

@Query<AssetRequest>
var toAsset: AssetData

@Query<TransactionsRequest>
var tokenApprovals: [TransactionExtended]

@State var model: SwapViewModel

Expand All @@ -25,9 +30,9 @@ struct SwapScene: View {

init(model: SwapViewModel) {
_model = State(initialValue: model)
_fromAsset = Query(model.fromAssetRequest, in: \.db.dbQueue)
_toAsset = Query(model.toAssetRequest, in: \.db.dbQueue)
_tokenApprovals = Query(model.tokenApprovalsRequest, in: \.db.dbQueue)
_fromAsset = Query(model.fromAssetRequest)
_toAsset = Query(model.toAssetRequest)
_tokenApprovals = Query(model.tokenApprovalsRequest)
}

var body: some View {
Expand Down
2 changes: 1 addition & 1 deletion Gem/Transactions/Scenes/TransactionScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct TransactionScene: View {
input: TransactionSceneInput
) {
self.input = input
_transactions = Query(input.transactionRequest, in: \.db.dbQueue)
_transactions = Query(input.transactionRequest)
}

@State private var showShareSheet = false
Expand Down
4 changes: 1 addition & 3 deletions Gem/Transactions/Scenes/TransactionsScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import Store
import Style

struct TransactionsScene: View {
@Environment(\.db) private var DB

@Query<TransactionsRequest>
private var transactions: [TransactionExtended]

Expand All @@ -19,7 +17,7 @@ struct TransactionsScene: View {
model: TransactionsViewModel
) {
self.model = model
_transactions = Query(constant: model.request, in: \.db.dbQueue)
_transactions = Query(constant: model.request)
}

var body: some View {
Expand Down
12 changes: 5 additions & 7 deletions Gem/Wallet/Scenes/WalletScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import GRDBQuery
import Style

struct WalletScene: View {

@Environment(\.db) private var DB
@Environment(\.keystore) private var keystore
@Environment(\.assetsService) private var assetsService
@Environment(\.transactionsService) private var transactionsService
Expand Down Expand Up @@ -48,11 +46,11 @@ struct WalletScene: View {

try? model.setupWallet()

_assets = Query(constant: model.assetsRequest, in: \.db.dbQueue)
_assetsPinned = Query(constant: model.assetsPinnedRequest, in: \.db.dbQueue)
_fiatValue = Query(constant: model.fiatValueRequest, in: \.db.dbQueue)
_dbWallet = Query(constant: model.walletRequest, in: \.db.dbQueue)
_banners = Query(constant: model.bannersRequest, in: \.db.dbQueue)
_assets = Query(constant: model.assetsRequest)
_assetsPinned = Query(constant: model.assetsPinnedRequest)
_fiatValue = Query(constant: model.fiatValueRequest)
_dbWallet = Query(constant: model.walletRequest)
_banners = Query(constant: model.bannersRequest)
}

var body: some View {
Expand Down
9 changes: 0 additions & 9 deletions Gem/Wallet/Types/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import Store
import Keystore
import GemstonePrimitives

struct DatabaseQueueKey: EnvironmentKey {
static var defaultValue: DB { DB.main }
}

struct NodeServiceKey: EnvironmentKey {
static var defaultValue: NodeService { NodeService.main }
}
Expand Down Expand Up @@ -80,11 +76,6 @@ struct IsWalletPresentedServiceKey: EnvironmentKey {
}

extension EnvironmentValues {
var db: DB {
get { self[DatabaseQueueKey.self] }
set { self[DatabaseQueueKey.self] = newValue }
}

var nodeService: NodeService {
get { self[NodeServiceKey.self] }
set { self[NodeServiceKey.self] = newValue }
Expand Down
5 changes: 2 additions & 3 deletions Gem/Wallets/Scenes/WalletsScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Style

struct WalletsScene: View {
@Environment(\.dismiss) private var dismiss
@Environment(\.db) private var DB
@Environment(\.keystore) private var keystore

@State private var isPresentingErrorMessage: String?
Expand All @@ -31,8 +30,8 @@ struct WalletsScene: View {
model: WalletsViewModel
) {
self.model = model
_pinnedWallets = Query(WalletsRequest(isPinned: true), in: \.db.dbQueue)
_wallets = Query(WalletsRequest(isPinned: false), in: \.db.dbQueue)
_pinnedWallets = Query(WalletsRequest(isPinned: true))
_wallets = Query(WalletsRequest(isPinned: false))
}

var body: some View {
Expand Down
27 changes: 10 additions & 17 deletions Packages/Store/Sources/Requests/AssetRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@ import GRDBQuery
import Combine
import Primitives

public struct AssetRequest: Queryable {

public struct AssetRequest: ValueObservationQueryable {
public static var defaultValue: AssetData { AssetData.empty }

public let walletId: String
public var assetId: String

public init(
walletId: String,
assetId: String
) {
private let walletId: String

public init(walletId: String, assetId: String) {
self.walletId = walletId
self.assetId = assetId
}

public func publisher(in dbQueue: DatabaseQueue) -> AnyPublisher<AssetData, Error> {
ValueObservation
.tracking { db in try fetch(db) }
.publisher(in: dbQueue, scheduling: .immediate)
.eraseToAnyPublisher()
}

private func fetch(_ db: Database) throws -> AssetData {
return try AssetRecord

public func fetch(_ db: Database) throws -> AssetData {
try AssetRecord
.including(optional: AssetRecord.price)
.including(optional: AssetRecord.balance)
.including(optional: AssetRecord.details)
Expand All @@ -43,7 +33,10 @@ public struct AssetRequest: Queryable {
}
}

// MARK: - Models Exttensions

//TODO: Find a way to remove .empty

extension AssetData {
static let empty: AssetData = {
return AssetData(
Expand Down
17 changes: 5 additions & 12 deletions Packages/Store/Sources/Requests/AssetsInfoRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ import GRDBQuery
import Combine
import Primitives
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove import Combine as well in each file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately not possible to remove for now, ref to xcode warning(ValueObservationQueryable):

Cannot use conformance of 'AnyPublisher<Output, Failure>' to 'Publisher' here; 'Combine' was not imported by this file; this is an error in Swift 6


public struct AssetsInfoRequest: Queryable {
public struct AssetsInfoRequest: ValueObservationQueryable {
public static var defaultValue: AssetsInfo { AssetsInfo(hidden: 0) }

let walletId: String
private let walletId: String

public init(walletId: String) {
self.walletId = walletId
}

public func publisher(in dbQueue: DatabaseQueue) -> AnyPublisher<AssetsInfo, Error> {
ValueObservation
.tracking { db in try fetch(db) }
.publisher(in: dbQueue, scheduling: .immediate)
.eraseToAnyPublisher()
}

private func fetch(_ db: Database) throws -> AssetsInfo {

public func fetch(_ db: Database) throws -> AssetsInfo {
let hidden = try AssetBalanceRecord
.filter(Columns.Balance.walletId == walletId)
.filter(Columns.Balance.isHidden == true)
Expand Down
Loading
Loading