Skip to content

Commit

Permalink
#3 Edit BoardDetailView
Browse files Browse the repository at this point in the history
  • Loading branch information
JunseokNoh committed Aug 11, 2021
1 parent 1822df6 commit dd79198
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
6 changes: 5 additions & 1 deletion KNU_CSE/KNU_CSE/Main Tab/Board/Board/BoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class BoardView : UIViewController{
self.setupConstraints()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

func initUI(){
self.cellRowHeight = self.view.frame.height
self.boardTableView = UITableView()
Expand All @@ -67,7 +71,7 @@ extension BoardView : UITableViewDataSource{
if boardViewModel.boards.count == 0{
switch parentType {
case .BoardTap:
self.setTablViewBackgroundView(text: "게시글이 없습니다")
self.setTablViewBackgroundView(text: "")
case .Search:
self.setTablViewBackgroundView(text: "검색 결과가 없습니다")
case .Write:
Expand Down
87 changes: 70 additions & 17 deletions KNU_CSE/KNU_CSE/Main Tab/Board/BoardDetail/BoardDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class BoardDetailView:BaseUIViewController, ViewProtocol, BoardDataDelegate{
var delegate:CommentDataDelegate?

let commentPlaceHolder = "댓글을 입력해주세요."
var stackViewSize: Int = 0

let titleHeight:CGFloat = 30
var imageSize:CGFloat!
var textViewHeight:CGFloat!
Expand Down Expand Up @@ -119,7 +121,6 @@ class BoardDetailView:BaseUIViewController, ViewProtocol, BoardDataDelegate{
didSet{
self.stackView.axis = .vertical
self.stackView.distribution = .fill
self.addToStackView()
}
}

Expand Down Expand Up @@ -192,6 +193,7 @@ class BoardDetailView:BaseUIViewController, ViewProtocol, BoardDataDelegate{
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.boardDetailViewModel.getBoardRequest()
self.boardDetailViewModel.getCommentRequest()
}
Expand Down Expand Up @@ -350,24 +352,73 @@ extension BoardDetailView{
}

/// StackView에 CommentCell, ReplyCell 추가
func addToStackView(){
func InitToStackView(){
for i in 0..<self.boardDetailViewModel.comments.count{
let comment = self.boardDetailViewModel.comments[i]
self.addToStackView(comment)
}
}

func addToStackView(_ comment:Comment){
DispatchQueue.main.async {
for i in 0..<self.boardDetailViewModel.comments.count{
let comment = self.boardDetailViewModel.comments[i]
let commentView = CommentCell(comment: comment)
commentView.replyBtn.addAction {
self.pushView(self.boardDetailViewModel.board.value, comment)
let commentView = CommentCell(comment: comment)
commentView.replyBtn.addAction {
self.pushView(self.boardDetailViewModel.board.value, comment)
}
self.stackView.addArrangedSubview(commentView)
if let replyList = comment.replyList{
for j in 0..<replyList.count{
let reply = comment.replyList[j]
let replyView = ReplyCell(reply: reply)
self.stackView.addArrangedSubview(replyView)
}
self.stackView.addArrangedSubview(commentView)
if let replyList = comment.replyList{
for j in 0..<replyList.count{
let reply = comment.replyList[j]
}
}
}

func updateToStackView(oldComments:[Comment], lateComments:[Comment]){

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(0)) {

var targetIndex = 0
print(oldComments.count, lateComments.count)
for i in 0..<oldComments.count{
targetIndex += 1
if let old_reply = oldComments[i].replyList, let late_reply = lateComments[i].replyList {
let old_reply_cnt = old_reply.count
let late_reply_cnt = late_reply.count

targetIndex += old_reply_cnt
print(old_reply_cnt, late_reply_cnt, targetIndex)
if old_reply_cnt < late_reply_cnt{
for j in old_reply_cnt..<late_reply_cnt{
print(j)
let reply = lateComments[i].replyList[j]
let replyView = ReplyCell(reply: reply)


self.stackView.insertArrangedSubview(replyView, at: targetIndex)
targetIndex += 1
}
}
}else if let late_reply = lateComments[i].replyList{
let late_reply_cnt = late_reply.count
for j in 0..<late_reply_cnt{
print(j)
let reply = lateComments[i].replyList[j]
let replyView = ReplyCell(reply: reply)
self.stackView.addArrangedSubview(replyView)
self.stackView.insertArrangedSubview(replyView, at: targetIndex)
targetIndex += 1
}
}
}
}

for i in oldComments.count..<lateComments.count{
let comment = lateComments[i]
addToStackView(comment)
}

}

func pushView(_ board:Board, _ comment:Comment){
Expand Down Expand Up @@ -498,6 +549,7 @@ extension BoardDetailView{
self.boardDetailViewModel.getCommentListener.binding(successHandler: { result in
if result.success{
if let comments = result.response{
self.boardDetailViewModel.oldcomments = self.boardDetailViewModel.comments
self.boardDetailViewModel.comments = comments
}else{
if let error = result.error?.message {
Expand All @@ -513,15 +565,16 @@ extension BoardDetailView{
, asyncHandler: {

}, endHandler: {
self.stackView.removeAllArrangedView()
self.addToStackView()
//self.stackView.removeAllArrangedView()
self.updateToStackView(oldComments: self.boardDetailViewModel.oldcomments, lateComments: self.boardDetailViewModel.comments)
//self.InitToStackView()
})
}

func BindingWriteComment(){
self.boardDetailViewModel.writeCommentListener.binding(successHandler: { result in
if result.success{

if result.success, let comment = result.response{
self.addToStackView(comment)
}else{
if let error = result.error?.message {
Alert(title: "실패", message: error, viewController: self).popUpDefaultAlert(action: { action in
Expand All @@ -536,7 +589,7 @@ extension BoardDetailView{
self.indicator.startIndicator()
}, endHandler: {
self.boardDetailViewModel.getBoardRequest()
self.boardDetailViewModel.getCommentRequest()
//self.boardDetailViewModel.getCommentRequest()
self.indicator.stopIndicator()
self.completedWriteComment()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct BoardDetailViewModel{

var board:Observable<Board> = Observable(Board(image: "", boardId: 0, category: "", title: "", content: "", author: "", time: "", commentCnt: 0))

var oldcomments:[Comment]!
var comments:[Comment] = []
var comment:CommentTextModel = CommentTextModel()
var listener:((String)->Void)?
Expand Down
11 changes: 10 additions & 1 deletion KNU_CSE/KNU_CSE/Main Tab/Board/BoardTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import UIKit

class BoardTabView:UIViewController, ViewProtocol{

// MARK: - 2번째 부터의 viewWillApper를 위해 사용
var isSecondLoaded:Bool = false

let titleList:[String] = ["자유게시판", "질의응답", "학생회 공지"]

let title_left_Margin:CGFloat = 10
Expand Down Expand Up @@ -72,7 +75,6 @@ class BoardTabView:UIViewController, ViewProtocol{

override func viewDidLoad() {
super.viewDidLoad()

self.initUI()
self.addView()
self.addBoardVC()
Expand All @@ -81,7 +83,14 @@ class BoardTabView:UIViewController, ViewProtocol{
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.reloadNavigationItems(selectedTabIndex:self.selectedTabIndex)

if isSecondLoaded{
self.BoardVC.boardViewModel.getBoards()
}else{
isSecondLoaded = true
}
}

func initUI() {
Expand Down
1 change: 1 addition & 0 deletions KNU_CSE/KNU_CSE/Main Tab/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TabView : UITabBarController{
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//push했기때문에 backbutton 없애기
self.navigationItem.setHidesBackButton(true, animated: true);
}
Expand Down

0 comments on commit dd79198

Please sign in to comment.