-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from mudkipme/feat/nested-tags
feat: collapsible tags
- Loading branch information
Showing
5 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// NestedTag.swift | ||
// MoeMemos | ||
// | ||
// Created by Mudkip on 2024/6/23. | ||
// | ||
|
||
import Foundation | ||
import Models | ||
|
||
struct NestedTag: Identifiable, Hashable { | ||
var id: String { fullName } | ||
var fullName: String | ||
var name: String | ||
var children: [NestedTag]? = nil | ||
|
||
static func fromTagList(_ tagList: [String], prefix: String? = nil) -> [NestedTag] { | ||
var tagDict = [String: [String]]() | ||
|
||
for tag in tagList { | ||
let parts = tag.split(separator: "/", maxSplits: 2).map(String.init) | ||
if tagDict[parts[0]] == nil { | ||
tagDict[parts[0]] = [] | ||
} | ||
|
||
if parts.count > 1 { | ||
tagDict[parts[0]]?.append(parts[1]) | ||
} | ||
} | ||
|
||
var nestedTags = [NestedTag]() | ||
for (key, value) in tagDict { | ||
let fullName = prefix != nil ? "\(prefix!)/\(key)" : key | ||
let children = fromTagList(value, prefix: fullName) | ||
let tag = NestedTag(fullName: fullName, name: key, children: children.isEmpty ? nil : children) | ||
nestedTags.append(tag) | ||
} | ||
|
||
return nestedTags.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters