Skip to content

Commit

Permalink
refactor and fixed ui for Profile, Event, Chat Views
Browse files Browse the repository at this point in the history
  • Loading branch information
saroar committed Nov 7, 2021
1 parent 19e7b5e commit c74d4c8
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 149 deletions.
6 changes: 3 additions & 3 deletions Addame.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.addame.AddaMeIOS;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -463,7 +463,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.addame.AddaMeIOS;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -491,7 +491,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.addame.AddaMeIOS;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
2 changes: 1 addition & 1 deletion AddameSPM/Sources/AppFeature/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import SharedModels

public enum AppState: Equatable {
case login(LoginState)
case tabs(TabsState)
case tabs(TabsViewState)

public init() { self = .login(.init()) }
}
Expand Down
2 changes: 1 addition & 1 deletion AddameSPM/Sources/ChatView/ChatListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ChatListView: View {
viewStore.send(.fetchMoreMessageIfNeeded(currentItem: messageViewStore.state))
}
.scaleEffect(x: 1, y: -1, anchor: .center)
.hideRowSeparator(background: .white)
.listRowSeparatorHidden()
}
}
}
Expand Down
48 changes: 7 additions & 41 deletions AddameSPM/Sources/ChatView/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,49 +110,15 @@ public struct ChatView: View {
// }

extension View {
/// 隐藏 List 中的 分割线
func hideRowSeparator(insets: EdgeInsets = .init(top: 0, leading: 0, bottom: 0, trailing: 0),
background: Color = .white) -> some View {
modifier(HideRowSeparatorModifier(insets: insets, background: background))
}
}

struct HideRowSeparatorModifier: ViewModifier {

static let defaultListRowHeight: CGFloat = 44

var insets: EdgeInsets
var background: Color

init(insets: EdgeInsets, background: Color) {
self.insets = insets

var alpha: CGFloat = 0
if #available(iOS 14.0, *) {
UIColor(background).getWhite(nil, alpha: &alpha)
assert(alpha == 1, "Setting background to a non-opaque color will result in separators remaining visible.")
@ViewBuilder func listRowSeparatorHidden() -> some View {
if #available(iOS 15.0, *) {
self.listRowSeparator(.hidden)
} else { // ios 14
self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.listRowInsets(EdgeInsets(top: -1, leading: 16, bottom: -1, trailing: 16))
.background(Color(.systemBackground))
}
self.background = background
}

func body(content: Content) -> some View {
content
.padding(insets)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: Self.defaultListRowHeight)
.listRowInsets(EdgeInsets())
.overlay(
VStack {
HStack {}
.frame(maxWidth: .infinity)
.frame(height: 1)
.background(background)
Spacer()
HStack {}
.frame(maxWidth: .infinity)
.frame(height: 1)
.background(background)
}
.padding(.top, -1)
)
}
}
5 changes: 5 additions & 0 deletions AddameSPM/Sources/ConversationsView/ConversationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public struct ConversationsView: View {
.redacted(reason: viewStore.isLoadingPage ? .placeholder : [])
}
}

