Skip to content

Commit

Permalink
PlusButtonタップでNewTweetViewを表示するように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
boardguy1024 committed Oct 17, 2023
1 parent 84f0616 commit 5baf45d
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 47 deletions.
26 changes: 13 additions & 13 deletions TwitterSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
FFACF2B22AC1519E00A2C911 /* ExploreViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B12AC1519E00A2C911 /* ExploreViewModel.swift */; };
FFACF2B42AC15A3400A2C911 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B32AC15A3400A2C911 /* SearchBar.swift */; };
FFACF2B62AC1937A00A2C911 /* TweetService.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B52AC1937A00A2C911 /* TweetService.swift */; };
FFACF2B82AC1A53600A2C911 /* UploadTweetViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B72AC1A53600A2C911 /* UploadTweetViewModel.swift */; };
FFACF2B82AC1A53600A2C911 /* NewTweetViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B72AC1A53600A2C911 /* NewTweetViewModel.swift */; };
FFACF2BA2AC1A78400A2C911 /* FeedViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2B92AC1A78400A2C911 /* FeedViewModel.swift */; };
FFACF2BC2AC1A82C00A2C911 /* Tweet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2BB2AC1A82C00A2C911 /* Tweet.swift */; };
FFACF2BE2AC1BD2A00A2C911 /* ProfileViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFACF2BD2AC1BD2A00A2C911 /* ProfileViewModel.swift */; };
Expand Down Expand Up @@ -164,7 +164,7 @@
FFACF2B12AC1519E00A2C911 /* ExploreViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreViewModel.swift; sourceTree = "<group>"; };
FFACF2B32AC15A3400A2C911 /* SearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
FFACF2B52AC1937A00A2C911 /* TweetService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TweetService.swift; sourceTree = "<group>"; };
FFACF2B72AC1A53600A2C911 /* UploadTweetViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadTweetViewModel.swift; sourceTree = "<group>"; };
FFACF2B72AC1A53600A2C911 /* NewTweetViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTweetViewModel.swift; sourceTree = "<group>"; };
FFACF2B92AC1A78400A2C911 /* FeedViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedViewModel.swift; sourceTree = "<group>"; };
FFACF2BB2AC1A82C00A2C911 /* Tweet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tweet.swift; sourceTree = "<group>"; };
FFACF2BD2AC1BD2A00A2C911 /* ProfileViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -268,7 +268,7 @@
FF2EE4E82ABEF042000522CF /* SideMenu */ = {
isa = PBXGroup;
children = (
FF2EE4EA2ABEF066000522CF /* Views */,
FF2EE4EA2ABEF066000522CF /* View */,
FF2EE4E92ABEF060000522CF /* ViewModel */,
);
path = SideMenu;
Expand All @@ -282,39 +282,39 @@
path = ViewModel;
sourceTree = "<group>";
};
FF2EE4EA2ABEF066000522CF /* Views */ = {
FF2EE4EA2ABEF066000522CF /* View */ = {
isa = PBXGroup;
children = (
FF2EE4EB2ABEF082000522CF /* SideMenuView.swift */,
FF2EE4ED2ABEF09C000522CF /* SideMenuRowView.swift */,
FF2EE4F32ABF0D5F000522CF /* SideMenuOptionRowView.swift */,
);
path = Views;
path = View;
sourceTree = "<group>";
};
FF2EE4F52ABF142A000522CF /* UploadTweetView */ = {
FF2EE4F52ABF142A000522CF /* NewTweetView */ = {
isa = PBXGroup;
children = (
FF2EE4F72ABF1441000522CF /* Views */,
FF2EE4F72ABF1441000522CF /* View */,
FF2EE4F62ABF143B000522CF /* ViewModel */,
);
path = UploadTweetView;
path = NewTweetView;
sourceTree = "<group>";
};
FF2EE4F62ABF143B000522CF /* ViewModel */ = {
isa = PBXGroup;
children = (
FFACF2B72AC1A53600A2C911 /* UploadTweetViewModel.swift */,
FFACF2B72AC1A53600A2C911 /* NewTweetViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
};
FF2EE4F72ABF1441000522CF /* Views */ = {
FF2EE4F72ABF1441000522CF /* View */ = {
isa = PBXGroup;
children = (
FF2EE4F82ABF144E000522CF /* NewTweetView.swift */,
);
path = Views;
path = View;
sourceTree = "<group>";
};
FF2EE4FC2ABF2080000522CF /* Authentication */ = {
Expand Down Expand Up @@ -394,7 +394,7 @@
children = (
FF44B9322ABC8170005B87F4 /* Components */,
FF2EE4FC2ABF2080000522CF /* Authentication */,
FF2EE4F52ABF142A000522CF /* UploadTweetView */,
FF2EE4F52ABF142A000522CF /* NewTweetView */,
FF2EE4E82ABEF042000522CF /* SideMenu */,
FF4C5D742AD27A3C003F3E90 /* TabBar */,
FF44B9412ABC8BB9005B87F4 /* Profile */,
Expand Down Expand Up @@ -732,7 +732,7 @@
FF2EE4F42ABF0D5F000522CF /* SideMenuOptionRowView.swift in Sources */,
FF170EF42AD2F2DB00B90E2D /* AuthHeaderView.swift in Sources */,
FFFB5D5A2AC045D600817497 /* ImageUploader.swift in Sources */,
FFACF2B82AC1A53600A2C911 /* UploadTweetViewModel.swift in Sources */,
FFACF2B82AC1A53600A2C911 /* NewTweetViewModel.swift in Sources */,
FF170EF02AD2DAB900B90E2D /* UIImage+Extensions.swift in Sources */,
FF63590A2AD7E2E500A7B114 /* PagerTabView.swift in Sources */,
FF44B93D2ABC8A97005B87F4 /* MainTabView.swift in Sources */,
Expand Down
Binary file not shown.
12 changes: 4 additions & 8 deletions TwitterSwiftUI/Core/FeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ struct FeedView: View {
@Binding var showSideMenu: Bool

@Namespace var animation
@State private var showNewTweetView = false
@StateObject var viewModel = FeedViewModel()

@EnvironmentObject var tabBarViewModel: MainTabBarViewModel

var body: some View {

NavigationStack {
Expand Down Expand Up @@ -51,13 +51,9 @@ struct FeedView: View {
}
}
.toolbar(.hidden, for: .navigationBar)
// FullScreen ModalView
.fullScreenCover(isPresented: $showNewTweetView, content: {
.fullScreenCover(isPresented: $tabBarViewModel.showNewTweetView, content: {
// 新しいTweet投稿画面をmodalで表示
NewTweetView() {
// 投稿後は画面を更新
Task { try await viewModel.fetchTweets() }
}
NewTweetView()
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ import Kingfisher
struct NewTweetView: View {

@State private var caption = ""
@Environment(\.presentationMode) var presentationMode
@StateObject var uploadViewModel = UploadTweetViewModel()

var postCompletion: () -> Void
init(postCompletion: @escaping () -> Void) {
self.postCompletion = postCompletion
}
@Environment(\.dismiss) var dismiss
@StateObject var uploadViewModel = NewTweetViewModel()

var body: some View {
VStack {
HStack {
Button {
// TODO: caption.isEmpTy == falseの場合にはアラートを出す
presentationMode.wrappedValue.dismiss()
dismiss()
} label: {
Text("Cancel")
.foregroundStyle(Color(.systemBlue))
Text("キャンセル")
.foregroundStyle(.black)
}

Spacer()

Button {
Task { try await uploadViewModel.uploadTweet(withCaption: self.caption) }
} label: {
Text("Tweet")
Text("ポストする")
.font(.caption)
.bold()
.foregroundStyle(Color.white)
.padding(.horizontal, 16)
Expand All @@ -51,18 +47,19 @@ struct NewTweetView: View {
KFImage(URL(string: user.profileImageUrl))
.resizable()
.scaledToFill()
.frame(width: 64, height: 64)
.frame(width: 30, height: 30)
.clipShape(Circle())
}

TextAreaView("What's happening?", text: $caption)
TextAreaView("いまどうしている?", text: $caption)
.offset(y: -8)

}
.padding()
}
.onReceive(uploadViewModel.$didUploadTweet, perform: { success in
if success {
postCompletion()
presentationMode.wrappedValue.dismiss()
dismiss()
} else {
// show error
}
Expand All @@ -71,5 +68,5 @@ struct NewTweetView: View {
}

#Preview {
NewTweetView() { }
NewTweetView()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// UploadTweetViewModel.swift
// NewTweetViewModel.swift
// TwitterSwiftUI
//
// Created by パクギョンソク on 2023/09/25.
//

import SwiftUI

class UploadTweetViewModel: ObservableObject {
class NewTweetViewModel: ObservableObject {

@Published var didUploadTweet = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ enum SideMenuListType: Int, CaseIterable {

var title: String {
switch self {
case .profile: return "Profile"
case .lists: return "Lists"
case .bookmarks: return "Bookmarks"
case .logout: return "Logout"
case .profile: return "プロフィール"
case .lists: return "リスト"
case .bookmarks: return "ブックマーク"
case .logout: return "ログアウト"
}
}

Expand Down
6 changes: 6 additions & 0 deletions TwitterSwiftUI/Core/TabBar/Explore/View/ExploreView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import SwiftUI
struct ExploreView: View {

@StateObject var viewModel = ExploreViewModel()
@EnvironmentObject var tabBarViewModel: MainTabBarViewModel

var body: some View {

NavigationStack {
Expand All @@ -29,6 +31,10 @@ struct ExploreView: View {
}
}
}
.fullScreenCover(isPresented: $tabBarViewModel.showNewTweetView, content: {
// 新しいTweet投稿画面をmodalで表示
NewTweetView()
})
}
.navigationBarTitleDisplayMode(.inline)
}
Expand Down
12 changes: 12 additions & 0 deletions TwitterSwiftUI/Core/TabBar/MainTab/MainTabBarViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ class MainTabBarViewModel: ObservableObject {
@Published var showSideMenu: Bool = false
@Published var hiddenNewTweetButton: Bool = false
@Published var showNewMessageView: Bool = false
@Published var showNewTweetView: Bool = false
@Published var selectedTab: MainTabBarFilter = .home

func updateNewTweetButton(isHidden: Bool) {
withAnimation {
hiddenNewTweetButton = isHidden
}
}

func newTweetButtonTapped(tap: MainTabBarFilter) {
switch tap {
case .messages:
showNewMessageView = true
case .home, .explore:
showNewTweetView = true
case .notifications:
break
}
}
}
5 changes: 1 addition & 4 deletions TwitterSwiftUI/Core/TabBar/MainTab/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ struct MainTabView: View {
)

NewTweetButton(selectedTab: $viewModel.selectedTab) { tab in
if tab == .messages {
viewModel.showNewMessageView = true
}
viewModel.newTweetButtonTapped(tap: tab)
}
.opacity(viewModel.hiddenNewTweetButton ? 0 : 1)
}
Expand All @@ -101,7 +99,6 @@ struct MainTabView: View {
}
.onChange(of: gestureOffset) { _ in


if gestureOffset != 0 {

// Draggingしたwidthが SideBarWidth以内の場合
Expand Down

0 comments on commit 5baf45d

Please sign in to comment.