Skip to content

Commit

Permalink
feat: buld preview views
Browse files Browse the repository at this point in the history
  • Loading branch information
FranAlarza committed Sep 12, 2024
1 parent 0de6070 commit 97135fe
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@
import SwiftUI

struct ScreenshootPreviewScreen: View {
let viewModel = ScreenshootViewModel()
@Environment(\.dismiss) var dismiss
@State var screenshot: UIImage?
@State var user: String = ""
@State var description: String = ""
@State var selectionType: FeedbackType = .feedback
@State var reportType: FeedbackType = .feedback
@State var imageLines: [Line] = []
@FocusState var focused: Bool

var body: some View {
NavigationView {
VStack(spacing: 12) {
Divider()
reportType
ReportTypeView(reportType: $reportType)
Divider()
userThatReports
EmailTextFieldView(user: $user) {
focused = true
}
Divider()
TextEditor(text: $description)
.frame(maxHeight: .infinity)
.lineLimit(0)
ScreenShootRowView(image: $screenshot)
.focused($focused)
ScreenShootRowView(image: $screenshot, lines: $imageLines)
.frame(height: 64)
Spacer()
}
.padding()
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Send \(selectionType.rawValue.capitalized)")
.navigationTitle("Send \(reportType.rawValue.capitalized)")
.toolbar(content: {
ToolbarItem(placement: .topBarLeading) {
Button(action: {
Expand All @@ -45,7 +51,7 @@ struct ScreenshootPreviewScreen: View {

ToolbarItem(placement: .topBarTrailing) {
Button(action: {
dismiss.callAsFunction()

}, label: {
Image(systemName: "location.fill")
.foregroundColor(.blue)
Expand All @@ -55,23 +61,6 @@ struct ScreenshootPreviewScreen: View {
})
}
}

var reportType: some View {
Picker("Select Type", selection: $selectionType) {
Text(literal(.feedbackTypeFeedback) ?? "").tag(FeedbackType.feedback)
Text(literal(.feedbackTypeBug) ?? "").tag(FeedbackType.bug)
}
.pickerStyle(.segmented)
}

var userThatReports: some View {
HStack(spacing: 16) {
Text("From:")
TextField("Introduce your email", text: $user)
.disabled(!user.isEmpty)
Spacer()
}
}
}

#Preview {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// VideoPreviewScreen.swift
//
//
// Created by Fran Alarza on 12/9/24.
//

import SwiftUI

struct VideoPreviewScreen: View {
let viewModel = ScreenshootViewModel()
@Environment(\.dismiss) var dismiss
@State var url: URL?
@State var user: String = ""
@State var description: String = ""
@State var reportType: FeedbackType = .feedback
@State var imageLines: [Line] = []
@FocusState var focused: Bool

var body: some View {
NavigationView {
VStack(spacing: 12) {
Divider()
ReportTypeView(reportType: $reportType)
Divider()
EmailTextFieldView(user: $user) {
focused = true
}
Divider()
TextEditor(text: $description)
.frame(maxHeight: .infinity)
.lineLimit(0)
.focused($focused)
Spacer()
VideoPreviewRow(videoURL: $url)
.frame(height: 64)
}
.padding()
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Send \(reportType.rawValue.capitalized)")
.toolbar(content: {
ToolbarItem(placement: .topBarLeading) {
Button(action: {
dismiss.callAsFunction()
}, label: {
Text("X")
.font(.system(size: 20))
.foregroundColor(.black)
})
}

ToolbarItem(placement: .topBarTrailing) {
Button(action: {

}, label: {
Image(systemName: "location.fill")
.foregroundColor(.blue)
.rotationEffect(.degrees(10))
})
}
})
}
}
}

#Preview {
VideoPreviewScreen()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@
//

import Foundation
import UIKit
import SwiftUI

final class ScreenshootViewModel {

private let feedbackService: PFeedbackService

init(feedbackService: PFeedbackService) {
init(
feedbackService: PFeedbackService = FeedbackService(
app: App(),
device: Device(),
config: GlobalConfig.shared
)
) {
self.feedbackService = feedbackService
}

func sendScreenshootFeedback(feedback: Feedback) {
feedbackService.postFeedback(feedback) { result in
//
switch result {
case .success(let successType):
print("Feedback sended succesfully")
case .error(let errorType):
print("Feedback sended error:")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ColorPickerView.swift
//
//
// Created by Fran Alarza on 12/9/24.
//

import SwiftUI

struct ColorPickerView: View {
@Binding var selectedColor: Color
let colors: [Color] = [.blue, .red, .black, .pink, .yellow, .green]
var body: some View {
HStack {
ForEach(colors, id: \.description) { color in
buildColorSelector(color: color)
}
}
}

func buildColorSelector(color: Color) -> some View {
Image(systemName: selectedColor == color ? "smallcircle.filled.circle.fill" : "circle.fill")
.foregroundColor(color)
.font(.system(size: 16))
.clipShape(Circle())
.frame(maxWidth: .infinity)
.onTapGesture {
selectedColor = color
}
}

}

#Preview {
return ColorPickerView(selectedColor: .constant(.blue))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,38 @@

import SwiftUI

struct Line {
var path: Path = Path()
var color: Color = .red
var lineWith: Double = 1.0
}

struct EditScreenshotView: View {
@Environment(\.dismiss) var dismiss
@Binding var screenshot: UIImage?
@State private var paths: [Path] = []
@State private var currentPath = Path()
@Binding var lines: [Line]
@State private var currentLine: Line = Line()
@State private var selectedColor: Color = .red
@State private var editMode = false

var body: some View {
VStack {
topBarView
drawedImage
Spacer()
drawedImage
VStack(spacing: 0) {
if editMode {
ColorPicker("Color", selection: $selectedColor)
} else {
ColorPicker("Color", selection: $selectedColor)
.hidden()
}
topBarView
}
}
.onChange(of: selectedColor, perform: { value in
currentLine.color = value
})
.animation(.easeIn, value: editMode)
}

var topBarView: some View {
Expand All @@ -33,13 +53,27 @@ struct EditScreenshotView: View {

Spacer()

Text("Edit Screenshoot")
.font(.system(size: 16, weight: .bold))
Button(action: {
editMode.toggle()
}, label: {
Image(systemName: "pencil.and.scribble")
.foregroundColor(editMode ? .blue : .black)
})

Spacer()

Button(action: {
dismiss.callAsFunction()
lines.remove(at: lines.count - 1)
}, label: {
Image(systemName: "arrow.uturn.backward")
})
.foregroundColor(lines.isEmpty ? .gray : .black)
.disabled(lines.isEmpty)

Spacer()

Button(action: {
saveDrawing()
}, label: {
Image(systemName: "checkmark")
.foregroundColor(.blue)
Expand All @@ -56,24 +90,62 @@ struct EditScreenshotView: View {
.scaledToFit()

Canvas { context, size in
for path in paths {
context.stroke(path, with: .color(.red), lineWidth: 2)
for line in lines {
context.stroke(line.path, with: .color(line.color), lineWidth: line.lineWith)
}
context.stroke(currentPath, with: .color(.red), lineWidth: 2)
context.stroke(currentLine.path, with: .color(selectedColor), lineWidth: currentLine.lineWith)
}
.gesture(DragGesture(minimumDistance: 0)
.onChanged { value in
currentPath.addLine(to: value.location)
paths.append(currentPath)
}
.onEnded { _ in
currentPath = Path()
.gesture(dragGesture)
}
}

var dragGesture: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onChanged { value in
if editMode {
currentLine.path.addLine(to: value.location)
}
)
}
.onEnded { _ in
lines.append(currentLine)
currentLine = Line()
}
}

func saveDrawing() {
guard let baseImage = screenshot else { return }

// Crear un renderizador con el tamaño de la imagen
let renderer = UIGraphicsImageRenderer(size: baseImage.size)

let transformedImage = renderer.image { context in
baseImage.draw(at: .zero)

// Dibujar las líneas en la imagen
let scaleX = baseImage.size.width / UIScreen.main.bounds.width
let scaleY = baseImage.size.height / UIScreen.main.bounds.height

// let firstLineStart = CGPoint(x: baseImage.size.width * 0.1, y: baseImage.size.height * 0.1)
// let firstLineEnd = CGPoint(x: baseImage.size.width * 0.9, y: baseImage.size.height * 0.1)
// context.cgContext.move(to: firstLineStart)
// context.cgContext.addLine(to: firstLineEnd)
// context.cgContext.strokePath()

for line in lines {
let path = line.path
let transformedPath = path.applying(CGAffineTransform(scaleX: scaleX, y: scaleY))
context.cgContext.move(to: line.path.boundingRect.origin)
context.cgContext.addLine(to: line.path.currentPoint ?? CGPoint())
context.cgContext.setStrokeColor(UIColor(line.color).cgColor)
context.cgContext.setLineWidth(line.lineWith)
context.cgContext.strokePath()
}
}

self.screenshot = transformedImage
}
}

#Preview {
EditScreenshotView(screenshot: .constant(UIImage(systemName: "checkmark.circle.fill") ?? UIImage()))
EditScreenshotView(screenshot: .constant(UIImage(systemName: "checkmark.circle.fill") ?? UIImage()), lines: .constant([]))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// EmailTextFieldView.swift
//
//
// Created by Fran Alarza on 12/9/24.
//

import SwiftUI

struct EmailTextFieldView: View {
@Binding var user: String
let onSubmit: () -> Void

var body: some View {
HStack(spacing: 16) {
Text("From:")
TextField("Introduce your email", text: $user)
.textContentType(.emailAddress)
.keyboardType(.emailAddress)
.submitLabel(.next)
.onSubmit {
onSubmit()
}
Spacer()
}
}
}

#Preview {
EmailTextFieldView(user: .constant("[email protected]"), onSubmit: {})
}
Loading

0 comments on commit 97135fe

Please sign in to comment.