Skip to content

Commit

Permalink
- OCActivity
Browse files Browse the repository at this point in the history
	- fill API gaps
	- implement missing pieces of OCSyncRecordActivity
	- OCCoreItemListTask now is an OCActivitySource, OCCore+ItemList now emits activity updates
- OCCore
	- use localID instead of fileID to track progress on a per-item basis
	- now posts OCCoreItemBeginsHavingProgress and OCCoreItemStopsHavingProgress notifications when an item starts having progress, and when the last progress has completed for an item
	- add localID property to NSProgress
- Sync Engine
	- OCSyncAction now properly serialized/deserializes the localizedDescription
	- gated access to OCDatabase for adding, updating and removing SyncRecords, used to emit activity updates
  • Loading branch information
felix-schwarz committed Jan 28, 2019
1 parent 8dcda9c commit ab78777
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 65 deletions.
2 changes: 2 additions & 0 deletions ownCloudSDK/Activity/OCActivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)withIdentifier:(OCActivityIdentifier)identifier description:(NSString *)description statusMessage:(nullable NSString *)statusMessage ranking:(NSInteger)ranking;

- (instancetype)initWithIdentifier:(OCActivityIdentifier)identifier;

- (NSError *)applyUpdate:(OCActivityUpdate *)update; //!< Applies an update to the activity. Returns nil if the update could be applied, an error otherwise.
- (NSError *)applyValue:(nullable id <NSObject>)value forKeyPath:(NSString *)keyPath; //!< Applies a new value to a keypath (entrypoint for subclassing)

Expand Down
10 changes: 10 additions & 0 deletions ownCloudSDK/Activity/OCActivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ + (instancetype)withIdentifier:(OCActivityIdentifier)identifier description:(NSS
return (activity);
}

- (instancetype)initWithIdentifier:(OCActivityIdentifier)identifier
{
if ((self = [super init]) != nil)
{
_identifier = identifier;
}

return (self);
}

- (void)cancel
{
if (_isCancellable)
Expand Down
5 changes: 5 additions & 0 deletions ownCloudSDK/Activity/OCActivityUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ typedef NS_ENUM(NSUInteger,OCActivityUpdateType)
};

@interface OCActivityUpdate : NSObject
{
OCActivityUpdateType _type;
OCActivityIdentifier _identifier;
NSMutableDictionary <NSString *, id<NSObject>> *_updatesByKeyPath;
}

@property(readonly) OCActivityUpdateType type; //!< The type of activity

Expand Down
9 changes: 0 additions & 9 deletions ownCloudSDK/Activity/OCActivityUpdate.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@

#import "OCActivityUpdate.h"

@interface OCActivityUpdate ()
{
OCActivityUpdateType _type;
OCActivityIdentifier _identifier;
NSMutableDictionary <NSString *, id<NSObject>> *_updatesByKeyPath;
}

@end

@implementation OCActivityUpdate

@synthesize type = _type;
Expand Down
8 changes: 7 additions & 1 deletion ownCloudSDK/Activity/OCSyncRecordActivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ NS_ASSUME_NONNULL_BEGIN
@property(assign) OCEventType type;
@property(assign,nonatomic) OCSyncRecordState recordState;

- (instancetype)initWithSyncRecord:(OCSyncRecord *)syncRecord;
- (instancetype)initWithSyncRecord:(OCSyncRecord *)syncRecord identifier:(OCActivityIdentifier)identifier;

@end

@interface OCActivityUpdate (OCSyncRecord)

- (instancetype)withRecordState:(OCSyncRecordState)recordState;

@end

Expand Down
26 changes: 20 additions & 6 deletions ownCloudSDK/Activity/OCSyncRecordActivity.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

@implementation OCSyncRecordActivity

- (instancetype)initWithSyncRecord:(OCSyncRecord *)syncRecord
- (instancetype)initWithSyncRecord:(OCSyncRecord *)syncRecord identifier:(OCActivityIdentifier)identifier
{
if ((self = [super init]) != nil)
if ((self = [super initWithIdentifier:identifier]) != nil)
{
_recordID = syncRecord.recordID;
_type = syncRecord.action.actionEventType;
Expand All @@ -47,21 +47,35 @@ - (void)setRecordState:(OCSyncRecordState)recordState
switch (_recordState)
{
case OCSyncRecordStatePending:
break;

case OCSyncRecordStateReady:
self.state = OCActivityStatePending;
self.localizedStatusMessage = OCLocalized(@"Pending");
break;

case OCSyncRecordStateProcessing:
break;

case OCSyncRecordStateCompleted:
self.state = OCActivityStateRunning;
self.localizedStatusMessage = OCLocalized(@"Running");
break;

case OCSyncRecordStateFailed:
self.state = OCActivityStateFailed;
self.localizedStatusMessage = OCLocalized(@"Failed");
break;
}
}
}

