chore: refactor publish bloc

This commit is contained in:
Lucas.Xu 2024-06-27 17:57:33 +08:00
parent 2557d90c0d
commit 39f2789e13
2 changed files with 46 additions and 19 deletions

View File

@ -7,7 +7,6 @@ import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_result/appflowy_result.dart';
import 'package:flowy_infra/uuid.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
@ -51,27 +50,31 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
),
);
},
publish: (url) async {
// todo: optimize the logic
const prefix = 'appflowy';
final name = '${view.name}-${uuid()}'.substring(0, 19);
publish: (nameSpace, publishName) async {
// set space name
try {
final nameSpace =
await ViewBackendService.getPublishNameSpace().getOrThrow();
if (nameSpace.namespace != prefix) {
await ViewBackendService.setPublishNameSpace(prefix).getOrThrow();
}
final getNameSpaceResult =
await ViewBackendService.getPublishNameSpace();
final name = await getNameSpaceResult.fold((s) async {
Log.error('get publish namespace success: ${s.namespace}');
return s.namespace;
}, (f) async {
Log.error('get publish namespace error: $f');
await ViewBackendService.setPublishNameSpace(nameSpace)
.getOrThrow();
return nameSpace;
});
final result = await ViewBackendService.publish(view, name: name);
await ViewBackendService.publish(
view,
name: publishName,
).getOrThrow();
emit(
state.copyWith(
isPublished: true,
publishResult: result,
url: '$_url/${nameSpace.namespace}/$name',
publishResult: FlowySuccess(null),
url: '$_url/$name/$publishName',
),
);
} catch (e) {
@ -173,7 +176,10 @@ class DocumentShareEvent with _$DocumentShareEvent {
DocumentShareType type,
String? path,
) = _Share;
const factory DocumentShareEvent.publish(String url) = _Publish;
const factory DocumentShareEvent.publish(
String nameSpace,
String pageId,
) = _Publish;
const factory DocumentShareEvent.unPublish() = _UnPublish;
}

View File

@ -4,11 +4,14 @@ import 'package:appflowy/plugins/document/application/document_share_bloc.dart';
import 'package:appflowy/workspace/presentation/home/toast.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/uuid.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flowy_infra_ui/widget/rounded_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
final _regExp = RegExp(r'[^\w\-\.@:/]');
class PublishTab extends StatelessWidget {
const PublishTab({super.key});
@ -36,13 +39,31 @@ class PublishTab extends StatelessWidget {
},
)
: _UnPublishWidget(
onPublish: () => context
.read<DocumentShareBloc>()
.add(const DocumentShareEvent.publish('')),
onPublish: () async {
final publishName = await _generatePublishName(context);
final nameSpace = await _generateNameSpace();
if (context.mounted) {
context.read<DocumentShareBloc>().add(
DocumentShareEvent.publish(nameSpace, publishName),
);
}
},
);
},
);
}
Future<String> _generateNameSpace() async {
const workspaceName = '';
final id = uuid().substring(0, 8);
return '$workspaceName$id'.replaceAll(_regExp, '_');
}
Future<String> _generatePublishName(BuildContext context) async {
final publishName = context.read<DocumentShareBloc>().view.name;
final id = uuid().substring(0, 8);
return '$publishName$id'.replaceAll(_regExp, '');
}
}
class _PublishedWidget extends StatefulWidget {