Skip to content

Commit

Permalink
fix server
Browse files Browse the repository at this point in the history
Signed-off-by: PhongND <[email protected]>
  • Loading branch information
PhongND committed Oct 5, 2023
1 parent be97052 commit d43a12e
Show file tree
Hide file tree
Showing 48 changed files with 869 additions and 1,226 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified BasicApp/.DS_Store
Binary file not shown.
Binary file modified BasicApp/SwiftUI/.DS_Store
Binary file not shown.
Binary file modified BasicApp/SwiftUI/Todo+Combine+SwiftUI/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@_exported import Transform
@_exported import MCombineRequest
@_exported import ComposableArchitecture
@_exported import Json
@_exported import SwiftLogger
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct TodoApp: App {

public extension DependencyValues {
var urlString: String {
"http://127.0.0.1:8080"
"http://127.0.0.1:8080/todos"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation

struct TodoModel: Codable, Identifiable, Equatable {
var id: UUID
var title: String
var text: String
var isCompleted: Bool
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ComposableArchitecture
import SwiftUI

struct Auth: Reducer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct Main: Reducer {
// MARK: State
struct State: Equatable {
var counterState = Counter.State()
@BindingState var title: String = ""
@BindingState var text: String = ""
var todos: IdentifiedArrayOf<TodoModel> = []
var isLoading: Bool = false
}
Expand Down Expand Up @@ -62,13 +62,13 @@ struct Main: Reducer {
case .logout:
return .send(.changeRootScreen(.auth))
case .viewCreateTodo:
if state.title.isEmpty {
if state.text.isEmpty {
return .none
}
let title = state.title
state.title = ""
let text = state.text
state.text = ""
let id = uuid()
let todo = TodoModel(id: id, title: title, isCompleted: false)
let todo = TodoModel(id: id, text: text, isCompleted: false)
return .send(.createOrUpdateTodo(todo))
// MARK: - Networking
/// GET TODO
Expand All @@ -79,7 +79,7 @@ struct Main: Reducer {
state.isLoading = true
state.todos.removeAll()
let request = MRequest {
RUrl(urlString: urlString)
RUrl(urlString)
RMethod(.get)
}
if isUsingPublisher {
Expand All @@ -95,23 +95,28 @@ struct Main: Reducer {
await send(.responseTodo(data))
}
}
case .responseTodo(let json):
if let items = json.toModel([TodoModel].self) {
case .responseTodo(let data):
log.info(Json(data))
state.isLoading = false
if let items = data.toModel([TodoModel].self) {
for item in items {
state.todos.updateOrAppend(item)
}
}
/// CREATE OR UPDATE TODO
case .createOrUpdateTodo(let todo):
log.info(todo.toJson())
let request = MRequest {
RUrl(urlString: urlString)
REncoding(JSONEncoding.default)
RUrl(urlString)
RMethod(.post)
Rbody(todo.toData())
REncoding(JSONEncoding.default)

}
if isUsingPublisher {
return .publisher {
request
.printCURLRequest()
.compactMap{$0.data}
.map(Main.Action.responseCreateOrUpdateTodo)
.eraseToAnyPublisher()
Expand All @@ -122,21 +127,24 @@ struct Main: Reducer {
await send(.responseCreateOrUpdateTodo(data))
}
}
case .responseCreateOrUpdateTodo(let json):
if let item = json.toModel(TodoModel.self) {
case .responseCreateOrUpdateTodo(let data):
log.info(Json(data))
if let item = data.toModel(TodoModel.self) {
state.todos.updateOrAppend(item)
}
/// UPDATE TODO
case .updateTodo(let todo):
let request = MRequest {
RUrl(urlString: urlString)
RUrl(urlString)
.withPath(todo.id.toString())
RMethod(.post)
Rbody(todo.toData())
RMethod(.post)
REncoding(JSONEncoding.default)
}
if isUsingPublisher {
return .publisher {
request
.printCURLRequest()
.compactMap{$0.data}
.map(Main.Action.responseUpdateTodo)
.eraseToAnyPublisher()
Expand All @@ -147,14 +155,15 @@ struct Main: Reducer {
await send(.responseUpdateTodo(data))
}
}
case .responseUpdateTodo(let json):
if let item = json.toModel(TodoModel.self) {
case .responseUpdateTodo(let data):
log.info(data.toJson())
if let item = data.toModel(TodoModel.self) {
state.todos.updateOrAppend(item)
}
/// DELETE TODO
case .deleteTodo(let todo):
let request = MRequest {
RUrl(urlString: urlString)
RUrl(urlString)
.withPath(todo.id.toString())
RMethod(.delete)
}
Expand Down Expand Up @@ -201,38 +210,12 @@ struct MainView: View {

var body: some View {
ZStack {
#if os(macOS)
content
.toolbar {
ToolbarItem(placement: .status) {
HStack {
CounterView(
store: store
.scope(
state: \.counterState,
action: Main.Action.counterAction
)
)
Spacer()
Button(action: {
viewStore.send(.logout)
}, label: {
Text("Logout")
.foregroundColor(Color.blue)
})
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
#endif
#if os(iOS)
NavigationView {
content
.navigationTitle("Todos")
.navigationBarItems(leading: leadingBarItems, trailing: trailingBarItems)
}
.navigationViewStyle(.stack)
#endif
}
.onAppear {
viewStore.send(.viewOnAppear)
Expand All @@ -243,67 +226,69 @@ struct MainView: View {
}
}


extension MainView {
/// create content view in screen
private var content: some View {
List {
Section {
ZStack {
HStack {
Spacer()
if viewStore.isLoading {
ProgressView()
} else {
Text("Reload")
.bold()
.onTapGesture {
viewStore.send(.getTodo)
}
}
Spacer()
}
.frame(height: 60)
}
}
HStack {
TextField("title", text: viewStore.$title)
Button(action: {
viewStore.send(.viewCreateTodo)
}, label: {
Text("Create")
.bold()
.foregroundColor(viewStore.title.isEmpty ? Color.gray : Color.green)
})
.disabled(viewStore.title.isEmpty)
}

topview
inputview
ForEach(viewStore.todos) { todo in
HStack {
HStack {
Image(systemName: todo.isCompleted ? "checkmark.square" : "square")
.frame(width: 40, height: 40, alignment: .center)
Text(todo.title)
Text(todo.text)
.underline(todo.isCompleted, color: Color.black)
Spacer()
}
.contentShape(Rectangle())
.onTapGesture {
viewStore.send(.toggleTodo(todo))
}
Button(action: {
Button {
viewStore.send(.deleteTodo(todo))
}, label: {
} label: {
Text("Delete")
.foregroundColor(Color.gray)
})
}
}
}
.padding(.all, 0)
}
.padding(.all, 0)
}

private var topview: some View {
Section {
ZStack(alignment: .center) {
if viewStore.isLoading {
ProgressView()
} else {
Text("Reload")
.bold()
.onTapGesture {
viewStore.send(.getTodo)
}
}
}
.frame(height: 60)
.frame(maxWidth: .infinity)
}
}

private var inputview: some View {
HStack {
TextField("text", text: viewStore.$text)
Button(action: {
viewStore.send(.viewCreateTodo)
}, label: {
Text("Create")
.bold()
.foregroundColor(viewStore.text.isEmpty ? Color.gray : Color.green)
})
.disabled(viewStore.text.isEmpty)
}
}

private var leadingBarItems: some View {
CounterView(
store: store
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ComposableArchitecture
import SwiftUI

struct Root: Reducer {
Expand Down Expand Up @@ -55,8 +54,6 @@ struct Root: Reducer {
}
}



struct RootView: View {

private let store: StoreOf<Root>
Expand Down Expand Up @@ -99,9 +96,6 @@ struct RootView: View {
.onDisappear {
viewStore.send(.viewOnDisappear)
}
#if os(macOS)
.frame(minWidth: 700, idealWidth: 700, maxWidth: .infinity, minHeight: 500, idealHeight: 500, maxHeight: .infinity, alignment: .center)
#endif
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit d43a12e

Please sign in to comment.