@end

@implementation OCActivityUpdate (OCSyncRecord)

- (instancetype)withRecordState:(OCSyncRecordState)recordState
{
_updatesByKeyPath[@"recordState"] = @(recordState);

return (self);
}

@end

17 changes: 5 additions & 12 deletions ownCloudSDK/Core/ItemList/OCCore+ItemList.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ - (OCCoreItemListTask *)_scheduleItemListTaskForPath:(OCPath)path
{
_itemListTasksByPath[task.path] = task;

[self.activityManager update:[OCActivityUpdate publishingActivityFor:task]];

// Start item list task
if (task.syncAnchorAtStart == nil)
{
Expand Down Expand Up @@ -153,6 +155,8 @@ - (void)_finishedItemListTask:(OCCoreItemListTask *)finishedTask
[self scheduleNextItemListTask];
}
}

[self.activityManager update:[OCActivityUpdate unpublishActivityFor:finishedTask]];
}
}

Expand Down Expand Up @@ -726,7 +730,6 @@ - (void)startCheckingForUpdates
- (void)_checkForUpdatesNotBefore:(NSDate *)notBefore
{
OCEventTarget *eventTarget;
OCActivityIdentifier activityIdentifier = [@"CheckForUpdates." stringByAppendingString:NSUUID.UUID.UUIDString];

if (self.state != OCCoreStateRunning)
{
Expand All @@ -735,17 +738,13 @@ - (void)_checkForUpdatesNotBefore:(NSDate *)notBefore

[self beginActivity:@"Check for updates"];

eventTarget = [OCEventTarget eventTargetWithEventHandlerIdentifier:self.eventHandlerIdentifier userInfo:@{ @"activityIdentifier" : activityIdentifier } ephermalUserInfo:@{ @"endActivity" : @(YES) }];

[self.activityManager update:[OCActivityUpdate publishingActivity:[OCActivity withIdentifier:activityIdentifier description:@"Scanning for changes.." statusMessage:nil ranking:0]]];
eventTarget = [OCEventTarget eventTargetWithEventHandlerIdentifier:self.eventHandlerIdentifier userInfo:nil ephermalUserInfo:@{ @"endActivity" : @(YES) }];

[self.connection retrieveItemListAtPath:@"/" depth:0 notBefore:notBefore options:((notBefore != nil) ? @{ OCConnectionOptionIsNonCriticalKey : @(YES) } : nil) resultTarget:eventTarget];
}

- (void)_handleRetrieveItemListEvent:(OCEvent *)event sender:(id)sender
{
OCActivityIdentifier activityIdentifier = (OCActivityIdentifier)event.userInfo[@"activityIdentifier"];

OCLogDebug(@"Handling background retrieved items: error=%@, path=%@, depth=%d, items=%@", OCLogPrivate(event.error), OCLogPrivate(event.path), event.depth, OCLogPrivate(event.result));

// Handle result
Expand Down Expand Up @@ -779,12 +778,6 @@ - (void)_handleRetrieveItemListEvent:(OCEvent *)event sender:(id)sender
}
}

// Update activity
if (activityIdentifier != nil)
{
[self.activityManager update:[OCActivityUpdate unpublishActivityForIdentifier:activityIdentifier]];
}

