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

Refactor [#7] mvvm 리팩토링 #8

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class DetailPageViewController: UIViewController {
}

extension DetailPageViewController: UIPageViewControllerDataSource {
// UIPageViewControllerDataSource 메서드
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
if let currentIndex = detailViewControllers.firstIndex(of: viewController as! DetailViewController), currentIndex > 0 {
return detailViewControllers[currentIndex - 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,39 @@ import SnapKit
import Then

final class DetailViewController: UIViewController {

var indexNumber: Int = 0
var minTempArray: [Int] = []
var maxTempArray: [Int] = []
var minMinTemp: Int = 0
var maxMaxTemp: Int = 0
var hourWeatherCount: Int = 0

var hourDetailWeathersData: [WeatherDetailResponseDTO] = [WeatherDetailResponseDTO(cod: "", message: 0, cnt: 0, list: [List(dt: 0, main: MainClass(temp: 0, feelsLike: 0, tempMin: 0, tempMax: 0, pressure: 0, seaLevel: 0, grndLevel: 0, humidity: 0, tempKf: 0), weather: [DetailWeather(id: 0, main: .clear, description: "", icon: "")], clouds: Clouds(all: 0), wind: Wind(speed: 0, deg: 0, gust: 0), visibility: 0, pop: 0, sys: DetailSys(pod: .d), dtTxt: "", rain: Rain(the3h: 0), snow: Rain(the3h: 0))], city: City(id: 0, name: "", coord: Coord(lon: 0, lat: 0), country: "", population: 0, timezone: 0, sunrise: 0, sunset: 0))]

var detailWeatherData: WeatherResponseDTO = WeatherResponseDTO(coord: Coord(lon: 0, lat: 0), weather: [Weathers(id: 0, main: "", description: "", icon: "")], base: "", main: Main(temp: 0.0, feelsLike: 0.0, tempMin: 0.0, tempMax: 0.0, pressure: 0, humidity: 0, seaLevel: 0, grndLevel: 0), visibility: 0, wind: Wind(speed: 0.0, deg: 0, gust: 0.0), clouds: Clouds(all: 0), dt: 0, sys: Sys(type: 0, id: 0, country: "", sunrise: 0, sunset: 0), timezone: 0, id: 0, name: "", cod: 0)

let backgroundImageView = UIImageView()

lazy var detailCollectionView = UICollectionView(frame: .zero,
collectionViewLayout: detailFlowLayout)
let detailFlowLayout = UICollectionViewFlowLayout()

let detailViewModel = DetailViewModel()

override func viewDidLoad() {
super.viewDidLoad()
loadWeatherDetailData()
setUI()
}

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

private func updateHourlyWeather() {
detailViewModel.loadWeatherDetailData {
self.detailCollectionView.reloadData()
}
}

private func setUI() {
setStyle()
setLayout()
setDelegate()
configCollectionView()
detailViewModel.loadWeatherDetailData {
self.setStyle()
self.setLayout()
self.setDelegate()
self.configCollectionView()
}
}

private func setDelegate() {
Expand Down Expand Up @@ -96,19 +95,43 @@ final class DetailViewController: UIViewController {
$0.bottom.equalToSuperview().inset(82)
}
}
}

extension DetailViewController: UICollectionViewDelegate { }

extension DetailViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

switch indexPath.section {
case 0:
if kind == UICollectionView.elementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: DetailLocalView.identifier, for: indexPath) as! DetailLocalView
headerView.configLocalView(data: detailViewModel.detailWeatherData)
return headerView
} else {
return UICollectionReusableView()
}
default:
return UICollectionReusableView()
}
}

func caculateMinMax(data: [TenDaysWeather]) {
data.forEach {
minTempArray.append($0.minTemp)
maxTempArray.append($0.maxTemp)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if collectionView == detailCollectionView {
switch section {
case 0:
return CGSize(width: UIScreen.main.bounds.width, height: 260)
default:
return CGSize.zero
}
} else {
return CGSize.zero
}

minMinTemp = minTempArray.min() ?? 0
maxMaxTemp = maxTempArray.max() ?? 0
}
}

extension DetailViewController: UICollectionViewDelegate { }
extension DetailViewController: UITableViewDelegate { }

extension DetailViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
Expand All @@ -123,7 +146,7 @@ extension DetailViewController: UICollectionViewDataSource {
if collectionView == detailCollectionView {
return 1
} else {
return hourWeatherCount
return detailViewModel.hourWeatherCount
}
}

Expand Down Expand Up @@ -158,8 +181,7 @@ extension DetailViewController: UICollectionViewDataSource {
return cell
case 1:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TenDaysCardView.identifier, for: indexPath) as? TenDaysCardView else { return UICollectionViewCell() }
self.caculateMinMax(data: tenDaysWeatherDummy)

self.detailViewModel.caculateMinMax(data: tenDaysWeatherDummy)
cell.tenDaysTableView.delegate = self
cell.tenDaysTableView.dataSource = self
return cell
Expand All @@ -168,45 +190,13 @@ extension DetailViewController: UICollectionViewDataSource {
}
} else {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DetailTimeCollectionViewCell.identifier, for: indexPath) as? DetailTimeCollectionViewCell else { return UICollectionViewCell() }
cell.bindData(data: hourDetailWeathersData[indexNumber], row: indexPath.row)
cell.bindData(data: detailViewModel.hourDetailWeathersData[indexNumber], row: indexPath.row)
return cell
}
}
}


extension DetailViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

switch indexPath.section {
case 0:
if kind == UICollectionView.elementKindSectionHeader {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: DetailLocalView.identifier, for: indexPath) as! DetailLocalView
headerView.configLocalView(data: detailWeatherData)
return headerView
} else {
return UICollectionReusableView()
}
default:
return UICollectionReusableView()
}
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if collectionView == detailCollectionView {
switch section {
case 0:
return CGSize(width: UIScreen.main.bounds.width, height: 260)
default:
return CGSize.zero
}
} else {
return CGSize.zero
}
}
}

