Skip to main content

Working with Albums

The Cloudike Photos SDK allows you to create and edit albums, as well as get a list of albums and their content.

Get List of Albums#

CloudikePhotosKit provides an album information for the client application through the special AlbumsAdapter object, which is based on NSFetchedResultsController.

To initialize AlbumsAdapter, you need to call the following methods:

let adapter = photosManager.albumsService.albumsAdapter(collectionView)
  • collectionView: UICollectionView - instance of the UICollectionView object used to display albums.

iOS13 or Higher Support#

In iOS13 or later, you should implement closures to initialize objects to display section headers and cells.

Cell#

var cell: ((UICollectionView, IndexPath)->UICollectionViewCell)?

Example closure for UICollectionViewCell:

adapter.cell = { collectionView, indexPath in    collectionView.dequeueReusableCell(withReuseIdentifier: <CellReuseIdentifier>, for: indexPath)}

Adapter Preparings#

This method should be called after setting up the closures.

adapter.configure()

iOS12 or Lower Support#

To display a header and a cell on devices with iOS12 or lower, you should implement the following delegate methods:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView

UICollectionViewDataSource#

You need to implement the UICollectionViewDataSource methods for your UICollectionView

// MARK: - UICollectionViewDataSourcefunc collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {    return adapter.itemsCount(section)}

Get all Albums#

Get an array which contains all albums items.

func getAlbums() -> [CloudikeAlbumItem]

Get Album#

Get an album by backend identifier:

func getAlbum(_ albumId: String) -> CloudikeAlbumItem?

The parameters:

  • albumId: String - album identifier.

Update Albums#

To start reading the albums from the backend, you need to call the following methods:

photosManager.albumsService.update(fullRefresh: fullRefresh, completion: { [weak self] (result) in    switch result {    case .success(_):        NSLog("Albums> update> success")    case let .failure(error):        NSLog("Albums> update> error: \(error)")    }})

