Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KateKashko committed May 6, 2024
1 parent 946e405 commit 9482fc2
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@ import SwiftUI

struct ActionButton: View {
let action: () -> Void
let imageName: String
let systemImageName: String
let label: String
let isToggled: Bool
let toggledColor: Color

init(systemImageName: String, label: String, isToggled: Bool, toggledColor: Color, action: @escaping () -> Void) {
self.action = action
self.systemImageName = systemImageName
self.label = label
self.isToggled = isToggled
self.toggledColor = toggledColor
}

var body: some View {
Button(action: action) {
Button {
action()
} label: {
VStack(spacing: 2) {
Image(systemName: imageName)
Image(systemName: systemImageName)
.imageScale(.medium)
.foregroundColor(isToggled ? toggledColor : .primary)

Expand Down
9 changes: 0 additions & 9 deletions SwiftBuddiesIOS/Targets/FeedModule/Sources/Feed.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@

import Foundation

struct Post: Identifiable, Hashable, Codable {
struct PostModel: Identifiable, Hashable, Codable {
let id: String
let authorId: String
let likeCount: Int
let commentsCount: Int
let content: String
let imageUrl: String?
var user: User?
var user: UserModel?
}

extension Post {
static var MOCK_POSTS: [Post] = [
extension PostModel {
static var MockPosts: [PostModel] = [
.init(
id: NSUUID().uuidString,
authorId: NSUUID().uuidString,
likeCount: 98,
commentsCount: 10,
content: "Lets do it...",
imageUrl: "example",
user: User.MOCK_USERS[0]
user: UserModel.MockUsers[0]
),
.init(
id: NSUUID().uuidString,
Expand All @@ -36,7 +36,7 @@ extension Post {
commentsCount: 15,
content: "Hello everyone",
imageUrl: "",
user: User.MOCK_USERS[0]
user: UserModel.MockUsers[0]
),
.init(
id: NSUUID().uuidString,
Expand All @@ -45,7 +45,7 @@ extension Post {
commentsCount: 20,
content: "Our first steps in project...",
imageUrl: "logo",
user: User.MOCK_USERS[0]
user: UserModel.MockUsers[0]
)

]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import Foundation

struct User: Identifiable, Hashable, Codable{
struct UserModel: Identifiable, Hashable, Codable {
let id: String
let name: String
let profileImageUrl: String
}

extension User {
static var MOCK_USERS: [User] = [
extension UserModel {
static var MockUsers: [UserModel] = [
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@
import SwiftUI

struct ActionButtonView: View {

@State private var isLiked = false
@State private var showComments = false
@State private var isSafed = false

var body: some View {
HStack {
Spacer()

ActionButton(action: {
ActionButton(systemImageName: isLiked ? "heart.fill" : "heart",
label: "Like",
isToggled: isLiked,
toggledColor: .red) {
isLiked.toggle()
print("like post")
}, imageName: isLiked ? "heart.fill" : "heart", label: "Like", isToggled: isLiked, toggledColor: .red)

}
Spacer()

ActionButton(action: {

ActionButton(systemImageName: "bubble.right",
label: "Comment",
isToggled: false,
toggledColor: .blue) {
showComments.toggle()
}, imageName: "bubble.right", label: "Comment", isToggled: false, toggledColor: .blue)
.sheet(isPresented: $showComments) {
CommentView()
.presentationDetents([.medium, .large])
}

Spacer()

ActionButton(action: {
ActionButton(systemImageName: isSafed ? "bookmark.fill" : "bookmark",
label: "Safe",
isToggled: isSafed,
toggledColor: .yellow) {
isSafed.toggle()
print("Safe")
}, imageName: isSafed ? "bookmark.fill" : "bookmark", label: "Safe", isToggled: isSafed, toggledColor: .yellow)
}

Spacer()
}
Expand All @@ -47,6 +48,6 @@ struct ActionButtonView: View {
}
}

#Preview {
ActionButtonView()
}
//#Preview {
// ActionButtonView()
//}
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,41 @@ struct AddPhotoButton: View {
@ObservedObject var viewModel: AddPostViewModel

var body: some View {
HStack{
Button{
HStack {
Button(action: {
isCameraViewPresented.toggle()
} label: {
ZStack{
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.cyan, lineWidth: 2)
)
Image(systemName: "camera.fill")
.foregroundColor(.gray)
.imageScale(.large)
}
.frame(width: 50, height: 50)
}) {
Image(systemName: "camera.fill")
.foregroundColor(.gray)
.imageScale(.large)
.frame(width: 50, height: 50)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.cyan, lineWidth: 2)
)
)
}
.fullScreenCover(isPresented: $isCameraViewPresented) {
CameraView()
}

Button{
Button(action: {
imagePickerPresented.toggle()
} label: {
ZStack{
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.cyan, lineWidth: 2)
)
Image(systemName: "photo.badge.plus")
.foregroundColor(.gray)
.imageScale(.large)
}
.frame(width: 50, height: 50)
}) {
Image(systemName: "photo.badge.plus")
.foregroundColor(.gray)
.imageScale(.large)
.frame(width: 50, height: 50)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.cyan, lineWidth: 2)
)
)
}
.photosPicker(isPresented: $imagePickerPresented, selection: $viewModel.selectedImage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ struct AddPostContentView: View {
let placeholder = "What is new?"

var body: some View {
ScrollView{
VStack{
ScrollView {
VStack {

// MARK: - Message

TextEditor(text: $customTF)
.foregroundColor(customTF.isEmpty ? .gray : .primary)
.onChange(of: customTF) { _ in
if customTF == placeholder {
customTF = ""
}
}
.overlay(
customTF.isEmpty ? Text(placeholder).foregroundColor(.gray).padding(.all, 4) : nil, alignment: .topLeading
)
Expand All @@ -49,17 +44,19 @@ struct AddPostContentView: View {
}) {
Image(systemName: "pencil.circle.fill")

.imageScale(.medium)
.foregroundColor(.black)
.imageScale(.large)
.symbolRenderingMode(.palette)
.foregroundStyle(Color.white, Color.cyan)
}

Button(action: {
viewModel.postImage = nil
}) {
Image(systemName: "xmark.circle.fill")

.imageScale(.medium)
.foregroundColor(.black)
.imageScale(.large)
.symbolRenderingMode(.palette)
.foregroundStyle(Color.white, Color.cyan)
}
}
.padding([.top, .trailing], 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ struct AddPostHeaderButtonsView: View {

var body: some View {

HStack{
Button{
HStack {
Button {
self.presentationMode.wrappedValue.dismiss()
} label: {
Image(systemName: "xmark.circle")
.imageScale(.large)
}
Spacer()
Button{
Button {
print("safe button pressed")
} label: {
Text("Post")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ struct AddPostView: View {

var body: some View {
NavigationStack {
VStack{
VStack {
AddPostHeaderButtonsView()

Divider()

AddPostContentView(viewModel: viewModel, imagePickerPresented: $imagePickerPresented)
AddPhotoButton(isCameraViewPresented: $isCameraViewPresented, imagePickerPresented: $imagePickerPresented, viewModel: viewModel)
AddPostContentView(viewModel: viewModel,
imagePickerPresented: $imagePickerPresented
)
AddPhotoButton(isCameraViewPresented: $isCameraViewPresented,
imagePickerPresented: $imagePickerPresented,
viewModel: viewModel
)
.padding(.vertical, 10)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import PhotosUI
import SwiftUI

@MainActor
class AddPostViewModel: ObservableObject{
class AddPostViewModel: ObservableObject {

@Published var selectedImage: PhotosPickerItem? {
didSet { Task { await loadImage(fromItem:selectedImage) } }
}

@Published var postImage: Image?


func loadImage(fromItem item: PhotosPickerItem?) async {
guard let item = item else { return }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct CommentCell: View {

let post: Post
let post: PostModel
var body: some View {
HStack(spacing: 8){
if let user = post.user{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct CommentView: View {
VStack{
ScrollView{
LazyVStack(spacing: 12) {
ForEach(Post.MOCK_POSTS) { post in
ForEach(PostModel.MockPosts) { post in
CommentCell(post: post)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import SwiftUI

struct FeedCell: View {
let post: Post
let post: PostModel

var body: some View{
var body: some View {

VStack{
FeedCellHeaderView(post: Post.MOCK_POSTS[0])
FeedCellContentView(post: Post.MOCK_POSTS[1])
FeedCellCountersView(post: Post.MOCK_POSTS[2])
VStack {
FeedCellHeaderView(post: PostModel.MockPosts[0])
FeedCellContentView(post: PostModel.MockPosts[1])
FeedCellCountersView(post: PostModel.MockPosts[2])

Divider()

Expand All @@ -28,5 +28,5 @@ struct FeedCell: View {
}

#Preview {
FeedCell(post: Post.MOCK_POSTS[0])
FeedCell(post: PostModel.MockPosts[0])
}
Loading

0 comments on commit 9482fc2

Please sign in to comment.