-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/org/ranobe/ranobe/network/repository/GithubRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.ranobe.ranobe.network.repository; | ||
|
||
import org.json.JSONObject; | ||
import org.ranobe.ranobe.BuildConfig; | ||
import org.ranobe.ranobe.network.HttpClient; | ||
import org.ranobe.ranobe.util.Version; | ||
|
||
import java.util.HashMap; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
|
||
public class GithubRepo { | ||
private static final String API_ENDPOINT = "https://api.github.com/repos/ranobe-org/ranobe/releases/latest"; | ||
private final HashMap<String, String> HEADERS = new HashMap<String, String>() {{ | ||
put("Accept", "application/vnd.github+json"); | ||
}}; | ||
private final Executor executor; | ||
|
||
public GithubRepo(){ | ||
this.executor = Executors.newCachedThreadPool(); | ||
} | ||
|
||
public interface Callback<T> { | ||
void onComplete(T result); | ||
|
||
void onError(Exception e); | ||
} | ||
|
||
public void getLatestRelease(Callback<GithubRelease> callback) { | ||
this.executor.execute(() -> { | ||
try { | ||
GithubRelease release = fetchLatestRelease(); | ||
callback.onComplete(release); | ||
} catch (Exception e) { | ||
callback.onError(e); | ||
} | ||
}); | ||
} | ||
|
||
private GithubRelease fetchLatestRelease() throws Exception { | ||
String json = HttpClient.GET(API_ENDPOINT, HEADERS); | ||
JSONObject response = new JSONObject(json); | ||
|
||
String tag = response.getString("tag_name"); | ||
Version latestVersion = new Version(Version.extractVersionNumber(tag)); | ||
Version currentVersion = new Version(Version.extractVersionNumber(BuildConfig.VERSION_NAME)); | ||
|
||
if(latestVersion.get().equals(currentVersion.get())) { | ||
return new GithubRelease(false, currentVersion.get(), null); | ||
} | ||
|
||
if(latestVersion.compareTo(currentVersion) > 0) { | ||
String url = response.getString("html_url"); | ||
return new GithubRelease(true, tag, url); | ||
} | ||
|
||
return new GithubRelease(false, currentVersion.get(), null); | ||
} | ||
|
||
public static class GithubRelease { | ||
public boolean updateAvailable; | ||
public String newReleaseVersion; | ||
public String newReleaseUrl; | ||
|
||
public GithubRelease(boolean updateAvailable, String newReleaseVersion, String newReleaseUrl) { | ||
this.updateAvailable = updateAvailable; | ||
this.newReleaseVersion = newReleaseVersion; | ||
this.newReleaseUrl = newReleaseUrl; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/main/java/org/ranobe/ranobe/ui/settings/viewmodel/SettingsViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.ranobe.ranobe.ui.settings.viewmodel; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
import org.ranobe.ranobe.network.repository.GithubRepo; | ||
|
||
public class SettingsViewModel extends ViewModel { | ||
private MutableLiveData<GithubRepo.GithubRelease> update; | ||
|
||
public MutableLiveData<GithubRepo.GithubRelease> getUpdate() { | ||
if (update == null) { | ||
update = new MutableLiveData<>(); | ||
} | ||
return update; | ||
} | ||
|
||
public void checkForUpdate() { | ||
new GithubRepo().getLatestRelease(new GithubRepo.Callback<GithubRepo.GithubRelease>() { | ||
@Override | ||
public void onComplete(GithubRepo.GithubRelease result) { | ||
update.postValue(result); | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.ranobe.ranobe.util; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class Version implements Comparable<Version> { | ||
private final String version; | ||
|
||
public final String get() { | ||
return this.version; | ||
} | ||
|
||
public Version(String version) { | ||
if(version == null) | ||
throw new IllegalArgumentException("Version can not be null"); | ||
if(!version.matches("[0-9]+(\\.[0-9]+)*")) | ||
throw new IllegalArgumentException("Invalid version format"); | ||
this.version = version; | ||
} | ||
|
||
public static String extractVersionNumber(String input) { | ||
Pattern regex = Pattern.compile("\\d+\\.\\d+\\.\\d+", Pattern.MULTILINE); // x.x.x | ||
Matcher match = regex.matcher(input); | ||
return match.find() ? match.group() : null; | ||
} | ||
|
||
@Override public int compareTo(Version that) { | ||
if(that == null) | ||
return 1; | ||
String[] thisParts = this.get().split("\\."); | ||
String[] thatParts = that.get().split("\\."); | ||
int length = Math.max(thisParts.length, thatParts.length); | ||
for(int i = 0; i < length; i++) { | ||
int thisPart = i < thisParts.length ? | ||
Integer.parseInt(thisParts[i]) : 0; | ||
int thatPart = i < thatParts.length ? | ||
Integer.parseInt(thatParts[i]) : 0; | ||
if(thisPart < thatPart) | ||
return -1; | ||
if(thisPart > thatPart) | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters