Skip to content

Commit

Permalink
Several fixes
Browse files Browse the repository at this point in the history
Fixed app crash on launching app by adding magnet link from Safari
Fixed some dark theme glitches
Now the torrent addition is waiting for the torrents to be restored on app launching to check if this torrent was added earlier
  • Loading branch information
XITRIX committed Jun 26, 2018
1 parent af7d17b commit 992a2fa
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 85 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# iTorrent - iOS Torrent client App - LibTorrent Core
# iTorrent - iOS Torrent client App

![](https://img.shields.io/badge/iOS-9.3+-blue.svg)
![](https://www.bitrise.io/app/26ce0756a727335c/status.svg?token=BLhjBICoPvmOtO1nzIVMYQ&branch=master)
Expand All @@ -18,15 +18,17 @@ Alternate version of [iTorrent app](https://github.com/XITRIX/iTorrent_CSharp) f
It is an ordinary torrent client for iOS with Files app support.

What can this app do:
- Download in the background by using music or microphone hack
- Download in the background
- Add torrent files from Share menu (Safari and other apps)
- Store files in Files app (only iOS 11)
- Support for <iOS 11 versions, like sending files directly from app
- Add magnet links directly from Safari
- Store files in Files app (iOS 11+)
- File sharing directly from app
- Download torrent by link
- Download torrent by magnet
- Send notification on torrent downloaded (iOS 10+)
- FTP Server (Read only)
- Select files to download
- Send notification on torrent downloaded
- FTP Server (unstable)
- Select files to download or not
- Change UI to dark theme
- ???

## Libraries used
Expand All @@ -37,7 +39,7 @@ What can this app do:

## Donate for donuts

- VISA CARD - 4890 4944 7168 8218
- VISA CARD - 5106 2110 2661 7147
- [QIWI Moneybox](https://qiwi.me/c5ec30ff-21d6-428b-9a10-29a1d18242db)

## License
Expand Down
122 changes: 68 additions & 54 deletions iTorrent/System/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class Manager {
public static var managersUpdatedDelegates : [ManagersUpdatedDelegate] = []
public static var managersStateChangedDelegade : [ManagerStateChangedDelegate] = []
public static var managerSaves : [String : UserManagerSettings] = [:]
public static var engineInited : Bool = false
public static var torrentsRestored : Bool = false

public static func InitManager() {
DispatchQueue.global(qos: .background).async {
init_engine(Manager.rootFolder)
engineInited = true
restoreAllTorrents()
while(true) {
mainLoop()
Expand All @@ -46,10 +49,6 @@ class Manager {
static func restoreAllTorrents() {
Utils.checkFolderExist(path: configFolder)
Utils.checkFolderExist(path: fastResumesFolder)
// This code may be the reason of this bug: https://github.com/XITRIX/iTorrent/issues/2
// if (FileManager.default.fileExists(atPath: configFolder+"/_temp.torrent")) {
// try! FileManager.default.removeItem(atPath: configFolder+"/_temp.torrent")
// }

if let loadedStrings = NSKeyedUnarchiver.unarchiveObject(withFile: fastResumesFolder + "/userData.dat") as? [String : UserManagerSettings] {
print("resumed")
Expand All @@ -61,6 +60,7 @@ class Manager {
if (file == "_temp.torrent") { continue }
addTorrent(configFolder + "/" + file)
}
torrentsRestored = true
}

static func removeTorrentFile(hash: String) {
Expand Down Expand Up @@ -149,46 +149,48 @@ class Manager {
}
}
}

let dest = Manager.configFolder+"/_temp.torrent"
print(filePath.startAccessingSecurityScopedResource())
do {
if (FileManager.default.fileExists(atPath: dest)) {
try FileManager.default.removeItem(atPath: dest)
}
print(FileManager.default.fileExists(atPath: filePath.path))
try FileManager.default.copyItem(at: filePath, to: URL(fileURLWithPath: dest))
} catch {
print(error.localizedDescription)

let controller = ThemedUIAlertController(title: "Error on torrent opening", message: error.localizedDescription, preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)

return
}
filePath.stopAccessingSecurityScopedResource()

let hash = String(validatingUTF8: get_torrent_file_hash(dest))!
if (hash == "-1") {
let controller = ThemedUIAlertController(title: "Error on torrent reading", message: "Torrent file opening error has been occured", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
return
} else if (torrentStates.contains(where: {$0.hash == hash})){
let controller = ThemedUIAlertController(title: "This torrent already exists", message: "Torrent with hash: \"" + hash + "\" already exists in download queue", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
return
}
do {

let controller = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "AddTorrent")
((controller as! UINavigationController).topViewController as! AddTorrentController).path = dest
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)

DispatchQueue.global(qos: .background).async {
while (!torrentsRestored) { sleep(1) }
DispatchQueue.main.async {
let dest = Manager.configFolder+"/_temp.torrent"
print(filePath.startAccessingSecurityScopedResource())
do {
if (FileManager.default.fileExists(atPath: dest)) {
try FileManager.default.removeItem(atPath: dest)
}
print(FileManager.default.fileExists(atPath: filePath.path))
try FileManager.default.copyItem(at: filePath, to: URL(fileURLWithPath: dest))
} catch {
let controller = ThemedUIAlertController(title: "Error on torrent opening", message: error.localizedDescription, preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)

return
}
filePath.stopAccessingSecurityScopedResource()

let hash = String(validatingUTF8: get_torrent_file_hash(dest))!
if (hash == "-1") {
let controller = ThemedUIAlertController(title: "Error on torrent reading", message: "Torrent file opening error has been occured", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
return
} else if (torrentStates.contains(where: {$0.hash == hash})) {
let controller = ThemedUIAlertController(title: "This torrent already exists", message: "Torrent with hash: \"" + hash + "\" already exists in download queue", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
return
}
do {
let controller = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "AddTorrent")
((controller as! UINavigationController).topViewController as! AddTorrentController).path = dest
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
}
}
}
}

Expand All @@ -208,16 +210,28 @@ class Manager {
}

static func addMagnet(_ magnetLink: String) {
if magnetLink.starts(with: "magnet:"),
let hash = String(validatingUTF8: add_magnet(magnetLink)) {
print(hash)
managerSaves[hash] = UserManagerSettings()
mainLoop()
} else {
let controller = ThemedUIAlertController(title: "Error", message: "Wrong magnet link, check it and try again!", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
if magnetLink.starts(with: "magnet:") {
DispatchQueue.global(qos: .background).async {
while (!torrentsRestored) { sleep(1) }
DispatchQueue.main.async {
let hash = String(validatingUTF8: get_magnet_hash(magnetLink))
if (Manager.torrentStates.contains(where: {$0.hash == hash})) {
let alert = ThemedUIAlertController(title: "This torrent already exists", message: "Torrent with hash: \"" + hash! + "\" already exists in download queue", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
alert.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true)
} else if let hash = String(validatingUTF8: add_magnet(magnetLink)) {
print(hash)
managerSaves[hash] = UserManagerSettings()
mainLoop()
} else {
let controller = ThemedUIAlertController(title: "Error", message: "Wrong magnet link, check it and try again!", preferredStyle: .alert)
let close = UIAlertAction(title: "Close", style: .cancel)
controller.addAction(close)
UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true)
}
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions iTorrent/Theme/ColorPalett.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Themes {
darkTheme.backgroundSecondary = UIColor(red: 55/255, green: 55/255, blue: 55/255, alpha: 1)
darkTheme.backgroundTertiary = UIColor(red: 55/255, green: 55/255, blue: 55/255, alpha: 1)
darkTheme.tableHeaderColor = UIColor(red: 0.05, green: 0.05, blue: 0.05, alpha: 0.9)
darkTheme.actionCancelButtonColor = UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)
darkTheme.statusBarStyle = .lightContent
darkTheme.barStyle = .black
darkTheme.blurEffect = .dark
Expand All @@ -46,6 +47,7 @@ class ColorPalett {
var backgroundSecondary = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
var backgroundTertiary = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1)
var tableHeaderColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 0.9)
var actionCancelButtonColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
var statusBarStyle : UIStatusBarStyle = .default
var barStyle : UIBarStyle = .default
var blurEffect : UIBlurEffect.Style = .light
Expand Down
51 changes: 36 additions & 15 deletions iTorrent/Theme/ThemedUIAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,42 @@ import UIKit

class ThemedUIAlertController : UIAlertController, Themed {

private struct AssociatedKeys {
static var blurStyleKey = "UIAlertController.blurStyleKey"
}

public var blurStyle: UIBlurEffectStyle {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.blurStyleKey) as? UIBlurEffectStyle ?? .extraLight
} set (style) {
objc_setAssociatedObject(self, &AssociatedKeys.blurStyleKey, style, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

view.setNeedsLayout()
view.layoutIfNeeded()
}
}

private var visualEffectView: UIVisualEffectView? {
if let presentationController = presentationController, presentationController.responds(to: Selector(("popoverView"))), let view = presentationController.value(forKey: "popoverView") as? UIView // We're on an iPad and visual effect view is in a different place.
{
return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
}

return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
}

open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

visualEffectView?.effect = UIBlurEffect(style: blurStyle)
}

func updateTheme() {
let theme = UserDefaults.standard.integer(forKey: UserDefaultsKeys.themeNum)

self.blurStyle = Themes.shared.theme[theme].blurEffect
if let cancelBackgroundViewType = NSClassFromString("_UIAlertControlleriOSActionSheetCancelBackgroundView") as? UIView.Type {
cancelBackgroundViewType.appearance().subviewsBackgroundColor = Themes.shared.theme[theme].backgroundSecondary
}

for v in view.searchVisualEffectsSubview() {
v.effect = UIBlurEffect(style: Themes.shared.theme[theme].blurEffect)
cancelBackgroundViewType.appearance().subviewsBackgroundColor = Themes.shared.theme[theme].actionCancelButtonColor
}

if let title = title {
Expand Down Expand Up @@ -62,15 +89,9 @@ fileprivate extension UIView {
}

extension UIView {
func searchVisualEffectsSubview() -> [UIVisualEffectView] {
if let visualEffectView = self as? UIVisualEffectView {
return [visualEffectView]
} else {
var list : [UIVisualEffectView] = []
for subview in subviews {
list.append(contentsOf: subview.searchVisualEffectsSubview())
}
return list
}
var recursiveSubviews: [UIView] {
var subviews = self.subviews.compactMap({$0})
subviews.forEach { subviews.append(contentsOf: $0.recursiveSubviews) }
return subviews
}
}
4 changes: 2 additions & 2 deletions iTorrent/Theme/ThemedUITableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ThemedUITableViewController : UITableViewController, Themed {
tableView.reloadData()
}

override func viewDidLoad() {
super.viewDidLoad()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateTheme()
}

Expand Down
1 change: 0 additions & 1 deletion iTorrent/Theme/ThemedUIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ThemedUIViewController : UIViewController, Themed {
func updateTheme() {
let theme = UserDefaults.standard.integer(forKey: UserDefaultsKeys.themeNum)


view.backgroundColor = Themes.shared.theme[theme].backgroundSecondary
navigationController?.navigationBar.barStyle = Themes.shared.theme[theme].barStyle
navigationController?.toolbar.barStyle = Themes.shared.theme[theme].barStyle
Expand Down
2 changes: 1 addition & 1 deletion iTorrent/Version.ver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4
1.4.1
6 changes: 6 additions & 0 deletions iTorrent/ViewControllers/AddTorrentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class AddTorrentController : ThemedUIViewController, UITableViewDataSource, UITa
var showSortMask : [Int] = []

var root : String!

override func updateTheme() {
super.updateTheme()
let theme = UserDefaults.standard.integer(forKey: UserDefaultsKeys.themeNum)
tableView.backgroundColor = Themes.shared.theme[theme].backgroundMain
}

deinit {
print("Add Torrent DEINIT!!")
Expand Down
16 changes: 12 additions & 4 deletions iTorrent/ViewControllers/TorrentFilesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class TorrentFilesController : ThemedUIViewController, UITableViewDataSource, UI
var root : String!

var runUpdate = false

override func updateTheme() {
super.updateTheme()
let theme = UserDefaults.standard.integer(forKey: UserDefaultsKeys.themeNum)
tableView.backgroundColor = Themes.shared.theme[theme].backgroundMain
}

deinit {
print("Files DEINIT!!")
}

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -53,6 +63,8 @@ class TorrentFilesController : ThemedUIViewController, UITableViewDataSource, UI
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

runUpdate = true
DispatchQueue.global(qos: .background).async {
while(self.runUpdate) {
Expand All @@ -79,10 +91,6 @@ class TorrentFilesController : ThemedUIViewController, UITableViewDataSource, UI

runUpdate = false
}

deinit {
print("Files DEINIT!!")
}

func initialize() {
filesContainer.files.removeAll()
Expand Down

0 comments on commit 992a2fa

Please sign in to comment.