83 lines
2.0 KiB
Dart
83 lines
2.0 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../../core/data/remote/api_client.dart';
|
|
import '../../core/data/repositories/account_repository.dart';
|
|
|
|
/// 注销申请与撤销状态。
|
|
class AccountDeactivateLogic extends ChangeNotifier {
|
|
AccountDeactivateLogic(this._repository, {required this.clearSession});
|
|
|
|
static const maxReasonLength = 200;
|
|
|
|
final AccountRepository _repository;
|
|
final Future<void> Function() clearSession;
|
|
DateTime? scheduledAt;
|
|
bool requesting = false;
|
|
bool cancelling = false;
|
|
String? error;
|
|
String? actionMessage;
|
|
bool cancelSucceeded = false;
|
|
bool _disposed = false;
|
|
|
|
/// 申请注销;成功后立即清理本地会话。
|
|
Future<bool> request(String reason) async {
|
|
requesting = true;
|
|
error = null;
|
|
cancelSucceeded = false;
|
|
_notify();
|
|
try {
|
|
scheduledAt = await _repository.deactivate(
|
|
reason.trim().substring(
|
|
0,
|
|
reason.trim().length > maxReasonLength
|
|
? maxReasonLength
|
|
: reason.trim().length,
|
|
),
|
|
);
|
|
await clearSession();
|
|
return true;
|
|
} catch (failure) {
|
|
error = failure is ApiException
|
|
? failure.message
|
|
: 'accountDeactivateFailed';
|
|
return false;
|
|
} finally {
|
|
requesting = false;
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
/// 撤销待处理申请;失败时保留后端文案。
|
|
Future<bool> cancel() async {
|
|
cancelling = true;
|
|
actionMessage = null;
|
|
cancelSucceeded = false;
|
|
_notify();
|
|
try {
|
|
await _repository.cancelDeactivation();
|
|
cancelSucceeded = true;
|
|
return true;
|
|
} catch (failure) {
|
|
actionMessage = failure is ApiException
|
|
? failure.message
|
|
: 'accountCancelFailed';
|
|
return false;
|
|
} finally {
|
|
cancelling = false;
|
|
_notify();
|
|
}
|
|
}
|
|
|
|
void _notify() {
|
|
if (!_disposed) notifyListeners();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
super.dispose();
|
|
}
|
|
}
|