Skip to content

Commit

Permalink
added app update notifier back
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-atul committed May 30, 2023
1 parent 0ad1a42 commit 3908389
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
- [x] reader text formatting with font size and bg/text color selection
- [x] continuous vertical scroll for reader
- [x] add error text/art to denote no results found in search
- [x] update app when a new source is selected
- [x] add settings page with theme selection
- [x] new app update notifier
- [x] sources list selection
- [x] add personal source
- [x] remembering source selection (only one source at a time, that's the rule)
Expand Down
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;
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/org/ranobe/ranobe/ui/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import org.ranobe.ranobe.R;
import org.ranobe.ranobe.config.Ranobe;
import org.ranobe.ranobe.databinding.FragmentSettingsBinding;
import org.ranobe.ranobe.network.repository.GithubRepo;
import org.ranobe.ranobe.ui.settings.viewmodel.SettingsViewModel;

public class Settings extends Fragment {

private FragmentSettingsBinding binding;
private SettingsViewModel viewModel;

public Settings() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = new ViewModelProvider(requireActivity()).get(SettingsViewModel.class);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -38,9 +48,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,

setCurrentThemeMode();

viewModel.getUpdate().observe(requireActivity(), this::release);
viewModel.checkForUpdate();

return binding.getRoot();
}

private void release(GithubRepo.GithubRelease release) {
if (release.updateAvailable) {
binding.updateCard.setVisibility(View.VISIBLE);
binding.versionString.setText(release.newReleaseVersion);
binding.getUpdate.setOnClickListener(v -> openLink(release.newReleaseUrl));
}
}

private void setCurrentThemeMode() {
int mode = Ranobe.getThemeMode(requireActivity().getApplicationContext());
if (mode == AppCompatDelegate.MODE_NIGHT_NO)
Expand Down
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();
}
});
}
}
46 changes: 46 additions & 0 deletions app/src/main/java/org/ranobe/ranobe/util/Version.java
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;
}

}
35 changes: 35 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@
android:orientation="vertical"
android:padding="10dp">

<com.google.android.material.card.MaterialCardView
android:id="@+id/update_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/new_version_available"
android:textColor="?colorOnSurface"
android:textSize="@dimen/text_med"/>

<TextView
android:id="@+id/version_string"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/get_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/update"
android:layout_gravity="end"/>

</LinearLayout>

</com.google.android.material.card.MaterialCardView>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
<string name="mp_lite_subtitle">an open source music player from same dev</string>
<string name="add_to_lib_info">Add to library</string>
<string name="no_novels_error">There are no novels in your library. Go to Explore and find some!</string>
<string name="new_version_available">A new version of the app is available</string>
<string name="update">Update</string>

</resources>

0 comments on commit 3908389

Please sign in to comment.