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

#17 - Refactor Firebase Processing Logic #18

Open
wants to merge 1 commit into
base: develop
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
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ github "Ekhoo/Device" "3.2.1"
github "ReactiveCocoa/ReactiveCocoa" "10.3.0"
github "ReactiveCocoa/ReactiveSwift" "6.3.0"
github "SnapKit/SnapKit" "5.0.1"
github "airbnb/lottie-ios" "4e5877425dae5c10792fc9d22d53dc6bf6824dc1"
github "airbnb/lottie-ios" "eebcf80fa5c502159f9f61cbf84551766d8e2832"
github "alickbass/CodableFirebase" "0.2.2"
github "ninjaprox/NVActivityIndicatorView" "4.8.0"
github "raulriera/TextFieldEffects" "1.7.0"
20 changes: 16 additions & 4 deletions DDD.Attendance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
BC1014932333D9BB0096E962 /* HomeHeaderCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC1014912333D9BB0096E962 /* HomeHeaderCell.xib */; };
BC1014982333DD4E0096E962 /* WelcomeCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC1014962333DD4E0096E962 /* WelcomeCell.swift */; };
BC1014992333DD4E0096E962 /* WelcomeCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC1014972333DD4E0096E962 /* WelcomeCell.xib */; };
BC381F302349F4760040202D /* Firebase.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC381F2F2349F4760040202D /* Firebase.swift */; };
BC381F302349F4760040202D /* FirebaseClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC381F2F2349F4760040202D /* FirebaseClient.swift */; };
BC3E50AE232A5E4600B56EBF /* Login.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BC3E50AD232A5E4600B56EBF /* Login.storyboard */; };
BC3E50B0232A5E5600B56EBF /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC3E50AF232A5E5600B56EBF /* LoginViewController.swift */; };
BC3E50B3232A5EF600B56EBF /* SignUp.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BC3E50B2232A5EF600B56EBF /* SignUp.storyboard */; };
Expand Down Expand Up @@ -107,6 +107,7 @@
BCE1B4102320034F001BB684 /* AttendanceListCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE1B40E2320034F001BB684 /* AttendanceListCell.swift */; };
BCE1B4112320034F001BB684 /* AttendanceListCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BCE1B40F2320034F001BB684 /* AttendanceListCell.xib */; };
BCE1B4132320054F001BB684 /* AttendanceListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCE1B4122320054F001BB684 /* AttendanceListModel.swift */; };
E53AF1E624E4312B00E517A4 /* FirebasePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = E53AF1E524E4312B00E517A4 /* FirebasePath.swift */; };
E571F75F2323F08700816C04 /* SnapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E571F75E2323F08700816C04 /* SnapKit.framework */; };
E571F7652324024D00816C04 /* Storyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = E571F7642324024D00816C04 /* Storyboard.swift */; };
E571F767232402D200816C04 /* ReuseableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E571F766232402D200816C04 /* ReuseableView.swift */; };
Expand All @@ -118,6 +119,8 @@
E571F77723241F5300816C04 /* Account.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E571F77623241F5300816C04 /* Account.storyboard */; };
E571F77A2324345400816C04 /* UIView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E571F7792324345400816C04 /* UIView+Extension.swift */; };
E571F77C23243D7600816C04 /* HomeTransitionCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E571F77B23243D7600816C04 /* HomeTransitionCoordinator.swift */; };
E5821C6524D5902F00A89DBE /* FirebaseDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5821C6424D5902F00A89DBE /* FirebaseDecoder.swift */; };
E5821C6724D596E100A89DBE /* FirebaseResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5821C6624D596E100A89DBE /* FirebaseResult.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -175,7 +178,7 @@
BC1014912333D9BB0096E962 /* HomeHeaderCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeHeaderCell.xib; sourceTree = "<group>"; };
BC1014962333DD4E0096E962 /* WelcomeCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeCell.swift; sourceTree = "<group>"; };
BC1014972333DD4E0096E962 /* WelcomeCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = WelcomeCell.xib; sourceTree = "<group>"; };
BC381F2F2349F4760040202D /* Firebase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Firebase.swift; sourceTree = "<group>"; };
BC381F2F2349F4760040202D /* FirebaseClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseClient.swift; sourceTree = "<group>"; };
BC3E50AD232A5E4600B56EBF /* Login.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Login.storyboard; sourceTree = "<group>"; };
BC3E50AF232A5E5600B56EBF /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
BC3E50B2232A5EF600B56EBF /* SignUp.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = SignUp.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -225,6 +228,7 @@
BCE1B40E2320034F001BB684 /* AttendanceListCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttendanceListCell.swift; sourceTree = "<group>"; };
BCE1B40F2320034F001BB684 /* AttendanceListCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AttendanceListCell.xib; sourceTree = "<group>"; };
BCE1B4122320054F001BB684 /* AttendanceListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttendanceListModel.swift; sourceTree = "<group>"; };
E53AF1E524E4312B00E517A4 /* FirebasePath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebasePath.swift; sourceTree = "<group>"; };
E571F75E2323F08700816C04 /* SnapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SnapKit.framework; path = Carthage/Build/iOS/SnapKit.framework; sourceTree = "<group>"; };
E571F7642324024D00816C04 /* Storyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Storyboard.swift; sourceTree = "<group>"; };
E571F766232402D200816C04 /* ReuseableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReuseableView.swift; sourceTree = "<group>"; };
Expand All @@ -236,6 +240,8 @@
E571F77623241F5300816C04 /* Account.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Account.storyboard; sourceTree = "<group>"; };
E571F7792324345400816C04 /* UIView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Extension.swift"; sourceTree = "<group>"; };
E571F77B23243D7600816C04 /* HomeTransitionCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HomeTransitionCoordinator.swift; sourceTree = "<group>"; };
E5821C6424D5902F00A89DBE /* FirebaseDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseDecoder.swift; sourceTree = "<group>"; };
E5821C6624D596E100A89DBE /* FirebaseResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseResult.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -344,8 +350,11 @@
BC381F2D2349F4600040202D /* Repository */ = {
isa = PBXGroup;
children = (
BC381F2F2349F4760040202D /* Firebase.swift */,
BC381F2F2349F4760040202D /* FirebaseClient.swift */,
BCDE474F2379A990009CEAC2 /* Curriculum.swift */,
E5821C6424D5902F00A89DBE /* FirebaseDecoder.swift */,
E5821C6624D596E100A89DBE /* FirebaseResult.swift */,
E53AF1E524E4312B00E517A4 /* FirebasePath.swift */,
);
path = Repository;
sourceTree = "<group>";
Expand Down Expand Up @@ -737,16 +746,18 @@
E571F77C23243D7600816C04 /* HomeTransitionCoordinator.swift in Sources */,
4648F78324BB1AEC0084B2A2 /* MenuModel.swift in Sources */,
BCE1B3ED231BE383001BB684 /* BaseDataSource.swift in Sources */,
E5821C6524D5902F00A89DBE /* FirebaseDecoder.swift in Sources */,
BC3E50B5232A5F0100B56EBF /* SignUpViewController.swift in Sources */,
BCE1B3F3231BE57F001BB684 /* HomeViewModel.swift in Sources */,
BC10148D232E32810096E962 /* LoginTransitionCoordinator.swift in Sources */,
4654355224C1FA0300541E14 /* AttendanceStatusHeader.swift in Sources */,
46481EA3249DF80100F3DB02 /* SetAttendanceViewController.swift in Sources */,
BC381F302349F4760040202D /* Firebase.swift in Sources */,
BC381F302349F4760040202D /* FirebaseClient.swift in Sources */,
E571F76E2324085C00816C04 /* InteractiveAnimator.swift in Sources */,
466B86BF24C475FE00855C94 /* NameHeaderCell.swift in Sources */,
4648F77724BB16040084B2A2 /* ManagerHomeViewController.swift in Sources */,
46AD886324A4DAFA0036B4AF /* BannerModel.swift in Sources */,
E5821C6724D596E100A89DBE /* FirebaseResult.swift in Sources */,
BC3E50C8232A6D6C00B56EBF /* Optional+Extension.swift in Sources */,
BCE1B3F5231BE67A001BB684 /* AccountView.swift in Sources */,
4654355424C2020000541E14 /* AttendanceStatusModel.swift in Sources */,
Expand All @@ -765,6 +776,7 @@
BC3E50B0232A5E5600B56EBF /* LoginViewController.swift in Sources */,
2074DB1D2348C6BE007D7377 /* SignUpViewModel.swift in Sources */,
E571F77423241D3300816C04 /* AccountViewController.swift in Sources */,
E53AF1E624E4312B00E517A4 /* FirebasePath.swift in Sources */,
BC8863FA234E36FE0031C7F1 /* PosterCell.swift in Sources */,
462EF76424B9B9590009768C /* ImageScrollViewController.swift in Sources */,
E571F76A2324033B00816C04 /* PresentingViewAnimator.swift in Sources */,
Expand Down
9 changes: 5 additions & 4 deletions DDD.Attendance/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ private extension AppDelegate {
let userDefault = UserDefaults.standard
if userDefault.value(forKey: "wasOpendApp") == nil {
userDefault.set(true, forKey: "wasOpendApp")
Firebase().signOut { isSuccess in
if isSuccess {
FirebaseClient().requestSignOut { result in
switch result {
case .success(_):
print("Success - Unauthenticate Firebase")
} else {
print("Failure - Unauthenticate Firebase")
case .failure(let error):
print(error)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions DDD.Attendance/Common/QRCode/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ScannerViewController: BaseViewController {

private var captureSession: AVCaptureSession!
private var previewLayer: AVCaptureVideoPreviewLayer!
private let firebase = Firebase()
private let firebase = FirebaseClient()
var attendanceTimeStamp: Int64? {
didSet {
descriptionLabel.text = "설정한 출석체크 시간은 \n\(Date().timeStampToString(timeStamp: attendanceTimeStamp ?? 0).dateAndTimetoString())입니다."
Expand Down Expand Up @@ -138,7 +138,7 @@ private extension ScannerViewController {
guard let attendanceTimeStamp = attendanceTimeStamp else { return }
let currentTimeStamp = Date().getTimeStamp()
let isLate = currentTimeStamp > attendanceTimeStamp
firebase.attendance(userId: userId, isLate: isLate, timeStamp: currentTimeStamp) { [weak self] result in
firebase.requestAttendance(userId: userId, isLate: isLate, timeStamp: currentTimeStamp) { [weak self] result in
self?.showResult(result: result)
}
}
Expand Down
16 changes: 0 additions & 16 deletions DDD.Attendance/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class HomeViewController: BaseViewController {
$0.separatorInset = UIEdgeInsets(top: 0, left: UIScreen.main.bounds.width, bottom: 0, right: 0)
$0.dataSource = dataSource
}

// profileButton.then {
// $0.action = #selector(signOut)
// $0.target = self
// }
}

override func bindStyle() {
Expand Down Expand Up @@ -143,17 +138,6 @@ private extension HomeViewController {
present(viewControllerToPresent, animated: true)
}
}

@objc func signOut() {
Firebase().signOut { [weak self] isSuccess in
if isSuccess {
let loginVC = LoginViewController.instantiateViewController()
UIApplication.shared.keyWindow?.rootViewController = loginVC
} else {
self?.showAlert(title: "로그아웃 실패", message: "로그아웃에 실패하였습니다.")
}
}
}
}

// MARK: - Reactive
Expand Down
4 changes: 2 additions & 2 deletions DDD.Attendance/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ protocol HomeViewModelTypes {

class HomeViewModel {

private let firebase: Firebase
private let firebase: FirebaseClient
private let accountModelProperty = MutableProperty<AccountModel?>(nil)
private let curriculumListProperty = MutableProperty<[Curriculum]?>(nil)
private let bannerProperty = MutableProperty<Banner?>(nil)

init(firebase: Firebase = Firebase()) {
init(firebase: FirebaseClient = FirebaseClient()) {
self.firebase = firebase
}
}
Expand Down
12 changes: 8 additions & 4 deletions DDD.Attendance/Login/LoginPopupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ class LoginPopupViewController: BaseViewController {
override func bindViewModel() {
super.bindViewModel()

loginPopupView.resultHandler = { [weak self] status in
loginPopupView.loginSuccessHandler = { [weak self] status in
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
switch status {
case .admin:
self?.moveManagerHomeViewController()
case .default:
self?.moveHomeViewController()
case .failure:
self?.loginFailureAction(with: "Email 또는 Password를 확인해주세요.")
}
}


loginPopupView.loginFailureHandler = { [weak self] error in
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
print(error)
self?.loginFailureAction(with: "Email 또는 Password를 확인해주세요.")
}

reactive.keyboardWillShow <~ NotificationCenter.default.reactive
.keyboard(.willShow)

Expand Down
17 changes: 13 additions & 4 deletions DDD.Attendance/Login/View/LoginPopupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class LoginPopupView: BaseView {

private let viewModel = LoginPopupViewModel()

var resultHandler: ((Firebase.LoginStatus) -> Void)?
var loginSuccessHandler: ((FirebaseClient.AccountType) -> Void)?
var loginFailureHandler: ((Error) -> Void)?

override func bindData() {
super.bindData()
Expand All @@ -45,7 +46,9 @@ class LoginPopupView: BaseView {

reactive.requestFirebaseAuth <~ viewModel.outputs.loginAccount

reactive.loginResultHandler <~ viewModel.outputs.loginResult
reactive.loginSuccessHandler <~ viewModel.outputs.loginSuccess

reactive.loginFailureHandler <~ viewModel.outputs.loginFailure

reactive.isEnabledLoginButton <~ viewModel.outputs.isValidAccount

Expand Down Expand Up @@ -113,11 +116,17 @@ extension Reactive where Base: LoginPopupView {
})
}

var loginResultHandler: BindingTarget<Firebase.LoginStatus> {
var loginSuccessHandler: BindingTarget<FirebaseClient.AccountType> {
return makeBindingTarget({ base, status in
base.resultHandler?(status)
base.loginSuccessHandler?(status)
})
}

var loginFailureHandler: BindingTarget<Error> {
return makeBindingTarget { base, error in
base.loginFailureHandler?(error)
}
}

var checkValidAccount: BindingTarget<(String, String)> {
return makeBindingTarget({ base, account in
Expand Down
56 changes: 28 additions & 28 deletions DDD.Attendance/Login/View/LoginPopupViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ protocol LoginPopupViewModelOutputs {

var loginAccount: Signal<LoginPopupViewModel.Account, Never> { get }

var loginResult: Signal<Firebase.LoginStatus, Never> { get }
var loginSuccess: Signal<FirebaseClient.AccountType, Never> { get }

var isValidAccount: Signal<Bool, Never> { get }

var loginFailure: Signal<Error, Never> { get }
}

protocol LoginPopupViewModelTypes {
Expand All @@ -38,16 +40,17 @@ class LoginPopupViewModel {

typealias Account = (String, String)

private let firebase: Firebase
private let firebase: FirebaseClient
private let isValidAccountProperty = MutableProperty<Bool?>(nil)
private let pressLoginButtonProperty = MutableProperty<Void?>(nil)
private let emailProperty = MutableProperty<String>("")
private let passwordProperty = MutableProperty<String>("")
private let loginResultProperty = MutableProperty<Firebase.LoginStatus?>(nil)
private let accountTypeProperty = MutableProperty<FirebaseClient.AccountType?>(nil)
private let loginFailureProperty = MutableProperty<Error?>(nil)

init(firebase: Firebase = Firebase()) {
init(firebase: FirebaseClient = FirebaseClient()) {
self.firebase = firebase
self.checkLoginSession()
// self.checkLoginSession()
}
}

Expand Down Expand Up @@ -86,49 +89,46 @@ extension LoginPopupViewModel: LoginPopupViewModelOutputs {
}
}

var loginResult: Signal<Firebase.LoginStatus, Never> {
return loginResultProperty.signal.skipNil()
var loginSuccess: Signal<FirebaseClient.AccountType, Never> {
return accountTypeProperty.signal.skipNil()
}

var isValidAccount: Signal<Bool, Never> {
return emailProperty.signal
.combineLatest(with: passwordProperty.signal)
.filterMap { [unowned self] (email, password) in
.compactMap { [unowned self] (email, password) in
let isValid = self.validateEmail(from: email) && self.validatePassword(from: password)
self.isValidAccountProperty.value = isValid
return isValid
}
}

var loginFailure: Signal<Error, Never> {
return loginFailureProperty.signal.skipNil()
}
}

private extension LoginPopupViewModel {

func loginFirebase(with email: String, _ password: String) {
firebase.login(with: email, password) { [weak self] result in
if result?.user != nil {
self?.fetchLoginStatus { status in
self?.loginResultProperty.value = status
}
} else {
self?.loginResultProperty.value = .failure
firebase.requestSignIn(with: email, password) { [weak self] result in
switch result {
case .success(_):
self?.fetchLoginStatus()
case .failure(let error):
self?.loginFailureProperty.value = error
}
}
}

func fetchLoginStatus(completion: @escaping (Firebase.LoginStatus) -> Void) {
firebase.checkAdminAccunt { status in
completion(status)
}
}

func checkLoginSession() {
let activityData = ActivityData(type: .pacman)
NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
fetchLoginStatus { [weak self] status in
if status != .failure {
self?.loginResultProperty.value = status
func fetchLoginStatus() {
firebase.verifyAccountType { [weak self] result in
switch result {
case .success(let accountType):
self?.accountTypeProperty.value = accountType
case .failure(let error):
self?.loginFailureProperty.value = error
}
NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
}
}

Expand Down
8 changes: 5 additions & 3 deletions DDD.Attendance/Manager/ManagerHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ extension ManagerHomeViewController: UITableViewDelegate {
}

private func signOut() {
Firebase().signOut { [weak self] isSuccess in
if isSuccess {
FirebaseClient().requestSignOut { [weak self] result in
switch result {
case .success(_):
let loginVC = LoginViewController.instantiateViewController()
UIApplication.shared.keyWindow?.rootViewController = loginVC
} else {
case .failure(let error):
print(error)
self?.showAlert(title: "로그아웃 실패", message: "로그아웃에 실패하였습니다.")
}
}
Expand Down
Loading