Skip to content
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

トーク選択画面を追加 #28

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chat-iOS/Model/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
//
// Created by 戸高新也 on 2020/07/04.
//
import Firebase

struct Room: Codable {
let name: String?
let thumbnailImageURL: String?
let members: [String]
let message: String
@DocumentID var id: String?
@ServerTimestamp var updateAt: Timestamp?
}
4 changes: 2 additions & 2 deletions chat-iOS/Views/Chats/ChatsViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import UIKit

struct ChatsViewBuilder {
static func create() -> UIViewController {
static func create(withRoomId roomId: String, withRoomName roomName: String) -> UIViewController {
guard let chatsViewController = ChatsViewController.loadFromStoryboard() as? ChatsViewController else {
fatalError("fatal: Failed to initialize the ChatsViewController")
}
let model = ChatsViewModel()
let presenter = ChatsViewPresenter(model: model)
let presenter = ChatsViewPresenter(model: model, withRoomId: roomId, withRoomName: roomName)
chatsViewController.inject(with: presenter)
return chatsViewController
}
Expand Down
2 changes: 1 addition & 1 deletion chat-iOS/Views/Chats/ChatsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class ChatsViewController: UIViewController, UICollectionViewDelegateFlowL
@IBOutlet weak var sendButton: UIButton!

var transScripts: [Transcript] = Array()

let chatsCellID = "chatsCellID"

override func viewDidLoad() {
Expand Down
6 changes: 5 additions & 1 deletion chat-iOS/Views/Chats/ChatsViewPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ protocol ChatsViewPresenterOutput: class {
final class ChatsViewPresenter: ChatsViewPresenterProtocol, ChatsViewModelOutput {
weak var view: ChatsViewPresenterOutput!
private var model: ChatsViewModelProtocol
private var roomId: String
private var roomName: String

init(model: ChatsViewModelProtocol) {
init(model: ChatsViewModelProtocol, withRoomId roomId: String, withRoomName roomName: String) {
self.model = model
self.roomId = roomId
self.roomName = roomName
self.model.presenter = self
}

Expand Down
9 changes: 4 additions & 5 deletions chat-iOS/Views/MainTabBar/MainTabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ final class MainTabBarViewController: UITabBarController {

let userProfileVC = UserProfileViewBuilder.create()
let userProfileNavigationController = UINavigationController(rootViewController: userProfileVC)

//TODO:- ここはチャットセレクト画面に遷移すること
let chatsVC = ChatsViewBuilder.create()
let chatsNavigationController = UINavigationController(rootViewController: chatsVC)

let selectChatVC = SelectChatViewBuilder.create()
let chatsNavigationController = UINavigationController(rootViewController: selectChatVC)

//TODO:- iOS12以下の場合の画像を用意すること
if #available(iOS 13.0, *) {
Expand All @@ -35,7 +34,7 @@ final class MainTabBarViewController: UITabBarController {
let userProfileTabBarItemSelectedImage = UIImage(systemName: "person.circle.fill")

userProfileVC.tabBarItem = UITabBarItem(title: nil, image: userProfileTabBarItemImage, selectedImage: userProfileTabBarItemSelectedImage)
chatsVC.tabBarItem = UITabBarItem(title: nil, image: chatsTabBarItemImage, selectedImage: chatsTabBarItemSelectedImage)
selectChatVC.tabBarItem = UITabBarItem(title: nil, image: chatsTabBarItemImage, selectedImage: chatsTabBarItemSelectedImage)
} else {
// Fallback on earlier versions
}
Expand Down
20 changes: 20 additions & 0 deletions chat-iOS/Views/SelectChat/SelectChatBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// SelectChatBuilder.swift
// chat-iOS
//
// Created by 松木周 on 2020/07/15.
//

import UIKit

struct SelectChatViewBuilder {
static func create() -> UIViewController {
guard let selectChatViewController = SelectChatViewController.loadFromStoryboard() as? SelectChatViewController else {
fatalError("fatal: Failed to initialize the ChatsViewController")
}
let model = SelectChatModel()
let presenter = SelectChatViewPresenter(model: model)
selectChatViewController.inject(with: presenter)
return selectChatViewController
}
}
67 changes: 67 additions & 0 deletions chat-iOS/Views/SelectChat/SelectChatModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// SelectChatModel.swift
// chat-iOS
//
// Created by 松木周 on 2020/07/15.
//

import Firebase

protocol SelectChatModelProtocol {
var presenter: SelectChatModelOutput! { get set }
func setUpFirestore()
func fetchCurrentChatRoom()
}

protocol SelectChatModelOutput: class {
func successFetchCurrentChatRooms(currentChatRooms rooms: [Room])
func onError(error: Error?)
}

final class SelectChatModel: SelectChatModelProtocol {

weak var presenter: SelectChatModelOutput!
private var firestore: Firestore!
private var listener: ListenerRegistration?

init() {
self.setUpFirestore()
}

deinit {
listener?.remove()
}

func setUpFirestore() {
self.firestore = Firestore.firestore()
let settings = FirestoreSettings()
self.firestore.settings = settings
}

func fetchCurrentChatRoom() {

guard let curretnUserId = Auth.auth().currentUser?.uid else { return }

self.listener = self.firestore.collection("rooms").whereField("members", arrayContains: curretnUserId).addSnapshotListener { [weak self] (documentSnapshot, error) in

if let error = error {
self?.presenter.onError(error: error)
return
}

guard let documents = documentSnapshot?.documents else {
print("The document doesn't exist.")
return
}

let rooms = documents.compactMap { queryDocumentSnapshot -> Room? in
return try? queryDocumentSnapshot.data(as: Room.self)
}

self?.presenter.successFetchCurrentChatRooms(currentChatRooms: rooms)
}

}

}

34 changes: 34 additions & 0 deletions chat-iOS/Views/SelectChat/SelectChatTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// SelectChatTableViewCell.swift
// chat-iOS
//
// Created by 松木周 on 2020/07/15.
//

import UIKit

class SelectChatTableViewCell: UITableViewCell {

@IBOutlet weak var userProfileImageView: UIImageView!
@IBOutlet weak var userNameLabel: UILabel!
@IBOutlet weak var lastMessageLabel: UILabel!
@IBOutlet weak var lastMessageTimeLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

func generateCell(withRoom room: Room) {
userNameLabel.text = room.name
lastMessageLabel.text = room.name
//TODO:- Storageから画像を取得して表示する
}

}
78 changes: 78 additions & 0 deletions chat-iOS/Views/SelectChat/SelectChatTableViewCell.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="SelectChatTableViewCell" rowHeight="80" id="KGk-i7-Jjw" customClass="SelectChatTableViewCell" customModule="chat_iOS" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="414" height="80"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
<rect key="frame" x="0.0" y="0.0" width="414" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="2gC-Cj-Q7U">
<rect key="frame" x="10" y="10" width="60" height="60"/>
<constraints>
<constraint firstAttribute="width" secondItem="2gC-Cj-Q7U" secondAttribute="height" multiplier="1:1" id="EkN-nk-5ft"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RUf-a9-7FA">
<rect key="frame" x="80" y="10" width="254" height="20"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="VfV-5S-hH0"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Message" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="s36-dq-cAX">
<rect key="frame" x="80" y="35" width="254" height="35"/>
<constraints>
<constraint firstAttribute="height" constant="35" id="TKY-oo-UjD"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="12:00" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fSn-KO-GXm">
<rect key="frame" x="361" y="29.5" width="43" height="21.5"/>
<constraints>
<constraint firstAttribute="width" secondItem="fSn-KO-GXm" secondAttribute="height" multiplier="2:1" id="UvM-kF-J7e"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="2gC-Cj-Q7U" firstAttribute="leading" secondItem="H2p-sc-9uM" secondAttribute="leading" constant="10" id="0Mg-FZ-GXl"/>
<constraint firstItem="2gC-Cj-Q7U" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="10" id="Tcd-3b-Och"/>
<constraint firstAttribute="trailing" secondItem="RUf-a9-7FA" secondAttribute="trailing" constant="80" id="aBP-vW-5se"/>
<constraint firstItem="RUf-a9-7FA" firstAttribute="leading" secondItem="2gC-Cj-Q7U" secondAttribute="trailing" constant="10" id="cK4-ly-L8d"/>
<constraint firstItem="fSn-KO-GXm" firstAttribute="centerY" secondItem="H2p-sc-9uM" secondAttribute="centerY" id="frq-JC-iPM"/>
<constraint firstItem="RUf-a9-7FA" firstAttribute="top" secondItem="H2p-sc-9uM" secondAttribute="top" constant="10" id="glk-o7-RvN"/>
<constraint firstAttribute="trailing" secondItem="s36-dq-cAX" secondAttribute="trailing" constant="80" id="lMx-4U-eO2"/>
<constraint firstItem="2gC-Cj-Q7U" firstAttribute="centerY" secondItem="H2p-sc-9uM" secondAttribute="centerY" id="sDh-wI-Gju"/>
<constraint firstItem="s36-dq-cAX" firstAttribute="leading" secondItem="2gC-Cj-Q7U" secondAttribute="trailing" constant="10" id="sb8-iH-huW"/>
<constraint firstItem="s36-dq-cAX" firstAttribute="top" secondItem="RUf-a9-7FA" secondAttribute="bottom" constant="5" id="ugB-A4-Qaj"/>
<constraint firstAttribute="trailing" secondItem="fSn-KO-GXm" secondAttribute="trailing" constant="10" id="zDm-rR-7x7"/>
</constraints>
</tableViewCellContentView>
<viewLayoutGuide key="safeArea" id="njF-e1-oar"/>
<connections>
<outlet property="lastMessageLabel" destination="s36-dq-cAX" id="Dse-v4-Rar"/>
<outlet property="lastMessageTimeLabel" destination="fSn-KO-GXm" id="7Ln-rM-fr1"/>
<outlet property="userNameLabel" destination="RUf-a9-7FA" id="NKh-84-hOF"/>
<outlet property="userProfileImageView" destination="2gC-Cj-Q7U" id="htG-bi-fL3"/>
</connections>
<point key="canvasLocation" x="137.68115942028987" y="154.01785714285714"/>
</tableViewCell>
</objects>
</document>
91 changes: 91 additions & 0 deletions chat-iOS/Views/SelectChat/SelectChatViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// SelectChatViewController.swift
// chat-iOS
//
// Created by 松木周 on 2020/07/15.
//

import UIKit

class SelectChatViewController: UIViewController {

@IBOutlet weak var selectChatTableView: UITableView!

private let reuseCellId = "SelectChatTableViewCell"
private var currentChatRooms: [Room] = []
private var presenter: SelectChatViewPresenterProtocol!

override func viewDidLoad() {
super.viewDidLoad()

setupSelectChatTableView()
self.presenter.didLoadViewController()
}

func inject(with presenter: SelectChatViewPresenterProtocol) {

self.presenter = presenter
self.presenter.view = self

}

func setupSelectChatTableView() {

self.selectChatTableView.delegate = self
self.selectChatTableView.dataSource = self
self.selectChatTableView.register(UINib(nibName: reuseCellId, bundle: nil), forCellReuseIdentifier: reuseCellId)
self.selectChatTableView.tableFooterView = UIView()

}

}

extension SelectChatViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let selectedRoom = currentChatRooms[indexPath.row]
self.presenter.didTapTableViewCell(selectedRoom: selectedRoom)
}

}

extension SelectChatViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentChatRooms.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseCellId) as! SelectChatTableViewCell
cell.generateCell(withRoom: currentChatRooms[indexPath.row])
return cell
}

}

extension SelectChatViewController: SelectChatViewPresenterOutput {

func setCurrentChatUsers() {
self.currentChatRooms = self.presenter.currentChatRooms
self.selectChatTableView.reloadData()
}

func showAlert(withMessage message: String) {
let alert = UIAlertController(title: "エラーが発生しました", message: message, preferredStyle: .alert)
let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: nil)

alert.addAction(defaultAction)
present(alert, animated: false, completion: nil)
}

func transitionToChatsViewController(selectedRoom room: Room) {

guard let roomId = room.id else { return }
guard let roomName = room.name else { return }
let chatsViewController = ChatsViewBuilder.create(withRoomId: roomId, withRoomName: roomName)
self.navigationController?.pushViewController(chatsViewController, animated: true)

}

}
Loading