pavankataria / SwiftDataTables

A Swift Data Table package, display grid-like data sets in a nicely formatted table for iOS. Subclassing UICollectionView that allows ordering, and searching with extensible options.
MIT License
448 stars 69 forks source link

Add support for swift package manager #44

Closed ShaharHD closed 4 years ago

ShaharHD commented 4 years ago

For more information: https://forums.swift.org/t/swift-pm-bundles-and-resources/13981

The xib files, if possible need to be generated by code - and that should solve the issue

ShaharHD commented 4 years ago

Be aware you'll need to get a new release for Xcode to get it "right" (otherwise the users will have to map to master)

ShaharHD commented 4 years ago

Additional issues you need to consider: It will compile correctly, but because of the reliance on xib files (you have 4 of those) - the project cannot be used directly with swift package manager, so those XIB files needs to be probably converted to be generated in code like https://github.com/akosma/nib2objc/tree/master and then migrated to swift?

pavankataria commented 4 years ago

I have no problems programmatically creating those views, I find it strange that swift package manager doesn't support xib files. But that's fine. My only concern is how many users are actually reliant on the xibs and figuring out an appropriate way to deprecate them.

pavankataria commented 4 years ago

This is great work btw. Thank you for this. Have you got some documentation on how swift package manager now expects the source files? You're not the first person to organise the files, though this PR is far more detailed which I'm grateful for. đź‘Ť

ShaharHD commented 4 years ago

We'er you testing using my fork? using swift package manager in a new SwiftUI or Swift Project?

Here's the code I have crashing (simple SwiftUI)


import os
import SwiftUI
import SwiftDataTables

private struct SwiftUIDataTable: UIViewRepresentable {
    func makeUIView(context: Context) -> SwiftDataTable {
        let v =  SwiftDataTable(data: [["1", "2"], ["3", "4"]], headerTitles: ["A", "B"])
        return v
    }

    func updateUIView(_ uiView: SwiftDataTable, context: UIViewRepresentableContext<SwiftUIDataTable>) {
        os_log("updateUIView")
    }
}

struct SamplesDataGrid: View {
    var body: some View {
        SwiftUIDataTable()
    }
}

#if DEBUG
struct SamplesDataGrid_Previews: PreviewProvider {
    static var previews: some View {
        SamplesDataGrid()
    }
}
#endif

The crashes I've been having are this:

2019-10-22 01:35:48.896827+0300 BPM Engineering[87318:4190481] updateUIView

time lapsed: 0.00034499168395996094
for 4 rows

2019-10-22 01:35:48.951302+0300 BPM Engineering[87318:4190481] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/shahar/Library/Developer/CoreSimulator/Devices/C9135227-B0C4-4B4D-B6DB-905B37EA67DF/data/Containers/Bundle/Application/00A60C58-1ADF-435B-8B96-0500D0F75761/BPM Engineering.app> (loaded)' with name 'DataCell''
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23baa1ee __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50864b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23baa02c +[NSException raise:format:] + 188
    3   UIKitCore                           0x00007fff471e8efb -[UINib instantiateWithOwner:options:] + 487
    4   UIKitCore                           0x00007fff46d76ab5 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 873
    5   UIKitCore                           0x00007fff46d7729e -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 88
    6   BPM Engineering                     0x0000000103ce8dc3 $s15SwiftDataTables0B13CellViewModelC07dequeueD0010collectionE09indexPathSo012UICollectioneD0CSo0kE0C_10Foundation05IndexJ0VtF + 403
    7   BPM Engineering                     0x0000000103d08871 $s15SwiftDataTables0aB5TableC14collectionView_13cellForItemAtSo012UICollectionF4CellCSo0kF0C_10Foundation9IndexPathVtF + 129
    8   BPM Engineering                     0x0000000103d08b15 $s15SwiftDataTables0aB5TableC14collectionView_13cellForItemAtSo012UICollectionF4CellCSo0kF0C_10Foundation9IndexPathVtFTo + 165
    9   UIKitCore                           0x00007fff46d61528 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 416
    10  UIKitCore                           0x00007fff46d61382 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31
    11  UIKitCore                           0x00007fff46d66d1b -[UICollectionView _updateVisibleCellsNow:] + 6589
    12  UIKitCore                           0x00007fff46d6bda1 -[UICollectionView layoutSubviews] + 353
    13  UIKitCore                           0x00007fff47a52ad5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    14  QuartzCore                          0x00007fff2b06e91d -[CALayer layoutSublayers] + 255
    15  QuartzCore                          0x00007fff2b073323 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    16  QuartzCore                          0x00007fff2b07fa7c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    17  QuartzCore                          0x00007fff2afc6e54 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    18  QuartzCore                          0x00007fff2affc32f _ZN2CA11Transaction6commitEv + 643
    19  QuartzCore                          0x00007fff2affcc96 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
    20  CoreFoundation                      0x00007fff23b0c667 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    21  CoreFoundation                      0x00007fff23b070fe __CFRunLoopDoObservers + 430
    22  CoreFoundation                      0x00007fff23b0777a __CFRunLoopRun + 1514
    23  CoreFoundation                      0x00007fff23b06e66 CFRunLoopRunSpecific + 438
    24  GraphicsServices                    0x00007fff38346bb0 GSEventRunModal + 65
    25  UIKitCore                           0x00007fff47578dd0 UIApplicationMain + 1621
    26  BPM Engineering                     0x0000000103bf198b main + 75
    27  libdyld.dylib                       0x00007fff516ecd29 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
ShaharHD commented 4 years ago

This is great work btw. Thank you for this. Have you got some documentation on how swift package manager now expects the source files? You're not the first person to organise the files, though this PR is far more detailed which I'm grateful for. đź‘Ť

I followed suite from here: https://swift.org/package-manager/ But after reading a bit more, I found out there's a path directive... meaning I didn't have move the directories ...

So I'll do another commit to fix that... :)

ShaharHD commented 4 years ago

Ok, rewrote the commit, this time without changing any of the folder structure = but I'm still seeing the issue.

For example DataCell.swift (and the other few) have an @IBOutlet which requires the xib file to be part of the package ... which is what causing the issue I think?

ShaharHD commented 4 years ago

Managed to remove all the XIB ... still a lot of work left - especially all the NSLayoutConstraint which needs to be converted ...

Is using https://github.com/PureLayout/PureLayout something worth thinking about?

ShaharHD commented 4 years ago

I have no problems programmatically creating those views, I find it strange that swift package manager doesn't support xib files. But that's fine. My only concern is how many users are actually reliant on the xibs and figuring out an appropriate way to deprecate them.

There's no harm in leaving them in ... just... you can't have the IBOutlet configuring in parallel to using building the code ... but I might be wrong here.

pavankataria commented 4 years ago

Hi, sorry I couldn't reply earlier! Why is NSLayoutConstraint an issue?

ShaharHD commented 4 years ago

Hi, sorry I couldn't reply earlier! Why is NSLayoutConstraint an issue?

Because it's a lot of work :) Manually converting the XML of the XIB file to actual NSLayoutConstraint (and figuring out who's inside who...)

I would love if you can assist ... as you probably know better than me the relations of the constraints you had in mind when you created the XIB files

If you can assist, do you want to move the work to your repo under a feature branch? or just give you permissions on my repo?

pavankataria commented 4 years ago

Ah is that what you meant. No problem, I can assist there. I don't mind just committing and merging this work in once it's all been taken care of my side if it's only the programmatic conversion we're waiting for.

pavankataria commented 4 years ago

Checkout this branch: https://github.com/pavankataria/SwiftDataTables/tree/feat/programmatic-cells This branch removes all xib files in the classes folder. Please do check if this works for you now? Maybe you can try your SwiftUI demo project. Let me know if you get any crashes and how you get on.

ShaharHD commented 4 years ago

@pavankataria your branch is missing the Package.swift file which is required for the swift package manager

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "SwiftDataTables",
    platforms: [ .iOS(.v8) ],
    products: [
        .library(
            name: "SwiftDataTables",
            targets: ["SwiftDataTables"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "SwiftDataTables",
            dependencies: [],
            path: "SwiftDataTables"),
        .testTarget(
            name: "SwiftDataTablesTests",
            dependencies: ["SwiftDataTables"],
            path: "Example/SwiftDataTablesTests"),
    ],
    swiftLanguageVersions: [.v5]
)
pavankataria commented 4 years ago

So the work I've done is to remove xibs completely. Can you rebase your branch off of the latest changes in master so you can just try your branch again. You may need to remove some of your commits that relate to trying to convert things to swift, which I've done for you already. It's to avoid conflicts. Or you can create another branch from master and try the Swift ui project with swift package manager and see if you get any more issues.

ShaharHD commented 4 years ago

I'll rebase off master and remove my un-needed commits.

Will update later today.

Thanks for the effort! :)

ShaharHD commented 4 years ago

@pavankataria, works even better than my conversion.

What tool did you use?

Had to update the iOS minimum version to 9 in both the swift package and cocoa pods file (some code elements you were using in the new code)

Thanks!

pavankataria commented 4 years ago

Hey, I didn’t use any tools. I just programmatically concerted the code myself. It’s not difficult to do, just need to understand the objective-c code or in this case the constraints applied in the xib to get a better understanding of the view hierarchy. Write the constraints one by one, test, and you’re good to go.

Could you do your initial test again after checking out in master with your Carthage changes to see if it crashes for you or not, and let me know how it goes on your side

ShaharHD commented 4 years ago

I've been using my master branch with no issue. As I said before

works even better than my conversion

ivanornes commented 3 years ago

Is happening the same issue when using use_frameworks! :linkage => :static in the Podfile.