Skip to content

Commit

Permalink
feat: DiaryEntity 디코딩 및 셀의 setModel 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhyunm committed Aug 29, 2023
1 parent 03b134c commit 265d5f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
63E527352A9D7EBF0000FBA6 /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 63E527342A9D7EBF0000FBA6 /* .swiftlint.yml */; };
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63E527362A9D87660000FBA6 /* DiaryListTableViewCell.swift */; };
BA1A55EB2A9D84AF0012C89D /* DiaryEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */; };
BA1A55ED2A9D90810012C89D /* DateFormatter+.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA1A55EC2A9D90810012C89D /* DateFormatter+.swift */; };
C739AE25284DF28600741E8F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE24284DF28600741E8F /* AppDelegate.swift */; };
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE26284DF28600741E8F /* SceneDelegate.swift */; };
C739AE29284DF28600741E8F /* DiaryListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C739AE28284DF28600741E8F /* DiaryListViewController.swift */; };
Expand All @@ -24,6 +25,7 @@
63E527362A9D87660000FBA6 /* DiaryListTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryListTableViewCell.swift; sourceTree = "<group>"; };
7B86D20E06F2B506DECF94F6 /* Pods-Diary.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Diary.release.xcconfig"; path = "Target Support Files/Pods-Diary/Pods-Diary.release.xcconfig"; sourceTree = "<group>"; };
BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryEntity.swift; sourceTree = "<group>"; };
BA1A55EC2A9D90810012C89D /* DateFormatter+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DateFormatter+.swift"; sourceTree = "<group>"; };
C739AE21284DF28600741E8F /* Diary.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Diary.app; sourceTree = BUILT_PRODUCTS_DIR; };
C739AE24284DF28600741E8F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C739AE26284DF28600741E8F /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -96,6 +98,7 @@
C739AE35284DF28600741E8F /* Info.plist */,
C739AE2D284DF28600741E8F /* Diary.xcdatamodeld */,
BA1A55EA2A9D84AF0012C89D /* DiaryEntity.swift */,
BA1A55EC2A9D90810012C89D /* DateFormatter+.swift */,
);
path = Diary;
sourceTree = "<group>";
Expand Down Expand Up @@ -219,6 +222,7 @@
C739AE29284DF28600741E8F /* DiaryListViewController.swift in Sources */,
C739AE25284DF28600741E8F /* AppDelegate.swift in Sources */,
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */,
BA1A55ED2A9D90810012C89D /* DateFormatter+.swift in Sources */,
63E527372A9D87660000FBA6 /* DiaryListTableViewCell.swift in Sources */,
C739AE2F284DF28600741E8F /* Diary.xcdatamodeld in Sources */,
BA1A55EB2A9D84AF0012C89D /* DiaryEntity.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Diary/DateFormatter+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// DateFormatter+.swift
// Diary
//
// Created by Maxhyunm, Hamg on 2023/08/29.
//

import Foundation

extension DateFormatter {
func formatToString(from date: Date, with format: String) -> String {
self.dateFormat = format

return self.string(from: date)
}
}
30 changes: 18 additions & 12 deletions Diary/DiaryListTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ class DiaryListTableViewCell: UITableViewCell {
return label
}()

private let bodyLabel: UILabel = {
private let dateLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = .preferredFont(forTextStyle: .callout)
label.setContentCompressionResistancePriority(.required, for: .horizontal)

return label
}()

private let dateLabel: UILabel = {
private let bodyLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = .preferredFont(forTextStyle: .caption1)
label.font = .preferredFont(forTextStyle: .caption2)
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

return label
}()

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
setUpLabel()
Expand All @@ -49,23 +47,31 @@ class DiaryListTableViewCell: UITableViewCell {
}

private func configureUI() {
accessoryType = .disclosureIndicator

NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
titleLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor)
titleLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 5),
titleLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
titleLabel.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -40)
])

NSLayoutConstraint.activate([
dateLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
dateLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
dateLabel.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor, constant: 20),
dateLabel.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -5)
])

NSLayoutConstraint.activate([
bodyLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
bodyLabel.leadingAnchor.constraint(equalTo: dateLabel.trailingAnchor, constant: 4),
bodyLabel.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -10),
bodyLabel.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor, constant: -40),
bodyLabel.centerYAnchor.constraint(equalTo: dateLabel.centerYAnchor)
])
}

func setModel(_ entity: DiaryEntity) {
titleLabel.text = entity.title
dateLabel.text = DateFormatter().formatToString(from: entity.createdAt, with: "YYYY년 MM월 dd일")
bodyLabel.text = entity.body
}
}
17 changes: 17 additions & 0 deletions Diary/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DiaryListViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

setUpData()
configureUI()
setUpTableView()
}
Expand All @@ -40,6 +41,18 @@ class DiaryListViewController: UIViewController {
tableView.dataSource = self
tableView.register(DiaryListTableViewCell.self, forCellReuseIdentifier: "cell")
}

private func setUpData() {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970

guard let dataAsset = NSDataAsset(name: "sample"),
let decodedData = try? decoder.decode([DiaryEntity].self, from: dataAsset.data) else {
return
}

diaryEntity = decodedData
}
}

extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
Expand All @@ -51,6 +64,10 @@ extension DiaryListViewController: UITableViewDataSource, UITableViewDelegate {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as?
DiaryListTableViewCell else { return UITableViewCell() }

let singleEntity = diaryEntity[indexPath.row]

cell.setModel(singleEntity)

return cell
}
}

0 comments on commit 265d5f4

Please sign in to comment.