Skip to content

Commit

Permalink
Target SDK 33.
Browse files Browse the repository at this point in the history
- Add notification settings.
  • Loading branch information
zjn0505 committed Aug 25, 2023
1 parent 6b9588d commit b155946
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 30 deletions.
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#Fri Jul 07 00:04:56 CST 2017
org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#org.gradle.jvmargs=-Xmx1536m
android.enableD8=true
android.useAndroidX=true
android.enableJetifier=true
android.debug.obsoleteApi=true
Expand Down
4 changes: 2 additions & 2 deletions xkcd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ android {
defaultConfig {
applicationId "xyz.jienan.xkcd"
minSdkVersion 16
targetSdkVersion 32
targetSdkVersion 33
versionCode 76
versionName "2.8.2"
vectorDrawables.useSupportLibrary = true
Expand Down Expand Up @@ -218,7 +218,7 @@ dependencies {

implementation 'androidx.work:work-runtime-ktx:2.8.0-beta02'
implementation 'androidx.work:work-rxjava2:2.8.0-beta02'
proprietaryImplementation 'com.google.firebase:firebase-analytics:21.2.0'
proprietaryImplementation 'com.google.firebase:firebase-analytics:19.0.2'
//noinspection GradleDependency
proprietaryImplementation('com.google.firebase:firebase-messaging:22.0.0') {
// 23.0.0 will require API level 19, Android 4.4 higher.
Expand Down
1 change: 1 addition & 0 deletions xkcd/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name=".XkcdApplication"
Expand Down
2 changes: 2 additions & 0 deletions xkcd/src/main/java/xyz/jienan/xkcd/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Const {

public static final String PREF_ARROW = "pref_arrow";
public static final String PREF_RANDOM = "pref_random";

public static final String PREF_NOTIFICATION = "pref_notification";
public static final String PREF_FONT = "pref_font";
public static final String PREF_ZOOM = "pref_zoom";
public static final String PREF_WHAT_IF_SEARCH = "pref_what_if_search";
Expand Down
11 changes: 10 additions & 1 deletion xkcd/src/main/java/xyz/jienan/xkcd/base/NotificationWorker.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package xyz.jienan.xkcd.base

import android.content.Context
import androidx.core.app.NotificationManagerCompat
import androidx.preference.PreferenceManager
import androidx.work.RxWorker
import androidx.work.WorkerParameters
import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import xyz.jienan.xkcd.Const
import xyz.jienan.xkcd.base.network.NetworkService
import xyz.jienan.xkcd.model.XkcdPic
import xyz.jienan.xkcd.model.persist.BoxManager
Expand All @@ -32,7 +35,13 @@ class NotificationWorker(appContext: Context, workerParams: WorkerParameters)
if (SharedPrefManager.latestXkcd < xkcdPic.num) {
SharedPrefManager.latestXkcd = xkcdPic.num
BoxManager.updateAndSave(xkcdPic)
NotificationUtils.showNotification(applicationContext, xkcdPic)
val allowNotification = PreferenceManager
.getDefaultSharedPreferences(applicationContext)
.getBoolean(Const.PREF_NOTIFICATION, true)
&& NotificationManagerCompat.from(applicationContext).areNotificationsEnabled()
if (allowNotification) {
NotificationUtils.showNotification(applicationContext, xkcdPic)
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions xkcd/src/main/java/xyz/jienan/xkcd/base/network/WhatIfAPI.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package xyz.jienan.xkcd.base.network


import io.reactivex.Observable
import io.reactivex.Single
import okhttp3.ResponseBody
import retrofit2.http.*
import xyz.jienan.xkcd.model.WhatIfArticle
Expand All @@ -10,16 +10,16 @@ interface WhatIfAPI {

@get:Headers("$HEADER_CACHEABLE: 600")
@get:GET("archive/")
val archive: Observable<ResponseBody>
val archive: Single<ResponseBody>

@GET("{article_id}/")
fun getArticle(@Path("article_id") id: Long): Observable<ResponseBody>
fun getArticle(@Path("article_id") id: Long): Single<ResponseBody>

@FormUrlEncoded
@POST
fun thumbsUpWhatIf(@Url url: String, @Field("what_if_id") whatIfId: Int): Observable<WhatIfArticle>
fun thumbsUpWhatIf(@Url url: String, @Field("what_if_id") whatIfId: Int): Single<WhatIfArticle>

@Headers("$HEADER_CACHEABLE: 60")
@GET
fun getTopWhatIfs(@Url url: String, @Query("sortby") sortby: String): Observable<List<WhatIfArticle>>
fun getTopWhatIfs(@Url url: String, @Query("sortby") sortby: String): Single<List<WhatIfArticle>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class SingleComicFragment : BaseFragment(), SingleComicContract.View {
if (currentPic == null) {
false
} else {
showInfoDialog(showAltText = comicOnly)
showInfoDialog(showAltText = comicOnly && resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT)
it.performHapticFeedback(LONG_PRESS)
true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xyz.jienan.xkcd.list.presenter

import android.view.View
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import timber.log.Timber
Expand Down Expand Up @@ -39,14 +41,14 @@ class WhatIfListPresenter(private val view: WhatIfListContract.View) : ListPrese
WhatIfModel.thumbUpList
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { view.setLoading(true) }
.flatMapSingle { whatIfArticles ->
Observable.fromIterable(whatIfArticles)
.flatMap { whatIfArticles ->
Flowable.fromIterable(whatIfArticles)
.map { it.num }
.filter { num -> num <= latest }
.map { WhatIfModel.loadArticleFromDB(it)!! }
.toList()
}
.doOnNext { view.setLoading(false) }
.doOnSuccess { view.setLoading(false) }
.subscribe({ view.updateData(it) },
{ e -> Timber.e(e, "get top what if error") })
.also { compositeDisposable.add(it) }
Expand Down
30 changes: 17 additions & 13 deletions xkcd/src/main/java/xyz/jienan/xkcd/model/WhatIfModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xyz.jienan.xkcd.model

import android.text.TextUtils
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
Expand All @@ -26,7 +28,7 @@ object WhatIfModel {
val favWhatIf: List<WhatIfArticle>
get() = BoxManager.favWhatIf

val thumbUpList: Observable<List<WhatIfArticle>>
val thumbUpList: Single<List<WhatIfArticle>>
get() = whatIfAPI.getTopWhatIfs(WHAT_IF_TOP, XKCD_TOP_SORT_BY_THUMB_UP)
.subscribeOn(Schedulers.io())

Expand All @@ -52,7 +54,6 @@ object WhatIfModel {
fun loadAllWhatIf(): Single<List<WhatIfArticle>> {
return whatIfAPI.archive
.subscribeOn(Schedulers.io())
.singleOrError()
.map { WhatIfArticleUtil.getArticlesFromArchive(it) }
.map { BoxManager.updateAndSaveWhatIf(it.toMutableList()) }
}
Expand All @@ -65,7 +66,6 @@ object WhatIfModel {
fun loadArticle(id: Long): Single<WhatIfArticle> {
return loadArticleContentFromDB(id)
.switchIfEmpty(loadArticleFromAPI(id))
.singleOrError()
.observeOn(AndroidSchedulers.mainThread())
}

Expand All @@ -90,42 +90,46 @@ object WhatIfModel {
* @param index
* @return thumb up count
*/
fun thumbsUp(index: Long): Observable<Long> {
fun thumbsUp(index: Long): Single<Long> {
return whatIfAPI.thumbsUpWhatIf(WHAT_IF_THUMBS_UP, index.toInt())
.subscribeOn(Schedulers.io())
.doOnSubscribe { BoxManager.likeWhatIf(index) }
.map { it.thumbCount }
}

fun fastLoadWhatIfs(index: Long): Completable =
Observable.just(index)
.flatMap<Long> {
Flowable.just(index)
.flatMap {
if (index == 0L) {
loadLatest().map { it.num }
.flatMapObservable { Observable.rangeLong(1, it) }
.toFlowable()
.flatMap { Flowable.rangeLong(1, it) }
} else {
Observable.rangeLong(1, index)
Flowable.rangeLong(1, index)
}
}.flatMap {
}.flatMapSingle {
if (loadArticleFromDB(it) == null || loadArticleFromDB(it)!!.content.isNullOrBlank()) {
loadArticleFromAPI(it)
.map { index }
.onErrorReturnItem(index)
} else {
Observable.just(it)
Single.just(it)
}
}
.toList()
.ignoreElement()
.onErrorComplete()

private fun loadArticleFromAPI(id: Long): Observable<WhatIfArticle> {
private fun loadArticleFromAPI(id: Long): Single<WhatIfArticle> {
return whatIfAPI.getArticle(id)
.subscribeOn(Schedulers.io())
.map { WhatIfArticleUtil.getArticleFromHtml(it) }
.map { it.html() }
.map { BoxManager.updateAndSaveWhatIf(id, it) }
}

private fun loadArticleContentFromDB(id: Long): Observable<WhatIfArticle> {
private fun loadArticleContentFromDB(id: Long): Maybe<WhatIfArticle> {
val article = BoxManager.getWhatIf(id)
return if (article == null || TextUtils.isEmpty(article.content)) Observable.empty() else Observable.just(article)
return if (article == null || TextUtils.isEmpty(article.content)) Maybe.empty() else Maybe.just(article)
}
}
Loading

0 comments on commit b155946

Please sign in to comment.