feat(account): 增加账号中心页面逻辑
This commit is contained in:
1 parent
ef7f4ce3b6
commit
c68f02449f
6 files changed
+503
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user