xiongzhu 2 лет назад
Родитель
Сommit
061d1a3c4f
1 измененных файлов с 25 добавлено и 42 удалено
  1. 25 42
      app/src/main/java/com/dar/nbook/async/VersionChecker.java

+ 25 - 42
app/src/main/java/com/dar/nbook/async/VersionChecker.java

@@ -14,6 +14,11 @@ import androidx.core.content.FileProvider;
 
 import com.dar.nbook.BuildConfig;
 import com.dar.nbook.R;
+import com.dar.nbook.api.HttpCallback;
+import com.dar.nbook.api.HttpClient;
+import com.dar.nbook.api.HttpError;
+import com.dar.nbook.api.response.AppConfig;
+import com.dar.nbook.api.response.SysConfigResponse;
 import com.dar.nbook.settings.Global;
 import com.dar.nbook.utility.LogUtility;
 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
@@ -46,35 +51,27 @@ public class VersionChecker {
         }
         String actualVersionName = Global.getVersionName(context);
         LogUtility.d("ACTUAL VERSION: " + actualVersionName);
-        Global.getClient(context).newCall(new Request.Builder().url(RELEASE_API_URL).build()).enqueue(new Callback() {
+
+        HttpClient.request(HttpClient.getApiService().getAppConfig(), new HttpCallback<>() {
             @Override
-            public void onFailure(@NonNull Call call, @NonNull IOException e) {
-                context.runOnUiThread(() -> {
-                    LogUtility.e(e.getLocalizedMessage(), e);
+            public void onSuccess(SysConfigResponse<AppConfig> data) {
+                if (data.getValue().getAppInfo().getVersionCode() > Global.getVersionCode(context)) {
+                    downloadUrl = data.getValue().getAppInfo().getUrl();
+                    context.runOnUiThread(() -> {
+                        createDialog(actualVersionName, data.getValue().getAppInfo().getVersionName());
+                    });
+                } else {
                     if (!silent)
-                        Toast.makeText(context, R.string.error_retrieving, Toast.LENGTH_SHORT).show();
-                });
+                        Toast.makeText(context, R.string.no_updates_found, Toast.LENGTH_SHORT).show();
+                }
             }
 
             @Override
-            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
-                JsonReader jr = new JsonReader(response.body().charStream());
-                GitHubRelease release = parseVersionJson(jr, withPrerelease);
-                jr.close();
-                if (release == null) {
-                    release = new GitHubRelease();
-                    release.versionCode = actualVersionName;
-                }
-                downloadUrl = release.downloadUrl;
-                GitHubRelease finalRelease = release;
+            public void onFailure(HttpError<SysConfigResponse<AppConfig>> error) {
                 context.runOnUiThread(() -> {
-                    if (downloadUrl == null || extractVersion(actualVersionName) >= extractVersion(finalRelease.versionCode)) {
-                        if (!silent)
-                            Toast.makeText(context, R.string.no_updates_found, Toast.LENGTH_SHORT).show();
-                    } else {
-                        LogUtility.d("Executing false");
-                        createDialog(actualVersionName, finalRelease);
-                    }
+                    LogUtility.e(error.getMessage());
+                    if (!silent)
+                        Toast.makeText(context, R.string.error_retrieving, Toast.LENGTH_SHORT).show();
                 });
             }
         });
@@ -153,39 +150,25 @@ public class VersionChecker {
         return url;
     }
 
-    private void createDialog(String versionName, GitHubRelease release) {
-        String finalBody = release.body;
-        String latestVersion = release.versionCode;
-        boolean beta = release.beta;
-        if (finalBody == null) return;
-        finalBody = finalBody
-            .replace("\r\n", "\n")//Remove ugly newline
-            .replace("NClientV2 " + latestVersion, "")//remove version header
-            .replaceAll("(\\s*\n\\s*)+", "\n")//remove multiple newline
-            .replaceAll("\\(.*\\)", "").trim();//remove things between ()
-        LogUtility.d("Evaluated: " + finalBody);
+    private void createDialog(String versionName, String latestVersion) {
         LogUtility.d("Creating dialog");
         MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
         LogUtility.d("" + context);
-        builder.setTitle(beta ? R.string.new_beta_version_found : R.string.new_version_found);
+        builder.setTitle(R.string.new_version_found);
         builder.setIcon(R.drawable.ic_file);
-        builder.setMessage(context.getString(R.string.update_version_format, versionName, latestVersion, finalBody));
+        builder.setMessage(context.getString(R.string.update_version_format, versionName, latestVersion));
         builder.setPositiveButton(R.string.install, (dialog, which) -> {
             if (Global.hasStoragePermission(context)) downloadVersion(latestVersion);
             else {
                 latest = latestVersion;
                 context.runOnUiThread(() -> context.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 2));
             }
-        }).setNegativeButton(R.string.cancel, null)
-            .setNeutralButton(R.string.github, (dialog, which) -> {
-                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(LATEST_RELEASE_URL));
-                context.startActivity(browserIntent);
-            });
+        }).setNegativeButton(R.string.cancel, null);
         if (!context.isFinishing()) builder.show();
     }
 
     private void downloadVersion(String latestVersion) {
-        final File f = new File(Global.UPDATEFOLDER, "NClientV2_" + latestVersion + ".apk");
+        final File f = new File(Global.UPDATEFOLDER, "nbook_" + latestVersion + ".apk");
         if (f.exists()) {
             if (context.getSharedPreferences("Settings", 0).getBoolean("downloaded", false)) {
                 installApp(f);