Commit 77da9609 authored by CZ1004's avatar CZ1004

【优化】部分代码优化

parent 401a24cd
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import Contacts
class ContactBackupDetailViewController : BaseViewController { class ContactBackupDetailViewController : BaseViewController {
...@@ -157,6 +158,7 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS ...@@ -157,6 +158,7 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS
return 20 return 20
} }
func setupCustomIndexView() { func setupCustomIndexView() {
customIndexView = nil
customIndexView = UIStackView() customIndexView = UIStackView()
customIndexView.axis = .vertical customIndexView.axis = .vertical
customIndexView.alignment = .center customIndexView.alignment = .center
...@@ -166,9 +168,9 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS ...@@ -166,9 +168,9 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS
self.customIndexView.snp.makeConstraints { make in self.customIndexView.snp.makeConstraints { make in
make.left.equalTo(self.tableView.snp.right).offset(0) make.left.equalTo(self.tableView.snp.right).offset(0)
make.top.equalTo(self.subTitleLabel.snp.bottom).offset(77) make.centerY.equalTo(self.view.snp.centerY)
make.width.equalTo(15 * RScreenW()) make.width.equalTo(15 * RScreenW())
make.height.equalTo(354) make.height.equalTo(self.sectionTitles.count * (14 + 2))
} }
for (index, section) in sectionTitles.enumerated() { for (index, section) in sectionTitles.enumerated() {
...@@ -193,8 +195,17 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS ...@@ -193,8 +195,17 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS
} }
} }
func scrollViewDidScroll(_ scrollView: UIScrollView) { func scrollViewDidScroll(_ scrollView: UIScrollView) {
let visibleRect = CGRect(origin: tableView.contentOffset, size: tableView.bounds.size) // 获取可见区域的中心点,稍微向下偏移以更准确地检测当前可见的 section
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.minY) let visibleRect = CGRect(
origin: tableView.contentOffset,
size: tableView.bounds.size
)
// 将检测点从顶部边缘下移一些,例如下移10%的可见区域高度
let adjustedY = visibleRect.minY + visibleRect.height * 0.1
let visiblePoint = CGPoint(x: visibleRect.midX, y: adjustedY)
// 获取可见区域最顶部的单元格的 indexPath
if let visibleIndexPath = tableView.indexPathForRow(at: visiblePoint) { if let visibleIndexPath = tableView.indexPathForRow(at: visiblePoint) {
let newIndex = visibleIndexPath.section let newIndex = visibleIndexPath.section
if newIndex != selectedIndex { if newIndex != selectedIndex {
...@@ -218,7 +229,6 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS ...@@ -218,7 +229,6 @@ extension ContactBackupDetailViewController:UITableViewDelegate,UITableViewDataS
let indexPath = IndexPath(row: 0, section: index) let indexPath = IndexPath(row: 0, section: index)
tableView.scrollToRow(at: indexPath, at: .top, animated: true) tableView.scrollToRow(at: indexPath, at: .top, animated: true)
selectedIndex = index selectedIndex = index
updateIndexStyles()
} }
} }
......
...@@ -12,6 +12,7 @@ class CustomContactViewController: CNContactViewController { ...@@ -12,6 +12,7 @@ class CustomContactViewController: CNContactViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
self.allowsEditing = false
setupCancelButton() setupCancelButton()
} }
......
...@@ -153,7 +153,7 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -153,7 +153,7 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
}else{ }else{
self.selectedContacts.removeAll(where: { $0.identifier == model.identifier }) self.selectedContacts.removeAll(where: { $0.identifier == model.identifier })
} }
DispatchQueue.main.async { DispatchQueue.main.async {
// 判断button是否显示 // 判断button是否显示
if self.selectedContacts.count > 0 { if self.selectedContacts.count > 0 {
...@@ -205,9 +205,9 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -205,9 +205,9 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
self.customIndexView.snp.makeConstraints { make in self.customIndexView.snp.makeConstraints { make in
make.left.equalTo(self.tableView.snp.right).offset(0) make.left.equalTo(self.tableView.snp.right).offset(0)
make.top.equalTo(self.subTitleLabel.snp.bottom).offset(77) make.centerY.equalTo(self.snp.centerY)
make.width.equalTo(15 * RScreenW()) make.width.equalTo(15 * RScreenW())
make.height.equalTo(354) make.height.equalTo(self.sectionTitles.count * (14 + 2))
} }
for (index, section) in sectionTitles.enumerated() { for (index, section) in sectionTitles.enumerated() {
...@@ -232,8 +232,17 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -232,8 +232,17 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
} }
} }
func scrollViewDidScroll(_ scrollView: UIScrollView) { func scrollViewDidScroll(_ scrollView: UIScrollView) {
let visibleRect = CGRect(origin: tableView.contentOffset, size: tableView.bounds.size) // 获取可见区域的中心点,稍微向下偏移以更准确地检测当前可见的 section
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.minY) let visibleRect = CGRect(
origin: tableView.contentOffset,
size: tableView.bounds.size
)
// 将检测点从顶部边缘下移一些,例如下移10%的可见区域高度
let adjustedY = visibleRect.minY + visibleRect.height * 0.1
let visiblePoint = CGPoint(x: visibleRect.midX, y: adjustedY)
// 获取可见区域最顶部的单元格的 indexPath
if let visibleIndexPath = tableView.indexPathForRow(at: visiblePoint) { if let visibleIndexPath = tableView.indexPathForRow(at: visiblePoint) {
let newIndex = visibleIndexPath.section let newIndex = visibleIndexPath.section
if newIndex != selectedIndex { if newIndex != selectedIndex {
...@@ -257,7 +266,6 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -257,7 +266,6 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
let indexPath = IndexPath(row: 0, section: index) let indexPath = IndexPath(row: 0, section: index)
tableView.scrollToRow(at: indexPath, at: .top, animated: true) tableView.scrollToRow(at: indexPath, at: .top, animated: true)
selectedIndex = index selectedIndex = index
updateIndexStyles()
} }
} }
...@@ -271,12 +279,32 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -271,12 +279,32 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
guard let self else {return} guard let self else {return}
if isSure { if isSure {
backupContactsByselect { backupContactsByselect {
// 备份完成后删除 // 删除之前弹出是否真的需要删除
self.deleteContacts() let alertVc = ContactDeleteAlertView()
alertVc.frame = (self.responderViewController()?.view.bounds)!
cWindow?.addSubview(alertVc)
alertVc.sureCallBack = {[weak self] isSure in
guard let self else {return}
if isSure {
// 提示是否删除
self.deleteContacts()
}
}
} }
}else{ }else{
// 如果不备份,直接删除 // 删除之前弹出是否真的需要删除
self.deleteContacts() let alertVc = ContactDeleteAlertView()
alertVc.frame = (self.responderViewController()?.view.bounds)!
cWindow?.addSubview(alertVc)
alertVc.sureCallBack = {[weak self] isSure in
guard let self else {return}
if isSure {
// 提示是否删除
self.deleteContacts()
}
}
} }
} }
} }
...@@ -284,15 +312,15 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate { ...@@ -284,15 +312,15 @@ extension ContactAllView : UITableViewDataSource,UITableViewDelegate {
func backupContactsByselect(success:@escaping()->Void){ func backupContactsByselect(success:@escaping()->Void){
// 开始备份联系人,备份完成提示 // 开始备份联系人,备份完成提示
let vm = BackupViewModel() let vm = BackupViewModel()
vm.backupPartialContacts(self.selectedContacts) { finised, error in vm.backupPartialContacts(self.dataSourceModel) { finised, error in
if finised { if finised {
// 备份成功 // 备份成功
success()
DispatchQueue.main.async { DispatchQueue.main.async {
let buAlertVc = ContactBackUpCompletedAlertView(frame: (cWindow?.bounds)!) let buAlertVc = ContactBackUpCompletedAlertView(frame: (cWindow?.bounds)!)
cWindow?.addSubview(buAlertVc) cWindow?.addSubview(buAlertVc)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
buAlertVc.removeFromSuperview() buAlertVc.removeFromSuperview()
success()
} }
} }
}else{ }else{
......
...@@ -99,7 +99,9 @@ class BackupViewModel { ...@@ -99,7 +99,9 @@ class BackupViewModel {
decoder.dateDecodingStrategy = .iso8601 decoder.dateDecodingStrategy = .iso8601
// 尝试解码为 BackupInfoModel 数组 // 尝试解码为 BackupInfoModel 数组
let backupInfos = try decoder.decode([BackupInfoModel].self, from: jsonData) let backupInfos = try decoder.decode([BackupInfoModel].self, from: jsonData)
completion(backupInfos, nil) // 根据时间倒序下
let sortedModels = backupInfos.sorted { $0.backupTime > $1.backupTime }
completion(sortedModels, nil)
} catch { } catch {
Print(error.localizedDescription) Print(error.localizedDescription)
completion(nil, error) completion(nil, error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment