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

Added BinaryPost results view #165

Merged
merged 1 commit into from
Mar 3, 2025
Merged
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
63 changes: 63 additions & 0 deletions Gauge/Feed/Views/BinaryResultView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// BinaryResultView.swift
// Gauge
//
// Created by Sameer Arora on 3/2/25.
//

import SwiftUI

struct BinaryResultView: View {
let post: BinaryPost

var body: some View {
// Avoid division by zero
let total = max(post.responseResult1 + post.responseResult2, 1)
let fraction1 = Double(post.responseResult1) / Double(total)
let fraction2 = Double(post.responseResult2) / Double(total)

GeometryReader { geometry in
HStack(spacing: 0) {
// Left side
ZStack(alignment: .leading) {
Rectangle()
.fill(Color.gray)
Text("\(Int(round(Double(fraction1 * 100))))% \(post.responseOption1)")
.foregroundColor(.white)
.padding(.leading, 8)
}
.frame(width: geometry.size.width * fraction1)

// Right side
ZStack(alignment: .trailing) {
Rectangle()
.fill(Color.gray.opacity(0.25))
Text("\(Int(round(Double(fraction2 * 100))))% \(post.responseOption2)")
.foregroundColor(.black)
.padding(.trailing, 8)
}
.frame(width: geometry.size.width * fraction2)
}
.frame(height: 40)
.cornerRadius(8)
}
.padding()
}
}

#Preview {
BinaryResultView(
post: BinaryPost(
postId: "sameer's post",
userId: "Sameer",
categories: [.arts(.painting)],
postDateAndTime: Date(),
question: "Picasso is the goat",
responseOption1: "Facts 💯",
responseOption2: "Nah 🤮",
responseResult1: 167, //167 people pressed Facts
responseResult2: 83, //83 people pressed Nah
favoritedBy: ["sameer"]
)
)
}