Skip to content

Commit

Permalink
Fixing update issue
Browse files Browse the repository at this point in the history
Adding no firebase versions to update
  • Loading branch information
Jacob Rein committed Jul 11, 2022
1 parent 43ea241 commit e8102c8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import androidx.compose.material.icons.filled.BrowseGallery
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.*
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.*
import androidx.compose.runtime.rxjava2.subscribeAsState
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -49,9 +47,14 @@ import com.programmersbox.helpfulutils.notificationManager
import com.programmersbox.models.ApiService
import com.programmersbox.models.ItemModel
import com.programmersbox.models.sourcePublish
import com.programmersbox.sharedutils.AppUpdate
import com.programmersbox.sharedutils.MainLogo
import com.programmersbox.sharedutils.appUpdateCheck
import com.programmersbox.uiviews.utils.*
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.koin.android.ext.android.inject
Expand Down Expand Up @@ -96,6 +99,13 @@ abstract class BaseMainActivity : AppCompatActivity() {

onCreate()

Single.create<AppUpdate.AppUpdates> { AppUpdate.getUpdate()?.let(it::onSuccess) ?: it.onError(Throwable("Something went wrong")) }
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.doOnError {}
.subscribe(appUpdateCheck::onNext)
.addTo(disposable)

setContent {
val bottomSheetNavigator = rememberBottomSheetNavigator()
navController = rememberAnimatedNavController(bottomSheetNavigator)
Expand All @@ -117,7 +127,7 @@ abstract class BaseMainActivity : AppCompatActivity() {
sheetContentColor = MaterialTheme.colorScheme.onSurface,
scrimColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.32f)
) {
androidx.compose.material3.Scaffold(
Scaffold(
bottomBar = {
Column {
genericInfo.BottomBarAdditions()
Expand All @@ -133,15 +143,26 @@ abstract class BaseMainActivity : AppCompatActivity() {
if (screen !is SScreen.AllScreen || showAllItem) {
NavigationBarItem(
icon = {
androidx.compose.material3.Icon(
when (screen) {
SScreen.RecentScreen -> Icons.Default.History
SScreen.AllScreen -> Icons.Default.BrowseGallery
SScreen.SettingsScreen -> Icons.Default.Settings
else -> Icons.Default.BrokenImage
},
null
)
BadgedBox(
badge = {
if (screen is SScreen.SettingsScreen) {
val updateAvailable = updateCheck()
if (updateAvailable) {
Badge { Text("") }
}
}
}
) {
Icon(
when (screen) {
SScreen.RecentScreen -> Icons.Default.History
SScreen.AllScreen -> Icons.Default.BrowseGallery
SScreen.SettingsScreen -> Icons.Default.Settings
else -> Icons.Default.BrokenImage
},
null
)
}
},
label = {
Text(
Expand Down Expand Up @@ -299,6 +320,16 @@ abstract class BaseMainActivity : AppCompatActivity() {

}

@Composable
fun updateCheck(): Boolean {
val appUpdate by appUpdateCheck.subscribeAsState(null)

return AppUpdate.checkForUpdate(
remember { packageManager.getPackageInfo(packageName, 0)?.versionName.orEmpty() },
appUpdate?.update_real_version.orEmpty()
)
}

private fun hideSystemBars() {
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ import com.programmersbox.uiviews.utils.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.Observables
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean

class SettingsDsl {
companion object {
Expand Down Expand Up @@ -142,14 +140,16 @@ fun SettingScreen(
.padding(bottom = 10.dp)
) {

/*Account*/
AccountSettings(
context = context,
activity = activity,
logo = logo
)
if (BuildConfig.FLAVOR != "noFirebase") {
/*Account*/
AccountSettings(
context = context,
activity = activity,
logo = logo
)

androidx.compose.material3.Divider()
Divider()
}

/*About*/
AboutSettings(
Expand All @@ -160,7 +160,7 @@ fun SettingScreen(
logo = logo
)

androidx.compose.material3.Divider(modifier = Modifier.padding(top = 5.dp))
Divider(modifier = Modifier.padding(top = 5.dp))

/*Notifications*/
NotificationSettings(
Expand All @@ -177,7 +177,7 @@ fun SettingScreen(
historyClick = historyClick
)

androidx.compose.material3.Divider()
Divider()

/*General*/
GeneralSettings(
Expand All @@ -189,7 +189,7 @@ fun SettingScreen(
globalSearchClick = globalSearchClick
)

androidx.compose.material3.Divider()
Divider()

/*Player*/
PlaySettings(
Expand All @@ -198,7 +198,7 @@ fun SettingScreen(
customSettings = customPreferences.playerSettings
)

androidx.compose.material3.Divider(modifier = Modifier.padding(top = 5.dp))
Divider(modifier = Modifier.padding(top = 5.dp))

/*More Info*/
InfoSettings(
Expand Down Expand Up @@ -266,6 +266,21 @@ class AboutViewModel : ViewModel() {
viewModelScope.launch { context.shouldCheckFlow.collect { canCheck = it } }
}

private val checker = AtomicBoolean(false)

suspend fun updateChecker(context: Context) {
try {
if (!checker.get()) {
checker.set(true)
AppUpdate.getUpdate()?.let(appUpdateCheck::onNext)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
checker.set(false)
withContext(Dispatchers.Main) { context.let { c -> Toast.makeText(c, "Done Checking", Toast.LENGTH_SHORT).show() } }
}
}
}

@ExperimentalMaterialApi
Expand All @@ -275,8 +290,11 @@ private fun AboutSettings(
scope: CoroutineScope,
activity: ComponentActivity,
genericInfo: GenericInfo,
logo: MainLogo
logo: MainLogo,
aboutViewModel: AboutViewModel = viewModel()
) {
LaunchedEffect(Unit) { aboutViewModel.init(context) }

CategorySetting(
settingTitle = { Text(stringResource(R.string.about)) },
settingIcon = {
Expand All @@ -298,7 +316,8 @@ private fun AboutSettings(
context.packageManager.getPackageInfo(context.packageName, 0)?.versionName.orEmpty()
)
)
}
},
modifier = Modifier.clickable { scope.launch(Dispatchers.IO) { aboutViewModel.updateChecker(context) } }
)

ShowWhen(
Expand All @@ -317,25 +336,21 @@ private fun AboutSettings(
confirmButton = {
TextButton(
onClick = {
if (BuildConfig.FLAVOR != "noFirebase") {
activity.requestPermissions(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
) {
if (it.isGranted) {
appUpdateCheck.value
?.let { a ->
val isApkAlreadyThere = File(
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath + "/",
a.let(genericInfo.apkString).toString()
)
if (isApkAlreadyThere.exists()) isApkAlreadyThere.delete()
DownloadUpdate(context, context.packageName).downloadUpdate(a)
}
}
activity.requestPermissions(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
) {
if (it.isGranted) {
appUpdateCheck.value
?.let { a ->
val isApkAlreadyThere = File(
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath + "/",
a.let(genericInfo.apkString).toString()
)
if (isApkAlreadyThere.exists()) isApkAlreadyThere.delete()
DownloadUpdate(context, context.packageName).downloadUpdate(a)
}
}
} else {
context.openInCustomChromeBrowser("https://github.com/jakepurple13/OtakuWorld/releases/latest")
}
showDialog = false
}
Expand Down Expand Up @@ -411,9 +426,6 @@ private fun AboutSettings(
}
)

val aboutViewModel: AboutViewModel = viewModel()
LaunchedEffect(Unit) { aboutViewModel.init(context) }

var showDialog by remember { mutableStateOf(false) }

if (showDialog) {
Expand Down Expand Up @@ -551,7 +563,7 @@ private fun NotificationSettings(
.padding(bottom = 16.dp, top = 8.dp)
)

androidx.compose.material3.Divider()
Divider()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class GenericAnime(val context: Context) : GenericInfo {

private val disposable = CompositeDisposable()

override val apkString: AppUpdate.AppUpdates.() -> String? get() = { anime_file }
override val apkString: AppUpdate.AppUpdates.() -> String? get() = { if (BuildConfig.FLAVOR == "noFirebase") anime_no_firebase_file else anime_file }
override val deepLinkUri: String get() = "animeworld://"

override fun chapterOnClick(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class GenericManga(val context: Context) : GenericInfo {

private val disposable = CompositeDisposable()

override val apkString: AppUpdate.AppUpdates.() -> String? get() = { manga_file }
override val apkString: AppUpdate.AppUpdates.() -> String? get() = { if (BuildConfig.FLAVOR == "noFirebase") manga_no_firebase_file else manga_file }
override val scrollBuffer: Int = 4

override fun chapterOnClick(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class GenericNovel(val context: Context) : GenericInfo {
) {
}

override val apkString: AppUpdate.AppUpdates.() -> String? get() = { novel_file }
override val apkString: AppUpdate.AppUpdates.() -> String? get() = { if (BuildConfig.FLAVOR == "noFirebase") novel_no_firebase_file else novel_file }

@OptIn(ExperimentalMaterialApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ object AppUpdate {
val anime_file: String?,
val novel_file: String?,
val animetv_file: String?,
val otakumanager_file: String?
val otakumanager_file: String?,
val manga_no_firebase_file: String?,
val anime_no_firebase_file: String?,
val novel_no_firebase_file: String?,
val animetv_no_firebase_file: String?,
) {
fun downloadUrl(url: AppUpdates.() -> String?) = "$update_url${url()}"
}
Expand Down
6 changes: 5 additions & 1 deletion update.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"anime_file": "animeworld-release.apk",
"novel_file": "novelworld-release.apk",
"animetv_file": "animeworldtv-release.apk",
"otakumanager_file": "otakumanager-release.apk"
"otakumanager_file": "otakumanager-release.apk",
"manga_no_firebase_file": "mangaworld-no-firebase-release.apk",
"anime_no_firebase_file": "animeworld-no-firebase-release.apk",
"novel_no_firebase_file": "novelworld-no-firebase-release.apk",
"animetv_no_firebase_file": "animeworldtv-no-firebase-release.apk"
}

0 comments on commit e8102c8

Please sign in to comment.