.navigationTitle("Chats")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Expand All @@ -110,6 +111,10 @@ public struct ConversationsView: View {
.alert(self.store.scope(state: { $0.alert }), dismiss: ConversationsAction.alertDismissed)
}
.debug("ConversationView")
.onAppear {
ViewStore(store.stateless).send(.onAppear)
}
.navigationViewStyle(StackNavigationViewStyle())
.sheet(
store.scope(state: \.contactsState, action: ConversationsAction.contacts),
state: replayNonNil(),
Expand Down
4 changes: 4 additions & 0 deletions AddameSPM/Sources/EventView/EventView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public struct EventView: View {
color: viewStore.state.isLocationAuthorized ? Color.green : Color.red, radius: 20, y: 5)
}
}
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarTitleDisplayMode(.automatic)
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
Expand All @@ -152,6 +153,9 @@ public struct EventView: View {
}
.alert(self.store.scope(state: { $0.alert }), dismiss: .alertDismissed)
}
.onAppear {
ViewStore(store.stateless).send(.onAppear)
}
.sheet(
store.scope(state: \.eventDetailsState, action: EventsAction.eventDetails),
state: replayNonNil(),
Expand Down
2 changes: 1 addition & 1 deletion AddameSPM/Sources/EventView/EventsAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public enum EventsAction: Equatable {
case currentLocationButtonTapped
case locationManager(LocationManager.Action)
case eventsResponse(Result<EventResponse, HTTPRequest.HRError>)
case eventCoordinate(Result<CLPlacemark, Never>)
case eventPlacemarkResponse(Result<CLPlacemark, Never>)
case eventTapped(EventResponse.Item)

case popupSettings
Expand Down
2 changes: 1 addition & 1 deletion AddameSPM/Sources/EventView/EventsEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct EventsEnvironment {
self.userDefaults = userDefaults
}

func getCoordinate(_ location: Location) -> Effect<CLPlacemark, Never> {
func getPlacemark(_ location: Location) -> Effect<CLPlacemark, Never> {
return Effect<CLPlacemark, Never>.future { callback in
let address = CLGeocoder()
address.reverseGeocodeLocation(
Expand Down
23 changes: 13 additions & 10 deletions AddameSPM/Sources/EventView/EventsReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ public let eventReducer = Reducer<EventsState, EventsAction, EventsEnvironment>
}

var fetchEvents: Effect<EventsAction, Never> {
guard state.isConnected, let location = state.location else { return .none }
state.location = location
guard state.isConnected && state.canLoadMorePages,
let location = state.location
else {
return .none
}

guard state.canLoadMorePages else { return .none }
state.location = location

let getDistanceType = environment.userDefaults.integerForKey("typee")
let maxDistance = getDistanceType == 0 ? (250 * 1000) : (250 / 1.609) * 1609
Expand Down Expand Up @@ -122,11 +125,11 @@ public let eventReducer = Reducer<EventsState, EventsAction, EventsEnvironment>
.map(EventsAction.eventsResponse)
}

func getLocation(_ location: Location) -> Effect<EventsAction, Never> {
return environment.getCoordinate(location)
func getPlacemark(_ location: Location) -> Effect<EventsAction, Never> {
return environment.getPlacemark(location)
.receive(on: environment.mainQueue)
.catchToEffect()
.map(EventsAction.eventCoordinate)
.map(EventsAction.eventPlacemarkResponse)
}

func presentChatView() -> Effect<EventsAction, Never> {
Expand All @@ -139,6 +142,7 @@ public let eventReducer = Reducer<EventsState, EventsAction, EventsEnvironment>

switch action {
case .onAppear:

let status = environment.locationManager.authorizationStatus()
if status == .authorizedAlways || status == .authorizedWhenInUse {
return fetchEvents
Expand Down Expand Up @@ -205,7 +209,7 @@ public let eventReducer = Reducer<EventsState, EventsAction, EventsEnvironment>
case let .addressResponse(.success(address)):
return .none

case let .eventCoordinate(.success(placemark)):
case let .eventPlacemarkResponse(.success(placemark)):
let formatter = CNPostalAddressFormatter()
guard let postalAddress = placemark.postalAddress else {
// handle error here
Expand All @@ -218,14 +222,13 @@ public let eventReducer = Reducer<EventsState, EventsAction, EventsEnvironment>
return .none

case let .locationManager(.didUpdateLocations(locations)):

state.isLoadingPage = true
guard state.isConnected, let location = locations.first else { return .none }
state.location = location

state.isLoadingPage = true
return .merge(
fetchEvents,
getLocation(location)
getPlacemark(location)
)

case .locationManager:
Expand Down
13 changes: 12 additions & 1 deletion AddameSPM/Sources/ProfileView/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension ProfileView {
public var isLoadingPage = false
public var canLoadMorePages = true
public var currentPage = 1
public var index = 0
public var settingsState: SettingsState?
public var imagePickerState: ImagePickerState?
public var imageURLs: [String] = []
Expand Down Expand Up @@ -66,7 +67,12 @@ public struct ProfileView: View {

@ViewBuilder
public var body: some View {
WithViewStore(self.store.scope(state: { $0.view }, action: ProfileAction.view)) { viewStore in
WithViewStore(
self.store.scope(
state: { $0.view },
action: ProfileAction.view
)
) { viewStore in

ScrollView(.vertical) {

Expand All @@ -76,6 +82,7 @@ public struct ProfileView: View {
maxIndex: viewStore.state.imageURLs.count - 1
) {
ForEach(viewStore.state.imageURLs, id: \.self) { url in

AsyncImage(
urlString: url,
placeholder: {
Expand Down Expand Up @@ -142,6 +149,10 @@ public struct ProfileView: View {
}
.alert(self.store.scope(state: { $0.alert }), dismiss: .alertDismissed)
}
.onAppear {
ViewStore(store.stateless).send(.onAppear)
}
.navigationViewStyle(StackNavigationViewStyle())
.sheet(
store.scope(
state: \.imagePickerState,
Expand Down
17 changes: 17 additions & 0 deletions AddameSPM/Sources/TabsView/TabsAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ public enum TabsAction: Equatable {
case receivedSocketMessage(Result<WebSocketClient.Message, NSError>)
case sendResponse(NSError?)
}

extension TabsAction {
static func view(_ localAction: TabsView.ViewAction) -> Self {
switch localAction {
case .onAppear:
return .onAppear
case let .didSelectTab(tabs):
return .didSelectTab(tabs)
case let .event(action):
return .event(action)
case let .conversation(action):
return .conversation(action)
case let .profile(action):
return .profile(action)
}
}
}
6 changes: 5 additions & 1 deletion AddameSPM/Sources/TabsView/TabsReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import KeychainService
import ProfileView
import SharedModels

public let tabsReducer = Reducer<TabsState, TabsAction, TabsEnvironment>.combine(
public let tabsReducer = Reducer<
TabsViewState,
TabsAction,
TabsEnvironment
>.combine(
eventReducer.pullback(
state: \.event,
action: /TabsAction.event,
Expand Down
13 changes: 12 additions & 1 deletion AddameSPM/Sources/TabsView/TabsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct TabsState: Equatable {
public var profile: ProfileState
}

struct TabsViewState: Equatable {
public struct TabsViewState: Equatable {
public init(
selectedTab: Tabs, event: EventsState, conversations: ConversationsState, profile: ProfileState
) {
Expand All @@ -49,3 +49,14 @@ struct TabsViewState: Equatable {
public var conversations: ConversationsState
public var profile: ProfileState
}

extension TabsViewState {
public var view: TabsView.ViewState {
TabsView.ViewState(
selectedTab: selectedTab,
event: event,
conversations: conversations,
profile: profile
)
}
}
Loading

0 comments on commit c74d4c8

Please sign in to comment.