VLCNetworkServerSearchBrowser.m 4.33 KB
Newer Older
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
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2016 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Vincent L. Cone <vincent.l.cone # tuta.io>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCNetworkServerSearchBrowser.h"

@interface VLCNetworkServerSearchBrowser()
@property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
@property (nonatomic, copy, nullable) NSIndexSet *filteredIndexes;
@property (nonatomic, copy, null_resettable) NSArray<id<VLCNetworkServerBrowserItem>> *filteredItems;
@property (nonatomic, null_resettable) VLCMediaList *filteredMediaList;
@end

@implementation VLCNetworkServerSearchBrowser
@synthesize delegate = _delegate;


- (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)serverBrowser
{
    self = [super init];
    if (self) {
        _serverBrowser = serverBrowser;
    }
    return self;
}

- (NSString *)title
{
    return self.serverBrowser.title;
}

- (VLCMediaList *)mediaList
{
    return self.filteredMediaList;
}

- (NSArray<id<VLCNetworkServerBrowserItem>> *)items
{
    return self.filteredItems;
}

- (void)update
{
    [self.serverBrowser update];
}

- (void)setSearchText:(NSString *)searchText
{
    if (![_searchText isEqualToString:searchText]) {
        _searchText = searchText;
        [self updateFilteredItems];
        [self.delegate networkServerBrowserDidUpdate:self];
    }
}

#pragma mark - filtering
- (void)setFilteredIndexes:(NSIndexSet *)filteredIndexes
{
    BOOL needUpdate = NO;
    if (_filteredIndexes) {
        needUpdate = ![_filteredIndexes isEqualToIndexSet:filteredIndexes];
    } else if (filteredIndexes) {
        needUpdate = YES;
    }
    if (needUpdate) {
        _filteredIndexes = [filteredIndexes copy];

        self.filteredItems = nil;
        self.filteredMediaList = nil;
    }
}
- (NSArray<id<VLCNetworkServerBrowserItem>> *)filteredItems
{
    if (!_filteredItems) {
        NSIndexSet *indexes = self.filteredIndexes;
        NSArray<id<VLCNetworkServerBrowserItem>> *items = self.serverBrowser.items;
        _filteredItems = indexes ? [items objectsAtIndexes:indexes] : items;
    }
    return _filteredItems;
}

- (VLCMediaList *)filteredMediaList
{
    if (!_filteredMediaList) {
        NSIndexSet *indexes = self.filteredIndexes;
        VLCMediaList *mediaList = self.serverBrowser.mediaList;
        if (indexes) {
            __block NSUInteger count = 0;
            VLCMediaList *filteredMediaList = [[VLCMediaList alloc] initWithArray:nil];
            [filteredMediaList lock];
            [mediaList lock];
            [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
                VLCMedia *media = [mediaList mediaAtIndex:idx];
                [filteredMediaList insertMedia:media atIndex:count];
                ++count;
            }];
            [mediaList unlock];
            [filteredMediaList unlock];

            _filteredMediaList = filteredMediaList;
        } else {
            _filteredMediaList = mediaList;
        }
    }
    return _filteredMediaList;
}

- (void)updateFilteredItems
{
    NSIndexSet *filteredIndexes = nil;
    if (self.searchText.length) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@",self.searchText];

        NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
        NSArray<id<VLCNetworkServerBrowserItem>> *items = self.serverBrowser.items;
        [items enumerateObjectsUsingBlock:^(id<VLCNetworkServerBrowserItem>  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([predicate evaluateWithObject:obj]) {
                [indexSet addIndex:idx];
            }
        }];
        filteredIndexes = indexSet;
    }
    self.filteredIndexes = filteredIndexes;
}


#pragma mark - VLCNetworkServerBrowserDelegate

- (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser
{
    [self updateFilteredItems];
    [self.delegate networkServerBrowserDidUpdate:self];
}

- (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error
{
    [self.delegate networkServerBrowser:self requestDidFailWithError:error];
}

@end