The parameters:

  • fullRefresh: Bool - If true, then the list of albums is fully read from the backend. The refresh procedure may take a long time. If false, then the reading is incremental, only the latest changes are read.
  • completion: (Swift.Result<Void, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects.

Create Album#

To create an album, you need to call the following methods:

photosManager.albumsService.createAlbum(description: name, type: .simple, isShareForEdit: false, sharedExpiration: nil, completion: { (result) in    switch result {    case let .success(album):        NSLog("Albums> create> success: \(album)")    case let .failure(error):        NSLog("Albums> create> error: \(error)")    }})

In this example, a simple album is created. The parameters of the createAlbum function are as follows:

  • description: String - The name of the album.
  • type: NewAlbumType - The type of the album (see below).
  • isShareForEdit: Bool - If true, then the album can be edited by accessing the album link.
  • sharedExpiration: Int64? - Expiration of shared link (optional).
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the CloudikeAlbumItem and the error objects.

An album can be one of the following types:

  • NewAlbumType.simple - The type of a simple album.
  • NewAlbumType.shared - The type of a shared album. An album that has a link for viewing and/or editing (depending on the isSharedForEdit parameter).
  • NewAlbumType.hiddenShared - The type of a hidden shared album.

Delete Album#

To delete an album, you need to call the following methods:

photosManager.albumsService.deleteAlbum(id: albumId, completion: { [weak self] (result) in    switch result {    case .success(_):        NSLog("Album> delete> success")    case let .failure(error):        NSLog("Album> delete> error: \(error)")    }})

The parameters of the deleteAlbum function are as follows:

  • id: String - The identifier of the album.
  • completion: (Swift.Result<Void, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects.

Update Album items#

To update albums photos/videos from the backend, you need to call the following methods:

photosManager.albumsService.updateItems(id: String, fullRefresh: fullRefresh, completion: { [weak self] (result) in    switch result {    case .success(_):        NSLog("Albums> update> success")    case let .failure(error):        NSLog("Albums> update> error: \(error)")    }})

The parameters of the updateItems function are as follows:

  • id: String - The identifier of the album.
  • fullRefresh: Bool - If true, then the list of albums is fully read from the backend. The refresh procedure may take a long time. If false, then the reading is incremental, only the latest changes are read.
  • completion: (Swift.Result<Void, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects.

Update Album Properties#

To update the album properties, you need to call the following methods:

photosManager.albumsService.updateAlbum(id: albumId, completion: { [weak self] (result) in    switch result {    case let .success(album):        NSLog("Album> updateProperties> success: \(album)")    case let .failure(error):        NSLog("Album> updateProperties> error: \(error)")    }})

The parameters of the updateAlbum function are as follows:

  • id: String - The identifier of the album.
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive CloudikeAlbumItem and the error objects.

Edit Album Properties#

To edit the album properties, you need to call the following methods:

photosManager.albumsService.editAlbum(id: albumId, description: name, type: type, isShareForEdit: isShareForEdit, sharedExpiration: expirationTime, completion: { [weak self] (result) in    switch result {    case let .success(album):        NSLog("Album> editProperties> success: \(album)")    case let .failure(error):        NSLog("Album> editProperties> error: \(error)")    }})

The parameters of the editAlbum function are as follows:

  • id: String - The identifier of the album.
  • description: String - The name of the album.
  • type: NewAlbumType - The type of the album (see below).
  • isShareForEdit: Bool - If true, then the album can be edited by accessing the album link.
  • sharedExpiration: Int64? - Expiration of a shared link (optional).
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the CloudikeAlbumItem and the error objects.

Create Public Links#

To create a public link to an album, you can use the shareAlbum function.

photosManager.albumsService.shareAlbum(id: albumId, completion: { result in    switch result {    case .success(let albumItem):        print("Album shared link> \(albumItem.sharedLink)")    case let .failure(error):        print("Album sharing error: \(error)")    }})

The parameters of the shareAlbum function are as follows:

  • id: String - The identifier of the album.
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects. CloudikeAlbumItem contains result link as a sharedLink property

Delete Public Links#

To delete a public link from album, you can use the shareAlbum function.

photosManager.albumsService.unshareAlbum(id: albumId, completion: { result in    switch result {    case .success(let albumItem):        NSLog("Album sharing success \(albumItem)")    case let .failure(error):        NSLog("Album sharing error: \(error)")    }})

The parameters of the unshareAlbum function are as follows:

  • id: String - The identifier of the album.
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects.

Working with Album Content

Get Album Content#

CloudikePhotosKit provides photo information in the album for the client application through the special AlbumItemsAdapter object, which is based on NSFetchedResultsController.

To initialize AlbumItemsAdapter, you need to call the following methods:

let adapter = photosManager.albumsService.albumItemsAdapter(albumId: id, collectionView: collectionView)

The parameters of the albumItemsAdapter function are as follows:

  • albumId: String - The identifier of the album.
  • collectionView: UICollectionView - instance of the UICollectionView object used to display photos.

Update Album Content#

To start reading the album content (list of album photos) from the backend, you need to call the following methods:

photosManager.albumsService.updateItems(id: albumId, fullRefresh: fullRefresh, completion: { [weak self] (result) in    switch result {    case .success(_):        NSLog("Album> updateItems> success")    case let .failure(error):        NSLog("Album> updateItems> error: \(error)")    }})

The parameters of the updateItems function are as follows:

  • id: String - The identifier of the album.
  • fullRefresh: Bool - If true, then the list of photos is fully read from the backend. The refresh procedure may take a long time. If false, then the reading is incremental, only the latest changes are read.
  • completion: (Swift.Result<Void, CloudikePhotosError>) -> () - The completion handler to receive the result and the error objects.

Add/delete Photos to/from Album#

To add photos to the existing album, you need to call the following methods:

photosManager.albumsService.editAlbumItems(id: albumId, itemsForAdd: items, itemsForDelete: [], completion: { [weak self] (result) in    switch result {    case let .success(album):        NSLog("Album> editItems> success: \(album)")    case let .failure(error):        NSLog("Album> editItems> error: \(error)")    }})

The parameters:

The parameters of the editAlbumItems function are as follows:

  • id: String - The identifier of the album.
  • itemsForAdd: Array<CloudikeMediaItem> - items to be added.
  • itemsForDelete: Array<CloudikeMediaItem> - items to be deleted.
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the CloudikeAlbumItem and the error objects.

The function editAlbumItems returns the Cancellable object. If you need to cancel editing call, use the following operation:

cancellable.cancel()

Remove Photos from Album#

To remove photos from an album, you need to call the following methods:

photosManager.albumsService.editAlbumItems(id: albumId, itemsForAdd: [], itemsForDelete: items, completion: { [weak self] (result) in    switch result {    case let .success(album):        NSLog("Album> editItems> success: \(album)")    case let .failure(error):        NSLog("Album> editItems> error: \(error)")    }})

The parameters:

  • id: String - The identifier of the album.
  • itemsForAdd: [CloudikeMediaItem] - The list of photos to be added to the album.
  • itemsForDelete: [CloudikeMediaItem] - The list of photos to be removed from the album.
  • completion: (Swift.Result<CloudikeAlbumItem, CloudikePhotosError>) -> () - The completion handler to receive the CloudikeAlbumItem and the error objects.