pentarex / react-native-gallery-manager

30 stars 41 forks source link

Suggestion: Convert the library to swift #5

Open rangav opened 6 years ago

rangav commented 6 years ago

Hi @pentarex

I have another suggestion - Better to convert this props from Objective -C to Swift to use latest features and better syntax and also Swift will be preferred language going forward for Apple.

I have wrote previously for my project code in swift and created a objc file bridge to expose method to RN.

Sample below:

// GalleryManager.swift

@objc func getAlbums(_ resolve:RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void{
    checkPhotoLibraryConfig()
    // check if the permission is set in info.plist
    let fetchOptions = PHFetchOptions()
    let albums: PHFetchResult<PHAssetCollection>? = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
    let result = NSMutableArray();

    albums?.enumerateObjects({ (album, index, stop) in
      result.add(["title": album.localizedTitle as Any, "assetCount": album.estimatedAssetCount])
    })

    resolve(["albums": result, "totalAlbums": albums?.count ?? 0])
  }

// bridging header file
GalleryManager-Bridging-Header.h

// then exposing to RN here
GalleryManager-bridge.m

@interface RCT_EXTERN_MODULE(GalleryManager, NSObject)

RCT_EXTERN_METHOD(getAlbums: (RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

@end

Let me know if its feasible to convert to Swift

Note - I did not test the above code - just for ref.

pentarex commented 6 years ago

yeah I was thinking about that as well, but my experience in the past shows me that compatibility is an issue. When apple introduced swift 3, it was hell to convert the code from swift 2 to swift 3. I don't know how is it now with Swift 4, but Objective C is not changing. And now its working with just fine.

rangav commented 6 years ago

Swift 4 is fairly stable now, there might be minor improvements as well in future.
But better to be future ready and gain experience in swift.