[Feature] Enhance the places deep linking support in the Wikipedia app to support coordinates (#2)
This PR contains the work done to add support to the **Wikipedia** app to show a custom location in the map of the *Places* view coming from a places deep link. To provide further details about the work: - [x] implemented the handling of location coordinates coming from a URL inside the function that generates a `NSUSerActivity` instance out of *places* deep links; - [x] added the `wmf_locationFromURL` property to the `NSUSerActivity` via an extension that returns a `CLLocation` instance with latitude and longitude coordinates if provided; - [x] implemented the "centerMap(onLocation:)" function in the `PlacesViewController` view controller that, essentially, center the map of this view controller to a given location; - [x] improved the handling of a *places* user activity in the `WMFAppViewController` view controller to center the map in case a location has been provided via a deep link; - [x] fixed some duplicated code that was blocking the test from compiling. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#2
This commit is contained in:
parent
9bcdaa697b
commit
6d76ba467c
@ -1,3 +1,4 @@
|
|||||||
|
@import CoreLocation;
|
||||||
@import Foundation;
|
@import Foundation;
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@ -46,6 +47,8 @@ extern NSString *const WMFNavigateToActivityNotification;
|
|||||||
|
|
||||||
- (nullable NSURL *)wmf_linkURL;
|
- (nullable NSURL *)wmf_linkURL;
|
||||||
|
|
||||||
|
- (nullable CLLocation *)wmf_locationFromURL;
|
||||||
|
|
||||||
- (NSURL *)wmf_contentURL;
|
- (NSURL *)wmf_contentURL;
|
||||||
|
|
||||||
+ (NSURL *)wmf_baseURLForActivityOfType:(WMFUserActivityType)type;
|
+ (NSURL *)wmf_baseURLForActivityOfType:(WMFUserActivityType)type;
|
||||||
|
@ -62,15 +62,42 @@ __attribute__((annotate("returns_localized_nsstring"))) static inline NSString *
|
|||||||
+ (instancetype)wmf_placesActivityWithURL:(NSURL *)activityURL {
|
+ (instancetype)wmf_placesActivityWithURL:(NSURL *)activityURL {
|
||||||
NSURLComponents *components = [NSURLComponents componentsWithURL:activityURL resolvingAgainstBaseURL:NO];
|
NSURLComponents *components = [NSURLComponents componentsWithURL:activityURL resolvingAgainstBaseURL:NO];
|
||||||
NSURL *articleURL = nil;
|
NSURL *articleURL = nil;
|
||||||
|
NSNumber *latitude = nil;
|
||||||
|
NSNumber *longitude = nil;
|
||||||
|
|
||||||
for (NSURLQueryItem *item in components.queryItems) {
|
for (NSURLQueryItem *item in components.queryItems) {
|
||||||
if ([item.name isEqualToString:@"WMFArticleURL"]) {
|
if ([item.name isEqualToString:@"WMFArticleURL"]) {
|
||||||
NSString *articleURLString = item.value;
|
NSString *articleURLString = item.value;
|
||||||
articleURL = [NSURL URLWithString:articleURLString];
|
articleURL = [NSURL URLWithString:articleURLString];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ([item.name isEqualToString:@"coordinates"]) {
|
||||||
|
NSArray *numbers = [item.value componentsSeparatedByString:@","];
|
||||||
|
|
||||||
|
if (numbers.count == 2) {
|
||||||
|
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
|
||||||
|
|
||||||
|
latitude = [formatter numberFromString:numbers.firstObject];
|
||||||
|
longitude = [formatter numberFromString:numbers.lastObject];
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSUserActivity *activity = [self wmf_pageActivityWithName:@"Places"];
|
NSUserActivity *activity = [self wmf_pageActivityWithName:@"Places"];
|
||||||
activity.webpageURL = articleURL;
|
activity.webpageURL = articleURL;
|
||||||
|
|
||||||
|
if (latitude != nil && longitude != nil) {
|
||||||
|
NSMutableDictionary *userInfo = [activity.userInfo mutableCopy];
|
||||||
|
|
||||||
|
userInfo[@"WMFCoordinatesLatitude"] = latitude;
|
||||||
|
userInfo[@"WMFCoordinatesLongitude"] = longitude;
|
||||||
|
|
||||||
|
activity.userInfo = [userInfo copy];
|
||||||
|
}
|
||||||
|
|
||||||
return activity;
|
return activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,6 +291,18 @@ __attribute__((annotate("returns_localized_nsstring"))) static inline NSString *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (CLLocation *)wmf_locationFromURL {
|
||||||
|
NSNumber *latitude = self.userInfo[@"WMFCoordinatesLatitude"];
|
||||||
|
NSNumber *longitude = self.userInfo[@"WMFCoordinatesLongitude"];
|
||||||
|
|
||||||
|
if (latitude != nil && longitude != nil) {
|
||||||
|
return [[CLLocation alloc] initWithLatitude:(CLLocationDegrees)[latitude floatValue]
|
||||||
|
longitude:(CLLocationDegrees)[longitude floatValue]];
|
||||||
|
} else {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (NSURL *)wmf_contentURL {
|
- (NSURL *)wmf_contentURL {
|
||||||
return self.userInfo[@"WMFURL"];
|
return self.userInfo[@"WMFURL"];
|
||||||
}
|
}
|
||||||
|
@ -1126,6 +1126,10 @@ class PlacesViewController: ViewController, UISearchBarDelegate, ArticlePopoverV
|
|||||||
viewMode = .map
|
viewMode = .map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func centerMap(onLocation location: CLLocation) {
|
||||||
|
zoomAndPanMapView(toLocation: location)
|
||||||
|
}
|
||||||
|
|
||||||
func selectArticlePlace(_ articlePlace: ArticlePlace) {
|
func selectArticlePlace(_ articlePlace: ArticlePlace) {
|
||||||
mapView.selectAnnotation(articlePlace, animated: articlePlace.identifier != previouslySelectedArticlePlaceIdentifier)
|
mapView.selectAnnotation(articlePlace, animated: articlePlace.identifier != previouslySelectedArticlePlaceIdentifier)
|
||||||
previouslySelectedArticlePlaceIdentifier = articlePlace.identifier
|
previouslySelectedArticlePlaceIdentifier = articlePlace.identifier
|
||||||
|
@ -1187,10 +1187,16 @@ NSString *const WMFLanguageVariantAlertsLibraryVersion = @"WMFLanguageVariantAle
|
|||||||
[self setSelectedIndex:WMFAppTabTypePlaces];
|
[self setSelectedIndex:WMFAppTabTypePlaces];
|
||||||
[self.navigationController popToRootViewControllerAnimated:animated];
|
[self.navigationController popToRootViewControllerAnimated:animated];
|
||||||
NSURL *articleURL = activity.wmf_linkURL;
|
NSURL *articleURL = activity.wmf_linkURL;
|
||||||
if (articleURL) {
|
CLLocation *locationFromURL = activity.wmf_locationFromURL;
|
||||||
|
if (articleURL || locationFromURL) {
|
||||||
// For "View on a map" action to succeed, view mode has to be set to map.
|
// For "View on a map" action to succeed, view mode has to be set to map.
|
||||||
[[self placesViewController] updateViewModeToMap];
|
[[self placesViewController] updateViewModeToMap];
|
||||||
[[self placesViewController] showArticleURL:articleURL];
|
if (locationFromURL) {
|
||||||
|
[[self placesViewController] centerMapOnLocation:locationFromURL];
|
||||||
|
}
|
||||||
|
else if (articleURL) {
|
||||||
|
[[self placesViewController] showArticleURL:articleURL];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case WMFUserActivityTypeContent: {
|
case WMFUserActivityTypeContent: {
|
||||||
|
@ -113,8 +113,6 @@ public enum WikimediaProject: Hashable {
|
|||||||
self = .wikibooks(languageCode, localizedLanguageName)
|
self = .wikibooks(languageCode, localizedLanguageName)
|
||||||
} else if siteURLString.contains(Configuration.Domain.wiktionary) {
|
} else if siteURLString.contains(Configuration.Domain.wiktionary) {
|
||||||
self = .wiktionary(languageCode, localizedLanguageName)
|
self = .wiktionary(languageCode, localizedLanguageName)
|
||||||
} else if siteURLString.contains(Configuration.Domain.wiktionary) {
|
|
||||||
self = .wiktionary(languageCode, localizedLanguageName)
|
|
||||||
} else if siteURLString.contains(Configuration.Domain.wikisource) {
|
} else if siteURLString.contains(Configuration.Domain.wikisource) {
|
||||||
self = .wikisource(languageCode, localizedLanguageName)
|
self = .wikisource(languageCode, localizedLanguageName)
|
||||||
} else if siteURLString.contains(Configuration.Domain.wikinews) {
|
} else if siteURLString.contains(Configuration.Domain.wikinews) {
|
||||||
|
@ -44,6 +44,76 @@
|
|||||||
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeSavedPages);
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypeSavedPages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURL {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinates {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=52.370216,4.895168"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssert([activity.userInfo[@"WMFCoordinatesLatitude"] isEqualToNumber:@52.370216]);
|
||||||
|
XCTAssert([activity.userInfo[@"WMFCoordinatesLongitude"] isEqualToNumber:@4.895168]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinatesButNegatives {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=-34.603722,-58.381592"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssert([activity.userInfo[@"WMFCoordinatesLatitude"] isEqualToNumber:@-34.603722]);
|
||||||
|
XCTAssert([activity.userInfo[@"WMFCoordinatesLongitude"] isEqualToNumber:@-58.381592]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinatesButNoLongitude {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=52.370216,"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinatesButNoLatitude {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=,4.895168"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinatesButNoCoordinates {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates=,"];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testPlacesURLWithCoordinatesButEmpty {
|
||||||
|
NSURL *url = [NSURL URLWithString:@"wikipedia://places?coordinates="];
|
||||||
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
|
||||||
|
XCTAssertEqual(activity.wmf_type, WMFUserActivityTypePlaces);
|
||||||
|
XCTAssertNil(activity.webpageURL);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLatitude"]);
|
||||||
|
XCTAssertNil(activity.userInfo[@"WMFCoordinatesLongitude"]);
|
||||||
|
}
|
||||||
|
|
||||||
- (void)testSearchURL {
|
- (void)testSearchURL {
|
||||||
NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/w/index.php?search=dog"];
|
NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/w/index.php?search=dog"];
|
||||||
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user