Skip to content

Commit

Permalink
[Feat] 도시 삭제 반영(#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
heerucan committed Aug 18, 2021
1 parent 57a4a64 commit f78a17c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

struct GeneralAPI {
static let baseURL = "https://api.openweathermap.org/data/2.5"
static let appid = "c01b53a14126d3873f86c0c8921524be"
static let appid = "cea1d8866a0ca2c205c2b5e2a30f160c"

///https://api.openweathermap.org/data/2.5/onecall?appid=c01b53a14126d3873f86c0c8921524be&units=metric&lat=36.756866455078125&lon=127.22335105296
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ class ListTVC: UITableViewCell {
static let identifier = "ListTVC"

// MARK: - Properties
let backImageView = UIImageView().then {
$0.image = UIImage(named: "backImage")
$0.contentMode = .scaleAspectFill
$0.clipsToBounds = true
}

let subTitleLabel = UILabel().then {
$0.font = .systemFont(ofSize: 12, weight: .semibold)
$0.textColor = .white
$0.textAlignment = .left
}

let titleLabel = UILabel().then {
$0.font = .systemFont(ofSize: 22, weight: .semibold)
$0.font = .systemFont(ofSize: 22, weight: .regular)
$0.textColor = .white
$0.textAlignment = .left
}

let tempLabel = UILabel().then {
$0.font = .systemFont(ofSize: 60, weight: .regular)
$0.font = .systemFont(ofSize: 60, weight: .thin)
$0.textColor = .white
$0.textAlignment = .left
}
Expand All @@ -36,15 +42,7 @@ class ListTVC: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configUI()
NotificationCenter.default.addObserver(self,
selector: #selector(changeToC),
name: NSNotification.Name("changeUnitToC"),
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(changeToF),
name: NSNotification.Name("changeUnitToF"),
object: nil)
registerNotification()
}

required init?(coder: NSCoder) {
Expand All @@ -60,12 +58,14 @@ class ListTVC: UITableViewCell {
subTitleLabel.getShadow()
titleLabel.getShadow()
tempLabel.getShadow()
backgroundColor = .brown

addSubviews([backImageView, subTitleLabel, titleLabel, tempLabel])
backImageView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}

func setupFirstCellAutoLayout() { /// first cell
addSubviews([subTitleLabel, titleLabel, tempLabel])

subTitleLabel.snp.makeConstraints { make in
make.leading.equalTo(20)
make.bottom.equalTo(titleLabel.snp.top).offset(-1)
Expand All @@ -82,9 +82,7 @@ class ListTVC: UITableViewCell {
}
}

func setupRemainCellAutoLayout() { /// 남은 cell들
addSubviews([subTitleLabel, titleLabel, tempLabel])

func setupRemainCellAutoLayout() { /// 남은 cell들
subTitleLabel.snp.makeConstraints { make in
make.top.equalToSuperview().offset(15)
make.leading.equalTo(20)
Expand All @@ -102,6 +100,18 @@ class ListTVC: UITableViewCell {
}
}

func registerNotification() {
NotificationCenter.default.addObserver(self,
selector: #selector(changeToC),
name: NSNotification.Name("changeUnitToC"),
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(changeToF),
name: NSNotification.Name("changeUnitToF"),
object: nil)
}

// MARK: - @objc
@objc func changeToC(_ sender: Notification) {
let temp = Double(tempLabel.text!)!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ class MainCVC: UICollectionViewCell {
super.init(frame: frame)
configUI()
setupAutoLayout()
// NotificationCenter.default.addObserver(self, selector: #selector(receiveFirstCell(_:)),
// name: NSNotification.Name("clickFirstCell"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(receiveOtherCell(_:)),
name: NSNotification.Name("clickCell"), object: nil)
registerNotification()
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -147,6 +144,11 @@ class MainCVC: UICollectionViewCell {
mainTV.register(DetailTVC.self, forCellReuseIdentifier: "DetailTVC")
mainTV.register(MapTVC.self, forCellReuseIdentifier: "MapTVC")
}

func registerNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(receiveOtherCell(_:)),
name: NSNotification.Name("clickCell"), object: nil)
}

// MARK: - setData
func setData(location: String, temp: String, condition: String, max: String, min: String) {
Expand All @@ -158,14 +160,6 @@ class MainCVC: UICollectionViewCell {
}

// MARK: - @objc
// @objc func receiveFirstCell(_ notifiction: Notification) {
// if let location = notifiction.object as? [String],
// let temp = notifiction.object as? [String] {
// locationLabel.text = location[0]
// tempLabel.text = temp[1]
// }
// }

@objc func receiveOtherCell(_ notifiction: Notification) {
if let location = notifiction.object as? [String],
let temp = notifiction.object as? [String] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Then

class ListViewController: UIViewController {
// MARK: - Properties
var cityList: [ListModel] = [ListModel(subTitle: "", title: "나의 위치", temp: "")]
var cityList: [ListModel] = [ListModel(subTitle: "", title: "나의 위치", temp: "33")]

let mainTV = UITableView(frame: .zero, style: .grouped)

Expand Down Expand Up @@ -54,7 +54,7 @@ class ListViewController: UIViewController {
func registerNotification() {
NotificationCenter.default.addObserver(self,
selector: #selector(addCity(_:)),
name: NSNotification.Name("appendCity"),
name: NSNotification.Name("appendListCell"),
object: nil)
}

Expand All @@ -78,7 +78,6 @@ class ListViewController: UIViewController {
}

@objc func addCity(_ notification: Notification) {
print(cityList, "addCity")
if let list = notification.object as? [ListModel] {
cityList.append(contentsOf: list)
}
Expand All @@ -100,6 +99,7 @@ extension ListViewController: UITableViewDelegate {
if editingStyle == .delete {
cityList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .none)
NotificationCenter.default.post(name: NSNotification.Name("deleteCell"), object: indexPath.row, userInfo: nil)
}
}

Expand Down Expand Up @@ -158,7 +158,9 @@ extension ListViewController: UITableViewDataSource {
tableView.deselectRow(at: indexPath, animated: true)
dismiss(animated: true) {
NotificationCenter.default.post(name: NSNotification.Name("pageControl"), object: indexPath.row)
// NotificationCenter.default.post(name: NSNotification.Name("selectCell"), object: indexPath, userInfo: nil)
if indexPath.row == 0 {

NotificationCenter.default.post(name: NSNotification.Name("clickCell"),
object: [self.cityList[indexPath.row].subTitle,
self.cityList[indexPath.row].temp])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ViewController: UIViewController {
var weatherModel: WeatherModel?

// MARK: - Properties
// private var vcList = [ListModel(subTitle: "", title: "나의 위치", temp: "")]
private var cityList = [ListModel(subTitle: "", title: "나의 위치", temp: "")]

var isAddNewCityView: Bool = false
Expand Down Expand Up @@ -237,10 +238,15 @@ class ViewController: UIViewController {
func registerNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(changePageControl(_:)),
name: NSNotification.Name("pageControl"), object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(addCity(_:)),
name: NSNotification.Name("appendCity"),
NotificationCenter.default.addObserver(self, selector: #selector(selectCell(_:)),
name: NSNotification.Name("selectCell"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(addCity(_:)),
name: NSNotification.Name("addCity"),
object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(deleteCity(_:)),
name: NSNotification.Name("deleteCell"),
object: nil)

}

// MARK: - @objc
Expand All @@ -253,7 +259,8 @@ class ViewController: UIViewController {
cityList.append(ListModel(subTitle: String(self.currentTime).toTime("a h:mm", self.timezoneOffset),
title: self.location,
temp: String(Int(self.temperature)) + "º"))
NotificationCenter.default.post(name: NSNotification.Name("appendCity"), object: cityList, userInfo: nil)
NotificationCenter.default.post(name: NSNotification.Name("appendListCell"), object: cityList, userInfo: nil)
NotificationCenter.default.post(name: NSNotification.Name("addCity"), object: cityList, userInfo: nil)
presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

Expand All @@ -263,6 +270,12 @@ class ViewController: UIViewController {
}
}

@objc func selectCell(_ notification: Notification) {
if let index = notification.object as? IndexPath {
mainCV.cellForItem(at: index)
}
}

@objc func touchupLeftBarButton(_ sender: UIButton) {
let application = UIApplication.shared
let weatherURL = URL(string: "https://weather.com/ko-KR/weather/today/")!
Expand All @@ -282,10 +295,20 @@ class ViewController: UIViewController {
}

@objc func addCity(_ notification: Notification) {
print(cityList, "addCity", "vc")
if let list = notification.object as? [ListModel] {
cityList.append(contentsOf: list)
}
pageControl.numberOfPages = cityList.count
pageControl.setIndicatorImage(UIImage(systemName: "location.fill"), forPage: 0)
mainCV.reloadData()
}

@objc func deleteCity(_ notification: Notification) {
if let index = notification.object as? Int {
cityList.remove(at: index)
print(cityList.count, cityList, "deleteCity")
}
pageControl.numberOfPages = cityList.count
mainCV.reloadData()
}
}
Expand Down Expand Up @@ -316,16 +339,13 @@ extension ViewController: UICollectionViewDataSource {
min: "최저: \(Int(min))º")
setBackgroundView(time: currentTime)
setAnimationView(condition: condition)
print(currentTime)
if isAddNewCityView {
cell.fetchWeather(lat: searchLatitude!, lon: searchLongtitude!, exclude: "")
cell.setData(location: location,
temp: "\(Int(temperature))º",
condition: condition,
max: "최고: \(Int(max))º",
min: "최저: \(Int(min))º")
// setBackgroundView(time: currentTime)
// setAnimationView(condition: condition)
}
return cell
}
Expand Down

0 comments on commit f78a17c

Please sign in to comment.