-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FollowStatusButtonとUserStatusRowViewを実装
- Loading branch information
1 parent
ff4551c
commit fa2a154
Showing
12 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+21.4 KB
(100%)
....xcodeproj/project.xcworkspace/xcuserdata/paku.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
TwitterSwiftUI/Core/Components/Users/FollowStatusButton.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// FollowStatusButton.swift | ||
// TwitterSwiftUI | ||
// | ||
// Created by paku on 2023/10/22. | ||
// | ||
|
||
import SwiftUI | ||
|
||
enum FollowStatus: Int, CaseIterable, Identifiable { | ||
case unfollowing | ||
case following | ||
|
||
var title: String { | ||
switch self { | ||
case .unfollowing: "フォローする" | ||
case .following: "フォロー中" | ||
} | ||
} | ||
|
||
var id: Int { self.rawValue } | ||
} | ||
|
||
struct FollowStatusButton: View { | ||
|
||
@Binding var status: FollowStatus | ||
var buttonTapped: () -> Void | ||
|
||
init(status: Binding<FollowStatus>, buttonTapped: @escaping () -> Void) { | ||
_status = status | ||
self.buttonTapped = buttonTapped | ||
} | ||
|
||
var body: some View { | ||
Button { | ||
buttonTapped() | ||
} label: { | ||
Text(status.title) | ||
.font(.footnote).bold() | ||
.frame(width: 140, height: 28) | ||
.foregroundColor(status == .unfollowing ? .white : .black) | ||
.background( | ||
Color(status == .unfollowing ? .black : .clear) | ||
.cornerRadius(20) | ||
) | ||
.overlay( | ||
RoundedRectangle(cornerRadius: 20) | ||
.stroke(Color.gray, lineWidth: 0.75) | ||
) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
FollowStatusButton(status: .constant(.unfollowing), buttonTapped: {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
TwitterSwiftUI/Core/Components/Users/UserStatusRowView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// UserStatusRowView.swift | ||
// TwitterSwiftUI | ||
// | ||
// Created by paku on 2023/10/22. | ||
// | ||
|
||
import SwiftUI | ||
import Kingfisher | ||
|
||
struct UserStatusRowView: View { | ||
|
||
@StateObject var viewModel: UserStatusRowViewModel | ||
|
||
init(user: User) { | ||
_viewModel = .init(wrappedValue: UserStatusRowViewModel(user: user)) | ||
} | ||
|
||
var body: some View { | ||
|
||
HStack(alignment: .top, spacing: 12) { | ||
KFImage(URL(string: viewModel.user.profileImageUrl)) | ||
.resizable() | ||
.scaledToFill() | ||
.frame(width: 48, height: 48) | ||
.clipShape(Circle()) | ||
|
||
VStack(alignment: .leading) { | ||
HStack { | ||
VStack(alignment: .leading) { | ||
Text(viewModel.user.username) | ||
.font(.subheadline).bold() | ||
.foregroundStyle(.black) | ||
|
||
Text("@\(viewModel.user.email.emailUsername ?? "")") | ||
.font(.subheadline) | ||
.foregroundColor(.gray) | ||
} | ||
Spacer() | ||
|
||
FollowStatusButton(status: $viewModel.followingStatus) { | ||
viewModel.followButtonTapped() | ||
} | ||
} | ||
if let bio = viewModel.user.bio { | ||
Text(bio) | ||
.font(.subheadline) | ||
.multilineTextAlignment(.leading) | ||
.foregroundColor(.black) | ||
} | ||
} | ||
} | ||
.padding(.horizontal) | ||
.padding(.vertical, 6) | ||
} | ||
} | ||
|
||
#Preview { | ||
UserStatusRowView(user: PreviewProvider.user) | ||
} |
72 changes: 72 additions & 0 deletions
72
TwitterSwiftUI/Core/Components/Users/UserStatusRowViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// UserStatusRowViewModel.swift | ||
// TwitterSwiftUI | ||
// | ||
// Created by paku on 2023/10/22. | ||
// | ||
|
||
import Foundation | ||
|
||
class UserStatusRowViewModel: ObservableObject { | ||
let user: User | ||
|
||
@Published var followingStatus: FollowStatus = .following | ||
|
||
init(user: User) { | ||
self.user = user | ||
|
||
Task { | ||
try await fetchFollowStatus() | ||
} | ||
} | ||
|
||
func followButtonTapped() { | ||
Task { | ||
try await toggleFollowStatus() | ||
} | ||
} | ||
|
||
private func toggleFollowStatus() async throws { | ||
Task { | ||
switch followingStatus { | ||
case .unfollowing: | ||
try await follow() | ||
case .following: | ||
try await unfollow() | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
private func unfollow() async throws { | ||
guard let uid = user.id else { return } | ||
|
||
followingStatus = .unfollowing | ||
do { | ||
let _ = try await UserService.shared.unfollowUser(uid: uid) | ||
} catch { | ||
print("Faild unfollow: \(error.localizedDescription)") | ||
followingStatus = .following | ||
} | ||
} | ||
|
||
@MainActor | ||
private func follow() async throws { | ||
guard let uid = user.id else { return } | ||
|
||
followingStatus = .following | ||
do { | ||
let _ = try await UserService.shared.followUser(uid: uid) | ||
} catch { | ||
print("Faild follow: \(error.localizedDescription)") | ||
followingStatus = .unfollowing | ||
} | ||
} | ||
|
||
@MainActor | ||
private func fetchFollowStatus() async throws { | ||
guard let uid = user.id else { return } | ||
let followed = try await UserService.shared.checkIfUserIsFollowing(for: uid) | ||
followingStatus = followed ? .following : .unfollowing | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters