1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-02 09:27:55 +01:00
Telegram/Telegraph/TGSynchronizationStateSignal.m
2015-10-01 19:19:52 +03:00

92 lines
2.4 KiB
Objective-C

#import "TGSynchronizationStateSignal.h"
#import "TGTelegramNetworking.h"
#import "ActionStage.h"
@interface TGSynchronizationStateHelper : NSObject <ASWatcher>
{
void (^_updated)(NSNumber *);
}
@property (nonatomic, strong) ASHandle *actionHandle;
@end
@implementation TGSynchronizationStateHelper
- (instancetype)initWithUpdated:(void (^)(NSNumber *))updated
{
self = [super init];
if (self != nil)
{
_updated = [updated copy];
_actionHandle = [[ASHandle alloc] initWithDelegate:self];
[ActionStageInstance() requestActor:@"/tg/service/synchronizationstate" options:nil flags:0 watcher:self];
[ActionStageInstance() watchForPaths:@[@"/tg/service/synchronizationstate"] watcher:self];
}
return self;
}
- (void)dealloc
{
[_actionHandle reset];
[ActionStageInstance() removeWatcher:self];
}
- (void)actionStageResourceDispatched:(NSString *)path resource:(id)resource arguments:(id)__unused arguments
{
if ([path isEqualToString:@"/tg/service/synchronizationstate"])
{
[self actorCompleted:ASStatusSuccess path:path result:resource];
}
}
- (void)actorCompleted:(int)status path:(NSString *)path result:(id)result
{
if ([path isEqualToString:@"/tg/service/synchronizationstate"])
{
if (status == ASStatusSuccess)
{
int state = [((SGraphObjectNode *)result).object intValue];
TGSynchronizationStateValue value = TGSynchronizationStateSynchronized;
if (state & 2)
{
if (state & 4)
value = TGSynchronizationStateWaitingForNetwork;
else
value = TGSynchronizationStateConnecting;
}
else if (state & 1)
value = TGSynchronizationStateUpdating;
if (_updated)
_updated(@((int)value));
}
}
}
@end
@implementation TGSynchronizationStateSignal
+ (SSignal *)synchronizationState
{
return [[SSignal alloc] initWithGenerator:^id<SDisposable>(SSubscriber *subscriber)
{
TGSynchronizationStateHelper *helper = [[TGSynchronizationStateHelper alloc] initWithUpdated:^(NSNumber *value)
{
[subscriber putNext:value];
}];
return [[SBlockDisposable alloc] initWithBlock:^
{
[helper description]; //keep reference
}];
}];
}
@end