Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame^] | 1 | // Copyright 2017 The Chromium Authors |
Yuwei Huang | 055b02a | 2017-07-11 23:56:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sylvain Defresne | 1f978c2 | 2020-06-11 09:54:09 | [diff] [blame] | 5 | #import "remoting/ios/app/user_status_presenter.h" |
| 6 | |
Eugene But | 71decb3 | 2020-06-11 20:05:34 | [diff] [blame] | 7 | #import <MaterialComponents/MaterialSnackbar.h> |
| 8 | |
Sylvain Defresne | 1f978c2 | 2020-06-11 09:54:09 | [diff] [blame] | 9 | #import "remoting/ios/facade/remoting_authentication.h" |
| 10 | #import "remoting/ios/facade/remoting_service.h" |
Sylvain Defresne | 1f978c2 | 2020-06-11 09:54:09 | [diff] [blame] | 11 | #include "base/strings/sys_string_conversions.h" |
| 12 | #include "remoting/base/string_resources.h" |
| 13 | #include "ui/base/l10n/l10n_util.h" |
| 14 | |
Eugene But | 71decb3 | 2020-06-11 20:05:34 | [diff] [blame] | 15 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 | #error "This file requires ARC support." |
| 17 | #endif |
| 18 | |
Yuwei Huang | 055b02a | 2017-07-11 23:56:32 | [diff] [blame] | 19 | @interface UserStatusPresenter () { |
| 20 | BOOL _isStarted; |
| 21 | } |
| 22 | @end |
| 23 | |
| 24 | @implementation UserStatusPresenter |
| 25 | |
| 26 | - (instancetype)init { |
| 27 | _isStarted = NO; |
| 28 | return self; |
| 29 | } |
| 30 | |
| 31 | - (void)dealloc { |
| 32 | [self stop]; |
| 33 | } |
| 34 | |
| 35 | - (void)start { |
| 36 | if (_isStarted) { |
| 37 | return; |
| 38 | } |
| 39 | _isStarted = YES; |
| 40 | if ([RemotingService.instance.authentication.user isAuthenticated]) { |
| 41 | [self presentUserStatus]; |
| 42 | } |
| 43 | [[NSNotificationCenter defaultCenter] |
| 44 | addObserver:self |
| 45 | selector:@selector(userDidUpdateNotification:) |
| 46 | name:kUserDidUpdate |
| 47 | object:nil]; |
| 48 | } |
| 49 | |
| 50 | - (void)stop { |
| 51 | if (!_isStarted) { |
| 52 | return; |
| 53 | } |
| 54 | _isStarted = NO; |
| 55 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 56 | } |
| 57 | |
| 58 | + (UserStatusPresenter*)instance { |
| 59 | static UserStatusPresenter* presenter; |
| 60 | static dispatch_once_t onceToken; |
| 61 | dispatch_once(&onceToken, ^{ |
| 62 | presenter = [[UserStatusPresenter alloc] init]; |
| 63 | }); |
| 64 | return presenter; |
| 65 | } |
| 66 | |
| 67 | #pragma mark - Private |
| 68 | |
| 69 | - (void)userDidUpdateNotification:(NSNotification*)notification { |
| 70 | [self presentUserStatus]; |
| 71 | } |
| 72 | |
| 73 | - (void)presentUserStatus { |
| 74 | UserInfo* user = RemotingService.instance.authentication.user; |
| 75 | if (![user isAuthenticated]) { |
| 76 | // No need to show the toast since we will pop up a sign-in view in this |
| 77 | // case. |
| 78 | return; |
| 79 | } |
| 80 | MDCSnackbarMessage* message = [[MDCSnackbarMessage alloc] init]; |
Yuwei Huang | fb41e82 | 2017-07-20 23:23:29 | [diff] [blame] | 81 | message.text = l10n_util::GetNSStringF( |
| 82 | IDS_LOG_IN_ACCOUNT_DESCRIPTION, base::SysNSStringToUTF16(user.userEmail)); |
Nohemi Fernandez | 397ad9f0 | 2020-08-19 09:45:11 | [diff] [blame] | 83 | [MDCSnackbarManager.defaultManager showMessage:message]; |
Yuwei Huang | 055b02a | 2017-07-11 23:56:32 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @end |