2021-02-05 08:58:48 +01:00
|
|
|
//
|
2021-02-07 07:42:50 +01:00
|
|
|
// HomeTimelineViewController+DebugAction.swift
|
2021-02-05 08:58:48 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-2-5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
2021-03-02 08:51:16 +01:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-02-05 08:58:48 +01:00
|
|
|
|
|
|
|
#if DEBUG
|
2021-02-07 07:42:50 +01:00
|
|
|
extension HomeTimelineViewController {
|
2021-02-05 08:58:48 +01:00
|
|
|
var debugMenu: UIMenu {
|
|
|
|
let menu = UIMenu(
|
|
|
|
title: "Debug Tools",
|
|
|
|
image: nil,
|
|
|
|
identifier: nil,
|
|
|
|
options: .displayInline,
|
|
|
|
children: [
|
2021-03-02 09:27:11 +01:00
|
|
|
moveMenu,
|
2021-03-02 08:51:16 +01:00
|
|
|
dropMenu,
|
2021-02-24 11:07:11 +01:00
|
|
|
UIAction(title: "Show Public Timeline", image: UIImage(systemName: "list.dash"), attributes: []) { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.showPublicTimelineAction(action)
|
|
|
|
},
|
2021-02-05 08:58:48 +01:00
|
|
|
UIAction(title: "Sign Out", image: UIImage(systemName: "escape"), attributes: .destructive) { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.signOutAction(action)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return menu
|
|
|
|
}
|
2021-03-02 08:51:16 +01:00
|
|
|
|
2021-03-02 09:27:11 +01:00
|
|
|
var moveMenu: UIMenu {
|
|
|
|
return UIMenu(
|
|
|
|
title: "Move to…",
|
|
|
|
image: UIImage(systemName: "arrow.forward.circle"),
|
|
|
|
identifier: nil,
|
|
|
|
options: [],
|
|
|
|
children: [
|
|
|
|
UIAction(title: "First Gap", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.moveToTopGapAction(action)
|
|
|
|
}),
|
2021-03-10 06:36:01 +01:00
|
|
|
UIAction(title: "First Reblog Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.moveToFirstReblogToot(action)
|
|
|
|
}),
|
2021-03-02 09:27:11 +01:00
|
|
|
UIAction(title: "First Poll Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.moveToFirstPollToot(action)
|
|
|
|
}),
|
2021-03-10 06:36:01 +01:00
|
|
|
UIAction(title: "First Audio Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.moveToFirstAudioToot(action)
|
|
|
|
}),
|
2021-03-02 09:27:11 +01:00
|
|
|
// UIAction(title: "First Reply Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// self.moveToFirstReplyToot(action)
|
|
|
|
// }),
|
|
|
|
// UIAction(title: "First Reply Reblog", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// self.moveToFirstReplyReblog(action)
|
|
|
|
// }),
|
|
|
|
// UIAction(title: "First Video Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// self.moveToFirstVideoToot(action)
|
|
|
|
// }),
|
|
|
|
// UIAction(title: "First GIF Toot", image: nil, attributes: [], handler: { [weak self] action in
|
|
|
|
// guard let self = self else { return }
|
|
|
|
// self.moveToFirstGIFToot(action)
|
|
|
|
// }),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-02 08:51:16 +01:00
|
|
|
var dropMenu: UIMenu {
|
|
|
|
return UIMenu(
|
|
|
|
title: "Drop…",
|
|
|
|
image: UIImage(systemName: "minus.circle"),
|
|
|
|
identifier: nil,
|
|
|
|
options: [],
|
|
|
|
children: [50, 100, 150, 200, 250, 300].map { count in
|
2021-03-02 09:27:11 +01:00
|
|
|
UIAction(title: "Drop Recent \(count) Toots", image: nil, attributes: [], handler: { [weak self] action in
|
2021-03-02 08:51:16 +01:00
|
|
|
guard let self = self else { return }
|
2021-03-02 09:27:11 +01:00
|
|
|
self.dropRecentTootsAction(action, count: count)
|
2021-03-02 08:51:16 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2021-02-05 08:58:48 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
extension HomeTimelineViewController {
|
2021-02-05 08:58:48 +01:00
|
|
|
|
2021-03-02 09:27:11 +01:00
|
|
|
@objc private func moveToTopGapAction(_ sender: UIAction) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let snapshotTransitioning = diffableDataSource.snapshot()
|
|
|
|
let item = snapshotTransitioning.itemIdentifiers.first(where: { item in
|
|
|
|
switch item {
|
|
|
|
case .homeMiddleLoader: return true
|
|
|
|
default: return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if let targetItem = item, let index = snapshotTransitioning.indexOfItem(targetItem) {
|
|
|
|
tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .middle, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 06:36:01 +01:00
|
|
|
@objc private func moveToFirstReblogToot(_ sender: UIAction) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let snapshotTransitioning = diffableDataSource.snapshot()
|
|
|
|
let item = snapshotTransitioning.itemIdentifiers.first(where: { item in
|
|
|
|
switch item {
|
|
|
|
case .homeTimelineIndex(let objectID, _):
|
|
|
|
let homeTimelineIndex = viewModel.fetchedResultsController.managedObjectContext.object(with: objectID) as! HomeTimelineIndex
|
|
|
|
return homeTimelineIndex.toot.reblog != nil
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if let targetItem = item, let index = snapshotTransitioning.indexOfItem(targetItem) {
|
|
|
|
tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .middle, animated: true)
|
|
|
|
tableView.blinkRow(at: IndexPath(row: index, section: 0))
|
|
|
|
} else {
|
|
|
|
print("Not found reblog toot")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 09:27:11 +01:00
|
|
|
@objc private func moveToFirstPollToot(_ sender: UIAction) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let snapshotTransitioning = diffableDataSource.snapshot()
|
|
|
|
let item = snapshotTransitioning.itemIdentifiers.first(where: { item in
|
|
|
|
switch item {
|
|
|
|
case .homeTimelineIndex(let objectID, _):
|
|
|
|
let homeTimelineIndex = viewModel.fetchedResultsController.managedObjectContext.object(with: objectID) as! HomeTimelineIndex
|
|
|
|
let toot = homeTimelineIndex.toot.reblog ?? homeTimelineIndex.toot
|
|
|
|
return toot.poll != nil
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if let targetItem = item, let index = snapshotTransitioning.indexOfItem(targetItem) {
|
|
|
|
tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .middle, animated: true)
|
|
|
|
tableView.blinkRow(at: IndexPath(row: index, section: 0))
|
|
|
|
} else {
|
|
|
|
print("Not found poll toot")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 06:36:01 +01:00
|
|
|
@objc private func moveToFirstAudioToot(_ sender: UIAction) {
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let snapshotTransitioning = diffableDataSource.snapshot()
|
|
|
|
let item = snapshotTransitioning.itemIdentifiers.first(where: { item in
|
|
|
|
switch item {
|
|
|
|
case .homeTimelineIndex(let objectID, _):
|
|
|
|
let homeTimelineIndex = viewModel.fetchedResultsController.managedObjectContext.object(with: objectID) as! HomeTimelineIndex
|
|
|
|
let toot = homeTimelineIndex.toot.reblog ?? homeTimelineIndex.toot
|
|
|
|
return toot.mediaAttachments?.contains(where: { $0.type == .audio }) ?? false
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if let targetItem = item, let index = snapshotTransitioning.indexOfItem(targetItem) {
|
|
|
|
tableView.scrollToRow(at: IndexPath(row: index, section: 0), at: .middle, animated: true)
|
|
|
|
tableView.blinkRow(at: IndexPath(row: index, section: 0))
|
|
|
|
} else {
|
|
|
|
print("Not found audio toot")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 09:27:11 +01:00
|
|
|
@objc private func dropRecentTootsAction(_ sender: UIAction, count: Int) {
|
2021-03-02 08:51:16 +01:00
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
let snapshotTransitioning = diffableDataSource.snapshot()
|
|
|
|
|
|
|
|
let droppingObjectIDs = snapshotTransitioning.itemIdentifiers.prefix(count).compactMap { item -> NSManagedObjectID? in
|
|
|
|
switch item {
|
|
|
|
case .homeTimelineIndex(let objectID, _): return objectID
|
|
|
|
default: return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var droppingTootObjectIDs: [NSManagedObjectID] = []
|
|
|
|
context.apiService.backgroundManagedObjectContext.performChanges { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
for objectID in droppingObjectIDs {
|
|
|
|
guard let homeTimelineIndex = try? self.context.apiService.backgroundManagedObjectContext.existingObject(with: objectID) as? HomeTimelineIndex else { continue }
|
|
|
|
droppingTootObjectIDs.append(homeTimelineIndex.toot.objectID)
|
|
|
|
self.context.apiService.backgroundManagedObjectContext.delete(homeTimelineIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.sink { [weak self] result in
|
|
|
|
guard let self = self else { return }
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.context.apiService.backgroundManagedObjectContext.performChanges { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
for objectID in droppingTootObjectIDs {
|
|
|
|
guard let toot = try? self.context.apiService.backgroundManagedObjectContext.existingObject(with: objectID) as? Toot else { continue }
|
|
|
|
self.context.apiService.backgroundManagedObjectContext.delete(toot)
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 12:10:45 +01:00
|
|
|
.sink { _ in
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
.store(in: &self.disposeBag)
|
2021-03-02 08:51:16 +01:00
|
|
|
case .failure(let error):
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:07:11 +01:00
|
|
|
@objc private func showPublicTimelineAction(_ sender: UIAction) {
|
|
|
|
coordinator.present(scene: .publicTimeline, from: self, transition: .show)
|
|
|
|
}
|
|
|
|
|
2021-02-05 08:58:48 +01:00
|
|
|
}
|
|
|
|
#endif
|