Exemple #1
0
		public Task<string> GetAlbumLocalIdentifier ()
		{
			if (albumLocalIdTcs == null || (int)albumLocalIdTcs.Task.Status >= 5) {
				albumLocalIdTcs = new TaskCompletionSource<string> ();
			} else if ((int)albumLocalIdTcs.Task.Status <= 4) {
				return albumLocalIdTcs.Task;
			}

			PHPhotoLibrary.RequestAuthorization(async status => {
			
				if (status == PHAuthorizationStatus.Authorized) {

					if (!string.IsNullOrEmpty(albumLocalIdentifier)) {
						
						albumLocalIdTcs.TrySetResult(albumLocalIdentifier);

					} else {

						var keychain = new Keychain<TrapAlbum> (new TrapAlbum ());
						var values = keychain.GetStoredKeyValues();

						var identifier = values.ContainsKey(DataKeys.LocalIdentifier) ? values[DataKeys.LocalIdentifier] : null;
						if (identifier != null) {

							// ensure the album with that localIdentifier is there
							var collection = PHAssetCollection.FetchAssetCollections(new [] { identifier }, null)?.firstObject as PHAssetCollection;
							if (collection != null) {
									
								albumLocalIdentifier = identifier;

								if (!albumLocalIdTcs.TrySetResult(identifier)) {
									var ex = new Exception ("GetAlbumLocalIdentifier Failed");
									albumLocalIdTcs.TrySetException(ex);
									// Log.Error(ex);
								}
							} else {
								if (!albumLocalIdTcs.TrySetResult(await createAlbumAndSaveLocalIdentifier())) {
									var ex = new Exception ("GetAlbumLocalIdentifier Failed");
									albumLocalIdTcs.TrySetException(ex);
									// Log.Error(ex);
								}
							}
						} else {
							if (!albumLocalIdTcs.TrySetResult(await createAlbumAndSaveLocalIdentifier())) {
								var ex = new Exception ("GetAlbumLocalIdentifier Failed");
								albumLocalIdTcs.TrySetException(ex);
								// Log.Error(ex);
							}
						}
					}
				} else {
					var ex = new Exception ("Unauthorized");
					albumLocalIdTcs.TrySetException(ex);
					// Log.Error(ex);
				}
			});

			return albumLocalIdTcs.Task;
		}