Skip to content

Feature/7 fetch user profile #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions chat-iOS/Views/EditProfile/EditProfileBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import UIKit

struct EditProfileViewBuilder {
static func create() -> UIViewController {
guard let EditProfileViewController = EditProfileViewController.loadFromStoryboard() as? EditProfileViewController else {
guard let editProfileViewController = EditProfileViewController.loadFromStoryboard() as? EditProfileViewController else {
fatalError("fatal: Failed to initialize the EditProfileViewController")
}
let model = EditProfileModel()
let presenter = EditProfileViewPresenter(model: model)
EditProfileViewController.inject(with: presenter)
return EditProfileViewController
editProfileViewController.inject(with: presenter)
return editProfileViewController
}
}
22 changes: 21 additions & 1 deletion chat-iOS/Views/EditProfile/EditProfileModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by 倉谷 明希 on 2020/07/03.
//
import Firebase

protocol EditProfileModelProtocol {
var presenter: EditProfileModelOutput! { get set }
Expand All @@ -17,8 +18,27 @@ protocol EditProfileModelOutput {
final class EditProfileModel: EditProfileModelProtocol {
var presenter: EditProfileModelOutput!

//TODO:- セーブする処理を書くこと
//MARK:- ここでセーブする処理を書く
func saveProfile() {

// let storage = Storage.storage().reference(forURL: "gs://mapapp6-bf5a1.appspot.com")
// let imageRef = storage.child("profileImage").child("\(user.uid).jpeg")
// var ProfileImageData: Data = Data()
// if imageView.image != nil {
//
// //画像を圧縮
// ProfileImageData = (imageView.image?.jpegData(compressionQuality: 0.01))!
//
// }
// imageRef.putData(ProfileImageData, metadata: nil) { (metaData, error) in
//
// //エラーであれば
// if error != nil {
//
// print(error.debugDescription)
// return //これより下にはいかないreturn
// }
// }
}
}

6 changes: 3 additions & 3 deletions chat-iOS/Views/EditProfile/EditProfilePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ final class EditProfileViewPresenter: EditProfileViewPresenterProtocol, EditProf
self.model = model
}

func didTapStopEditProfileButton(){
func didTapStopEditProfileButton() {
view.dismissEditProfileViewController()
}

func didTapSaveEditProfileButton() {
self.model.saveProfile()

}
func didTapChangePhotoButton(){
func didTapChangePhotoButton() {
view.showActionSheet()
}

func didTapPickupPhotoAction(){
func didTapPickupPhotoAction() {
view.showImagePickerControllerAsPhotoLibrary()
}

Expand Down
20 changes: 9 additions & 11 deletions chat-iOS/Views/EditProfile/EditProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import Firebase

final class EditProfileViewController: UIViewController {
private var presenter: EditProfileViewPresenterProtocol!
Expand All @@ -14,6 +15,8 @@ final class EditProfileViewController: UIViewController {
@IBOutlet weak var changePhotoButton: UIButton!
@IBOutlet weak var nameTextField: UITextField!

var userName = ""
var profileImage = UIImage()
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserProfileViewControllerからEditProfileViewControllerにへの値渡しはViewBuilderを介して行った方が良いです!
UserProfileViewControllerで

func presentEditProfileViewController(user: User) {
 let editProfileVC = EditProfileViewBuilder.create(user: user)
 let navigationController = UINavigationController(rootViewController:  editProfileVC)
 navigationController.modalPresentationStyle = .fullScreen
 present(navigationController, animated: true, completion: nil)
}

こんな感じになるようにして

struct EditProfileViewBuilder {
    static func create(user: User) -> UIViewController {
        guard let EditProfileViewController = EditProfileViewController.loadFromStoryboard() as? EditProfileViewController else {
            fatalError("fatal: Failed to initialize the EditProfileViewController")
        }
        let model = EditProfileModel()
        let presenter = EditProfileViewPresenter(model: model, user: user)
        EditProfileViewController.inject(with: presenter)
        return EditProfileViewController
    }
}

EditProfileViewBuilderはこんな感じでしょうか

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

値の受け渡し、もう少し調べてから頑張ります。。


override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -24,11 +27,9 @@ final class EditProfileViewController: UIViewController {
}



func inject(with presenter: EditProfileViewPresenterProtocol) {
self.presenter = presenter
self.presenter.view = self

}

func setupNavigationItem() {
Expand All @@ -42,10 +43,12 @@ final class EditProfileViewController: UIViewController {
}

func setupNameTextField() {
self.nameTextField.text = self.userName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presenterにuserを渡すことになるので
nameTextField.text = presenter.user.displayName になります!

self.nameTextField.addBorderBottom(borderWidth: 1.0, color: .gray)
}

func setupImageView() {
self.imageView.image = self.profileImage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここもuserのprofileImageURLを使ってnukeでimageViewの画像を変えたほうがいいです!

self.imageView.layer.cornerRadius = self.imageView.frame.width / 2
}

Expand All @@ -54,28 +57,23 @@ final class EditProfileViewController: UIViewController {
print("キャンセルボタンタップされた")

self.presenter.didTapStopEditProfileButton()

}

//TODO: ここでデータをセーブする処理を行う
@objc func tapSaveEditProfileButton() {
print("セーブボタンタップされた")

self.presenter.didTapSaveEditProfileButton()

}

@IBAction func tapChangePhotoButton(_ sender: Any) {

self.presenter.didTapChangePhotoButton()
}


}

extension EditProfileViewController: EditProfileViewPresenterOutput {
func dismissEditProfileViewController() {
self.dismiss(animated: true, completion: nil)

}

func showActionSheet() {
Expand Down Expand Up @@ -129,7 +127,6 @@ extension EditProfileViewController: EditProfileViewPresenterOutput {
self.present(photoPickerVC, animated: true, completion: nil)
}
}

}

extension EditProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
Expand All @@ -145,6 +142,7 @@ extension EditProfileViewController: UIImagePickerControllerDelegate, UINavigati
self.imageView.image = pickerImage
picker.dismiss(animated: true)
}

}



40 changes: 36 additions & 4 deletions chat-iOS/Views/Profile/UserProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import Nuke

final class UserProfileViewController: UIViewController {
private var presenter: UserProfileViewPresenterProtocol!
Expand All @@ -16,10 +17,21 @@ final class UserProfileViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
editProfileButton.layer.cornerRadius = 10.0

self.setupEditProfileButton()
self.setupProfileImageView()
self.presenter.didLoadViewController()
}

func setupEditProfileButton() {
editProfileButton.isEnabled = false
editProfileButton.layer.cornerRadius = 10.0
}

func setupProfileImageView() {
profileImageView.layer.cornerRadius = profileImageView.frame.height / 2
}

@IBAction func tapEditProfileButton(_ sender: Any) {
self.presenter.didTapEditProfileButton()
}
Expand All @@ -32,12 +44,32 @@ final class UserProfileViewController: UIViewController {

extension UserProfileViewController: UserProfileViewPresenterOutput {
func presentEditProfileViewController() {
let editProfileVC = EditProfileViewBuilder.create()
let editProfileVC = EditProfileViewBuilder.create() as! EditProfileViewController
editProfileVC.userName = self.profileNameLabel.text ?? ""
editProfileVC.profileImage = self.profileImageView.image!

let navigationController = UINavigationController(rootViewController: editProfileVC)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true, completion: nil)
}
func setUser() {
func setUserName(userName: String) {
DispatchQueue.main.async {
self.profileNameLabel.text = userName
self.navigationItem.title = userName
}
}
func setUserProfileImage(imageURL: URL) {
//TODO:- URLの確認とか画像の用意とかすること
var defaultImage = UIImage()
if #available(iOS 13.0, *) {
defaultImage = UIImage(systemName: "person.circle.fill") ?? UIImage()
} else {
// Fallback on earlier versions
}
DispatchQueue.main.async {
self.editProfileButton.isEnabled = true
let options = ImageLoadingOptions(placeholder: defaultImage, failureImage: defaultImage)
loadImage(with: imageURL, options: options, into: self.profileImageView, progress: nil, completion: nil)
}
}

}
37 changes: 35 additions & 2 deletions chat-iOS/Views/Profile/UserProfileViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,53 @@
//
// Created by 倉谷 明希 on 2020/06/22.
//
import Firebase

protocol UserProfileViewModelProtocol {
var presenter: UserProfileViewModelOutput! { get set }
func fetchUser()
}

protocol UserProfileViewModelOutput {
func successFetchUser()
func successFetchUser(user: User)
}

final class UserProfileViewModel: UserProfileViewModelProtocol {
var presenter: UserProfileViewModelOutput!
var firestore: Firestore!
private var listner: ListenerRegistration?

init() {
self.firestore = Firestore.firestore()
let setting = FirestoreSettings()
self.firestore.settings = setting
}

deinit {
self.listner?.remove()
}

func fetchUser() {
self.presenter.successFetchUser()
//TODO:- 認証が終わったらuidを後で変更すること
//guard let uid = Auth.auth().currentUser?.uid else { return }
let userReference = self.firestore.collection("message/v1/users").document("y783WJnXJqDfDED0nBvK")
self.listner = userReference.addSnapshotListener { (document, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}

guard let document = document, document.exists else {
print("The document doesn't exist.")
return
}

do {
let user = try Firestore.Decoder().decode(User.self, from: document.data()!)
self.presenter.successFetchUser(user: user)
} catch {
fatalError()
}
}
}
}
12 changes: 9 additions & 3 deletions chat-iOS/Views/Profile/UserProfileViewPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by 倉谷 明希 on 2020/06/22.
//
import Foundation

protocol UserProfileViewPresenterProtocol {
var view: UserProfileViewPresenterOutput! { get set }
Expand All @@ -12,7 +13,8 @@ protocol UserProfileViewPresenterProtocol {
}

protocol UserProfileViewPresenterOutput {
func setUser()
func setUserName(userName: String)
func setUserProfileImage(imageURL: URL)
func presentEditProfileViewController()
}

Expand All @@ -29,8 +31,12 @@ final class UserProfileViewPresenter: UserProfileViewPresenterProtocol, UserProf
func didLoadViewController() {
self.model.fetchUser()
}
func successFetchUser() {
self.view.setUser()
func successFetchUser(user: User) {
self.view.setUserName(userName: user.displayName)
guard let URLStr = user.profileImageURL else { return }
guard let encodeURLStr = URLStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
guard let url = URL(string: encodeURLStr) else { return }
self.view.setUserProfileImage(imageURL: url)
}
func didTapEditProfileButton() {
self.view.presentEditProfileViewController()
Expand Down
6 changes: 3 additions & 3 deletions chat-iOS/Views/Storyboards/UserProfile.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
<action selector="tapEditProfileButton:" destination="QLB-Jb-sKT" eventType="touchUpInside" id="bKN-vO-h56"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GiA-YA-9D0">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="GiA-YA-9D0">
<rect key="frame" x="92.5" y="140" width="135" height="60"/>
<constraints>
<constraint firstAttribute="height" constant="60" id="4TJ-cL-AYn"/>
<constraint firstAttribute="width" constant="135" id="kFp-vx-dF0"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="50"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="30"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
Expand All @@ -68,7 +68,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="gFc-71-pOa" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="211.59420289855075" y="-308.03571428571428"/>
<point key="canvasLocation" x="-2" y="-290"/>
</scene>
</scenes>
</document>