Bgerstle has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/193389

Change subject: refactor subview searching categories
......................................................................

refactor subview searching categories

- renamed category methods to use wmf_ prefix
- documented cateogries
- class check to use `isKindOfClass:` so it's less strict
- added assertion to ensure early feedback for when this approach breaks
- use blockskit to improve readability

Change-Id: I728ea3ef2b3815185d4c58eddf2a3f2bcf1cd600
---
M wikipedia/Categories/Alerts/UIViewController+Alert.m
M wikipedia/Categories/TopActionSheet/UINavigationController+TopActionSheet.m
M wikipedia/Categories/UIView+SearchSubviews.h
M wikipedia/Categories/UIView+SearchSubviews.m
M wikipedia/Categories/UIWebView+WMFTrackingView.m
5 files changed, 39 insertions(+), 34 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/89/193389/1

diff --git a/wikipedia/Categories/Alerts/UIViewController+Alert.m 
b/wikipedia/Categories/Alerts/UIViewController+Alert.m
index 95c58ee..f15dedd 100644
--- a/wikipedia/Categories/Alerts/UIViewController+Alert.m
+++ b/wikipedia/Categories/Alerts/UIViewController+Alert.m
@@ -33,14 +33,14 @@
 -(void)fadeAlert
 {
     // Fade existing alert labels if any.
-    NSArray *alertLabels = [self.view getSubviewsOfClass:[AlertLabel class]];
+    NSArray *alertLabels = [self.view wmf_subviewsOfClass:[AlertLabel class]];
     [alertLabels makeObjectsPerformSelector:@selector(fade)];
 }
 
 -(void)hideAlert
 {
     // Hide existing alert labels if any.
-    NSArray *alertLabels = [self.view getSubviewsOfClass:[AlertLabel class]];
+    NSArray *alertLabels = [self.view wmf_subviewsOfClass:[AlertLabel class]];
     [alertLabels makeObjectsPerformSelector:@selector(hide)];
 }
 
diff --git 
a/wikipedia/Categories/TopActionSheet/UINavigationController+TopActionSheet.m 
b/wikipedia/Categories/TopActionSheet/UINavigationController+TopActionSheet.m
index 59f8d2e..10d7935 100644
--- 
a/wikipedia/Categories/TopActionSheet/UINavigationController+TopActionSheet.m
+++ 
b/wikipedia/Categories/TopActionSheet/UINavigationController+TopActionSheet.m
@@ -17,7 +17,7 @@
         UIView *superView = self.view;
         
         // Reuse existing container if any.
