summaryrefslogtreecommitdiff
path: root/ios/iosremote/iosremote/Communication/SlideShow.m
blob: 8f714a4ef031b2917a51c4f84949da6a69fdec64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


#import "SlideShow.h"
#import "NSString+Base64.h"
#import "SlideShowViewController~iphone.h"
#import "UIImageView+setImageAnimated.h"
#import <dispatch/dispatch.h>

@interface SlideShow()

@property (atomic, strong) NSMutableDictionary* imagesDictionary;
@property (atomic, strong) NSMutableDictionary* notesDictionary;

@property (atomic, strong) NSMutableDictionary* loadBuffer;
@property (nonatomic, strong) id slideShowImageReadyObserver;
@property (nonatomic, strong) id slideShowNoteReadyObserver;

@end

@implementation SlideShow

@synthesize size = _size;
@synthesize currentSlide = _currentSlide;
@synthesize delegate = _delegate;
@synthesize secondaryDelegate = _secondaryDelegate;

dispatch_queue_t backgroundQueue;
NSLock *dictLock;
NSString *sTitle;

- (NSString *) title
{
    return sTitle;
}

- (void) setTitle:(NSString *)title
{
    sTitle = title;
}

- (void) setDelegate:(id<AsyncLoadHorizontalTableDelegate>)delegate
{
    NSLog(@"setting slideshow delegate to a %@", [delegate class]);
    _delegate = delegate;
}

- (SlideShow *) init{
    self = [super init];
    NSLog(@"SlideShow got init");
    self.imagesDictionary = [[NSMutableDictionary alloc] init];
    self.notesDictionary = [[NSMutableDictionary alloc] init];
    self.loadBuffer = [[NSMutableDictionary alloc] init];
    _size = 0;
    _currentSlide = 0;
    
    backgroundQueue = dispatch_queue_create("org.libreoffice.iosremote.bgqueue", DISPATCH_QUEUE_CONCURRENT);
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    
    /**
     This observer waits for storage updates like new image added or notes received. 
     It then checks in the loadBuffer to see if there is a view waiting for this update in loadBuffer, if yes, it loads it up and remove the waiting entry.
     loadBuffer stores key-value pair with viewTag as a key and slideIndex as value. 
     For the same view, we only keep the last requested slide index on the waiting list.
     It is thus indispensable to identify each view with an unique tag in its view controller. Here we use 0-20 to indentify central vc views and 21-N for swipe-in tableViewController which allows direct slide number change. 
     We handle lecturer's notes at the same time as an entry in the load buffer via an instrospection.
     */
    self.slideShowImageReadyObserver =[[NSNotificationCenter defaultCenter]
                                              addObserverForName:@"storage_update_ready"
                                                          object:nil
                                                           queue:mainQueue
                                                      usingBlock:^(NSNotification *note) {
                                                          dispatch_async(backgroundQueue, ^(void) {
                                                          if ([[self.loadBuffer allKeysForObject:[NSNumber numberWithInt:[[[note userInfo] objectForKey:@"index"] intValue]]] count]) {
                                                              NSArray * tagArray = [self.loadBuffer allKeysForObject:[NSNumber numberWithInt:[[[note userInfo] objectForKey:@"index"] intValue]]];
                                                              for (NSNumber *tag in tagArray) {
                                                                  UIView * view;
                                                                  if ([tag integerValue] > 20)
                                                                      view = [[self.secondaryDelegate view] viewWithTag: [tag integerValue]];
                                                                  else if ([tag integerValue] >= 0){
                                                                      NSLog(@"Received image, try to load for tag:%d", [tag integerValue]);
                                                                      view = [[self.delegate view] viewWithTag:[tag integerValue]];
                                                                      if (!view) {
                                                                          if (!self.delegate) {
                                                                              NSLog(@"Delegate nil");
                                                                          } else if (![self.delegate view])
                                                                              NSLog(@"view nil");
                                                                      }
                                                                      NSLog(@"Will load it into a %@", [view class]);
                                                                  }
                                                                  else
                                                                      view = [[self.delegate horizontalTableView] viewWithTag:[tag integerValue]];
                                                                  if ([view isKindOfClass:[UIImageView class]]){
                                                                      UIImage *image = [self.imagesDictionary objectForKey:[self.loadBuffer objectForKey:tag]];
                                                                      if (image) {
//                                                                          image = [image resizedImage:view.frame.size interpolationQuality:kCGInterpolationDefault];
                                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                                              NSLog(@"Setting image to tag: %ld", (long)[tag integerValue]);
                                                                             [(UIImageView *)view setImage:image animated:YES];
                                                                          });
                                                                          [self.loadBuffer removeObjectForKey:tag];
                                                                      }
                                                                  }
                                                                  else if ([view isKindOfClass:[UIWebView class]]){
//                                                                      NSLog(@"Async notes");
                                                                      NSString *note = [self.notesDictionary objectForKey:[self.loadBuffer objectForKey:tag]];
                                                                      if (note) {
                                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                                              [(UIWebView *)view loadHTMLString:note baseURL:nil];
                                                                          });
                                                                          [self.loadBuffer removeObjectForKey:tag];
                                                                      }
                                                                  } else if ([view isKindOfClass:[UITableViewCell class]]){
                                                                      UIImage *image = [self.imagesDictionary objectForKey:[self.loadBuffer objectForKey:tag]];
                                                                      if (image){
                                                                          UIImageView *imageView = (UIImageView *)[view viewWithTag:1];
                                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                                              [imageView setImage:image animated:YES];
                                                                          });
                                                                          [self.loadBuffer removeObjectForKey:tag];
                                                                      }
                                                                  }
                                                              }
                                                          }
                                                          });
                                                      }];
    dictLock = [[NSLock alloc] init];
    return self;
}

