selfprivacy.org.app/lib/logic/cubit/forms/initializing/domain_cloudflare.dart

79 lines
1.8 KiB
Dart
Raw Normal View History

2021-02-15 18:58:29 +00:00
import 'package:cubit_form/cubit_form.dart';
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
class DomainSetupCubit extends Cubit<DomainSetupState> {
DomainSetupCubit(this.initializingCubit) : super(Initial()) {
2021-03-15 15:39:44 +00:00
var token = initializingCubit.state.cloudFlareKey;
2021-02-15 18:58:29 +00:00
assert(token != null, 'no cloudflare token');
api = CloudflareApi(token);
}
AppConfigCubit initializingCubit;
2021-03-15 15:39:44 +00:00
late CloudflareApi api;
2021-02-15 18:58:29 +00:00
Future<void> load() async {
emit(Loading(LoadingTypes.loadingDomain));
2021-03-23 19:21:42 +00:00
var list = await api.domainList();
2021-02-15 18:58:29 +00:00
if (list.isEmpty) {
emit(Empty());
} else if (list.length == 1) {
emit(Loaded(list.first));
} else {
emit(MoreThenOne());
}
}
@override
Future<void> close() {
api.close();
return super.close();
}
Future<void> saveDomain() async {
assert(state is Loaded, 'wrong state');
var domainName = (state as Loaded).domain;
emit(Loading(LoadingTypes.saving));
var zoneId = await api.getZoneId(
initializingCubit.state.cloudFlareKey,
domainName,
);
var domain = CloudFlareDomain(
domainName: domainName,
zoneId: zoneId,
);
initializingCubit.setDomain(domain);
emit(DomainSetted());
}
}
abstract class DomainSetupState {}
class Initial extends DomainSetupState {}
class Empty extends DomainSetupState {}
class MoreThenOne extends DomainSetupState {}
class Loading extends DomainSetupState {
Loading(this.type);
final LoadingTypes type;
}
enum LoadingTypes { loadingDomain, saving }
class Loaded extends DomainSetupState {
final String domain;
Loaded(this.domain);
}
class DomainSetted extends DomainSetupState {}