Skip to content

Commit

Permalink
Join room history
Browse files Browse the repository at this point in the history
  • Loading branch information
vince-hz committed Nov 22, 2023
1 parent a312caa commit 535f09f
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Flat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
8A8507EF27E180A00072AF49 /* CloudStorageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8507EE27E180A00072AF49 /* CloudStorageViewController.swift */; };
8A8507F127E186450072AF49 /* CloudStorageInClassViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8507F027E186450072AF49 /* CloudStorageInClassViewController.swift */; };
8A86A4EF2806B6D60095F499 /* SMSAuthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A86A4EE2806B6D60095F499 /* SMSAuthView.swift */; };
8A8797F92B0CA661009A951C /* HistoryJoinRoomPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8797F82B0CA661009A951C /* HistoryJoinRoomPickerViewController.swift */; };
8A8A240329879F63008319BB /* ClassroomStatusBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8A240229879F63008319BB /* ClassroomStatusBar.swift */; };
8A8A79B52A11E03900B000CF /* AppDelegate+keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8A79B42A11E03900B000CF /* AppDelegate+keyboard.swift */; };
8A8A79B72A11E09000B000CF /* GlobalKeyboardShortcutRespondable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A8A79B62A11E09000B000CF /* GlobalKeyboardShortcutRespondable.swift */; };
Expand Down Expand Up @@ -590,6 +591,7 @@
8A8507EE27E180A00072AF49 /* CloudStorageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudStorageViewController.swift; sourceTree = "<group>"; };
8A8507F027E186450072AF49 /* CloudStorageInClassViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudStorageInClassViewController.swift; sourceTree = "<group>"; };
8A86A4EE2806B6D60095F499 /* SMSAuthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMSAuthView.swift; sourceTree = "<group>"; };
8A8797F82B0CA661009A951C /* HistoryJoinRoomPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryJoinRoomPickerViewController.swift; sourceTree = "<group>"; };
8A8A240229879F63008319BB /* ClassroomStatusBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassroomStatusBar.swift; sourceTree = "<group>"; };
8A8A79B42A11E03900B000CF /* AppDelegate+keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+keyboard.swift"; sourceTree = "<group>"; };
8A8A79B62A11E09000B000CF /* GlobalKeyboardShortcutRespondable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalKeyboardShortcutRespondable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1152,6 +1154,7 @@
8ACE3A292730CD4600BC37F7 /* CreateClassRoomViewController.swift */,
8A0CA23628BF4A860035F71A /* CameraPreviewView.swift */,
8AAE53CE2731153500C35796 /* JoinRoomViewController.swift */,
8A8797F82B0CA661009A951C /* HistoryJoinRoomPickerViewController.swift */,
8A59FF8528D8410C00318407 /* JoinRoomInputAccessView.swift */,
8AFB942D28B7550100B8795D /* CameraMicToggleView.swift */,
8A0CA23828C0519A0035F71A /* DeviceAutorizationHelper.swift */,
Expand Down Expand Up @@ -2222,6 +2225,7 @@
8A270BA12719686C00450DC6 /* LocaleManager.swift in Sources */,
8A8020932755F12600F5691C /* UIView+LoopForSuperView.swift in Sources */,
8A21DABC279FAA4C0060E25D /* UpdateRecordEndTimeRequest.swift in Sources */,
8A8797F92B0CA661009A951C /* HistoryJoinRoomPickerViewController.swift in Sources */,
8AAC69B32717E3EB00F9A3B4 /* User.swift in Sources */,
8A80209A27570A4A00F5691C /* CloudStorageTableViewCell.swift in Sources */,
8A97EBB12719993C00965185 /* JoinRoomInfo.swift in Sources */,
Expand Down
25 changes: 25 additions & 0 deletions Flat/Assets.xcassets/Common/triangle_down.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Flat/Extensions/ViewController/UIViewController+SafaArea.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ extension UIViewController {
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.top)
}
}

func fillBottomSafeAreaWith(color: UIColor) {
let fillView = UIView()
fillView.backgroundColor = color
view.addSubview(fillView)
fillView.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.top.equalTo(view.safeAreaLayoutGuide.snp.bottom)
make.bottom.equalTo(view)
}
}
}
11 changes: 7 additions & 4 deletions Flat/Models/Flat/RoomBasicInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import Fastboard
import Foundation
import RxSwift

extension String {
var formatterInviteCode: String {
split(every: 3).joined(separator: " ")
}
}

