Skip to content

Commit

Permalink
Merge pull request #7 from Akashkamble/bugFix
Browse files Browse the repository at this point in the history
Bump compose version to 1.0.0-alpha02
  • Loading branch information
Akashkamble committed Sep 9, 2020
2 parents bdf4228 + e2a1c5e commit 9681c84
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Newzz-Compose
News App built with JetPack Compose
News App built with JetPack Compose(v1.0.0-alpha02)

## Gif
![compose](https://user-images.githubusercontent.com/13314984/83972646-00b1ca80-a8ff-11ea-93b0-92daf03bc0a0.gif)
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.akash.newzzcompose"
minSdkVersion 21
targetSdkVersion 29
versionCode 3
versionName "0.0.3"
versionCode 4
versionName "0.0.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -76,7 +76,7 @@ dependencies {
implementation 'androidx.browser:browser:1.3.0-alpha05'

/*---------------------CoilImageLoader------------------------------*/
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.2.0"
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.2.1"

/*----------------------------Moshi-------------------------------------*/
implementation 'com.squareup.moshi:moshi:1.9.1'
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/akash/newzz_compose/ui/NewzzActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.akash.newzz_compose.ui

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.livedata.observeAsState
Expand All @@ -23,7 +22,6 @@ class NewzzActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContent {
val darkTheme = viewModel.isDarkTheme.observeAsState(false)
Log.d("THEME", "darkTheme: ${darkTheme.value}")
NewzzTheme(darkTheme = darkTheme.value) {
NewzzAppUI(viewModel = viewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.compose.ui.unit.dp
import com.akash.newzz_compose.R
import com.akash.newzz_compose.ui.style.NewzzTheme
import com.akash.newzz_compose.ui.style.titleColorDark
import dev.chrisbanes.accompanist.coil.CoilImage
import dev.chrisbanes.accompanist.coil.CoilImageWithCrossfade

/**
* Created by Akash on 29/08/20
Expand Down Expand Up @@ -62,7 +62,7 @@ fun RemoteImage(
color = Color.Transparent,
shape = shape
) {
CoilImage(
CoilImageWithCrossfade(
data = url,
modifier = modifier,
contentScale = contentScale,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ import com.akash.newzz_compose.viewmodel.NewzzViewModel
fun NewzzAppUI(viewModel: NewzzViewModel) {
val categoryList = viewModel.categoryList.observeAsState().value!!
val activeCategory = viewModel.activeCategory.observeAsState().value!!
val activeCategoryUiState = viewModel.activeCategoryUiState.observeAsState().value!!
val firstCategoryUiState = viewModel.firstCategoryUiState.observeAsState().value!!
val secondCategoryUiState = viewModel.secondCategoryUiState.observeAsState().value!!
val thirdCategoryUiState = viewModel.thirdCategoryUiState.observeAsState().value!!
val listOfUiState = listOf(
firstCategoryUiState,
secondCategoryUiState,
thirdCategoryUiState
)
val activeIndex = categoryList.indexOf(activeCategory)
Scaffold(
bodyContent = {
BodyContent(
activeCategory = activeCategory,
onThemeSwitch = {
viewModel.performAction(NewzzViewModel.Action.SwitchTheme)
},
activeCategoryUiState = activeCategoryUiState,
activeCategoryUiStateList = listOfUiState,
activeIndex = activeIndex,
retryFetchingArticles = { category ->
viewModel.performAction(NewzzViewModel.Action.FetchArticles(category))
}
Expand All @@ -49,8 +58,9 @@ fun NewzzAppUI(viewModel: NewzzViewModel) {
@Composable
fun BodyContent(
activeCategory: Category,
activeCategoryUiState: ArticleListUiState,
activeCategoryUiStateList: List<ArticleListUiState>,
onThemeSwitch: () -> Unit,
activeIndex: Int,
retryFetchingArticles: (Category) -> Unit
) {
val stringRes = getTitleResource(activeCategory)
Expand All @@ -62,12 +72,37 @@ fun BodyContent(
TopAppBar(stringRes, onThemeSwitch = {
onThemeSwitch()
})
NewzzListContainer(
uiState = activeCategoryUiState,
retry = {
retryFetchingArticles(activeCategory)
}
)
/* This when statement does not make any sense. Need to figure out better solution.
the idea was to change uiState based on activeCategory, but when 2 category has different number of articles
and you switch between pages LazyColumnFor remembers scroll position of previous state which leads to ArrayIndexOutOfBound Exception.
*/
when (activeIndex) {
0 -> {
NewzzListContainer(
uiState = activeCategoryUiStateList[activeIndex],
retry = {
retryFetchingArticles(activeCategory)
}
)
}
1 -> {
NewzzListContainer(
uiState = activeCategoryUiStateList[activeIndex],
retry = {
retryFetchingArticles(activeCategory)
}
)
}
2 -> {
NewzzListContainer(
uiState = activeCategoryUiStateList[activeIndex],
retry = {
retryFetchingArticles(activeCategory)
}
)
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ class NewzzViewModel @ViewModelInject constructor(private val repo: NewsReposito
}
val activeCategory: LiveData<Category> = _activeCategory

private val _activeCategoryUiState = MutableLiveData<ArticleListUiState>().apply {
/*private val _activeCategoryUiState = MutableLiveData<ArticleListUiState>().apply {
value = ArticleListUiState()
}
val activeCategoryUiState: LiveData<ArticleListUiState> = _activeCategoryUiState
private val activeCategoryUiState: LiveData<ArticleListUiState> = _activeCategoryUiState*/

private var firstPageUiState = ArticleListUiState()
private val _firstCategoryUiState = MutableLiveData<ArticleListUiState>().apply {
value = ArticleListUiState()
}
val firstCategoryUiState: LiveData<ArticleListUiState> = _firstCategoryUiState
private var secondPageUiState = ArticleListUiState()
private val _secondCategoryUiState = MutableLiveData<ArticleListUiState>().apply {
value = ArticleListUiState()
}
val secondCategoryUiState: LiveData<ArticleListUiState> = _secondCategoryUiState
private var thirdPageUiState = ArticleListUiState()
private val _thirdCategoryUiState = MutableLiveData<ArticleListUiState>().apply {
value = ArticleListUiState()
}
val thirdCategoryUiState: LiveData<ArticleListUiState> = _thirdCategoryUiState

init {
getArticlesByCategory(categoryList.value!![0])
Expand Down Expand Up @@ -74,12 +86,24 @@ class NewzzViewModel @ViewModelInject constructor(private val repo: NewsReposito
is Action.ChangePageTo -> {
_activeCategory.value = action.category
when (categoryList.value!!.indexOf(action.category)) {
0 -> _activeCategoryUiState.value = firstPageUiState
1 -> _activeCategoryUiState.value = secondPageUiState
2 -> _activeCategoryUiState.value = thirdPageUiState
}
if (activeCategoryUiState.value!!.list == null || activeCategoryUiState.value!!.list!!.isEmpty()) {
getArticlesByCategory(action.category)
0 -> {
_firstCategoryUiState.value = firstPageUiState
if (_firstCategoryUiState.value!!.list == null || _firstCategoryUiState.value!!.list!!.isEmpty()) {
getArticlesByCategory(action.category)
}
}
1 -> {
_secondCategoryUiState.value = secondPageUiState
if (_secondCategoryUiState.value!!.list == null || _secondCategoryUiState.value!!.list!!.isEmpty()) {
getArticlesByCategory(action.category)
}
}
2 -> {
_thirdCategoryUiState.value = thirdPageUiState
if (_thirdCategoryUiState.value!!.list == null || _thirdCategoryUiState.value!!.list!!.isEmpty()) {
getArticlesByCategory(action.category)
}
}
}
}
is Action.FetchArticles -> {
Expand All @@ -99,23 +123,23 @@ class NewzzViewModel @ViewModelInject constructor(private val repo: NewsReposito
list = null,
error = error
)
_activeCategoryUiState.value = firstPageUiState
_firstCategoryUiState.value = firstPageUiState
}
1 -> {
secondPageUiState = secondPageUiState.copy(
isLoading = false,
list = null,
error = error
)
_activeCategoryUiState.value = secondPageUiState
_secondCategoryUiState.value = secondPageUiState
}
2 -> {
thirdPageUiState = thirdPageUiState.copy(
isLoading = false,
list = null,
error = error
)
_activeCategoryUiState.value = thirdPageUiState
_thirdCategoryUiState.value = thirdPageUiState
}
}
}
Expand All @@ -128,37 +152,37 @@ class NewzzViewModel @ViewModelInject constructor(private val repo: NewsReposito
list = data.articles,
error = null
)
_activeCategoryUiState.value = firstPageUiState
_firstCategoryUiState.value = firstPageUiState
}
1 -> {
secondPageUiState = secondPageUiState.copy(
isLoading = false,
list = data.articles,
error = null
)
_activeCategoryUiState.value = secondPageUiState
_secondCategoryUiState.value = secondPageUiState
}
2 -> {
thirdPageUiState = thirdPageUiState.copy(
isLoading = false,
list = data.articles,
error = null
)
_activeCategoryUiState.value = thirdPageUiState
_thirdCategoryUiState.value = thirdPageUiState
}
}
}

private fun setLoadingState(category: Category) {
when (categoryList.value!!.indexOf(category)) {
0 -> {
_activeCategoryUiState.value = firstPageUiState.copy(isLoading = true)
_firstCategoryUiState.value = firstPageUiState.copy(isLoading = true)
}
1 -> {
_activeCategoryUiState.value = secondPageUiState.copy(isLoading = true)
_secondCategoryUiState.value = secondPageUiState.copy(isLoading = true)
}
2 -> {
_activeCategoryUiState.value = thirdPageUiState.copy(isLoading = true)
_thirdCategoryUiState.value = thirdPageUiState.copy(isLoading = true)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.0"
ext.compose_version = "1.0.0-alpha01"
ext.compose_version = "1.0.0-alpha02"
ext.coroutines_version = "1.3.9"
ext.hilt_version = "2.28-alpha"
ext.hilt_jetpack_config_version = "1.0.0-alpha02"
Expand Down

0 comments on commit 9681c84

Please sign in to comment.