blob: 635c726362b46492be0463e8cef4abfa4818ceef [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2017 The Chromium Authors
Yuwei Huang055b02a2017-07-11 23:56:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sylvain Defresne1f978c22020-06-11 09:54:095#import "remoting/ios/app/user_status_presenter.h"
6
Eugene But71decb32020-06-11 20:05:347#import <MaterialComponents/MaterialSnackbar.h>
8
Sylvain Defresne1f978c22020-06-11 09:54:099#import "remoting/ios/facade/remoting_authentication.h"
10#import "remoting/ios/facade/remoting_service.h"
Sylvain Defresne1f978c22020-06-11 09:54:0911#include "base/strings/sys_string_conversions.h"
12#include "remoting/base/string_resources.h"
13#include "ui/base/l10n/l10n_util.h"
14
Eugene But71decb32020-06-11 20:05:3415#if !defined(__has_feature) || !__has_feature(objc_arc)
16#error "This file requires ARC support."
17#endif
18
Yuwei Huang055b02a2017-07-11 23:56:3219@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 Huangfb41e822017-07-20 23:23:2981 message.text = l10n_util::GetNSStringF(
82 IDS_LOG_IN_ACCOUNT_DESCRIPTION, base::SysNSStringToUTF16(user.userEmail));
Nohemi Fernandez397ad9f02020-08-19 09:45:1183 [MDCSnackbarManager.defaultManager showMessage:message];
Yuwei Huang055b02a2017-07-11 23:56:3284}
85
86@end