コード例 #1
0
        public AccountService()
        {
            _firebaseAuth = CrossFirebaseAuth.Current.Instance;
            _firestore    = CrossCloudFirestore.Current.Instance;

            IsInitialized     = _isInitialized.ToReadOnlyReactivePropertySlim();
            IsLoggedIn        = _isLoggedIn.ToReadOnlyReactivePropertySlim();
            UserId            = _userId.ToReadOnlyReactivePropertySlim();
            UserName          = _userName.ToReadOnlyReactivePropertySlim();
            UserImage         = _userImage.ToReadOnlyReactivePropertySlim();
            ContributionCount = _contributionCount.ToReadOnlyReactivePropertySlim();

            UserId.Select(userId => string.IsNullOrEmpty(userId) ?
                          Observable.Return <User>(null) :
                          _firestore.GetCollection(User.CollectionPath)
                          .GetDocument(userId)
                          .AsObservable()
                          .Select(d => d.ToObject <User>()))
            .Switch()
            .Subscribe(user =>
            {
                if (user != null)
                {
                    _userName.Value          = user.Name;
                    _userImage.Value         = user.Image;
                    _contributionCount.Value = user.ContributionCount;
                }
                else
                {
                    _userName.Value          = null;
                    _userImage.Value         = null;
                    _contributionCount.Value = 0;
                }
            })
            .AddTo(_disposables);
        }