Skip to content

Commit

Permalink
support tree flow
Browse files Browse the repository at this point in the history
  • Loading branch information
underthestars-zhy committed Apr 29, 2023
1 parent bdd24a7 commit 86a127a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
30 changes: 30 additions & 0 deletions Sources/MobbinAPI/MobbinExtenions/FlowTreeStructure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// FlowTreeStructure.swift
//
//
// Created by 朱浩宇 on 2023/4/29.
//

import Foundation

extension MobbinAPI {
func generateTreeSturctureFlow(from flows: [Flow], parent: String? = nil) -> [Tree<Flow>] {
let topLevelFlows = flows.filter {
$0.parentAppSectionId == parent
}

var res = [Tree<Flow>]()

for topLevelFlow in topLevelFlows {
let secondLevelFlows = flows.filter {
$0.parentAppSectionId == topLevelFlow.id
}.map { flow in
Tree<Flow>.children(flow, generateTreeSturctureFlow(from: flows, parent: flow.id))
}

res.append(.children(topLevelFlow, secondLevelFlows))
}

return res
}
}
12 changes: 12 additions & 0 deletions Sources/MobbinAPI/Model/Tree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Tree.swift
//
//
// Created by 朱浩宇 on 2023/4/29.
//

import Foundation

enum Tree<Item> {
case children(Item, [Self])
}
14 changes: 13 additions & 1 deletion Tests/MobbinAPITests/MobbinAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class MobbinAPITests: XCTestCase {

func testLogin() async throws {
let api = MobbinAPI(email: "[email protected]")
let res = try await api.verify(code: "538917")
let res = try await api.verify(code: "989496")
print(api.token?.accessToken ?? "No Token")

XCTAssertTrue(res)
Expand Down Expand Up @@ -144,4 +144,16 @@ final class MobbinAPITests: XCTestCase {

try await api.delete(collection: collection)
}

func testGetiOSTreeFlows() async throws {
let api = MobbinAPI(email: "[email protected]", token: Token(accessToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjgyODY2MDg4LCJzdWIiOiJkMmI0MTU5MC01M2E0LTRjNGQtOWQ5NC0wNmY1ZDcxODA3YzMiLCJlbWFpbCI6InpodWhhb3l1MDkwOUBpY2xvdWQuY29tIiwicGhvbmUiOiIiLCJhcHBfbWV0YWRhdGEiOnsicHJvdmlkZXIiOiJlbWFpbCIsInByb3ZpZGVycyI6WyJlbWFpbCJdfSwidXNlcl9tZXRhZGF0YSI6eyJhdmF0YXJfdXJsIjoiaHR0cHM6Ly91amFzbnRrZnBoeXdpenNkYWFwaS5zdXBhYmFzZS5jby9zdG9yYWdlL3YxL29iamVjdC9wdWJsaWMvdXNlci91c2VyX2F2YXRhcnMvYTcyZTgwNjQtZjdlYi00YjBhLTg0MWMtMzlhM2ExMGRmY2E0LmpwZWciLCJmdWxsX25hbWUiOiJVVFN6aHkifSwicm9sZSI6ImF1dGhlbnRpY2F0ZWQiLCJhYWwiOiJhYWwxIiwiYW1yIjpbeyJtZXRob2QiOiJvdHAiLCJ0aW1lc3RhbXAiOjE2ODI3Nzk2ODh9XSwic2Vzc2lvbl9pZCI6IjlhMGUwNzQwLTZhZGItNGJlMy1hNGFmLWQ5MDE0Y2FmNDYyZiJ9.nWNXx2FMjtzbEx7Ddr9LKC2v2NrP7Jbd2_b8tymkGho", refreshToken: ""))

let apps = try await api.queryNextPage(nil)

let flows = try await api.getiOSFlows(of: apps[0])

let tree = api.generateTreeSturctureFlow(from: flows)

XCTAssertEqual(tree.count, 7)
}
}

0 comments on commit 86a127a

Please sign in to comment.