// Schedule next
if ((event.depth == 0) && ([event.path isEqual:@"/"]))
{
Expand Down
3 changes: 2 additions & 1 deletion ownCloudSDK/Core/ItemList/OCCoreItemListTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "OCTypes.h"
#import "OCItem.h"
#import "OCCoreItemList.h"
#import "OCActivity.h"

@class OCCore;
@class OCCoreItemListTask;
Expand All @@ -32,7 +33,7 @@ typedef NS_ENUM(NSUInteger, OCCoreTaskMergeStatus)

typedef void(^OCCoreItemListTaskChangeHandler)(OCCore *core, OCCoreItemListTask *task);

@interface OCCoreItemListTask : NSObject
@interface OCCoreItemListTask : NSObject <OCActivitySource>

@property(weak) OCCore *core;
@property(strong) OCPath path;
Expand Down
37 changes: 36 additions & 1 deletion ownCloudSDK/Core/ItemList/OCCoreItemListTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
#import "OCCore+ConnectionStatus.h"
#import "OCCore+ItemList.h"
#import "OCMacros.h"
#import "NSProgress+OCExtensions.h"

@interface OCCoreItemListTask ()
{
OCActivityIdentifier _activityIdentifier;
}

@end

@implementation OCCoreItemListTask

Expand Down Expand Up @@ -136,7 +144,9 @@ - (void)_updateRetrievedSet
void (^RetrieveItems)(OCItem *parentDirectoryItem) = ^(OCItem *parentDirectoryItem){
[self->_core queueConnectivityBlock:^{
[self->_core queueRequestJob:^(dispatch_block_t completionHandler) {
[self->_core.connection retrieveItemListAtPath:self.path depth:1 completionHandler:^(NSError *error, NSArray<OCItem *> *items) {
NSProgress *retrievalProgress;

retrievalProgress = [self->_core.connection retrieveItemListAtPath:self.path depth:1 completionHandler:^(NSError *error, NSArray<OCItem *> *items) {
[self->_core queueBlock:^{ // Update inside the core's serial queue to make sure we never change the data while the core is also working on it
OCSyncAnchor latestSyncAnchor = [self.core retrieveLatestSyncAnchorWithError:NULL];

Expand Down Expand Up @@ -221,6 +231,11 @@ - (void)_updateRetrievedSet
completionHandler();
}];
}];

if (retrievalProgress != nil)
{
[self.core.activityManager update:[[OCActivityUpdate updatingActivityFor:self] withProgress:retrievalProgress]];
}
}];
}];
};
Expand Down Expand Up @@ -299,4 +314,24 @@ - (void)_update
[_core endActivity:@"update unstarted sets"];
}

#pragma mark - Activity source
- (OCActivityIdentifier)activityIdentifier
{
if (_activityIdentifier == nil)
{
_activityIdentifier = [@"ItemListTask:" stringByAppendingString:NSUUID.UUID.UUIDString];
}

return (_activityIdentifier);
}

- (OCActivity *)provideActivity
{
OCActivity *activity = [OCActivity withIdentifier:self.activityIdentifier description:[NSString stringWithFormat:@"Retrieving items for %@", self.path] statusMessage:nil ranking:0];

activity.progress = NSProgress.indeterminateProgress;

return (activity);
}

@end
5 changes: 4 additions & 1 deletion ownCloudSDK/Core/OCCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ NS_ASSUME_NONNULL_BEGIN
BOOL _automaticItemListUpdatesEnabled;
NSDate *_lastScheduledItemListUpdateDate;

NSMutableDictionary <OCFileID, NSMutableArray<NSProgress *> *> *_progressByFileID;
NSMutableDictionary <OCFileID, NSMutableArray<NSProgress *> *> *_progressByLocalID;

__weak id <OCCoreDelegate> _delegate;
}
Expand Down Expand Up @@ -293,3 +293,6 @@ extern OCConnectionSignalID OCConnectionSignalIDCoreOnline;
extern OCCoreOption OCCoreOptionImportByCopying; //!< [BOOL] Determines whether -[OCCore importFileNamed:..] should make a copy of the provided file, or move it (default).
extern OCCoreOption OCCoreOptionImportTransformation; //!< [OCCoreImportTransformation] Transformation to be applied on local item before upload
extern OCCoreOption OCCoreOptionReturnImmediatelyIfOfflineOrUnavailable; //!< [BOOL] Determines whether -[OCCore downloadItem:..] should return immediately if the core is currently offline or unavailable.

extern NSNotificationName OCCoreItemBeginsHavingProgress; //!< Notification sent when an item starts having progress. The object is the localID of the item.
extern NSNotificationName OCCoreItemStopsHavingProgress; //!< Notification sent when an item no longer has any progress. The object is the localID of the item.
Loading

0 comments on commit ab78777

Please sign in to comment.