user_status_presenter.mm 2.1 KB

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