Skip to content

Commit

Permalink
feat: DiaryEntity타입 생성 및 JSON파일 디코딩을 위한 메서드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
iOS-Yetti committed Aug 29, 2023
1 parent 93d6c80 commit 9c6dcf7
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
disabled_rules:
- trailing_whitespace
- line_length

excluded:
- Diary/Resource/AppDelegate.swift
- Diary/Resource/SceneDelegate.swift


12 changes: 12 additions & 0 deletions Diary.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3BBC98902A9D73D70047DE81 /* DiaryCollectionViewListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BBC988F2A9D73D70047DE81 /* DiaryCollectionViewListCell.swift */; };
3BBC98942A9D89D20047DE81 /* DiaryEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BBC98932A9D89D20047DE81 /* DiaryEntity.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 @@ -19,6 +20,7 @@

/* Begin PBXFileReference section */
3BBC988F2A9D73D70047DE81 /* DiaryCollectionViewListCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryCollectionViewListCell.swift; sourceTree = "<group>"; };
3BBC98932A9D89D20047DE81 /* DiaryEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiaryEntity.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 @@ -68,6 +70,14 @@
path = View;
sourceTree = "<group>";
};
3BBC98922A9D897B0047DE81 /* Model */ = {
isa = PBXGroup;
children = (
3BBC98932A9D89D20047DE81 /* DiaryEntity.swift */,
);
path = Model;
sourceTree = "<group>";
};
C739AE18284DF28600741E8F = {
isa = PBXGroup;
children = (
Expand All @@ -88,6 +98,7 @@
C739AE23284DF28600741E8F /* Diary */ = {
isa = PBXGroup;
children = (
3BBC98922A9D897B0047DE81 /* Model */,
3BBC988E2A9D683C0047DE81 /* View */,
3BBC988D2A9D68290047DE81 /* Controller */,
3BBC988C2A9D67EC0047DE81 /* Resource */,
Expand Down Expand Up @@ -196,6 +207,7 @@
C739AE27284DF28600741E8F /* SceneDelegate.swift in Sources */,
C739AE2F284DF28600741E8F /* Diary.xcdatamodeld in Sources */,
3BBC98902A9D73D70047DE81 /* DiaryCollectionViewListCell.swift in Sources */,
3BBC98942A9D89D20047DE81 /* DiaryEntity.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 21 additions & 4 deletions Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@
import UIKit

final class DiaryListViewController: UIViewController {
private var diaryEntity: [DiaryEntity]?

private let collectionView: UICollectionView = {
let configuration: UICollectionLayoutListConfiguration = UICollectionLayoutListConfiguration(appearance: .plain)
let layout = UICollectionViewCompositionalLayout.list(using: configuration)
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.translatesAutoresizingMaskIntoConstraints = false
view.register(DiaryCollectionViewListCell.self, forCellWithReuseIdentifier: DiaryCollectionViewListCell.identifier)

return view
}()

override func viewDidLoad() {
super.viewDidLoad()
decodeData()
collectionView.dataSource = self
collectionView.delegate = self
configureUI()
configureAutoLayout()

}

private func configureUI() {
view.addSubview(collectionView)
view.backgroundColor = .systemBackground
}

private func configureAutoLayout() {
let safeArea = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
Expand All @@ -36,18 +43,28 @@ final class DiaryListViewController: UIViewController {
collectionView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor)
])
}

private func decodeData() {
guard let asset = NSDataAsset(name: "sample") else {
return
}

let decodedData = try? JSONDecoder().decode([DiaryEntity].self, from: asset.data)
diaryEntity = decodedData
}
}

extension DiaryListViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
return diaryEntity?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DiaryCollectionViewListCell.identifier, for: indexPath)
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DiaryCollectionViewListCell.identifier, for: indexPath) as? DiaryCollectionViewListCell else {
return UICollectionViewCell()
}
cell.configureLabel(diaryEntity![indexPath.item])

return cell
}


}
17 changes: 17 additions & 0 deletions Diary/Model/DiaryEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// DiaryEntity.swift
// Diary
//
// Created by idinaloq, yetti on 2023/08/29.
//

struct DiaryEntity: Decodable {
let title: String
let body: String
let createdAt: Int

enum CodingKeys: String, CodingKey {
case title, body
case createdAt = "created_at"
}
}
13 changes: 13 additions & 0 deletions Diary/Resource/Assets.xcassets/sample.dataset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data" : [
{
"filename" : "sample.json",
"idiom" : "universal",
"universal-type-identifier" : "public.json"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit 9c6dcf7

Please sign in to comment.