- (void) putImage: (NSString *)img AtIndex: (uint) index{
        NSData* data = [NSData dataWithBase64String:img];
        UIImage* image = [UIImage imageWithData:data];
        [dictLock lock];
        [self.imagesDictionary setObject:image forKey:[NSNumber numberWithUnsignedInt:index]];
        [dictLock unlock];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"storage_update_ready"
                                                            object:nil
                                                          userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"index"]];

}

- (void) putNotes: (NSString *)notes AtIndex: (uint) index{
    [self.notesDictionary setObject:notes forKey:[NSNumber numberWithUnsignedInt:index]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"storage_update_ready"
                                                        object:nil
                                                      userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"index"]];
}

- (void) getContentAtIndex: (uint) index forView: (UIView*) view
{
    if (index >= self.size)
    {
        if ([view isKindOfClass:[UIImageView class]])
            [(UIImageView* )view setImage:[UIImage imageNamed:@"slide_finished"]];
        else if ([view isKindOfClass:[UITableViewCell class]])
        {
            UIImageView *image = (UIImageView *)[view viewWithTag:1];
            [image setImage:[UIImage imageNamed:@"slide_finished"]];
        }
        else if ([view isKindOfClass:[UIWebView class]])
            [(UIWebView* )view loadHTMLString: @"SlideShow finished" baseURL:nil];
        return;
    }
    if (![self.imagesDictionary objectForKey:[NSNumber numberWithUnsignedInt:index]])
    {
        NSLog(@"Didn't find %u, putting tag: %d into buffer", index, [view tag]);
        [self.loadBuffer setObject:[NSNumber numberWithInt:index ] forKey:[NSNumber numberWithInt:[view tag]]];
    }
    else{
        if ([view isKindOfClass:[UIImageView class]])
            [(UIImageView* )view setImage:[self.imagesDictionary objectForKey:[NSNumber numberWithUnsignedInt:index]]];
        else if ([view isKindOfClass:[UITableViewCell class]])
        {
            UIImageView *image = (UIImageView *)[view viewWithTag:1];
            [image setImage:[self.imagesDictionary objectForKey:[NSNumber numberWithUnsignedInt:index]]];
        }
        else if ([view isKindOfClass:[UIWebView class]])
            [(UIWebView* )view loadHTMLString: [self.notesDictionary objectForKey:[NSNumber numberWithUnsignedInt:index]] baseURL:nil];
    }
}

@end