A framework for building convergent cross-platform Nextcloud clients using Flutter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

2 years ago
import 'dart:async';
import 'package:neon/src/models/account.dart';
import 'package:neon/src/neon.dart';
2 years ago
import 'package:nextcloud/nextcloud.dart';
import 'package:rx_bloc/rx_bloc.dart';
import 'package:rxdart/rxdart.dart';
import 'package:window_manager/window_manager.dart';
2 years ago
part 'user_status.rxb.g.dart';
abstract class UserStatusBlocEvents {}
abstract class UserStatusBlocStates {
BehaviorSubject<Result<UserStatus?>> get userStatus;
}
@RxBloc()
class UserStatusBloc extends $UserStatusBloc {
UserStatusBloc(
this._requestManager,
this._platform,
2 years ago
this._account,
) {
_timer = instantPeriodicTimer(
const Duration(minutes: 5),
(final _) async {
await _heartbeat();
},
);
2 years ago
}
Future _heartbeat() async {
final isAway =
_platform.canUseWindowManager && (!(await windowManager.isFocused()) || !(await windowManager.isVisible()));
_requestManager.wrapWithoutCache(
() async {
try {
return await _account.client.userStatus.heartbeat(
status: isAway ? UserStatusType.away : UserStatusType.online,
);
} on ApiException catch (e) {
if (e.statusCode == 204) {
return null;
}
rethrow;
}
},
).listen(_userStatusSubject.add);
2 years ago
}
final RequestManager _requestManager;
final NeonPlatform _platform;
2 years ago
final Account _account;
late final StreamSubscription<Account?> _activeAccountStreamSubscription;
late Timer _timer;
2 years ago
final _userStatusSubject = BehaviorSubject<Result<UserStatus?>>();
@override
void dispose() {
_timer.cancel();
unawaited(_activeAccountStreamSubscription.cancel());
unawaited(_userStatusSubject.close());
2 years ago
super.dispose();
}
@override
BehaviorSubject<Result<UserStatus?>> _mapToUserStatusState() => _userStatusSubject;
}
Timer instantPeriodicTimer(
final Duration duration,
final void Function(Timer timer) callback,
) {
final timer = Timer.periodic(duration, callback);
callback(timer);
return timer;
}