motion-assets-library 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae60627d5fcf001d02990ac44609f6c0d5ed3764
4
- data.tar.gz: bdd66da4c30fffa15d8bdd0e198e280f31c2a0aa
3
+ metadata.gz: 3e02eb3f8cd1a385657cfb4c9897b0cae4c90602
4
+ data.tar.gz: d6019f9f6e859ace296f0500ce9964c84beb3606
5
5
  SHA512:
6
- metadata.gz: faf250119a22cde0e397229d9e38689bf655dc231ed34b7a5365e5692f6a5df3504e3e6109b400c912211d3e72b0a397b7be6564b5563c652cb8872976a2ab8f
7
- data.tar.gz: 00c4f9802505a39fd9ebdf0fe604d7208044b49de91ffed844370bd1464a2aba7bf526c0b99847de5e871cea803cbb0e148a742b9c4f1b4eff312f83b21d90b0
6
+ metadata.gz: 2e9811d2c90b27a44ac7eb5cae107deb4c6838ab78de516c6ef0a21205f7d22045a94b88bf6cd2458e0ada4f2b6151c67f20f7dcbb4194197160d26a94b74686
7
+ data.tar.gz: 7fa87d1c8846236c637739b7686c03c2a417d4ea483dd1e0ca44b44f788c05dfba13dad519e5f889bfca50f9866ec1d959ea3b0ee515a531118dee6792b3e24f
@@ -0,0 +1,81 @@
1
+ class Motion
2
+ class AssetsLibrary
3
+ class Saver
4
+ attr_reader :success_callback, :failure_callback, :denied_callback
5
+
6
+ def failure(callback)
7
+ @failure_callback = callback
8
+ end
9
+
10
+ def denied(callback)
11
+ @denied_callback = callback
12
+ end
13
+
14
+ def save(image, &block)
15
+ save(image, to_album: nil, &block)
16
+ end
17
+
18
+ def save(image, to_album: album_name, &block)
19
+ @success_callback = block
20
+
21
+ if access_denied?
22
+ denied_callback.call if denied_callback
23
+ else
24
+ do_save(image, album: album_name)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def do_save(image, album: album_name)
31
+ asset_url = nil
32
+
33
+ asset_url = assets_library.writeImageToSavedPhotosAlbum(image.CGImage,
34
+ orientation: image.imageOrientation,
35
+ completionBlock: lambda { |asset_url, error|
36
+ if error
37
+ failure_callback.call(error) if failure_callback
38
+ else
39
+ add_asset_url(asset_url, to_album: album_name) if album_name
40
+
41
+ success_callback.call(asset_url) if success_callback
42
+ end
43
+ })
44
+ end
45
+
46
+ def add_asset_url(asset_url, to_album: album_name)
47
+ album_found = false
48
+
49
+ assets_library.enumerateGroupsWithTypes(ALAssetsGroupAlbum, usingBlock: lambda { |group, stop|
50
+ if group && album_name === group.valueForProperty(ALAssetsGroupPropertyName)
51
+ album_found = stop = true
52
+
53
+ assets_library.assetForURL(asset_url, resultBlock: lambda { |asset|
54
+ group.addAsset(asset)
55
+ }, failureBlock: nil)
56
+ end
57
+
58
+ if album_found == false
59
+ assets_library.addAssetsGroupAlbumWithName(album_name, resultBlock: lambda { |group|
60
+ assets_library.assetForURL(asset_url, resultBlock: lambda { |asset|
61
+ group.addAsset(asset)
62
+ }, failureBlock: nil)
63
+ }, failureBlock: nil)
64
+ end
65
+ }, failureBlock: nil)
66
+ end
67
+
68
+ def assets_library
69
+ @assets_library ||= ALAssetsLibrary.alloc.init
70
+ end
71
+
72
+ def access_denied?
73
+ [ALAuthorizationStatusDenied, ALAuthorizationStatusRestricted].include? authorization_status
74
+ end
75
+
76
+ def authorization_status
77
+ ALAssetsLibrary.authorizationStatus
78
+ end
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-assets-library
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devon Blandin
@@ -36,6 +36,7 @@ files:
36
36
  - lib/project/assets_library/asset_wrapper.rb
37
37
  - lib/project/assets_library/data_sources/assets_data_source.rb
38
38
  - lib/project/assets_library/loader.rb
39
+ - lib/project/assets_library/saver.rb
39
40
  - lib/project/assets_library/views/asset_cell.rb
40
41
  - lib/project/assets_library/views/assets_collection_view.rb
41
42
  - lib/project/assets_library.rb