In the button listener, I launch a stream in which I upload files. After starting the flow, the button text changes from “Download” to “Read”. I didn’t figure out how to do this after the thread / load is complete. Therefore, I put the launch of ProgressDialog on the event - so that the user waited until the download was completed. Otherwise, it receives an error, because the files have not yet been downloaded. Please tell me how to remove ProgressDialog exactly after the download is complete? I load third-party library Fetch
Here is the code for the listener:
buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Thread mThread = new Thread(new Runnable() { @Override public void run() { String fileListB = "list_" + "book_" + bookId + ".json"; String jsReadFile = MyJSON.getData(getApplicationContext(), fileListB); Log.d(TAG, jsReadFile); Gson gson = new Gson(); Book book = gson.fromJson(jsReadFile, Book.class); List<String> pages = book.getPageUrl(); List<String> sounds = book.getSoundUrl(); String[] urlsPages = pages.toArray(new String[0]); String[] urlsSounds = sounds.toArray(new String[0]); String[] urlsFiles = ArrayAndArrayNewArray(urlsPages, urlsSounds); DownloadFilesBook(urlsFiles); } }); mThread.start(); // запустили поток 2 ProgressDialog progressDialog = new ProgressDialog(BookCardActivity.this); progressDialog.setMessage(getString(R.string.progressDialogText)); progressDialog.show(); buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); } }); } else { buttonDownload.setText(R.string.buttonRead); buttonDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NextActivity(); } }); } Here is the code for the file upload method.
private void downloadFilesBook(String[] urlsFiles) { mFetch = Fetch.newInstance(this); String folderB = "bookfiles_" + bookId; String fileNameForWrite = "book_" + bookId + ".json"; File bookfolder = new File(String.valueOf(getExternalFilesDir(folderB))); ArrayList<String> pagesFiles = new ArrayList<>(); for (int i = 0; i < urlsFiles.length; i++) { String url = urlsFiles[i]; String path = String.valueOf(bookfolder); String fileName = Uri.parse(url).getLastPathSegment(); Log.d("my2", fileName); Request request = new Request(url, path, fileName); String pageFilePath = path + "/" + fileName; Log.d("my2", pageFilePath); pagesFiles.add(pageFilePath); downloadId = mFetch.enqueue(request); } BookFiles bookFiles = new BookFiles(); bookFiles.setBookID(bookId); ArrayList<String> pagesPath = getPagesArray(pagesFiles); ArrayList<String> soundsPath = getSoundsArray(pagesFiles); bookFiles.setPagesPath(pagesPath); bookFiles.setSoundsPath(soundsPath); Gson gson11 = new Gson(); String filesJson = gson11.toJson(bookFiles); MyJSON.saveData(getApplicationContext(), filesJson, fileNameForWrite); }