Commit b15d163a authored by David Jean's avatar David Jean Committed by Commit Bot

[ios] add change monitor to card and address fallbacks

Bug: 845472
Change-Id: Id077ed820ad559e00da61d96bf82f890333d9c86
Reviewed-on: https://chromium-review.googlesource.com/c/1341523
Commit-Queue: David Jean <djean@chromium.org>
Reviewed-by: default avatarJavier Ernesto Flores Robles <javierrobles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610002}
parent 2a3ca402
......@@ -9,6 +9,7 @@
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/ios/browser/autofill_driver_ios.h"
#import "components/autofill/ios/browser/personal_data_manager_observer_bridge.h"
#include "components/keyed_service/core/service_access_type.h"
#include "ios/chrome/browser/autofill/personal_data_manager_factory.h"
#import "ios/chrome/browser/ui/autofill/manual_fill/address_list_delegate.h"
......@@ -23,7 +24,15 @@
#error "This file requires ARC support."
#endif
@interface AddressCoordinator ()<AddressListDelegate>
@interface AddressCoordinator () <AddressListDelegate,
PersonalDataManagerObserver> {
// Personal data manager to be observed.
autofill::PersonalDataManager* _personalDataManager;
// C++ to ObjC bridge for PersonalDataManagerObserver.
std::unique_ptr<autofill::PersonalDataManagerObserverBridge>
_personalDataManagerObserver;
}
// The view controller presented above the keyboard where the user can select
// a field from one of their addresses.
......@@ -52,11 +61,16 @@ initWithBaseViewController:(UIViewController*)viewController
_addressViewController = [[AddressViewController alloc] init];
_addressViewController.contentInsetsAlwaysEqualToSafeArea = YES;
autofill::PersonalDataManager* personalDataManager =
_personalDataManager =
autofill::PersonalDataManagerFactory::GetForBrowserState(browserState);
DCHECK(_personalDataManager);
_personalDataManagerObserver.reset(
new autofill::PersonalDataManagerObserverBridge(self));
_personalDataManager->AddObserver(_personalDataManagerObserver.get());
std::vector<autofill::AutofillProfile*> profiles =
personalDataManager->GetProfilesToSuggest();
_personalDataManager->GetProfilesToSuggest();
_addressMediator =
[[ManualFillAddressMediator alloc] initWithProfiles:profiles];
......@@ -67,6 +81,12 @@ initWithBaseViewController:(UIViewController*)viewController
return self;
}
- (void)dealloc {
if (_personalDataManager) {
_personalDataManager->RemoveObserver(_personalDataManagerObserver.get());
}
}
#pragma mark - FallbackCoordinator
- (UIViewController*)viewController {
......@@ -82,4 +102,13 @@ initWithBaseViewController:(UIViewController*)viewController
}];
}
#pragma mark - PersonalDataManagerObserver
- (void)onPersonalDataChanged {
std::vector<autofill::AutofillProfile*> profiles =
_personalDataManager->GetProfilesToSuggest();
[self.addressMediator reloadWithProfiles:profiles];
}
@end
......@@ -7,6 +7,8 @@
#import "ios/chrome/browser/ui/autofill/manual_fill/address.h"
#include <vector>
namespace autofill {
class AutofillProfile;
}
......@@ -18,6 +20,11 @@ class AutofillProfile;
// Convenience initializer from an autofill::AutofillProfile.
- (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile;
// Converts a list of |autofill::AutofillProfile| into a list of
// |ManualFillAddress|.
+ (NSArray<ManualFillAddress*>*)manualFillAddressesFromProfiles:
(std::vector<autofill::AutofillProfile*>)profiles;
@end
#endif // IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_FORM_H_
......@@ -63,4 +63,15 @@ NSString* FieldValueOfTypeOnProfile(const autofill::AutofillProfile& profile,
country:country];
}
+ (NSArray<ManualFillAddress*>*)manualFillAddressesFromProfiles:
(std::vector<autofill::AutofillProfile*>)profiles {
NSMutableArray<ManualFillAddress*>* manualFillAddresses =
[[NSMutableArray alloc] initWithCapacity:profiles.size()];
for (autofill::AutofillProfile* profile : profiles) {
[manualFillAddresses
addObject:[[ManualFillAddress alloc] initWithProfile:*profile]];
}
return manualFillAddresses;
}
@end
......@@ -40,6 +40,9 @@ extern NSString* const ManageAddressAccessibilityIdentifier;
// Unavailable. Use |initWithProfiles:|.
- (instancetype)init NS_UNAVAILABLE;
// Updates the |profiles| being presented.
- (void)reloadWithProfiles:(std::vector<autofill::AutofillProfile*>)profiles;
@end
#endif // IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_ADDRESS_MEDIATOR_H_
......@@ -42,13 +42,7 @@ NSString* const ManageAddressAccessibilityIdentifier =
(std::vector<autofill::AutofillProfile*>)profiles {
self = [super init];
if (self) {
NSMutableArray<ManualFillAddress*>* manualFillAddresses =
[[NSMutableArray alloc] initWithCapacity:profiles.size()];
for (autofill::AutofillProfile* profile : profiles) {
[manualFillAddresses
addObject:[[ManualFillAddress alloc] initWithProfile:*profile]];
}
_addresses = manualFillAddresses;
_addresses = [ManualFillAddress manualFillAddressesFromProfiles:profiles];
}
return self;
}
......@@ -62,6 +56,14 @@ NSString* const ManageAddressAccessibilityIdentifier =
[self postActionsToConsumer];
}
- (void)reloadWithProfiles:(std::vector<autofill::AutofillProfile*>)profiles {
self.addresses = [ManualFillAddress manualFillAddressesFromProfiles:profiles];
if (self.consumer) {
[self postAddressesToConsumer];
[self postActionsToConsumer];
}
}
#pragma mark - Private
// Posts the addresses to the consumer.
......
......@@ -8,6 +8,7 @@
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/ios/browser/autofill_driver_ios.h"
#import "components/autofill/ios/browser/personal_data_manager_observer_bridge.h"
#include "ios/chrome/browser/autofill/personal_data_manager_factory.h"
#import "ios/chrome/browser/ui/autofill/manual_fill/card_list_delegate.h"
#import "ios/chrome/browser/ui/autofill/manual_fill/card_mediator.h"
......@@ -25,7 +26,14 @@
#error "This file requires ARC support."
#endif
@interface CardCoordinator ()<CardListDelegate>
@interface CardCoordinator () <CardListDelegate, PersonalDataManagerObserver> {
// Personal data manager to be observed.
autofill::PersonalDataManager* _personalDataManager;
// C++ to ObjC bridge for PersonalDataManagerObserver.
std::unique_ptr<autofill::PersonalDataManagerObserverBridge>
_personalDataManagerObserver;
}
// The view controller presented above the keyboard where the user can select
// one of their cards.
......@@ -58,15 +66,16 @@ initWithBaseViewController:(UIViewController*)viewController
_cardViewController = [[CardViewController alloc] init];
_cardViewController.contentInsetsAlwaysEqualToSafeArea = YES;
autofill::PersonalDataManager* personalDataManager =
_personalDataManager =
autofill::PersonalDataManagerFactory::GetForBrowserState(browserState);
DCHECK(_personalDataManager);
std::vector<autofill::CreditCard*> cards =
personalDataManager->GetCreditCardsToSuggest(true);
_personalDataManagerObserver.reset(
new autofill::PersonalDataManagerObserverBridge(self));
_personalDataManager->AddObserver(_personalDataManagerObserver.get());
// TODO(crbug.com/845472): add observer using
// PersonalDataManagerObserverBridge and refresh data when personal data
// changes. Applies to addresses too.
std::vector<autofill::CreditCard*> cards =
_personalDataManager->GetCreditCardsToSuggest(true);
_cardMediator = [[ManualFillCardMediator alloc] initWithCards:cards];
_cardMediator.navigationDelegate = self;
......@@ -81,6 +90,12 @@ initWithBaseViewController:(UIViewController*)viewController
return self;
}
- (void)dealloc {
if (_personalDataManager) {
_personalDataManager->RemoveObserver(_personalDataManagerObserver.get());
}
}
#pragma mark - FallbackCoordinator
- (UIViewController*)viewController {
......@@ -111,4 +126,13 @@ initWithBaseViewController:(UIViewController*)viewController
}];
}
#pragma mark - PersonalDataManagerObserver
- (void)onPersonalDataChanged {
std::vector<autofill::CreditCard*> cards =
_personalDataManager->GetCreditCardsToSuggest(true);
[self.cardMediator reloadWithCards:cards];
}
@end
......@@ -48,6 +48,9 @@ extern NSString* const OtherCardsAccessibilityIdentifier;
// Finds the original autofill::CreditCard from given |GUID|.
- (const autofill::CreditCard*)findCreditCardfromGUID:(NSString*)GUID;
// Updates the |cards| being presented.
- (void)reloadWithCards:(std::vector<autofill::CreditCard*>)cards;
@end
#endif // IOS_CHROME_BROWSER_UI_AUTOFILL_MANUAL_FILL_CARD_MEDIATOR_H_
......@@ -78,6 +78,14 @@ NSString* const ManageCardsAccessibilityIdentifier =
return nil;
}
- (void)reloadWithCards:(std::vector<autofill::CreditCard*>)cards {
self.cards = cards;
if (self.consumer) {
[self postCardsToConsumer];
[self postActionsToConsumer];
}
}
#pragma mark - Private
// Posts the cards to the consumer.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment