250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 개발자
- HTML
- css3
- CSS
- 풀스택
- IOS
- keyframes
- javascript
- 백엔드
- SWIFT
- iPhone
- iOS 개발자
- ipad
- button
- html5
- 프론트엔드
- 애니메이션
- 비전공 개발자
- 자바스크립트
- hover
- image
- jQuery
- 비전공자
- Animation
- front-end
- php
- MAC
- react
- effect
- xcode
Archives
- Today
- Total
비전공자 개발일기
Diary 본문
728x90
SMALL
import Foundation
struct Diary {
var uuidString: String
var title: String
var contents: String
var date: Date
var isStar: Bool
}
import UIKit
class DiaryCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
required init?(coder: NSCoder) {
super.init(coder: coder)
contentView.layer.cornerRadius = 3.0
contentView.layer.borderWidth = 1.0
contentView.layer.borderColor = UIColor.black.cgColor
}
}
import UIKit
class DiaryDetailViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var contentsTextView: UITextView!
@IBOutlet weak var dateLabel: UILabel!
var starButton: UIBarButtonItem?
var diary: Diary?
var indexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
configureView()
NotificationCenter.default.addObserver(self, selector: #selector(starDiaryNotification(_:)), name: NSNotification.Name("starDiary"),
object: nil)
}
private func configureView() {
guard let diary = diary else { return }
titleLabel.text = diary.title
contentsTextView.text = diary.contents
dateLabel.text = dateToString(date: diary.date)
starButton = UIBarButtonItem(image: nil, style: .plain, target: self, action: #selector(starBTN))
starButton?.image = diary.isStar ? UIImage(systemName: "star.fill") : UIImage(systemName: "star")
starButton?.tintColor = .yellow
navigationItem.rightBarButtonItem = starButton
}
@objc func starBTN() {
guard let isStar = diary?.isStar else { return }
if isStar {
starButton?.image = UIImage(systemName: "star")
} else {
starButton?.image = UIImage(systemName: "star.fill")
}
diary?.isStar = !isStar
NotificationCenter.default.post(name: NSNotification.Name("starDiary"), object: ["diary": diary, "isStar" : diary?.isStar ?? false, "uuidString": diary?.uuidString], userInfo: nil)
}
@objc func starDiaryNotification(_ notification: Notification) {
guard let starDiary = notification.object as? [String: Any], let isStar = starDiary["isStar"] as? Bool, let uuidString = starDiary["uuidString"] as? String, let diary = self.diary else { return }
if diary.uuidString == uuidString {
self.diary?.isStar = isStar
self.configureView()
}
}
private func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd(EEEEE)"
formatter.locale = Locale(identifier: "ko_KR")
return formatter.string(from: date)
}
@IBAction func editBTN(_ sender: UIButton) {
guard let viewController = storyboard?.instantiateViewController(identifier: "WriteDiaryViewController") as? WriteDiaryViewController else { return }
guard let indexPath = indexPath, let diary = diary else { return }
viewController.diaryEditorMode = .edit(indexPath, diary)
NotificationCenter.default.addObserver(self, selector: #selector(editDiaryNotification(_:)), name: NSNotification.Name("editDiary"), object: nil)
navigationController?.pushViewController(viewController, animated: true)
}
@objc func editDiaryNotification(_ notification: Notification) {
guard let diary = notification.object as? Diary else { return }
self.diary = diary
configureView()
}
@IBAction func removeBTN(_ sender: UIButton) {
guard let uuidString = self.diary?.uuidString else { return }
NotificationCenter.default.post(name: NSNotification.Name("deleteDiary"), object: uuidString, userInfo: nil)
navigationController?.popViewController(animated: true)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
import UIKit
class StarCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
required init?(coder: NSCoder) {
super.init(coder: coder)
contentView.layer.cornerRadius = 3.0
contentView.layer.borderWidth = 1.0
contentView.layer.borderColor = UIColor.black.cgColor
}
}
import UIKit
class StarViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private var diaryList = [Diary]()
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
loadStarDiaryList()
NotificationCenter.default.addObserver(self, selector: #selector(editDiaryNotification(_:)), name: NSNotification.Name("editDiary"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(starDiaryNotification(_:)), name: NSNotification.Name("starDiary"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(deleteDiaryNotification(_:)), name: NSNotification.Name("deleteDiary"), object: nil)
}
private func configureCollectionView() {
collectionView.collectionViewLayout = UICollectionViewFlowLayout()
collectionView.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
collectionView.delegate = self
collectionView.dataSource = self
}
private func loadStarDiaryList() {
let userDefaults = UserDefaults.standard
guard let data = userDefaults.object(forKey: "diaryList") as? [[String: Any]] else { return }
diaryList = data.compactMap{
guard let uuidString = $0["uuidString"] as? String, let title = $0["title"] as? String, let contents = $0["contents"] as? String, let date = $0["date"] as? Date, let isStar = $0["isStar"] as? Bool else { return nil }
return Diary(uuidString: uuidString, title: title, contents: contents, date: date, isStar: isStar)
}.filter {$0.isStar == true}.sorted(by: {$0.date.compare($1.date) == .orderedDescending})
}
private func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd(EEEEE)"
formatter.locale = Locale(identifier: "ko_KR")
return formatter.string(from: date)
}
@objc func editDiaryNotification(_ notification: Notification) {
guard let diary = notification.object as? Diary, let index = self.diaryList.firstIndex(where: { $0.uuidString == diary.uuidString }) else { return }
self.diaryList[index] = diary
self.diaryList = self.diaryList.sorted(by: {
$0.date.compare($1.date) == .orderedDescending
})
self.collectionView.reloadData()
}
@objc func starDiaryNotification(_ notification: Notification) {
guard let starDiary = notification.object as? [String: Any], let dairy = starDiary["diary"] as? Diary, let isStar = starDiary["isStar"] as? Bool, let uuidString = starDiary["uuidString"] as? String else { return }
if isStar {
self.diaryList.append(dairy)
self.diaryList = self.diaryList.sorted(by: {
$0.date.compare($1.date) == .orderedDescending
})
self.collectionView.reloadData()
} else {
guard let index = self.diaryList.firstIndex(where: { $0.uuidString == uuidString }) else { return }
self.diaryList.remove(at: index)
self.collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
}
}
@objc func deleteDiaryNotification(_ notification: Notification) {
guard let uuidString = notification.object as? String, let index = self.diaryList.firstIndex(where: { $0.uuidString == uuidString }) else { return }
self.diaryList.remove(at: index)
self.collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
}
}
extension StarViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return diaryList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StarCell", for: indexPath) as? StarCell else { return UICollectionViewCell() }
let diary = diaryList[indexPath.row]
cell.titleLabel.text = diary.title
cell.dateLabel.text = dateToString(date: diary.date)
return cell
}
}
extension StarViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width - 20, height: 80)
}
}
extension StarViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let viewController = storyboard?.instantiateViewController(identifier: "DiaryDetailViewController") as? DiaryDetailViewController else { return }
let diary = diaryList[indexPath.row]
viewController.diary = diary
viewController.indexPath = indexPath
navigationController?.pushViewController(viewController, animated: true)
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
private var diaryList = [Diary]() {
didSet {
saveDiaryList()
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
loadDiaryList()
NotificationCenter.default.addObserver(self, selector: #selector(editDiaryNotification(_:)), name: NSNotification.Name("editDiary"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(starDiaryNotification(_:)), name: NSNotification.Name("starDiary"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(deleteDiaryNotification(_:)), name: Notification.Name("deleteDiary"), object: nil)
}
@objc func editDiaryNotification(_ notification: Notification) {
guard let diary = notification.object as? Diary, let index = diaryList.firstIndex(where: { $0.uuidString == diary.uuidString }) else { return }
diaryList[index] = diary
diaryList = self.diaryList.sorted(by: {
$0.date.compare($1.date) == .orderedDescending
})
self.collectionView.reloadData()
}
@objc func starDiaryNotification(_ notification: Notification) {
guard let starDiary = notification.object as? [String: Any], let isStar = starDiary["isStar"] as? Bool, let uuidString = starDiary["uuidString"] as? String, let index = self.diaryList.firstIndex(where: { $0.uuidString == uuidString }) else { return }
diaryList[index].isStar = isStar
}
@objc func deleteDiaryNotification(_ notification: Notification) {
guard let uuidString = notification.object as? String, let index = diaryList.firstIndex(where: { $0.uuidString == uuidString }) else { return }
diaryList.remove(at: index)
collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
}
private func configureCollectionView() {
collectionView.collectionViewLayout = UICollectionViewFlowLayout()
collectionView.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
collectionView.delegate = self
collectionView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let writeDiaryViewController = segue.destination as? WriteDiaryViewController {
writeDiaryViewController.delegate = self
}
}
private func saveDiaryList() {
let date = diaryList.map {
[
"uuidString": $0.uuidString,
"title": $0.title,
"contents": $0.contents,
"date": $0.date,
"isStar": $0.isStar
]
}
let userDefaults = UserDefaults.standard
userDefaults.set(date, forKey: "diaryList")
}
private func loadDiaryList() {
let userDefaults = UserDefaults.standard
guard let data = userDefaults.object(forKey: "diaryList") as? [[String: Any]] else { return }
diaryList = data.compactMap{
guard let uuidString = $0["uuidString"] as? String, let title = $0["title"] as? String, let contents = $0["contents"] as? String, let date = $0["date"] as? Date, let isStar = $0["isStar"] as? Bool else { return nil }
return Diary(uuidString: uuidString, title: title, contents: contents, date: date, isStar: isStar)
}
diaryList = diaryList.sorted(by: {
$0.date.compare($1.date) == .orderedDescending
})
}
private func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd(EEEEE)"
formatter.locale = Locale(identifier: "ko_KR")
return formatter.string(from: date)
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return diaryList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DiaryCell", for: indexPath) as? DiaryCell else { return UICollectionViewCell() }
let diary = diaryList[indexPath.row]
// print(diary)
cell.titleLabel.text = diary.title
// print(diary.title)
cell.dateLabel.text = dateToString(date: diary.date)
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (UIScreen.main.bounds.width / 2) - 20, height: 200)
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let viewController = storyboard?.instantiateViewController(identifier: "DiaryDetailViewController") as? DiaryDetailViewController else { return }
let diary = diaryList[indexPath.row]
viewController.diary = diary
viewController.indexPath = indexPath
navigationController?.pushViewController(viewController, animated: true)
}
}
extension ViewController: WriteDiaryViewDelegate {
func didSelectRegister(diary: Diary) {
diaryList.append(diary)
diaryList = diaryList.sorted(by: {$0.date.compare($1.date) == .orderedDescending})
collectionView.reloadData()
}
}
import UIKit
enum DiaryEditorMode {
case new
case edit(IndexPath, Diary)
}
protocol WriteDiaryViewDelegate: AnyObject {
func didSelectRegister(diary: Diary)
}
class WriteDiaryViewController: UIViewController {
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var dateTextField: UITextField!
@IBOutlet weak var contentsTextView: UITextView!
@IBOutlet weak var confirmButton: UIBarButtonItem!
private let datePicker = UIDatePicker()
private var diaryDate: Date?
weak var delegate: WriteDiaryViewDelegate?
var diaryEditorMode: DiaryEditorMode = .new
override func viewDidLoad() {
super.viewDidLoad()
configureContentsTextView()
configureDatePicker()
confirmButton.isEnabled = false
configureInputField()
cofigureEditMode()
}
private func cofigureEditMode() {
switch diaryEditorMode {
case let .edit(_, diary):
titleTextField.text = diary.title
contentsTextView.text = diary.contents
dateTextField.text = dateToString(date: diary.date)
diaryDate = diary.date
confirmButton.title = "Edit"
default:
break
}
}
private func configureContentsTextView() {
let borderColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
contentsTextView.layer.borderColor = borderColor.cgColor
contentsTextView.layer.borderWidth = 0.5
contentsTextView.layer.cornerRadius = 5.0
}
private func configureDatePicker() {
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .wheels
datePicker.addTarget(self, action: #selector(datePickerValueDidChange(_:)), for: .valueChanged)
dateTextField.inputView = datePicker
}
private func configureInputField() {
contentsTextView.delegate = self
titleTextField.addTarget(self, action: #selector(titleTextFieldDidChange(_:)), for: .editingChanged)
dateTextField.addTarget(self, action: #selector(dateTextFieldDidChange(_:)), for: .editingChanged)
}
@IBAction func confirmButton(_ sender: UIBarButtonItem) {
guard let title = titleTextField.text, let contents = contentsTextView.text, let date = diaryDate else { return }
switch diaryEditorMode {
case .new:
let diary = Diary(uuidString: UUID().uuidString, title: title, contents: contents, date: date, isStar: false)
delegate?.didSelectRegister(diary: diary)
case let .edit(indexPath, diary):
let diary = Diary(uuidString: diary.uuidString, title: title, contents: contents, date: date, isStar: diary.isStar)
NotificationCenter.default.post(name: NSNotification.Name("editDiary"), object: diary, userInfo: nil)
}
navigationController?.popViewController(animated: true)
}
@objc private func datePickerValueDidChange(_ datePicker: UIDatePicker) {
let formmater = DateFormatter()
formmater.dateFormat = "yyyy-MM-dd(EEEEE)"
formmater.locale = Locale(identifier: "ko_KR")
diaryDate = datePicker.date
dateTextField.text = formmater.string(from: datePicker.date)
dateTextField.sendActions(for: .editingChanged)
}
@objc private func titleTextFieldDidChange(_ textField: UITextField) {
vaildateInputField()
}
@objc private func dateTextFieldDidChange(_ textField: UITextField) {
vaildateInputField()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
private func vaildateInputField() {
confirmButton.isEnabled = !(titleTextField.text?.isEmpty ?? true) && !(dateTextField.text?.isEmpty ?? true) && !contentsTextView.text.isEmpty
}
private func dateToString(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yy-MM-dd(EEEEE)"
formatter.locale = Locale(identifier: "ko_KR")
return formatter.string(from: date)
}
}
extension WriteDiaryViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
vaildateInputField()
}
}
728x90
LIST
'SWIFT' 카테고리의 다른 글
Local Push Notification (0) | 2022.12.19 |
---|---|
Remote Push Notification(APNs, FCM) (0) | 2022.12.19 |
Status of COVID-19 outbreak(CocoaPods, Alamfire, Charts) (0) | 2022.12.17 |
Do not Work pod init in workspace (0) | 2022.12.17 |
Weather Information (0) | 2022.12.16 |