Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostWritePage 내용 임시 저장(tempSave) #211

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@
"confirm": "OK",
"noBlockedUsers": "There are no blocked users.",
"noNickname": "No nickname",
"exitConfirm": "Do you really want to go back? Your post will not be saved"
"exitConfirm": "Do you really want to go back? Your post will not be saved",
"tempSave" : "Save in drafts"
},
"popUpMenuButtons": {
"downloadSucceed": "File downloaded successfully",
Expand Down
3 changes: 2 additions & 1 deletion assets/translations/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@
"confirm": "확인",
"noBlockedUsers": "차단한 유저가 없습니다.",
"noNickname": "닉네임이 없음",
"exitConfirm": "정말로 돌아가시겠습니까? 작성하신 글은 저장되지 않습니다"
"exitConfirm": "정말로 돌아가시겠습니까? 작성하신 글은 저장되지 않습니다",
"tempSave" : "임시 저장하기"
},
"popUpMenuButtons": {
"downloadSucceed": "파일 다운로드에 성공했습니다",
Expand Down
99 changes: 95 additions & 4 deletions lib/pages/post_write_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,36 @@ class _PostWritePageState extends State<PostWritePage>
/// 이전 게시물의 데이터를 가져온다.
Future<void> _initPostWritePost() async {
await _getBoardList();
await _getPostContent();
await _getCachedContents();
}

Future<void> cacheCurrentData() async {
var data = {
'title': _titleController.text,
'content':
DeltaToHTML.encodeJson(_quillController.document.toDelta().toJson()),
'is_content_sexual': _selectedCheckboxes[1],
'is_content_social': _selectedCheckboxes[2],
'name_type': (_chosenBoardValue != null)
? (_defaultBoardDetailActionModel.slug == 'with-school'
? 'REALNAME'
: _chosenBoardValue!.slug == "talk" &&
_selectedCheckboxes[0]! == true
? 'ANONYMOUS'
: 'REGULAR')
: 'REGULAR',
};
data['parent_topic'] =
_chosenTopicValue!.id == -1 ? '' : _chosenTopicValue!.slug;
data['parent_board'] =
_chosenBoardValue!.id == -1 ? '' : _chosenBoardValue!.slug;

data['attachments'] = _attachmentList;

String key =
_isEditingPost ? '/cache/${widget.previousArticle!.id}/' : '/cache/';

await cacheApiData(key, data);
}

/// 사용자가 선택 가능한 게시판 목록을 가져오는 함수.
Expand Down Expand Up @@ -326,6 +355,60 @@ class _PostWritePageState extends State<PostWritePage>
}
}

Future<void> _getCachedContents() async {
String key =
(_isEditingPost) ? '/cache/${widget.previousArticle!.id}/' : '/cache/';
dynamic cachedData = await fetchCachedApiData(key);

if (cachedData == null && _isEditingPost) {
await _getPostContent();
}

String? title = cachedData['title'];
_titleController.text = title ?? '';

for (int i = 0; i < cachedData['attachments'].length; i++) {
AttachmentModel attachment = cachedData['attachments'][i];
int id = attachment.id;
String? fileUrlPath = attachment.file;
String fileUrlName = _extractAndDecodeFileNameFromUrl(attachment.file);
int? fileUrlSize = attachment.size ?? 0;

// TODO: fileType이 이미지인지 아닌지 판단해서 넣기.
_attachmentList.add(AttachmentsFormat(
fileType: FileType.image,
isNewFile: false,
id: id,
fileUrlPath: fileUrlPath,
fileUrlName: fileUrlName,
fileUrlSize: fileUrlSize));
}

setState(() {
_quillController.document = (cachedData['content'] != null)
? quill.Document.fromDelta(_htmlToQuillDelta(cachedData['content']))
: '';
_isFileMenuBarSelected = _attachmentList.isNotEmpty;

//TODO: 명명 규칙 다름
_selectedCheckboxes[0] = cachedData['name_type'] == 2 ? true : false;
_selectedCheckboxes[1] = cachedData['is_content_sexual'] ?? false;
_selectedCheckboxes[2] = cachedData['is_content_social'] ?? false;
_isLoading = false;
});

setState(() {
BoardDetailActionModel boardDetailActionModel =
_findBoardListValue(cachedData['parent_board']);
_specTopicList = [_defaultTopicModelNone];
_specTopicList.addAll(boardDetailActionModel.topics);
_chosenTopicValue = (cachedData['parent_topic'] == null)
? _specTopicList[0]
: _findSpecTopicListValue(cachedData['parent_topic'].toString());
_chosenBoardValue = boardDetailActionModel;
});
}

/// 기존 게시물의 내용과 첨부 파일 가져오기.
Future<void> _getPostContent() async {
// 새로 작성하는 게시물의 경우 함수 종료.
Expand Down Expand Up @@ -382,6 +465,7 @@ class _PostWritePageState extends State<PostWritePage>
: _findSpecTopicListValue(widget.previousArticle!.parent_topic!.slug);
_chosenBoardValue = boardDetailActionModel;
});

}

/// 주어진 slug 값을 사용하여 게시판 목록에서 해당 게시판을 찾는 함수.
Expand Down Expand Up @@ -436,8 +520,12 @@ class _PostWritePageState extends State<PostWritePage>
builder: (context) => ExitConfirmDialog(
userProvider: userProvider,
targetContext: context,
onTap: () {
Navigator.pop(context, true); //dialog pop
onTapConfirm: () async {
Navigator.pop(context, true);
//dialog pop
},
onTapSave: () async {
await cacheCurrentData();
},
));
return shouldPop ?? false;
Expand Down Expand Up @@ -497,7 +585,7 @@ class _PostWritePageState extends State<PostWritePage>
builder: (context) => ExitConfirmDialog(
userProvider: userProvider,
targetContext: context,
onTap: () {
onTapConfirm: () async {
// 사용자가 미리 뒤로가기 버튼을 누르는 경우 에러 방지를 위해
// try-catch 문을 도입함.
try {
Expand All @@ -508,6 +596,9 @@ class _PostWritePageState extends State<PostWritePage>
debugPrint("pop error: $error");
}
},
onTapSave: () async {
await cacheCurrentData();
},
));
} else {
Navigator.pop(context);
Expand Down
6 changes: 4 additions & 2 deletions lib/translations/codegen_loader.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ class CodegenLoader extends AssetLoader{
"confirm": "OK",
"noBlockedUsers": "There are no blocked users.",
"noNickname": "No nickname",
"exitConfirm": "Do you really want to go back? Your post will not be saved"
"exitConfirm": "Do you really want to go back? Your post will not be saved",
"tempSave": "Save in drafts"
},
"popUpMenuButtons": {
"downloadSucceed": "File downloaded successfully",
Expand Down Expand Up @@ -359,7 +360,8 @@ static const Map<String,dynamic> ko = {
"confirm": "확인",
"noBlockedUsers": "차단한 유저가 없습니다.",
"noNickname": "닉네임이 없음",
"exitConfirm": "정말로 돌아가시겠습니까? 작성하신 글은 저장되지 않습니다"
"exitConfirm": "정말로 돌아가시겠습니까? 작성하신 글은 저장되지 않습니다",
"tempSave": "임시 저장하기"
},
"popUpMenuButtons": {
"downloadSucceed": "파일 다운로드에 성공했습니다",
Expand Down
Loading
Loading