Skip to content

Commit

Permalink
ProfileEditViewにてProfile情報をサーバーに更新するロジックを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
boardguy1024 committed Oct 21, 2023
1 parent 1b1ddb5 commit fbd2d46
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 91 deletions.
4 changes: 4 additions & 0 deletions TwitterSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
2DC4D9452ADBEB9C0066FF02 /* BlurView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC4D9442ADBEB9C0066FF02 /* BlurView.swift */; };
2DC4D9472ADBF5B30066FF02 /* NewMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC4D9462ADBF5B30066FF02 /* NewMessageView.swift */; };
2DC4D9492ADC14F30066FF02 /* NewMessageViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DC4D9482ADC14F30066FF02 /* NewMessageViewModel.swift */; };
2DD92B992AE3CEEA00252362 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DD92B982AE3CEEA00252362 /* String+Extensions.swift */; };
2DE08BE72ADAEBF900DAA3C4 /* NewTweetButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2DE08BE62ADAEBF900DAA3C4 /* NewTweetButton.swift */; };
FF170EE32AD29E7800B90E2D /* PreviewProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF170EE22AD29E7800B90E2D /* PreviewProvider.swift */; };
FF170EE52AD2A09E00B90E2D /* RegistrationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF170EE42AD2A09E00B90E2D /* RegistrationViewModel.swift */; };
Expand Down Expand Up @@ -129,6 +130,7 @@
2DC4D9442ADBEB9C0066FF02 /* BlurView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurView.swift; sourceTree = "<group>"; };
2DC4D9462ADBF5B30066FF02 /* NewMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewMessageView.swift; sourceTree = "<group>"; };
2DC4D9482ADC14F30066FF02 /* NewMessageViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewMessageViewModel.swift; sourceTree = "<group>"; };
2DD92B982AE3CEEA00252362 /* String+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
2DE08BE62ADAEBF900DAA3C4 /* NewTweetButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTweetButton.swift; sourceTree = "<group>"; };
FF170EE22AD29E7800B90E2D /* PreviewProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewProvider.swift; sourceTree = "<group>"; };
FF170EE42AD2A09E00B90E2D /* RegistrationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegistrationViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +439,7 @@
FF170EEF2AD2DAB900B90E2D /* UIImage+Extensions.swift */,
FF170EF12AD2DB9700B90E2D /* View+Extensions.swift */,
2D6173E92AE011250089F1E7 /* Timestamp+Extensions.swift */,
2DD92B982AE3CEEA00252362 /* String+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -755,6 +758,7 @@
2DC291462ADA2B7D006E9E23 /* ConversationsView.swift in Sources */,
FFACF2B22AC1519E00A2C911 /* ExploreViewModel.swift in Sources */,
2D9F42BB2AE16912000FD971 /* FollowingUserListView.swift in Sources */,
2DD92B992AE3CEEA00252362 /* String+Extensions.swift in Sources */,
FF2EE4F42ABF0D5F000522CF /* SideMenuOptionRowView.swift in Sources */,
FF170EF42AD2F2DB00B90E2D /* AuthHeaderView.swift in Sources */,
FFFB5D5A2AC045D600817497 /* ImageUploader.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class RegistrationViewModel: ObservableObject {

@Published var email = ""
@Published var username = ""
@Published var fullname = ""
@Published var password = ""

@Published var didAuthenticateUser = false
Expand All @@ -26,7 +25,6 @@ class RegistrationViewModel: ObservableObject {
func register() {
AuthService.shared.register(withEmail: email,
password: password,
fullname: fullname,
username: username)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct RegistrationView: View {
VStack(spacing: 40) {
CustomTextField(imageName: "envelope", placeholdeer: "メールアドレス", text: $viewModel.email)
CustomTextField(imageName: "person", placeholdeer: "ニックネーム", text: $viewModel.username)
CustomTextField(imageName: "person", placeholdeer: "名前", text: $viewModel.fullname)

CustomTextField(imageName: "lock",
placeholdeer: "パスワード",
isSecureField: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ struct TweetRowView: View {

VStack(alignment: .leading, spacing: 4) {
HStack {
Text(user.fullname)
.font(.subheadline).bold()

Text("@\(user.username)")
Text("\(user.username)")
.font(.subheadline).bold()

Text("@\(user.email.emailUsername ?? "")")
.foregroundColor(.gray)
.font(.caption)

Text("2w")
.foregroundColor(.gray)
.font(.caption)
Expand Down
13 changes: 10 additions & 3 deletions TwitterSwiftUI/Core/Components/Users/UserRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ struct UserRowView: View {
.clipShape(Circle())

VStack(alignment: .leading) {
Text(user.fullname)
Text(user.username)
.font(.subheadline).bold()
.foregroundStyle(.black)
Text(user.username)

Text("@\(user.email.emailUsername ?? "")")
.font(.subheadline)
.foregroundColor(.gray)
}
Expand All @@ -37,5 +38,11 @@ struct UserRowView: View {
}

#Preview {
UserRowView(user: .init(username: "", fullname: "", profileImageUrl: "", email: ""))
UserRowView(user: .init(username: "",
profileImageUrl: "",
profileHeaderImageUrl: "profileHeaderImageUrl",
email: "",
bio: "",
location: "",
webUrl: ""))
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ extension UserStatusDetailView {
#Preview {
UserStatusDetailView(initialTab: .followers,
user: .init(username: "username",
fullname: "fullname",
profileImageUrl: "profileImageUrl",
email: "email"))
profileHeaderImageUrl: "profileHeaderImageUrl",
email: "email",
bio: "",
location: "",
webUrl: ""))
}
142 changes: 100 additions & 42 deletions TwitterSwiftUI/Core/Profile/View/ProfileEditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,36 @@ struct ProfileEditView: View {

var body: some View {

VStack(alignment: .leading, spacing: 0) {
header

ZStack {
Color.gray
.frame(height: 120).opacity(0.6)
ZStack {
VStack(alignment: .leading, spacing: 0) {

Image(systemName: "camera")
.font(.title)
.foregroundColor(.white)
}
.onTapGesture {
headerImage
.padding(.top, 40)

}

ZStack {
KFImage(URL(string: viewModel.user.profileImageUrl))
.resizable()
.frame(width: 70, height: 70)
.clipShape(Circle())
.overlay(
Circle().strokeBorder(Color.white, lineWidth: 3)
)
Image(systemName: "camera")
.font(.title2)
.foregroundColor(.white)
}
.padding(.leading, 20)
.offset(y: -20)
.onTapGesture {
profileImage

nameInput

bioInput

loactionInput

webUrlInput

Spacer()
}
.overlay(header, alignment: .top)
.sheet(isPresented: $viewModel.showImagePickerForProfileImage , content: {
ImagePicker(selectedImage: $viewModel.profileImage)
})
.sheet(isPresented: $viewModel.showImagePrickerForHeaderImage, content: {
ImagePicker(selectedImage: $viewModel.headerImage)
})

nameInput

bioInput

loactionInput

webUrlInput

Spacer()
if viewModel.showProgressView {
Color.black.opacity(0.1).ignoresSafeArea()
ProgressView()
}
}
}
}
Expand All @@ -77,13 +65,15 @@ extension ProfileEditView {
Spacer()

Button {
dismiss()
Task {
try await viewModel.updateProfile()
dismiss()
}
} label: {
Text("保存")
.opacity(viewModel.saveButtonDisable ? 0.5 : 1)
}
.disabled(viewModel.saveButtonDisable)

}

Text("編集")
Expand All @@ -96,6 +86,68 @@ extension ProfileEditView {
.padding(.top, 5)
}

var headerImage: some View {
ZStack {

Group {
if let selectedImageFromImagePicker = viewModel.headerImage {
Image(uiImage: selectedImageFromImagePicker)
.resizable()
.scaledToFill()
} else if let headerImageUrl = viewModel.user.profileHeaderImageUrl {
KFImage(URL(string: headerImageUrl))
.resizable()
.scaledToFill()
} else {
Color.gray
.opacity(0.6)
}
}
.frame(height: 120)
.clipShape(Rectangle())

Image(systemName: "camera")
.font(.title)
.foregroundColor(.white)
}
.onTapGesture {
viewModel.showImagePrickerForHeaderImage.toggle()
}

}

var profileImage: some View {
ZStack {

Group {
// プロフィールイメージを変更した場合、そのイメージを表示
if let selectedImageFromImagePicker = self.viewModel.profileImage {
Image(uiImage: selectedImageFromImagePicker)
.resizable()
.scaledToFill()
} else {
KFImage(URL(string: viewModel.user.profileImageUrl))
.resizable()
.scaledToFill()
}
}
.frame(width: 70, height: 70)
.clipShape(Circle())
.overlay(
Circle().strokeBorder(Color.white, lineWidth: 3)
)

Image(systemName: "camera")
.font(.title2)
.foregroundColor(.white)
}
.padding(.leading, 20)
.offset(y: -20)
.onTapGesture {
viewModel.showImagePickerForProfileImage.toggle()
}
}

var nameInput: some View {
VStack(spacing: 0) {
Divider()
Expand Down Expand Up @@ -139,7 +191,7 @@ extension ProfileEditView {
HStack {
Text("場所")
.fontWeight(.semibold)
TextField("場所を追加", text: $viewModel.name)
TextField("場所を追加", text: $viewModel.location)
.foregroundColor(.blue)
.frame(maxWidth: .infinity)
}
Expand All @@ -155,7 +207,7 @@ extension ProfileEditView {
HStack {
Text("Web")
.fontWeight(.semibold)
TextField("Webサイトを追加", text: $viewModel.name)
TextField("Webサイトを追加", text: $viewModel.webUrl)
.keyboardType(.URL)
.foregroundColor(.blue)
.frame(maxWidth: .infinity)
Expand All @@ -168,5 +220,11 @@ extension ProfileEditView {
}

#Preview {
ProfileEditView(user: .init(username: "username", fullname: "fullname", profileImageUrl: "profileImageUrl", email: "email"))
ProfileEditView(user: .init(username: "username",
profileImageUrl: "profileImageUrl",
profileHeaderImageUrl: "profileHeaderImageUrl",
email: "email",
bio: "",
location: "",
webUrl: ""))
}
58 changes: 41 additions & 17 deletions TwitterSwiftUI/Core/Profile/View/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,33 @@ struct ProfileView: View {
}

#Preview {
ProfileView(user: .init(username: "", fullname: "", profileImageUrl: "", email: ""))
ProfileView(user: .init(username: "",
profileImageUrl: "",
profileHeaderImageUrl: "profileHeaderImageUrl",
email: "",
bio: "",
location: "",
webUrl: ""))
}

extension ProfileView {

var headerView: some View {
ZStack(alignment: .bottomLeading) {
Color(.systemCyan)
.ignoresSafeArea()

if let headerImageUrl = viewModel.user.profileHeaderImageUrl {
KFImage(URL(string: headerImageUrl))
.resizable()
.scaledToFill()
.frame(maxWidth: .infinity)
.frame(height: 120)
.clipShape(Rectangle())
.edgesIgnoringSafeArea(.top)
} else {
Color(.systemCyan)
.ignoresSafeArea()
}

VStack(spacing: 0) {
Button {
dismiss()
Expand Down Expand Up @@ -151,30 +168,37 @@ extension ProfileView {
var userInfoDetails: some View {
VStack(alignment: .leading, spacing: 4) {
HStack {
Text(viewModel.user.fullname)
.font(.title2).bold()

Text("\(viewModel.user.username)")
.font(.title2).bold()

Image(systemName: "checkmark.seal.fill")
.foregroundColor(Color(.systemBlue))
}

Text("@\(viewModel.user.username)")
Text("@\(viewModel.user.email.emailUsername ?? "")")
.font(.subheadline)
.foregroundColor(.gray)

Text("Your moms favorite villain")
.font(.subheadline)
.padding(.vertical)

if let bio = viewModel.user.bio {
Text(bio)
.font(.subheadline)
.padding(.vertical)
}

HStack(spacing: 24) {
HStack {
Image(systemName: "mappin.and.ellipse")
Text("Gotham, NY")
if let location = viewModel.user.location {
HStack {
Image(systemName: "mappin.and.ellipse")
Text(location)
}
}

HStack {
Image(systemName: "link")
Text("www.thejoker.com")

if let web = viewModel.user.webUrl {
HStack {
Image(systemName: "link")
Text(web)
}
}
}
.font(.caption)
Expand Down
Loading

0 comments on commit fbd2d46

Please sign in to comment.