TwitterKit filter timeline, but vice versa?

I am showing twitter timeline in my app via Fabric

TwitterKit

.

I want to filter the timeline to only show @ specific user's tweets. I read the documentation from here on how to filter tweets in the timeline. Unfortunately for me the feature they provide excludes any tweet that contains the specified keyword / s in the timeline.

The behavior I'm looking for is one that will only include tweets that contain the specified keyword / s in the timeline.

Can you do this with TwitterKit? I searched for an inverse filter for this, but it doesn't seem to be there.

+3


source to share


1 answer


First of all, it's not clear what your source timeline is. Have you considered using TWTRSearchTimelineDataSource and setting up the Search API Query ? The search API supports complex queries with some logical functionality, so you can create a query that covers all of your needs.

Note : if you provided the link provided in your question, I am assuming your target platform is iOS. You can apply a similar solution for Android.

If the search API isn't enough for you, your other choice is to do some client-side filtering. I couldn't find the iOS source code, but there is a Twitter Kit for Android available on GitHub . If you look at BasicTimelineFilter and FilterTimelineDelegate , you can see that in Android filtering is actually done on the client side. This way you can do the same with any custom filtering in your iOS app. All you need to do is create a wrapper class that will implement the TWTRTimelineDataSource protocol and do its own filtering. Here are some examples of how the code might look in Objective-C (you can do the same in Swift, of course):

Note: beware of errors, I didn't even compile this code

.h file



typedef BOOL (^SOTweetFilter)(TWTRTweet * tweet);

@interface SOFilteredTimelineDataSourceWrapper : NSObject<TWTRTimelineDataSource>

- (instancetype)initWithDataSource:(id<TWTRTimelineDataSource>)dataSource filter:(SOTweetFilter)filter;

@end

      

.m file

@implementation SOFilteredTimelineDataSourceWrapper
@property (nonatomic, strong, readwrite) id<TWTRTimelineDataSource> wrapped;
@property (nonatomic, copy) SOTweetFilter filter;

- (instancetype)initWithDataSource:(id<TWTRTimelineDataSource>)dataSource filter:(SOTweetFilter)filter {
    if(!(self = [super init])) return self;

    self.wrapped = dataSource;
    self.filter = filter;

    return self;
}

- (void)loadPreviousTweetsBeforePosition:(nullable NSString *)position completion:(TWTRLoadTimelineCompletion)completion {
    // typedef void (^TWTRLoadTimelineCompletion)(NSArray<TWTRTweet *> * _Nullable tweets, TWTRTimelineCursor * _Nullable cursor, NSError * _Nullable error);
    [wrapped loadPreviousTweetsBeforePosition:position completion:^(NSArray<TWTRTweet *> * _Nullable tweets, TWTRTimelineCursor * _Nullable cursor, NSError * _Nullable error) {
            if(error) {
                // forward error
                completion(tweets, cursor, error);
            }
            else {
                // filter results
                NSArray* filtered = [tweets filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:(id evaluatedObject, NSDictionary<NSString *,id> *bindings) {
                    return self.filter(evaluatedObject);
                }]];
                completion(filtered, cursor, error);
            }
    }];
}


// delegate all properties to the wrapped

- (TWTRTimelineType)timelineType {
    return wrapped.timelineType;
}

-(TWTRTimelineFilter *)timelineFilter {
    return wrapped.timelineFilter;
}

-(void)setTimelineFilter:(TWTRTimelineFilter *)timelineFilter {
    wrapped.timelineFilter = timelineFilter;
}

- (TWTRAPIClient *)APIClient{ 
    return wrapped.APIClient;
}

- (void)setAPIClient:(TWTRAPIClient *)APIClient{ 
    wrapped.APIClient = APIClient;
}

@end

      

The basic idea is that you intercept the call loadPreviousTweetsBeforePosition:completion:

and add additional processing before calling the original completion

callback. Using SOFilteredTimelineDataSourceWrapper

, you can wrap any other TWTRTimelineDataSource

and do whatever filtering you want TWTRTweet.text to parse . For a mention of (e.g., handle-based) filtering, you can take a look at Android's implementation of normalizeHandle .

+1


source







All Articles