forked from rafaelvcaetano/melonDS-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows users to have nightly builds alongside the production builds
- Loading branch information
1 parent
439094a
commit c03c02f
Showing
37 changed files
with
313 additions
and
58 deletions.
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
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
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
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
5 changes: 5 additions & 0 deletions
5
app/src/gitHub/java/me/magnum/melonds/github/GitHubConstants.kt
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,5 @@ | ||
package me.magnum.melonds.github | ||
|
||
const val APK_CONTENT_TYPE = "application/vnd.android.package-archive" | ||
|
||
const val PREF_KEY_GITHUB_CHECK_FOR_UPDATES = "github_check_for_updates" |
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
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,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<PreferenceScreen | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<SwitchPreference | ||
android:key="github_check_for_updates" | ||
android:title="@string/check_for_updates" | ||
android:summary="@string/check_for_updates_summary" | ||
app:iconSpaceReserved="false" | ||
android:defaultValue="true" /> | ||
</PreferenceScreen> |
24 changes: 24 additions & 0 deletions
24
app/src/gitHubNightly/java/me/magnum/melonds/di/GitHubNightlyModule.kt
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,24 @@ | ||
package me.magnum.melonds.di | ||
|
||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import me.magnum.melonds.domain.repositories.UpdatesRepository | ||
import me.magnum.melonds.github.GitHubApi | ||
import me.magnum.melonds.github.repositories.GitHubNightlyUpdatesRepository | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object GitHubNightlyModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideUpdatesRepository(@ApplicationContext context: Context, gitHubApi: GitHubApi): UpdatesRepository { | ||
val gitHubPreferences = context.getSharedPreferences("preferences-github", Context.MODE_PRIVATE) | ||
return GitHubNightlyUpdatesRepository(gitHubApi, gitHubPreferences) | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...itHubNightly/java/me/magnum/melonds/github/repositories/GitHubNightlyUpdatesRepository.kt
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,115 @@ | ||
package me.magnum.melonds.github.repositories | ||
|
||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import androidx.core.net.toUri | ||
import io.reactivex.Maybe | ||
import io.reactivex.Single | ||
import me.magnum.melonds.domain.model.Version | ||
import me.magnum.melonds.domain.model.appupdate.AppUpdate | ||
import me.magnum.melonds.domain.repositories.UpdatesRepository | ||
import me.magnum.melonds.github.APK_CONTENT_TYPE | ||
import me.magnum.melonds.github.GitHubApi | ||
import me.magnum.melonds.github.PREF_KEY_GITHUB_CHECK_FOR_UPDATES | ||
import me.magnum.melonds.github.dtos.ReleaseDto | ||
import java.time.Duration | ||
import java.time.Instant | ||
|
||
class GitHubNightlyUpdatesRepository(private val api: GitHubApi, private val preferences: SharedPreferences) : UpdatesRepository { | ||
companion object { | ||
private const val KEY_NEXT_CHECK_DATE = "github_updates_nightly_next_check_date" | ||
private const val KEY_LAST_RELEASE_DATE = "github_updates_nightly_last_release_date" | ||
} | ||
|
||
override fun checkNewUpdate(): Maybe<AppUpdate> { | ||
return shouldCheckUpdates() | ||
.flatMapMaybe { checkUpdates -> | ||
if (checkUpdates) { | ||
api.getLatestNightlyRelease().flatMapMaybe { release -> | ||
if (shouldUpdate(release)) { | ||
val apkBinary = release.assets.firstOrNull { it.contentType == APK_CONTENT_TYPE } | ||
if (apkBinary != null) { | ||
val update = AppUpdate( | ||
AppUpdate.Type.NIGHTLY, | ||
apkBinary.id, | ||
apkBinary.url.toUri(), | ||
Version.fromString(release.tagName), | ||
release.body, | ||
apkBinary.size, | ||
Instant.parse(release.createdAt), | ||
) | ||
Maybe.just(update) | ||
} else { | ||
Maybe.empty() | ||
} | ||
} else { | ||
Maybe.empty() | ||
} | ||
} | ||
} else { | ||
Maybe.empty() | ||
} | ||
} | ||
} | ||
|
||
override fun skipUpdate(update: AppUpdate) { | ||
scheduleNextUpdate() | ||
} | ||
|
||
override fun notifyUpdateDownloaded(update: AppUpdate) { | ||
// This doesn't mean that the user has actually installed the update, but we have no way to determine that. As such, just assume that the update will be installed and | ||
// store the date of the update | ||
preferences.edit { | ||
putLong(KEY_LAST_RELEASE_DATE, update.updateDate.toEpochMilli()) | ||
} | ||
} | ||
|
||
private fun shouldCheckUpdates(): Single<Boolean> { | ||
return Single.create { emitter -> | ||
val updateCheckEnabled = preferences.getBoolean(PREF_KEY_GITHUB_CHECK_FOR_UPDATES, true) | ||
if (!updateCheckEnabled) { | ||
emitter.onSuccess(false) | ||
return@create | ||
} | ||
|
||
val nextUpdateCheckTime = preferences.getLong(KEY_NEXT_CHECK_DATE, -1) | ||
if (nextUpdateCheckTime == (-1).toLong()) { | ||
emitter.onSuccess(true) | ||
return@create | ||
} | ||
|
||
val now = Instant.now() | ||
val shouldCheckUpdates = now.toEpochMilli() > nextUpdateCheckTime | ||
|
||
emitter.onSuccess(shouldCheckUpdates) | ||
} | ||
} | ||
|
||
private fun scheduleNextUpdate() { | ||
val now = Instant.now() | ||
val nextUpdateDate = now + Duration.ofDays(1) | ||
|
||
preferences.edit { | ||
putLong(KEY_NEXT_CHECK_DATE, nextUpdateDate.toEpochMilli()) | ||
} | ||
} | ||
|
||
private fun shouldUpdate(releaseDto: ReleaseDto): Boolean { | ||
val lastReleaseDate = preferences.getLong(KEY_LAST_RELEASE_DATE, -1) | ||
if (lastReleaseDate == -1L) { | ||
// If there is no last release date, then it's the first time the user is running the app and checking for updates. Ignore this release since we can't check if | ||
// it's actually different from the one the user has installed, and save the release date in the preferences so that we can have a future reference | ||
|
||
val releaseDate = Instant.parse(releaseDto.createdAt) | ||
preferences.edit { | ||
putLong(KEY_LAST_RELEASE_DATE, releaseDate.toEpochMilli()) | ||
} | ||
scheduleNextUpdate() | ||
return false | ||
} | ||
|
||
val thisReleaseDate = Instant.parse(releaseDto.createdAt) | ||
|
||
return thisReleaseDate.toEpochMilli() > lastReleaseDate | ||
} | ||
} |
Oops, something went wrong.