| 38 | } |
| 39 | |
| 40 | UIDownloadWindow* UIDownloadWindow::startDownload( const std::string& url ) { |
| 41 | findByClass( "download_url" )->asType<UITextView>()->setText( url ); |
| 42 | URI uri( url ); |
| 43 | mURI = uri; |
| 44 | mProxyURI = Http::getEnvProxyURI(); |
| 45 | mStatus = Status::Running; |
| 46 | auto id = getId(); |
| 47 | mReqId = Http::getAsync( |
| 48 | [uri, id]( const Http&, Http::Request&, Http::Response& response ) { |
| 49 | std::string path( URI::getTempPathFromURI( uri ) ); |
| 50 | FileSystem::fileWrite( path, (const Uint8*)response.getBody().c_str(), |
| 51 | response.getBody().size() ); |
| 52 | if ( App::instance() && !App::instance()->isDestroyingApp() ) { |
| 53 | App::instance()->getUISceneNode()->runOnMainThread( [path] { |
| 54 | if ( App::instance() && !App::instance()->isDestroyingApp() ) |
| 55 | App::instance()->loadFileFromPath( path, true, nullptr, nullptr, false, |
| 56 | true ); |
| 57 | } ); |
| 58 | } |
| 59 | |
| 60 | if ( SceneManager::isActive() ) { |
| 61 | auto nodeWin = SceneManager::instance()->getUISceneNode()->getRoot()->find( id ); |
| 62 | if ( nodeWin ) { |
| 63 | auto me = nodeWin->asType<UIDownloadWindow>(); |
| 64 | me->mStatus = Status::Completed; |
| 65 | me->closeWindow(); |
| 66 | } |
| 67 | } |
| 68 | }, |
| 69 | url, Seconds( 10 ), |
| 70 | [id]( const Http& http, const Http::Request& request, const Http::Response& response, |
| 71 | const Http::Request::Status& status, std::size_t totalBytes, |
| 72 | std::size_t currentBytes ) { |
| 73 | if ( SceneManager::isActive() ) { |
| 74 | auto nodeWin = SceneManager::instance()->getUISceneNode()->getRoot()->find( id ); |
| 75 | if ( nodeWin ) { |
| 76 | auto me = nodeWin->asType<UIDownloadWindow>(); |
| 77 | auto progress = me->findByType<UIProgressBar>( UI_TYPE_PROGRESSBAR ); |
| 78 | if ( status == Http::Request::Status::HeaderReceived || |
| 79 | status == Http::Request::Status::ContentReceived ) { |
| 80 | progress->runOnMainThread( [progress, currentBytes, totalBytes] { |
| 81 | progress->setProgress( |
| 82 | totalBytes > 0 |
| 83 | ? ( static_cast<Float>( currentBytes ) / totalBytes ) * 100.f |
| 84 | : 50 ); |
| 85 | } ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | }, |
| 91 | {}, "", true, mProxyURI ); |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | } // namespace ecode |
no test coverage detected