Skip to content

Instantly share code, notes, and snippets.

@Androz2091
Created July 18, 2021 20:49
Show Gist options
  • Save Androz2091/529d2b911ae2cd2435d164e53f7e5f3d to your computer and use it in GitHub Desktop.
Save Androz2091/529d2b911ae2cd2435d164e53f7e5f3d to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// WeatherApp
//
// Created by Simon on 18/07/2021.
//
import SwiftUI
struct ContentView: View {
@State private var isNight = false
var body: some View {
ZStack {
BackgroundView(isNight: $isNight)
VStack {
CityTextView(cityName: "Toulouse, France")
MainWeatherStatusView(imageName: isNight ? "moon.stars.fill" : "cloud.sun.fill", temperature: 74)
HStack(spacing: 20) {
WeatherDayView(
dayOfWeek: "TUE",
imageName: "cloud.sun.fill",
temperature: 74)
WeatherDayView(
dayOfWeek: "WED",
imageName: "sun.max.fill",
temperature: 88)
WeatherDayView(
dayOfWeek: "THU",
imageName: "wind.snow",
temperature: 55)
WeatherDayView(
dayOfWeek: "FRI",
imageName: "sunset.fill",
temperature: 60)
WeatherDayView(
dayOfWeek: "SAT",
imageName: "snow",
temperature: 25)
}
Spacer()
Button {
isNight.toggle()
print("Tapped!")
} label: {
WeatherButtonView(title: "Change Day Time",
textColor: .blue,
backgroundColor: .white)
}
Spacer()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct WeatherDayView: View {
var dayOfWeek: String
var imageName: String
var temperature: Int
var body: some View {
VStack {
Text(dayOfWeek)
.font(.system(size: 16, weight: .medium, design: .default))
.foregroundColor(.white)
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 40, height: 40)
Text("\(temperature)°")
.font(.system(size: 28, weight: .medium, design: .default))
.foregroundColor(.white)
}
}
}
struct BackgroundView: View {
@Binding var isNight: Bool
var body: some View {
LinearGradient(gradient: Gradient(colors: [isNight ? .black : .blue, isNight ? .gray : Color("lightBlue")]), startPoint: .topLeading, endPoint: .bottomTrailing)
.edgesIgnoringSafeArea(.all)
}
}
struct CityTextView: View {
var cityName: String
var body: some View {
Text(cityName)
.font(.system(size: 32, weight: .medium, design: .default))
.foregroundColor(.white)
.padding()
}
}
struct MainWeatherStatusView: View {
var imageName: String
var temperature: Int
var body: some View {
VStack(spacing: 8) {
Image(systemName: imageName)
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 180, height: 180)
Text("\(temperature)°")
.font(.system(size: 70, weight: .medium))
.foregroundColor(.white)
}
.padding(.bottom, 40)
}
}
struct WeatherButtonView: View {
var title: String
var textColor: Color
var backgroundColor: Color
var body: some View {
Text(title)
.frame(width: 280, height: 50)
.background(backgroundColor)
.foregroundColor(textColor)
.font(.system(size: 20, weight: .bold, design: .default))
.cornerRadius(10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment