Skip to content

Commit

Permalink
- added offline states and offline protection
Browse files Browse the repository at this point in the history
- app still needs to be usable offline since AnimeWorld still allows you to watch videos
  • Loading branch information
jakepurple13 committed Apr 13, 2021
1 parent 341352e commit 9aaf195
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 27 deletions.
2 changes: 2 additions & 0 deletions UIViews/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ dependencies {

implementation 'me.zhanghai.android.fastscroll:library:1.1.5'

implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.8'

implementation project(':Models')
implementation project(':favoritesdatabase')

Expand Down
1 change: 1 addition & 0 deletions UIViews/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmersbox.uiviews">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
11 changes: 11 additions & 0 deletions UIViews/src/main/java/com/programmersbox/uiviews/AllFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.programmersbox.uiviews

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -77,6 +79,15 @@ class AllFragment : BaseListFragment() {
})
}

ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
view.findViewById<RelativeLayout>(R.id.offline_view).visibility = if (it) View.GONE else View.VISIBLE
refresh.visibility = if (it) View.VISIBLE else View.GONE
}
.addTo(disposable)

sourcePublish
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ abstract class BaseMainActivity : AppCompatActivity(), GenericInfo {
val controller = findViewById<BottomNavigationView>(R.id.navLayout2)
.also {
GlobalScope.launch {
val request = Request.Builder()
.url("https://github.com/jakepurple13/OtakuWorld/releases/latest")
.get()
.build()
@Suppress("BlockingMethodInNonBlockingContext") val response = OkHttpClient().newCall(request).execute()
val f = response.request().url().path.split("/").lastOrNull()?.toDoubleOrNull()
runOnUIThread {
if (packageManager?.getPackageInfo(packageName, 0)?.versionName?.toDoubleOrNull() ?: 0.0 < f ?: 0.0) {
it.getOrCreateBadge(R.id.setting_nav).number = 1
try {
val request = Request.Builder()
.url("https://github.com/jakepurple13/OtakuWorld/releases/latest")
.get()
.build()
@Suppress("BlockingMethodInNonBlockingContext") val response = OkHttpClient().newCall(request).execute()
val f = response.request().url().path.split("/").lastOrNull()?.toDoubleOrNull()
runOnUIThread {
if (packageManager?.getPackageInfo(packageName, 0)?.versionName?.toDoubleOrNull() ?: 0.0 < f ?: 0.0) {
it.getOrCreateBadge(R.id.setting_nav).number = 1
}
}
} catch (e: Exception) {
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions UIViews/src/main/java/com/programmersbox/uiviews/RecentFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.programmersbox.uiviews

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.programmersbox.favoritesdatabase.DbModel
import com.programmersbox.favoritesdatabase.ItemDatabase
import com.programmersbox.models.ApiService
Expand Down Expand Up @@ -63,6 +65,15 @@ class RecentFragment : BaseListFragment() {
})
}

ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
view.findViewById<RelativeLayout>(R.id.offline_view).visibility = if (it) View.GONE else View.VISIBLE
refresh.visibility = if (it) View.VISIBLE else View.GONE
}
.addTo(disposable)

refresh.setOnRefreshListener { sourceLoad(sourcePublish.value!!) }

sourcePublish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,29 @@ class SettingsFragment : PreferenceFragmentCompat() {
fun updateSetter() {
if (!checker.get()) {
GlobalScope.launch {
checker.set(true)
val request = Request.Builder()
.url("https://github.com/jakepurple13/OtakuWorld/releases/latest")
.get()
.build()
@Suppress("BlockingMethodInNonBlockingContext") val response = OkHttpClient().newCall(request).execute()
val f = response.request().url().path.split("/").lastOrNull()?.toDoubleOrNull()
runOnUIThread {
findPreference<Preference>("updateAvailable")?.let { p1 ->
p1.summary = "Version: $f"
p1.isVisible =
context?.packageManager?.getPackageInfo(
requireContext().packageName,
0
)?.versionName?.toDoubleOrNull() ?: 0.0 < f ?: 0.0
try {
checker.set(true)
val request = Request.Builder()
.url("https://github.com/jakepurple13/OtakuWorld/releases/latest")
.get()
.build()
@Suppress("BlockingMethodInNonBlockingContext") val response = OkHttpClient().newCall(request).execute()
val f = response.request().url().path.split("/").lastOrNull()?.toDoubleOrNull()
runOnUIThread {
findPreference<Preference>("updateAvailable")?.let { p1 ->
p1.summary = "Version: $f"
p1.isVisible =
context?.packageManager?.getPackageInfo(
requireContext().packageName,
0
)?.versionName?.toDoubleOrNull() ?: 0.0 < f ?: 0.0
}
}
} catch (e: Exception) {

} finally {
checker.set(false)
}
checker.set(false)
}
}
}
Expand Down Expand Up @@ -323,12 +328,12 @@ class SettingsDsl {
}
}

abstract class CoroutineTask<Params, Progress, Result> : CoroutineScope {
abstract class CoroutineTask<Progress, Result> : CoroutineScope {
protected open val job: Job = Job()
override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
protected suspend fun publishProgress(vararg values: Progress) = withContext(Dispatchers.Main) { onProgressUpdate(*values) }
open fun onProgressUpdate(vararg values: Progress) {}
abstract suspend fun doInBackground(vararg params: Params): Result
abstract suspend fun doInBackground(): Result
open fun onPreExecute() {}
open fun onPostExecute(result: Result) {}
fun cancel() = job.cancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.programmersbox.uiviews.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Paint
import com.google.gson.*
import com.programmersbox.gsonutils.sharedPrefNotNullObjectDelegate
import com.programmersbox.gsonutils.sharedPrefObjectDelegate
Expand All @@ -21,6 +25,23 @@ val updateCheckPublish = BehaviorSubject.create<Long>()

var Context.batteryAlertPercent: Int by sharedPrefNotNullDelegate(20)

fun Bitmap.glowEffect(glowRadius: Int, glowColor: Int): Bitmap? {
val margin = 24
val halfMargin = margin / 2f
val alpha = extractAlpha()
val out = Bitmap.createBitmap(width + margin, height + margin, Bitmap.Config.ARGB_8888)
val canvas = Canvas(out)
val paint = Paint()
paint.color = glowColor

// Outer glow, For Inner glow set Blur.INNER
paint.maskFilter = BlurMaskFilter(glowRadius.toFloat(), BlurMaskFilter.Blur.OUTER)
canvas.drawBitmap(alpha, halfMargin, halfMargin, paint)
canvas.drawBitmap(this, halfMargin, halfMargin, null)
alpha.recycle()
return out
}

class ChapterModelSerializer : JsonSerializer<ChapterModel> {
override fun serialize(src: ChapterModel, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
val json = JsonObject()
Expand Down
10 changes: 10 additions & 0 deletions UIViews/src/main/res/drawable/ic_baseline_cloud_off_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4c-1.48,0 -2.85,0.43 -4.01,1.17l1.46,1.46C10.21,6.23 11.08,6 12,6c3.04,0 5.5,2.46 5.5,5.5v0.5H19c1.66,0 3,1.34 3,3 0,1.13 -0.64,2.11 -1.56,2.62l1.45,1.45C23.16,18.16 24,16.68 24,15c0,-2.64 -2.05,-4.78 -4.65,-4.96zM3,5.27l2.75,2.74C2.56,8.15 0,10.77 0,14c0,3.31 2.69,6 6,6h11.73l2,2L21,20.73 4.27,4 3,5.27zM7.73,10l8,8H6c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4h1.73z" />
</vector>
29 changes: 29 additions & 0 deletions UIViews/src/main/res/layout/fragment_all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,33 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RelativeLayout
android:id="@+id/offline_view"
android:visibility="gone"
tools:visibility="visible"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/offline_icon"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_baseline_cloud_off_24"
android:layout_width="0dp"
android:layout_height="50dp" />

<TextView
style="@style/TextAppearance.MaterialComponents.Headline5"
android:text="You're Offline"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@id/offline_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
29 changes: 29 additions & 0 deletions UIViews/src/main/res/layout/fragment_recent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@
android:layout_height="match_parent"
tools:context=".RecentFragment">

<RelativeLayout
android:id="@+id/offline_view"
android:visibility="gone"
tools:visibility="visible"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/offline_icon"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_baseline_cloud_off_24"
android:layout_width="0dp"
android:layout_height="50dp" />

<TextView
style="@style/TextAppearance.MaterialComponents.Headline5"
android:text="You're Offline"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@id/offline_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/recentRefresh"
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.programmersbox.thirdpartyutils.into
import com.programmersbox.uiviews.BaseListFragment
import com.programmersbox.uiviews.ItemListAdapter


class MangaGalleryAdapter(context: Context, baseListFragment: BaseListFragment) :
ItemListAdapter<GalleryHolder>(context, baseListFragment) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryHolder =
Expand All @@ -35,6 +36,7 @@ class MangaGalleryAdapter(context: Context, baseListFragment: BaseListFragment)
//.error(R.drawable.manga_world_round_logo)
.into<Bitmap> {
resourceReady { image, _ ->
//cover.setImageBitmap(image.glowEffect(10, title.currentTextColor) ?: image)
cover.setImageBitmap(image)
/*if (context.usePalette) {
swatch = image.getPalette().vibrantSwatch
Expand Down

0 comments on commit 9aaf195

Please sign in to comment.