Skip to content

Commit

Permalink
Swift6 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 10, 2024
1 parent 1ff9851 commit fe7f8e2
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.build
/Packages
/*.xcodeproj
/*.xcworkspace
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
Expand Down
2 changes: 2 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
tuist = "4.18"
4 changes: 2 additions & 2 deletions Development/Development.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -380,7 +380,7 @@
PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
SWIFT_VERSION = 6.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
4 changes: 3 additions & 1 deletion Development/Development/BookUIKitBasedCompositional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import DynamicList
import os
import MondrianLayout

fileprivate var globalCount: Int = 0

nonisolated(unsafe) fileprivate var globalCount: Int = 0

fileprivate func getGlobalCount() -> Int {
globalCount &+= 1
return globalCount
Expand Down
3 changes: 2 additions & 1 deletion Development/Development/BookUIKitBasedFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DynamicList
import os


fileprivate var globalCount: Int = 0
nonisolated(unsafe) fileprivate var globalCount: Int = 0
fileprivate func getGlobalCount() -> Int {
globalCount &+= 1
return globalCount
Expand Down Expand Up @@ -101,6 +101,7 @@ struct BookUIKitBasedFlow: View, PreviewProvider {
case .a(let v):
return context.cell(reuseIdentifier: "A") { state, _ in
ComposableCell {
TextField("Hello", text: .constant("Hoge"))
HStack {
Text("\(state.isHighlighted.description)")
Text("\(v.name)")
Expand Down
70 changes: 0 additions & 70 deletions Development/Development/BookVariadicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ struct BookVariadicView: View, PreviewProvider {

List {

NavigationLink {
BackedContent()
.navigationTitle("Backed")
} label: {
Text("Backed")
}

NavigationLink {
NativeContent()
.navigationTitle("Native")
Expand All @@ -31,13 +24,6 @@ struct BookVariadicView: View, PreviewProvider {
NavigationView {
List {

NavigationLink {
BackedContent()
.navigationTitle("Backed")
} label: {
Text("Backed")
}

NavigationLink {
NativeContent()
.navigationTitle("Native")
Expand Down Expand Up @@ -69,62 +55,6 @@ struct BookVariadicView: View, PreviewProvider {
}
}

private struct BackedContent: View {

@State var items: [Message] = MockData.randomMessages(count: 2000)

var body: some View {
VStack {
CustomList {
ForEach(
items,
content: {
ComplexCell(message: $0)
}
)
}
}
}

struct Cell1: View {

@State var flag = false

var body: some View {
VStack {
HStack {
Text("Hello")
Toggle("Flag", isOn: $flag)
}
Rectangle()
.frame(height: flag ? 10 : 50)
}
.padding(.horizontal, 20)
.padding(.vertical, 30)
}
}

struct Cell2: View {

@State var flag = false

var body: some View {
VStack {
HStack {
Text("Hello")
Toggle("Flag", isOn: $flag)
}
Rectangle()
.fill(Color.red)
.frame(height: flag ? 10 : 50)
}
.padding(.horizontal, 20)
.padding(.vertical, 30)
}
}

}

static let url = URL(
string:
"https://images.unsplash.com/photo-1686726754283-3cf793dec0e6?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1000&q=80"
Expand Down
10 changes: 5 additions & 5 deletions Sources/DynamicList/ContentPagingTrigger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ final class ContentPagingTrigger {
self.leadingScreensForBatching = leadingScreensForBatching
self.trackingScrollDirection = trackingScrollDirection

offsetObservation = scrollView.observe(\.contentOffset, options: [.initial, .new]) {
@MainActor(unsafe) [weak self] scrollView, _ in
offsetObservation = scrollView.observe(\.contentOffset, options: [.initial, .new]) { [weak self] scrollView, _ in
guard let `self` = self else { return }
self.didScroll(scrollView: scrollView)
MainActor.assumeIsolated {
self.didScroll(scrollView: scrollView)
}
}

contentSizeObservation = scrollView.observe(\.contentSize, options: [.initial, .new]) {
@MainActor(unsafe) scrollView, _ in
contentSizeObservation = scrollView.observe(\.contentSize, options: [.initial, .new]) { scrollView, _ in
// print(scrollView.contentSize)
}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/DynamicList/DynamicList.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import SwiftUI
import UIKit

public enum Selection<Data: Hashable> {
case single(Data)
case multiple(Set<Data>)
}

public struct DynamicList<Section: Hashable, Item: Hashable>: UIViewRepresentable {
public struct DynamicList<Section: Hashable & Sendable, Item: Hashable & Sendable>: UIViewRepresentable {

public struct ScrollTarget {
public let item: Item
Expand Down
12 changes: 7 additions & 5 deletions Sources/DynamicList/DynamicListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public protocol CustomStateKey {
*/
public struct CellState {

public static let empty = CellState()
public static var empty: CellState {
.init()
}

private var stateMap: [AnyKeyPath : Any] = [:]

Expand Down Expand Up @@ -63,7 +65,7 @@ public enum DynamicListViewScrollAction {
///
/// - TODO: Currently supported only vertical scrolling.
@available(iOS 13, *)
public final class DynamicListView<Section: Hashable, Data: Hashable>: UIView,
public final class DynamicListView<Section: Hashable & Sendable, Data: Hashable & Sendable>: UIView,
UICollectionViewDelegate, UIScrollViewDelegate
{

Expand Down Expand Up @@ -135,7 +137,7 @@ public final class DynamicListView<Section: Hashable, Data: Hashable>: UIView,

if _collectionView.cellForIdentifiers.contains(_reuseIdentifier) == false {

Log.debug(.generic, "Register Cell : \(_reuseIdentifier)")
Log.generic.debug("Register Cell : \(_reuseIdentifier)")

_collectionView.register(VersatileCell.self, forCellWithReuseIdentifier: _reuseIdentifier)
}
Expand Down Expand Up @@ -636,12 +638,12 @@ internal final class CollectionView: UICollectionView {
public typealias DynamicCompositionalLayoutSingleSectionView<Data: Hashable> =
DynamicListView<DynamicCompositionalLayoutSingleSection, Data>

public enum DynamicCompositionalLayoutSingleSection: Hashable {
public enum DynamicCompositionalLayoutSingleSection: Hashable, Sendable {
case main
}

private enum CellContextKey: EnvironmentKey {
static var defaultValue: VersatileCell?
static var defaultValue: VersatileCell? { nil }
}

extension EnvironmentValues {
Expand Down
29 changes: 6 additions & 23 deletions Sources/DynamicList/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,22 @@
import os.log

enum Log {

static func debug(
dso: UnsafeRawPointer = #dsohandle,
file: StaticString = #file,
line: UInt = #line,
_ log: OSLog,
_ object: @autoclosure () -> Any
) {
os_log(.debug, dso: dso, log: log, "%{public}@", "\(object())")
}

static func error(
dso: UnsafeRawPointer = #dsohandle,
file: StaticString = #file,
line: UInt = #line,
_ log: OSLog,
_ object: @autoclosure () -> Any
) {
os_log(.error, dso: dso, log: log, "%{public}@", "\(object())")
}


static let generic = Logger(OSLog.makeOSLogInDebug { OSLog.init(subsystem: "DynamicList", category: "generic") })

}

extension OSLog {

@inline(__always)
private static func makeOSLogInDebug(isEnabled: Bool = true, _ factory: () -> OSLog) -> OSLog {
fileprivate static func makeOSLogInDebug(isEnabled: Bool = true, _ factory: () -> OSLog) -> OSLog {
#if DEBUG
return factory()
#else
return .disabled
#endif
}

static let generic: OSLog = makeOSLogInDebug { OSLog.init(subsystem: "app.muukii", category: "generic") }
}


14 changes: 7 additions & 7 deletions Sources/DynamicList/NSDiffableDataSourceSnapshot+Unique.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import UIKit

public enum DiffableDataSourceError: Error {
case duplicatedSectionIdentifiers(Set<AnyHashable>)
case duplicatedItemIdentifiers(Set<AnyHashable>)
public enum DiffableDataSourceError: Error, Sendable {
case duplicatedSectionIdentifiers(debugDescription: String)
case duplicatedItemIdentifiers(debugDescription: String)
}

extension NSDiffableDataSourceSnapshot {
Expand All @@ -17,7 +17,7 @@ extension NSDiffableDataSourceSnapshot {
for section in sections {
let (inserted, _) = sectionSet.insert(section)
guard inserted else {
throw DiffableDataSourceError.duplicatedSectionIdentifiers([section])
throw DiffableDataSourceError.duplicatedSectionIdentifiers(debugDescription: String(describing: [section]))
}
}

Expand All @@ -28,7 +28,7 @@ extension NSDiffableDataSourceSnapshot {
if set.isEmpty {
appendSections(sections)
} else {
throw DiffableDataSourceError.duplicatedSectionIdentifiers(set)
throw DiffableDataSourceError.duplicatedSectionIdentifiers(debugDescription: String(describing: set))
}

}
Expand All @@ -43,7 +43,7 @@ extension NSDiffableDataSourceSnapshot {
for item in items {
let (inserted, _) = itemSet.insert(item)
guard inserted else {
throw DiffableDataSourceError.duplicatedItemIdentifiers([item])
throw DiffableDataSourceError.duplicatedItemIdentifiers(debugDescription: String(describing: [item]))
}
}

Expand All @@ -53,7 +53,7 @@ extension NSDiffableDataSourceSnapshot {
if set.isEmpty {
appendItems(items, toSection: sectionIdentifier)
} else {
throw DiffableDataSourceError.duplicatedItemIdentifiers(set)
throw DiffableDataSourceError.duplicatedItemIdentifiers(debugDescription: String(describing: set))
}

}
Expand Down
Loading

0 comments on commit fe7f8e2

Please sign in to comment.