/// Get from list or by 'ordinary' request
struct RoomBasicInfo: Decodable, Equatable {
let roomUUID: String
Expand All @@ -31,10 +37,6 @@ struct RoomBasicInfo: Decodable, Equatable {
let hasRecord: Bool
let inviteCode: String
let ownerAvatarURL: String

var formatterInviteCode: String {
inviteCode.split(every: 3).joined(separator: " ")
}
}

extension RoomBasicInfo {
Expand Down Expand Up @@ -115,4 +117,5 @@ private struct RoomInfo: Decodable {
let ownerUserName: String
let region: String
let inviteCode: String
let isPmi: Bool
}
47 changes: 47 additions & 0 deletions Flat/Modules/ClassRoom/ClassroomCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import Foundation
import RxSwift

struct JoinRoomHistoryItem: Codable {
let roomName: String
let roomInviteId: String
}

class ClassroomCoordinator: NSObject {
override private init() {
super.init()
Expand All @@ -20,6 +25,45 @@ class ClassroomCoordinator: NSObject {

static let shared = ClassroomCoordinator()

private(set) var joinRoomHisotryItems: [JoinRoomHistoryItem] = []
private var joinRoomHistoryUserDefaultsKey: String { "joinRoomHistory-" + (AuthStore.shared.user?.userUUID ?? "") }
func clearJoinRoomHistoryItem() {
UserDefaults.standard.setValue(nil, forKey: joinRoomHistoryUserDefaultsKey)
updateJoinRoomHistoryItem()
}

func insertJoinRoomHistoryItem(_ item: JoinRoomHistoryItem) {
var items = updateJoinRoomHistoryItem()
if let index = items.firstIndex(where: { $0.roomInviteId == item.roomInviteId }) {
// Removing exist.
items.remove(at: index)
}
if items.count >= 10 {
items = items.dropLast()
}
items.insert(item, at: 0)
do {
let data = try JSONEncoder().encode(items)
UserDefaults.standard.setValue(data, forKey: joinRoomHistoryUserDefaultsKey)
updateJoinRoomHistoryItem()
} catch {
logger.error("joinRoomHistory encode error \(error)")
}
}

@discardableResult
func updateJoinRoomHistoryItem() -> [JoinRoomHistoryItem] {
if let data = UserDefaults.standard.value(forKey: joinRoomHistoryUserDefaultsKey) as? Data {
do {
let items = try JSONDecoder().decode([JoinRoomHistoryItem].self, from: data)
self.joinRoomHisotryItems = items
} catch {
logger.error("joinRoomHistory decode error \(error)")
}
}
return self.joinRoomHisotryItems
}

var currentClassroomUUID: String?
var enterClassDate: Date?

Expand Down Expand Up @@ -140,6 +184,9 @@ class ClassroomCoordinator: NSObject {
.map { (p, $0) }
}
}
.do(onNext: { [weak self] _ , info in
self?.insertJoinRoomHistoryItem(.init(roomName: info.title, roomInviteId: info.inviteCode))
})
.map { ClassroomFactory.getClassRoomViewController(withPlayInfo: $0.0, basicInfo: $0.1, deviceStatus: deviceState) }
.asSingle()
}
Expand Down
140 changes: 140 additions & 0 deletions Flat/Modules/Home/HistoryJoinRoomPickerViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// HistoryJoinRoomPickerViewController.swift
// Flat
//
// Created by xuyunshi on 2023/11/21.
// Copyright © 2023 agora.io. All rights reserved.
//

import UIKit

class HistoryJoinRoomPickerViewController: UIViewController {
var items = ClassroomCoordinator.shared.joinRoomHisotryItems

var roomIdConfirmHandler: ((String) ->Void)?

override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}

func setupViews() {
view.backgroundColor = UIColor.black.withAlphaComponent(0.3)

let operationStack = UIStackView(arrangedSubviews: [clearButton, UIView(), confirmButton])
operationStack.addLine(direction: .bottom, color: .borderColor, width: commonBorderWidth, inset: .init(top: 0, left: 14, bottom: 0, right: 14))
operationStack.axis = .horizontal

let stack = UIStackView(arrangedSubviews: [operationStack, picker, cancelButton])
stack.backgroundColor = .color(type: .background)
stack.axis = .vertical
view.addSubview(stack)
operationStack.snp.makeConstraints { make in
make.height.equalTo(44)
}
stack.snp.makeConstraints { make in
make.left.right.bottom.equalTo(view.safeAreaLayoutGuide)
}
picker.snp.makeConstraints { make in
make.height.equalTo(144)
}
cancelButton.snp.makeConstraints { $0.height.equalTo(44) }
fillBottomSafeAreaWith(color: .color(type: .background))
}

@objc func onCancel() {
dismiss(animated: true)
}

@objc func onConfirm() {
let row = picker.selectedRow(inComponent: 0)
if row + 1 > items.count {
dismiss(animated: true)
return
}
let item = items[row]
dismiss(animated: true)
roomIdConfirmHandler?(item.roomInviteId)
}

@objc func onClear() {
ClassroomCoordinator.shared.clearJoinRoomHistoryItem()
items = []
picker.reloadAllComponents()
}

lazy var picker: UIPickerView = {
let view = UIPickerView()
view.backgroundColor = .color(type: .background)
view.delegate = self
view.dataSource = self
return view
}()

lazy var clearButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle(localizeStrings("ClearHistory"), for: .normal)
btn.addTarget(self, action: #selector(onClear), for: .touchUpInside)
btn.tintColor = .color(type: .text)
btn.contentEdgeInsets = .init(top: 0, left: 14, bottom: 0, right: 14)
return btn
}()

lazy var confirmButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle(localizeStrings("Confirm"), for: .normal)
btn.addTarget(self, action: #selector(onConfirm), for: .touchUpInside)
btn.tintColor = .color(type: .primary)
btn.contentEdgeInsets = .init(top: 0, left: 14, bottom: 0, right: 14)
return btn
}()

lazy var cancelButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle(localizeStrings("Cancel"), for: .normal)
btn.addTarget(self, action: #selector(onCancel), for: .touchUpInside)
btn.tintColor = .color(type: .text)
return btn
}()
}

extension HistoryJoinRoomPickerViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
items.count
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let view = UIView(frame: .zero)
let leftLabel = UILabel()
leftLabel.font = .systemFont(ofSize: 14)
leftLabel.textColor = .color(type: .text)
leftLabel.text = items[row].roomName
leftLabel.textAlignment = .left

let rightLabel = UILabel()
rightLabel.font = .systemFont(ofSize: 14)
rightLabel.textColor = .color(type: .text)
rightLabel.text = items[row].roomInviteId.formatterInviteCode
rightLabel.textAlignment = .right

let stack = UIStackView(arrangedSubviews: [leftLabel, rightLabel])
stack.axis = .horizontal
view.addSubview(stack)
stack.snp.makeConstraints { make in
make.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14))
}
return view
}

func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
44
}
}
Loading

0 comments on commit 535f09f

Please sign in to comment.