[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:
Javier Cicchelli 2023-04-09 16:27:53 +00:00
parent 9bcdaa697b
commit 6d76ba467c
6 changed files with 124 additions and 4 deletions

View File

@ -1,3 +1,4 @@
@import CoreLocation;
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@ -46,6 +47,8 @@ extern NSString *const WMFNavigateToActivityNotification;
- (nullable NSURL *)wmf_linkURL;
- (nullable CLLocation *)wmf_locationFromURL;
- (NSURL *)wmf_contentURL;
+ (NSURL *)wmf_baseURLForActivityOfType:(WMFUserActivityType)type;

View File

@ -62,15 +62,42 @@ __attribute__((annotate("returns_localized_nsstring"))) static inline NSString *
+ (instancetype)wmf_placesActivityWithURL:(NSURL *)activityURL {
NSURLComponents *components = [NSURLComponents componentsWithURL:activityURL resolvingAgainstBaseURL:NO];
NSURL *articleURL = nil;
NSNumber *latitude = nil;
NSNumber *longitude = nil;
for (NSURLQueryItem *item in components.queryItems) {
if ([item.name isEqualToString:@"WMFArticleURL"]) {
NSString *articleURLString = item.value;
articleURL = [NSURL URLWithString:articleURLString];
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"];
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;
}
@ -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 {
return self.userInfo[@"WMFURL"];
}

View File

@ -1126,6 +1126,10 @@ class PlacesViewController: ViewController, UISearchBarDelegate, ArticlePopoverV
viewMode = .map
}
@objc func centerMap(onLocation location: CLLocation) {
zoomAndPanMapView(toLocation: location)
}
func selectArticlePlace(_ articlePlace: ArticlePlace) {
mapView.selectAnnotation(articlePlace, animated: articlePlace.identifier != previouslySelectedArticlePlaceIdentifier)
previouslySelectedArticlePlaceIdentifier = articlePlace.identifier

View File

@ -1187,11 +1187,17 @@ NSString *const WMFLanguageVariantAlertsLibraryVersion = @"WMFLanguageVariantAle
[self setSelectedIndex:WMFAppTabTypePlaces];
[self.navigationController popToRootViewControllerAnimated:animated];
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.
[[self placesViewController] updateViewModeToMap];
if (locationFromURL) {
[[self placesViewController] centerMapOnLocation:locationFromURL];
}
else if (articleURL) {
[[self placesViewController] showArticleURL:articleURL];
}
}
} break;
case WMFUserActivityTypeContent: {
[self dismissPresentedViewControllers];

View File

@ -113,8 +113,6 @@ public enum WikimediaProject: Hashable {
self = .wikibooks(languageCode, localizedLanguageName)
} else if siteURLString.contains(Configuration.Domain.wiktionary) {
self = .wiktionary(languageCode, localizedLanguageName)
} else if siteURLString.contains(Configuration.Domain.wiktionary) {
self = .wiktionary(languageCode, localizedLanguageName)
} else if siteURLString.contains(Configuration.Domain.wikisource) {
self = .wikisource(languageCode, localizedLanguageName)
} else if siteURLString.contains(Configuration.Domain.wikinews) {

View File

@ -44,6 +44,76 @@
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 {
NSURL *url = [NSURL URLWithString:@"wikipedia://en.wikipedia.org/w/index.php?search=dog"];
NSUserActivity *activity = [NSUserActivity wmf_activityForWikipediaScheme:url];