| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- //
- // LNMineUserInfoView.swift
- // Lanu
- //
- // Created by OneeChan on 2025/12/19.
- //
- import Foundation
- import UIKit
- import SnapKit
- class LNMineUserInfoView: UIView {
- private let avatar = UIImageView()
- private let nameLabel = UILabel()
- private let idLabel = UILabel()
- private let fansCountLabel = UILabel()
- private let followCountLabel = UILabel()
- private let visitorCountLabel = UILabel()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
-
- LNEventDeliver.addObserver(self)
- onUserInfoChanged(userInfo: myUserInfo)
- onMyRelationInfoChanged()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNMineUserInfoView: LNProfileManagerNotify, LNRelationManagerNotify {
- func onUserInfoChanged(userInfo: LNUserProfileVO) {
- guard userInfo.userNo.isMyUid else { return }
- avatar.showAvatar(userInfo.avatar)
- nameLabel.text = userInfo.nickname
- idLabel.text = userInfo.userNo
- }
-
- func onMyRelationInfoChanged() {
- fansCountLabel.text = "\(myRelationInfo.fansCount)"
- followCountLabel.text = "\(myRelationInfo.followCount)"
- visitorCountLabel.text = "\(myRelationInfo.visitCount)"
- }
- }
- extension LNMineUserInfoView {
- private func setupViews() {
- let container = UIView()
- container.backgroundColor = .fill
- container.layer.cornerRadius = 12
- addSubview(container)
- container.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.bottom.equalToSuperview()
- make.top.equalToSuperview().offset(16)
- }
-
- let home = UIImageView()
- home.image = .icMineHome
- home.onTap { [weak self] in
- guard let self else { return }
- pushToProfile(uid: myUid)
- }
- container.addSubview(home)
- home.snp.makeConstraints { make in
- make.top.trailing.equalToSuperview()
- make.size.equalTo(home.image?.size ?? .zero)
- }
-
- let avatar = buildAvatar()
- addSubview(avatar)
- avatar.snp.makeConstraints { make in
- make.leading.equalToSuperview().offset(16)
- make.top.equalToSuperview()
- }
-
- let userInfo = buildUserInfo()
- container.addSubview(userInfo)
- userInfo.snp.makeConstraints { make in
- make.leading.equalTo(avatar.snp.trailing).offset(10)
- make.bottom.equalTo(avatar)
- make.trailing.equalTo(home.snp.leading).offset(6)
- }
-
- let relation = buildRelation()
- container.addSubview(relation)
- relation.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.bottom.equalToSuperview().offset(-16)
- make.top.equalTo(userInfo.snp.bottom).offset(16)
- }
- }
-
- private func buildAvatar() -> UIView {
- avatar.layer.cornerRadius = 35
- avatar.layer.borderWidth = 2
- avatar.layer.borderColor = UIColor.fill.cgColor
- avatar.backgroundColor = .fill
- avatar.clipsToBounds = true
- avatar.contentMode = .scaleAspectFill
- avatar.onTap { [weak self] in
- guard let self else { return }
- pushToProfile(uid: myUid)
- }
- avatar.snp.makeConstraints { make in
- make.width.height.equalTo(70)
- }
-
- return avatar
- }
-
- private func buildRelation() -> UIView {
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.spacing = 16
- stackView.distribution = .fillEqually
-
- let followView = buildUserCountView(title: .init(key: "A00213"), countLabel: followCountLabel)
- followView.onTap { [weak self] in
- guard let self else { return }
- pushToRelationList(uid: myUid, tabType: .follow)
- }
- stackView.addArrangedSubview(followView)
-
- let line1 = buildLine()
- stackView.addSubview(line1)
- line1.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.centerX.equalTo(followView.snp.trailing)
- }
-
- let fansView = buildUserCountView(title: .init(key: "A00214"), countLabel: fansCountLabel)
- fansView.onTap { [weak self] in
- guard let self else { return }
- pushToRelationList(uid: myUid, tabType: .fans)
- }
- stackView.addArrangedSubview(fansView)
-
- let line2 = buildLine()
- stackView.addSubview(line2)
- line2.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.centerX.equalTo(fansView.snp.trailing)
- }
-
- let visitorView = buildUserCountView(title: .init(key: "B00109"), countLabel: visitorCountLabel)
- visitorView.onTap { [weak self] in
- guard let self else { return }
- pushToVisitors()
- }
- stackView.addArrangedSubview(visitorView)
-
- return stackView
- }
-
- private func buildUserCountView(title: String, countLabel: UILabel) -> UIView {
- let container = UIView()
-
- countLabel.font = .heading_h2
- countLabel.textColor = .text_5
- container.addSubview(countLabel)
- countLabel.snp.makeConstraints { make in
- make.centerX.equalToSuperview()
- make.leading.greaterThanOrEqualToSuperview()
- make.top.equalToSuperview()
- }
-
- let textLabel = UILabel()
- textLabel.text = title
- textLabel.font = .body_s
- textLabel.textColor = .text_3
- textLabel.textAlignment = .center
- container.addSubview(textLabel)
- textLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.bottom.equalToSuperview()
- make.top.equalTo(countLabel.snp.bottom).offset(2)
- }
-
- return container
- }
-
- private func buildLine() -> UIView {
- let line = UIView()
- line.backgroundColor = .init(hex: "#D9D9D9")
- line.snp.makeConstraints { make in
- make.width.equalTo(1)
- make.height.equalTo(22)
- }
-
- return line
- }
-
- private func buildUserInfo() -> UIView {
- let container = UIView()
-
- nameLabel.font = .heading_h1_5
- nameLabel.textColor = .text_5
- container.addSubview(nameLabel)
- nameLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- let ID = UILabel()
- ID.text = .init(key: "ID")
- ID.font = .heading_h5
- ID.textColor = .text_3
- container.addSubview(ID)
- ID.snp.makeConstraints { make in
- make.leading.bottom.equalToSuperview()
- make.top.equalTo(nameLabel.snp.bottom).offset(2)
- }
-
- idLabel.font = .body_xs
- idLabel.textColor = .text_4
- container.addSubview(idLabel)
- idLabel.snp.makeConstraints { make in
- make.leading.equalTo(ID.snp.trailing).offset(4)
- make.centerY.equalTo(ID)
- }
-
- let copyButton = UIButton()
- copyButton.setImage(.icCopyLeft, for: .normal)
- copyButton.addAction(UIAction(handler: { _ in
- let pasteboard = UIPasteboard.general
- pasteboard.string = myUid
-
- showToast(.init(key: "A00047"))
- }), for: .touchUpInside)
- container.addSubview(copyButton)
- copyButton.snp.makeConstraints { make in
- make.leading.equalTo(idLabel.snp.trailing).offset(4)
- make.centerY.equalTo(ID)
- make.trailing.lessThanOrEqualToSuperview()
- }
-
- return container
- }
- }
- #if DEBUG
- import SwiftUI
- struct LNMineUserInfoViewPreview: UIViewRepresentable {
- func makeUIView(context: Context) -> some UIView {
- let container = UIView()
- container.backgroundColor = .lightGray
-
- let view = LNMineUserInfoView()
- container.addSubview(view)
- view.snp.makeConstraints { make in
- make.leading.trailing.centerY.equalToSuperview()
- }
-
- return container
- }
-
- func updateUIView(_ uiView: UIViewType, context: Context) { }
- }
- #Preview(body: {
- LNMineUserInfoViewPreview()
- })
- #endif
|