extension DetailViewController: UITableViewDelegate { }
extension DetailViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
Expand All @@ -216,46 +206,12 @@ extension DetailViewController: UITableViewDataSource {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TenDaysTableViewCell.identifier, for: indexPath) as? TenDaysTableViewCell else { return UITableViewCell() }
cell.bindData(data: tenDaysWeatherDummy[indexPath.row])
cell.selectionStyle = .none
cell.minMinTemp = minMinTemp
cell.maxMaxTemp = maxMaxTemp
cell.minMinTemp = detailViewModel.minMinTemp
cell.maxMaxTemp = detailViewModel.maxMaxTemp
return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
}

extension DetailViewController {
private func loadWeatherDetailData() {
Task {
do {
let cities = ["seoul", "daegu", "busan", "daejeon", "mokpo"]

self.hourDetailWeathersData = []
var hourDetailWeatherDataArray: [WeatherDetailResponseDTO] = []

for cityName in cities {
do {
if let receivedData = try await WeatherDetailService.shared.GetDetailWeatherData(cityName: cityName) {
hourDetailWeatherDataArray.append(receivedData)
}
} catch {
print("Failed to get weather data for \(cityName): \(error)")
}
}

DispatchQueue.main.async {
self.hourDetailWeathersData = hourDetailWeatherDataArray
self.hourWeatherCount = 9
self.detailCollectionView.reloadData()
print(hourDetailWeatherDataArray)
print("💛💛💛💛💛💛💛")
}
} catch {
print(error)
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// DetailViewModel.swift
// DO_SOPT_33_Assignment
//
// Created by 정채은 on 12/5/23.
//

import UIKit

final class DetailViewModel: NSObject {

var minTempArray: [Int] = []
var maxTempArray: [Int] = []
var minMinTemp: Int = 0
var maxMaxTemp: Int = 0
var hourWeatherCount: Int = 0

var detailWeatherData: WeatherResponseDTO = WeatherResponseDTO(coord: Coord(lon: 0, lat: 0), weather: [Weathers(id: 0, main: "", description: "", icon: "")], base: "", main: Main(temp: 0.0, feelsLike: 0.0, tempMin: 0.0, tempMax: 0.0, pressure: 0, humidity: 0, seaLevel: 0, grndLevel: 0), visibility: 0, wind: Wind(speed: 0.0, deg: 0, gust: 0.0), clouds: Clouds(all: 0), dt: 0, sys: Sys(type: 0, id: 0, country: "", sunrise: 0, sunset: 0), timezone: 0, id: 0, name: "", cod: 0)

var hourDetailWeathersData: [WeatherDetailResponseDTO] = [WeatherDetailResponseDTO(cod: "", message: 0, cnt: 0, list: [List(dt: 0, main: MainClass(temp: 0, feelsLike: 0, tempMin: 0, tempMax: 0, pressure: 0, seaLevel: 0, grndLevel: 0, humidity: 0, tempKf: 0), weather: [DetailWeather(id: 0, main: .clear, description: "", icon: "")], clouds: Clouds(all: 0), wind: Wind(speed: 0, deg: 0, gust: 0), visibility: 0, pop: 0, sys: DetailSys(pod: .d), dtTxt: "", rain: Rain(the3h: 0), snow: Rain(the3h: 0))], city: City(id: 0, name: "", coord: Coord(lon: 0, lat: 0), country: "", population: 0, timezone: 0, sunrise: 0, sunset: 0))]

func loadWeatherDetailData(completion : @escaping() -> Void) {
Task {
do {
let cities = ["seoul", "daegu", "busan", "daejeon", "mokpo"]

self.hourDetailWeathersData = []

for cityName in cities {
do {
if let receivedData = try await WeatherDetailService.shared.GetDetailWeatherData(cityName: cityName) {
hourDetailWeathersData.append(receivedData)
}
} catch {
print("Failed to get weather data for \(cityName): \(error)")
}
}

DispatchQueue.main.async {
self.hourWeatherCount = 9
print("💛💛💛💛💛💛💛")
completion()
}
}
}
}

func caculateMinMax(data: [TenDaysWeather]) {
data.forEach {
minTempArray.append($0.minTemp)
maxTempArray.append($0.maxTemp)
}

minMinTemp = minTempArray.min() ?? 0
maxMaxTemp = maxTempArray.max() ?? 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import UIKit
import SnapKit
import Then

protocol WeatherButtonDelegate: AnyObject {
func weatherButtonTapped(sender: WeatherListButton)
}

class WeatherListButton: UIButton {

var indexNumber = 0
weak var weatherButtonDelegate: WeatherButtonDelegate?

let myPlaceLabel = UILabel()
let localLabel = UILabel()
Expand All @@ -29,24 +24,18 @@ class WeatherListButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setUI()
setAddTarget()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
setUI()
setAddTarget()
}

private func setUI() {
setStyle()
setLayout()
}

private func setAddTarget() {
self.addTarget(self, action: #selector(weatherButtonTapped), for: .touchUpInside)
}

func setStyle() {
self.setBackgroundImage(UIImage(named: "imgList"), for: .normal)
self.imageView?.contentMode = .scaleAspectFill
Expand Down Expand Up @@ -126,8 +115,4 @@ class WeatherListButton: UIButton {
$0.trailing.equalTo(minTempLabel.snp.leading).offset(-6)
}
}

@objc func weatherButtonTapped() {
weatherButtonDelegate?.weatherButtonTapped(sender: self)
}
}
Loading