Skip to content

Commit

Permalink
Merge pull request #4 from therxmv/dev
Browse files Browse the repository at this point in the history
[V1.2.1] Fixed crash after like/dislike post, fixed blinks while navi…
  • Loading branch information
therxmv authored Aug 21, 2023
2 parents 0503164 + e2d662f commit 7af41c9
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 57 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'com.google.dagger.hilt.android'
id "com.google.gms.google-services"
id "com.google.firebase.crashlytics"
id 'org.jetbrains.kotlin.plugin.parcelize'
}

android {
Expand All @@ -15,8 +16,8 @@ android {
applicationId "com.therxmv.dirolreader"
minSdk 23
targetSdk 33
versionCode 120
versionName "1.2.0"
versionCode 121
versionName "1.2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand All @@ -36,7 +37,7 @@ android {
}

release {
debuggable false
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
Expand Down Expand Up @@ -74,6 +75,9 @@ dependencies {
implementation project(path: ':ota-updates')
implementation project(path: ':constants')

// MMKV(SharedPrefs)
implementation 'com.tencent:mmkv:1.3.1'

// Gson
implementation 'com.google.code.gson:gson:2.10.1'

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/therxmv/dirolreader/DirolApp.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.therxmv.dirolreader

import android.app.Application
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class DirolApp: Application()
class DirolApp: Application() {
override fun onCreate() {
super.onCreate()
MMKV.initialize(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.therxmv.dirolreader.data.models

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class ChannelsRatingListModel(
val list: MutableList<ChannelRatingModel> = mutableListOf()
) : Parcelable

@Parcelize
data class ChannelRatingModel(
val channelId: Long,
val rating: Int,
): Parcelable
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.therxmv.dirolreader.data.repository

import android.content.SharedPreferences
import com.therxmv.dirolreader.data.models.ChannelsRatingListModel
import com.therxmv.dirolreader.data.source.locale.AppSharedPrefsDataSource

class AppSharedPrefsRepository(
Expand All @@ -22,7 +23,7 @@ class AppSharedPrefsRepository(
appSharedPrefsDataSource.isAutoDeleteEnabled = value
}

var channelsRating: MutableMap<Long, Int>
var channelsRating: ChannelsRatingListModel
get() {
return appSharedPrefsDataSource.channelsRating
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,46 @@ package com.therxmv.dirolreader.data.source.locale
import android.content.Context
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import androidx.core.content.edit
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import com.therxmv.constants.SharedPrefs.SHARED_PREFS
import com.therxmv.constants.SharedPrefs.SHARED_PREFS_CHANNELS_RATING
import com.therxmv.constants.SharedPrefs.SHARED_PREFS_IS_AUTO_DELETE_ENABLED
import com.therxmv.constants.SharedPrefs.SHARED_PREFS_IS_DYNAMIC
import com.therxmv.constants.SharedPrefs.SHARED_PREFS_IS_UPDATE_DOWNLOADED
import com.therxmv.dirolreader.data.models.ChannelsRatingListModel

class AppSharedPrefsDataSource(
private val context: Context
) {
private val sharedPrefs = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE)
private val mmkv = MMKV.defaultMMKV()

var isDynamic: Boolean
get() {
return sharedPrefs.getBoolean(SHARED_PREFS_IS_DYNAMIC, false)
return mmkv.decodeBool(SHARED_PREFS_IS_DYNAMIC, false)
}
set(value) {
sharedPrefs.edit(true) {
putBoolean(SHARED_PREFS_IS_DYNAMIC, value)
}
mmkv.encode(SHARED_PREFS_IS_DYNAMIC, value)
}

var isAutoDeleteEnabled: Boolean
get() {
return sharedPrefs.getBoolean(SHARED_PREFS_IS_AUTO_DELETE_ENABLED, false)
return mmkv.decodeBool(SHARED_PREFS_IS_AUTO_DELETE_ENABLED, false)
}
set(value) {
sharedPrefs.edit(true) {
putBoolean(SHARED_PREFS_IS_AUTO_DELETE_ENABLED, value)
}
mmkv.encode(SHARED_PREFS_IS_AUTO_DELETE_ENABLED, value)
}

var channelsRating: MutableMap<Long, Int>
var channelsRating: ChannelsRatingListModel
get() {
val str = sharedPrefs.getString(SHARED_PREFS_CHANNELS_RATING, "")

if(str.isNullOrEmpty()) return mutableMapOf()

val type = TypeToken.getParameterized(Map::class.java, Long::class.java, Int::class.java).type

return Gson().fromJson(str, type)
return mmkv.decodeParcelable(SHARED_PREFS_CHANNELS_RATING, ChannelsRatingListModel::class.java)
?: ChannelsRatingListModel()
}
set(value) {
sharedPrefs.edit(true) {
val str = if(value.isEmpty()) "" else Gson().toJson(value)

putString(SHARED_PREFS_CHANNELS_RATING, str)
}
mmkv.encode(SHARED_PREFS_CHANNELS_RATING, value)
}

// TODO make listener for mmkv
var isUpdateDownloaded: Boolean
get() = sharedPrefs.getBoolean(SHARED_PREFS_IS_UPDATE_DOWNLOADED, false)
set(value) {
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/com/therxmv/dirolreader/ui/news/NewsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.therxmv.dirolreader.data.models.ChannelRatingModel
import com.therxmv.dirolreader.data.models.ChannelsRatingListModel
import com.therxmv.dirolreader.data.models.MediaModel
import com.therxmv.dirolreader.data.models.MediaType
import com.therxmv.dirolreader.data.repository.AppSharedPrefsRepository
Expand Down Expand Up @@ -31,7 +33,7 @@ class NewsViewModel @Inject constructor(
private val _state = MutableStateFlow(NewsUiState())
val state = _state.asStateFlow()

private val ratingMap = mutableMapOf<Long, Int>()
private val channelsRating = ChannelsRatingListModel()

private val readMessages = mutableListOf<Long>()

Expand All @@ -47,11 +49,18 @@ class NewsViewModel @Inject constructor(
fun onEvent(event: NewsUiEvent) {
when(event) {
is NewsUiEvent.UpdateRating -> {
if(ratingMap.contains(event.id)) {
ratingMap[event.id] = ratingMap[event.id]!! + event.num
val itemIndex = channelsRating.list.indexOfFirst { it.channelId == event.id }

if(itemIndex == -1) {
channelsRating.list.add(ChannelRatingModel(event.id, event.num))
}
else {
val item = channelsRating.list[itemIndex]

channelsRating.list[itemIndex] = ChannelRatingModel(event.id, item.rating + event.num)
}
else ratingMap[event.id] = event.num
appSharedPrefsRepository.channelsRating = ratingMap

appSharedPrefsRepository.channelsRating = channelsRating
}
is NewsUiEvent.MarkAsRead -> {
if(!readMessages.contains(event.messageId)) {
Expand All @@ -68,13 +77,13 @@ class NewsViewModel @Inject constructor(
}

private fun updateRating() {
appSharedPrefsRepository.channelsRating.forEach {
appSharedPrefsRepository.channelsRating.list.forEach {
viewModelScope.launch {
useCases.updateChannelRatingUseCase(it.key, it.value)
useCases.updateChannelRatingUseCase(it.channelId, it.rating)
}
}
ratingMap.clear()
appSharedPrefsRepository.channelsRating = ratingMap
channelsRating.list.clear()
appSharedPrefsRepository.channelsRating = channelsRating
}

suspend fun loadMessageMedia(mediaList: List<MediaModel>, loadVideo: Boolean): List<String?> {
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/therxmv/dirolreader/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.therxmv.dirolreader.ui.theme

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -88,6 +89,9 @@ fun AppTheme(
dynamicColor: Boolean = false,
content: @Composable () -> Unit
) {
val view = LocalView.current
val window = (view.context as Activity).window

val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
Expand All @@ -98,16 +102,20 @@ fun AppTheme(
else -> LightColors
}

// Fix for blinks when navigate
window.setBackgroundDrawable(
ColorDrawable(colorScheme.surface.toArgb())
)

val systemUiController = rememberSystemUiController()
systemUiController.setSystemBarsColor(
color = colorScheme.surface
)

// Immersive mode
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = Color.Transparent.toArgb()
window.navigationBarColor = Color.Transparent.toArgb()
window.isStatusBarContrastEnforced = false
Expand Down

0 comments on commit 7af41c9

Please sign in to comment.