hulop / NavCogIOSv3

NavCog version 3 for iOS
MIT License
12 stars 13 forks source link

About Agreement and Download #57

Closed hiroshi-shin closed 2 years ago

hiroshi-shin commented 2 years ago

Situation

This is about the first time running the app.

In current app:

  1. Open the app: SeverList downloaded and displayed
  2. Choose the server: map is downloaded
  3. Choose the mode: Agreement page appears

My customization:

  1. Open the app: download the SeverList and choose the first one
  2. Show the agreement
  3. Download the map
  4. Display the home page

Source Code

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if firstLoad {
        firstLoad = false
        checkConfig()
    }

}

private func checkConfig() {
    let shared = ServerConfig.shared()
    let board = UIStoryboard(name: "Main", bundle: nil)

    if shared.selected == nil {
        shared.requestServerList { [weak self] list in
            guard let _self = self else { return }
            if let _list = list,
               let server = _list.first {
                shared.selected = server
                NavDataStore.shared().selectUserLanguage(server.userLanguage)
                _self.checkConfig()
            }
        }
    }

    if let agreeConfig = shared.agreementConfig,
       let agreed = agreeConfig["agreed"] as? Bool {
        if !agreed {
            let vc = board.instantiateViewController(identifier: "agreement_vc") as! AgreementViewController
            DispatchQueue.main.async {
                self.show(vc, sender: nil)
            }
        }

    } else if let userId = NavDataStore.shared().userID {
        shared.checkAgreement(forIdentifier: userId, withCompletion: { [weak self] _ in
            guard let _self = self else { return }
            _self.checkConfig()
        })
    }

    if let serverConfig = shared.selectedServerConfig {
        let files = shared.checkDownloadFiles()

        if files.count > 0 && !isDownloading {
            DispatchQueue.main.async {
                self.firstLoad = false
                let vc = board.instantiateViewController(identifier: "download_vc") as! DownloadViewController
                self.show(vc, sender: nil)
                self.isDownloading = true
            }
        } else if let mapFiles = shared.downloadConfig?["map_files"] as? [String] {
            let fm = FileManager.default
            let docPath = fm.urls(for: .documentDirectory, in: .userDomainMask).first!

            do {
                for path in mapFiles {
                    let filename = path.split(separator: "/").last!
                    UserDefaults.standard.setValue(filename, forKey: "bleloc_map_data")
                    let toPath = "\(docPath)\(filename)"
                    try fm.removeItem(atPath: toPath)
                    try fm.copyItem(atPath: path, toPath: toPath)
                }

                let presetsDir = "\(docPath)/presets"
                try fm.createDirectory(atPath: presetsDir, withIntermediateDirectories: true, attributes: nil)

                var modes = [(String, String)]()
                shared.enumerateModes {
                    modes += [($0, $1)]
                }

                for (mode, obj) in modes {
                    let path = "\(presetsDir)\(mode).plist"
                    try! fm.removeItem(atPath: path)
                    try fm.copyItem(atPath: obj, toPath: path)
                }

                NotificationCenter.default.post(name: NSNotification.Name("SERVER_CONFIG_CHANGED_NOTIFICATION"),
                                                object: self,
                                                userInfo: serverConfig)
            } catch {
                print(error)
            }

        }
    } else {
        shared.request { [weak self] _ in
            guard let _self = self else { return }
            _self.checkConfig()
        }
    }

}

Problem

  1. agreed is always true -> not showing AgreementViewController at the first run.
  2. DownloadViewController is called many times.
  3. All the fm functions gives error
    • fm.removeItem() and fm.copyItem(): "No such a file or directory"
    • fm.createDirectory(): "permission denied"

I also checked about the path in DownloadViewController. In fact, the application id is different from it is in the function I created.

daisukes commented 2 years ago

agreed is managed on the server side. I don't know your server setting but your account, generated ID for the app, already agreed or the server does not ask for agreement (no agreement setting)

2 can happen if the map data is not correctly downloaded (network, server side setting, etc). It checks if there is a local map file. If not it will try to download it.

It looks like you don't have file sharing option in your info list. UIFileSharingEnabled Please check all the plist flags.

hiroshi-shin commented 2 years ago

Update

I forgot to mention that, when I tried to open BlindViewController or ViewController, it gives a blank page and asked to open the browser.

I don't know your server setting but your account, generated ID for the app, already agreed or the server does not ask for agreement

Thanks for explaining. This seems not like reason.

After removing the part for agreementConfig, problem 2 was solved.

if let agreeConfig = shared.agreementConfig,
   let agreed = agreeConfig["agreed"] as? Bool {
    if !agreed {
        let vc = board.instantiateViewController(identifier: "agreement_vc") as! AgreementViewController
        DispatchQueue.main.async {
            self.show(vc, sender: nil)
        }
    }

} else if let userId = NavDataStore.shared().userID {
    shared.checkAgreement(forIdentifier: userId, withCompletion: { [weak self] _ in
        guard let _self = self else { return }
        _self.checkConfig()
    })
}

It looks like you don't have file sharing option in your info list. UIFileSharingEnabled Please check all the plist flags.

Currently, I'm still using the plist of NavCog3, in which UIFileSharingEnabled is set to YES. In the DownloadViewController, the unwind segue was performed after downloading finished.

Then, for example, here is the destination path for _2dMapDatafin.zip in DownloadViewController:

destLocation NSURL * @"2dMapData_fin.zip -- file:///var/mobile/Containers/Data/Application/0496460B-861C-4224-A047-6420E28A4C1D/Documents/location/" 0x00000002805a0230

and the path for fm.removeItem() in my function

NSFilePath=file:///var/mobile/Containers/Data/Application/0496460B-861C-4224-A047-6420E28A4C1D/Documents/2dMapData_fin.zip

hiroshi-shin commented 2 years ago

Update 2

I also tried another approach:

Original code: https://github.com/hulop/NavCogIOSv3/blob/a25a85834ffb9644025692a69c91c26264a196ac/NavCog3/WelcomViewController.m#L193-L197

My customization:

dispatch_async(dispatch_get_main_queue(), ^{
    NSString *hostname = config.selected.hostname;
    [[NSUserDefaults standardUserDefaults] setObject:hostname forKey:@"selected_hokoukukan_server"];
//                    [self performSegueWithIdentifier:@"show_mode_selection" sender:self];
    TabController *vc = [[TabController alloc] init];
    UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
    window.rootViewController = vc;
});

It's ok for the first launch. However, after reopening the app, it gives such an error after BlindViewController#viewDidLoad function: Screen Shot 2021-10-08 at 18 07 53

I understand it's difficult to figure it out the without the source code. I will ask my team leader if I can show you my implementation.

daisukes commented 2 years ago

It looks like you have a lot of issues. Let's take a look one by one.

I forgot to mention that, when I tried to open BlindViewController or ViewController, it gives a blank page and asked to open the browser.

What does this mean? The app asked you to open Safari? What is the URL? Could you share the screenshots?

Thanks for explaining. This seems not like reason. After removing the part for agreementConfig, problem 2 was solved.

I believe problems 1 and 2 are independent issues. It looks like somehow you could not write the file.

I understand it's difficult to figure it out the without the source code.

Yes, please consider making the source code public, or inviting me to a private repository. Also, even if I can see the code, it is difficult to figure out the issues without a stack trace. Please include the stack trace by typing thread backtrace in the debug view so that we can see where it happened (see the figure)

Your questions are getting off focus from the original NavCog. I think it would be better to manage your issues in your repository. I'm happy to help you so feel free to invite me to your repo.

Thanks and I will close this issue.

Screen Shot 2021-10-08 at 11 12 16 AM