Add support for cumulative changelogs

This commit is contained in:
moezbhatti
2019-08-30 20:05:38 +02:00
parent f92afde645
commit 3200956f4f
10 changed files with 156 additions and 52 deletions
+1
View File
@@ -78,6 +78,7 @@ dependencies {
testImplementation "org.mockito:mockito-core:$mockito_version"
implementation "com.f2prateek.rx.preferences2:rx-preferences:$rx_preferences_version"
implementation 'com.google.code.gson:gson:2.8.5'
implementation "com.jakewharton.timber:timber:$timber_version"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
@@ -19,9 +19,19 @@
package com.moez.QKSMS.manager
import android.content.Context
import com.moez.QKSMS.model.SyncLog
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.moez.QKSMS.util.Preferences
import io.realm.Realm
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Call
import okhttp3.Callback
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import javax.inject.Inject
class ChangelogManagerImpl @Inject constructor(
@@ -29,21 +39,89 @@ class ChangelogManagerImpl @Inject constructor(
private val prefs: Preferences
) : ChangelogManager {
override fun didUpdate(): Boolean {
val oldVersion = prefs.version.get()
val newVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionCode
val lastSync = Realm.getDefaultInstance().use { realm -> realm.where(SyncLog::class.java)?.max("date") ?: 0 }
private val oldVersion: Int get() = prefs.version.get()
private val versionCode: Int get() = context.packageManager.getPackageInfo(context.packageName, 0).versionCode
if (oldVersion != newVersion) {
prefs.version.set(newVersion)
override fun didUpdate(): Boolean {
if (oldVersion == 0) {
prefs.version.set(versionCode)
}
return when {
oldVersion == 0 && lastSync != 0 -> true // Just updated to 3.6.5. TODO: Remove this after 3.6.5
oldVersion == 0 -> false // First install
oldVersion != newVersion -> true // Update
oldVersion != versionCode -> true
else -> false
}
}
override fun getChangelog(): Single<ChangelogManager.Changelog> {
val url = "https://firestore.googleapis.com/v1/projects/qksms-app/databases/(default)/documents/changelog"
val httpUrl = HttpUrl.parse(url)
val request = Request.Builder().url(httpUrl).build()
val call = OkHttpClient().newCall(request)
return Single
.create<Response> { emitter ->
emitter.setCancellable { call.cancel() }
call.enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) = emitter.onSuccess(response)
override fun onFailure(call: Call, e: IOException) = emitter.onError(e)
})
}
.map { response -> Gson().fromJson(response.body()?.string(), ChangelogResponse::class.java) }
.map { response ->
response.documents
.sortedBy { document -> document.fields.versionCode.value }
.filter { document -> document.fields.versionCode.value.toInt() in (oldVersion + 1)..versionCode }
}
.map { documents ->
val added = documents.fold(listOf<String>()) { acc, document ->
acc + document.fields.added?.value?.values?.map { value -> value.value }.orEmpty()
}
val improved = documents.fold(listOf<String>()) { acc, document ->
acc + document.fields.improved?.value?.values?.map { value -> value.value }.orEmpty()
}
val fixed = documents.fold(listOf<String>()) { acc, document ->
acc + document.fields.fixed?.value?.values?.map { value -> value.value }.orEmpty()
}
ChangelogManager.Changelog(added, improved, fixed)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
override fun markChangelogSeen() {
prefs.version.set(versionCode)
}
private data class ChangelogResponse(
@SerializedName("documents") val documents: List<Document>
)
private data class Document(
@SerializedName("fields") val fields: Changelog
)
private data class Changelog(
@SerializedName("added") val added: ArrayField?,
@SerializedName("improved") val improved: ArrayField?,
@SerializedName("fixed") val fixed: ArrayField?,
@SerializedName("versionName") val versionName: StringField,
@SerializedName("versionCode") val versionCode: IntegerField
)
private data class ArrayField(
@SerializedName("arrayValue") val value: ArrayValues
)
private data class ArrayValues(
@SerializedName("values") val values: List<StringField>
)
private data class StringField(
@SerializedName("stringValue") val value: String
)
private data class IntegerField(
@SerializedName("integerValue") val value: String
)
}
@@ -18,11 +18,23 @@
*/
package com.moez.QKSMS.manager
import io.reactivex.Single
interface ChangelogManager {
data class Changelog(
val added: List<String>,
val improved: List<String>,
val fixed: List<String>
)
/**
* Returns true if the app has benn updated since the last time this method was called
*/
fun didUpdate(): Boolean
fun getChangelog(): Single<Changelog>
fun markChangelogSeen()
}
@@ -18,18 +18,38 @@
*/
package com.moez.QKSMS.feature.changelog
import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.manager.ChangelogManager
import kotlinx.android.synthetic.main.changelog_list_item.*
class ChangelogAdapter(val keywords: List<String>, data: List<String>) : QkAdapter<String>() {
class ChangelogAdapter(private val context: Context) : QkAdapter<ChangelogAdapter.ChangelogItem>() {
init {
this.data = data
data class ChangelogItem(val type: Int, val label: String)
fun setChangelog(changelog: ChangelogManager.Changelog) {
val changes = mutableListOf<ChangelogItem>()
if (changelog.added.isNotEmpty()) {
changes += ChangelogItem(0, context.getString(R.string.changelog_added))
changes += changelog.added.map { change -> ChangelogItem(1, change) }
changes += ChangelogItem(0, "")
}
if (changelog.improved.isNotEmpty()) {
changes += ChangelogItem(0, context.getString(R.string.changelog_improved))
changes += changelog.improved.map { change -> ChangelogItem(1, change) }
changes += ChangelogItem(0, "")
}
if (changelog.fixed.isNotEmpty()) {
changes += ChangelogItem(0, context.getString(R.string.changelog_fixed))
changes += changelog.fixed.map { change -> ChangelogItem(1, change) }
changes += ChangelogItem(0, "")
}
data = changes
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
@@ -44,11 +64,11 @@ class ChangelogAdapter(val keywords: List<String>, data: List<String>) : QkAdapt
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val item = getItem(position)
holder.changelogItem.text = item
holder.changelogItem.text = item.label
}
override fun getItemViewType(position: Int): Int {
return if (getItem(position) in keywords) 0 else 1
return getItem(position).type
}
}
@@ -23,6 +23,7 @@ import androidx.appcompat.app.AlertDialog
import com.moez.QKSMS.BuildConfig
import com.moez.QKSMS.R
import com.moez.QKSMS.feature.main.MainActivity
import com.moez.QKSMS.manager.ChangelogManager
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.changelog_dialog.view.*
@@ -32,9 +33,7 @@ class ChangelogDialog(activity: MainActivity) {
val moreClicks: Subject<Unit> = PublishSubject.create()
private val dialog: AlertDialog
private val adapter = ChangelogAdapter(
activity.resources.getStringArray(R.array.changelog_keywords).asList(),
activity.resources.getStringArray(R.array.changelog).asList())
private val adapter = ChangelogAdapter(activity)
init {
val layout = LayoutInflater.from(activity).inflate(R.layout.changelog_dialog, null)
@@ -50,6 +49,9 @@ class ChangelogDialog(activity: MainActivity) {
layout.dismiss.setOnClickListener { dialog.dismiss() }
}
fun show() = dialog.show()
fun show(changelog: ChangelogManager.Changelog) {
adapter.setChangelog(changelog)
dialog.show()
}
}
@@ -53,6 +53,7 @@ import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.feature.changelog.ChangelogDialog
import com.moez.QKSMS.feature.conversations.ConversationItemTouchCallback
import com.moez.QKSMS.feature.conversations.ConversationsAdapter
import com.moez.QKSMS.manager.ChangelogManager
import com.moez.QKSMS.repository.SyncRepository
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
@@ -106,7 +107,7 @@ class MainActivity : QkThemedActivity(), MainView {
override val conversationsSelectedIntent by lazy { conversationsAdapter.selectionChanges }
override val confirmDeleteIntent: Subject<List<Long>> = PublishSubject.create()
override val swipeConversationIntent by lazy { itemTouchCallback.swipes }
override val changelogMoreIntent by lazy { changelog.moreClicks }
override val changelogMoreIntent by lazy { changelogDialog.moreClicks }
override val undoArchiveIntent: Subject<Unit> = PublishSubject.create()
override val snackbarButtonIntent: Subject<Unit> = PublishSubject.create()
@@ -114,7 +115,7 @@ class MainActivity : QkThemedActivity(), MainView {
private val toggle by lazy { ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.main_drawer_open_cd, 0) }
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
private val progressAnimator by lazy { ObjectAnimator.ofInt(syncingProgress, "progress", 0, 0) }
private val changelog by lazy { ChangelogDialog(this) }
private val changelogDialog by lazy { ChangelogDialog(this) }
private val snackbar by lazy { findViewById<View>(R.id.snackbar) }
private val syncing by lazy { findViewById<View>(R.id.syncing) }
private val backPressedSubject: Subject<NavItem> = PublishSubject.create()
@@ -342,8 +343,8 @@ class MainActivity : QkThemedActivity(), MainView {
.show()
}
override fun showChangelog() {
changelog.show()
override fun showChangelog(changelog: ChangelogManager.Changelog) {
changelogDialog.show(changelog)
}
override fun showArchivedSnackbar() {
@@ -19,6 +19,7 @@
package com.moez.QKSMS.feature.main
import com.moez.QKSMS.common.base.QkView
import com.moez.QKSMS.manager.ChangelogManager
import io.reactivex.Observable
interface MainView : QkView<MainState> {
@@ -44,7 +45,7 @@ interface MainView : QkView<MainState> {
fun clearSearch()
fun clearSelection()
fun showDeleteDialog(conversations: List<Long>)
fun showChangelog()
fun showChangelog(changelog: ChangelogManager.Changelog)
fun showArchivedSnackbar()
}
@@ -50,6 +50,7 @@ import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import io.realm.Realm
import timber.log.Timber
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
@@ -145,8 +146,17 @@ class MainViewModel @Inject constructor(
.subscribe { syncMessages.execute(Unit) }
// Show changelog
if (changelogManager.didUpdate() && Locale.getDefault().language.startsWith("en")) {
view.showChangelog()
if (changelogManager.didUpdate()) {
if (Locale.getDefault().language.startsWith("en")) {
disposables += changelogManager.getChangelog()
.timeout(3, TimeUnit.SECONDS) // If it takes long than 3s, we'll just try again next time
.subscribe({ changelog ->
changelogManager.markChangelogSeen()
view.showChangelog(changelog)
}, Timber::w)
} else {
changelogManager.markChangelogSeen()
}
}
view.changelogMoreIntent
@@ -58,6 +58,7 @@
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:overScrollMode="ifContentScrolls"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/dismiss"
@@ -32,31 +32,9 @@
<string name="changelog_title">New in QKSMS</string>
<string name="changelog_version">Version %s</string>
<string-array name="changelog_keywords">
<item>Added</item>
<item>Improved</item>
<item>Fixed</item>
</string-array>
<string-array name="changelog">
<item>Added</item>
<item>- Signature support</item>
<item>- System dark mode support for Android Q</item>
<item></item>
<item>Improved</item>
<item>- You\'ll be asked for storage permission before trying to save a photo if necessary</item>
<item>- Smaller download size</item>
<item>- Load high resolution contact photos</item>
<item></item>
<item>Fixed</item>
<item>- Contact photos never update</item>
<item>- Can\'t create new conversation on Huawei devices</item>
<item>- Can\'t receive MMS on Xiaomi devices</item>
<item>- Visual glitch on compose bar in night mode</item>
<item>- Theme doesn\'t update in preview after going back to settings</item>
<item>- Search hint gets cut off after rotating screen</item>
<item>- Messages can\'t be sent or received on certain older devices</item>
<item>- Norwegian translations not working</item>
</string-array>
<string name="changelog_added">Added</string>
<string name="changelog_improved">Improved</string>
<string name="changelog_fixed">Fixed</string>
<string name="changelog_more">More</string>
<string name="changelog_dismiss">Dismiss</string>