-        containerView = [superView getFirstSubviewOfClass:[TabularScrollView 
class]];
+        containerView = [superView wmf_firstSubviewOfClass:[TabularScrollView 
class]];
 
         // Remove container view if no views were specified.
         if (!views || (views.count == 0)) {
@@ -67,7 +67,7 @@
 
-(void)topActionSheetChangeOrientation:(TabularScrollViewOrientation)orientation
 {
     [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
-        TabularScrollView *containerView = [self.view 
getFirstSubviewOfClass:[TabularScrollView class]];
+        TabularScrollView *containerView = [self.view 
wmf_firstSubviewOfClass:[TabularScrollView class]];
 
         [containerView setOrientation:orientation];
             
diff --git a/wikipedia/Categories/UIView+SearchSubviews.h 
b/wikipedia/Categories/UIView+SearchSubviews.h
index aea20a6..13b992c 100644
--- a/wikipedia/Categories/UIView+SearchSubviews.h
+++ b/wikipedia/Categories/UIView+SearchSubviews.h
@@ -5,8 +5,17 @@
 
 @interface UIView (SearchSubviews)
 
--(id)getFirstSubviewOfClass:(Class)class;
+/**
+ * Returns the first object in the receiver's @c subviews array that is an 
instance or subclass of @c aClass.
+ * @return A matching subview or @c nil if none are found.
+ */
+- (id)wmf_firstSubviewOfClass:(Class)aClass;
 
--(NSArray *)getSubviewsOfClass:(Class)class;
+/**
+ * Get a filtered view of the receiver's @c subviews array which only objects 
that are instances or subclasses of
+ * @c aClass.
+ * @return A possibly empty array of matching subviews.
+ */
+- (NSArray*)wmf_subviewsOfClass:(Class)aClass;
 
 @end
diff --git a/wikipedia/Categories/UIView+SearchSubviews.m 
b/wikipedia/Categories/UIView+SearchSubviews.m
index 7ccd815..86e768f 100644
--- a/wikipedia/Categories/UIView+SearchSubviews.m
+++ b/wikipedia/Categories/UIView+SearchSubviews.m
@@ -2,28 +2,24 @@
 //  Copyright (c) 2013 Wikimedia Foundation. Provided under MIT-style license; 
please copy and modify!
 
 #import "UIView+SearchSubviews.h"
+#import <BlocksKit/BlocksKit.h>
 
 @implementation UIView (SearchSubviews)
 
--(id)getFirstSubviewOfClass:(Class)class
+- (id)wmf_firstSubviewOfClass:(Class)aClass
 {
-    for (id view in self.subviews.copy) {
-        if([view isMemberOfClass:class]){
-            return view;
-        }
-    }
-    return nil;
+    // ideally we'd reuse -wmf_subviewsOfClass, but since the filtering isn't 
lazy, we have to
+    // strictly find and return the first match
+    return [self.subviews bk_match:^BOOL(UIView* subview) {
+        return [subview isKindOfClass:aClass];
+    }];
 }
 
--(NSArray *)getSubviewsOfClass:(Class)class
+- (NSArray*)wmf_subviewsOfClass:(Class)aClass
 {
-    NSMutableArray *output = @[].mutableCopy;
-    for (id view in self.subviews.copy) {
-        if([view isMemberOfClass:class]){
-            [output addObject:view];
-        }
-    }
-    return output;
+    return [self.subviews bk_select:^BOOL(UIView* subview) {
+        return [subview isKindOfClass:aClass];
+    }];
 }
 
 @end
diff --git a/wikipedia/Categories/UIWebView+WMFTrackingView.m 
b/wikipedia/Categories/UIWebView+WMFTrackingView.m
index eb6d8d3..ec067df 100644
--- a/wikipedia/Categories/UIWebView+WMFTrackingView.m
+++ b/wikipedia/Categories/UIWebView+WMFTrackingView.m
@@ -10,27 +10,27 @@
                 atLocation: (WMFTrackingViewLocation)location
 {
     view.translatesAutoresizingMaskIntoConstraints = NO;
-    
+
     UIView *webScrollView = self.scrollView;
     [webScrollView addSubview:view];
-    
+
     // Reminder - this webView subview has the sizes we want constrain
     // "view" to, but the constraints themselves need to be added to
     // the webView's scrollView.
-    UIView *browserView = [self.scrollView 
getFirstSubviewOfClass:NSClassFromString(@"UIWebBrowserView")];
-    
-    void (^constrainEqually)(NSLayoutAttribute) = ^void(NSLayoutAttribute 
attr) {
+    UIView *browserView = [self.scrollView 
wmf_firstSubviewOfClass:NSClassFromString(@"UIWebBrowserView")];
+    NSParameterAssert(browserView);
+    void (^constrainEqually)(NSLayoutAttribute) = ^(NSLayoutAttribute attr) {
         [webScrollView addConstraint:
-         [NSLayoutConstraint constraintWithItem: view
-                                      attribute: attr
-                                      relatedBy: NSLayoutRelationEqual
-                                         toItem: browserView
-                                      attribute: attr
-                                     multiplier: 1.0
-                                       constant: 0.0]
+         [NSLayoutConstraint constraintWithItem:view
+                                      attribute:attr
+                                      relatedBy:NSLayoutRelationEqual
+                                         toItem:browserView
+                                      attribute:attr
+                                     multiplier:1.0
+                                       constant:0.0]
          ];
     };
-    
+
     constrainEqually([self layoutAttributeForTrackingViewLocation:location]);
     constrainEqually(NSLayoutAttributeLeading);
     constrainEqually(NSLayoutAttributeWidth);

-- 
To view, visit https://gerrit.wikimedia.org/r/193389
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I728ea3ef2b3815185d4c58eddf2a3f2bcf1cd600
Gerrit-PatchSet: 1
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Bgerstle <bgers...@wikimedia.org>
Gerrit-Reviewer: Mhurd <mh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to