Merge pull request #41 from msfjarvis/ui-tweaks

This commit is contained in:
Harsh Shandilya
2020-10-10 19:22:50 +05:30
committed by GitHub
5 changed files with 30 additions and 20 deletions
@@ -9,8 +9,8 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumnForIndexed
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Scaffold
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Providers
import androidx.compose.runtime.ambientOf
@@ -21,7 +21,6 @@ import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import dagger.hilt.android.AndroidEntryPoint
import dev.msfjarvis.lobsters.api.LobstersApi
import dev.msfjarvis.lobsters.compose.utils.IconResource
import dev.msfjarvis.lobsters.data.LobstersViewModel
import dev.msfjarvis.lobsters.ui.LobstersItem
@@ -34,7 +33,6 @@ val UrlLauncherAmbient = ambientOf<UrlLauncher> { error("Needs to be provided")
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject lateinit var urlLauncher: UrlLauncher
@Inject lateinit var apiClient: LobstersApi
private val viewModel: LobstersViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
@@ -58,7 +56,6 @@ fun LobstersApp(
val lastIndex = state.value.lastIndex
Scaffold(
topBar = { TopAppBar({ Text(text = stringResource(R.string.app_name)) }) },
bodyContent = {
if (state.value.isEmpty()) {
Column(
@@ -67,7 +64,7 @@ fun LobstersApp(
horizontalAlignment = Alignment.CenterHorizontally,
) {
IconResource(R.drawable.ic_sync_problem_24px)
Text(stringResource(R.string.nothing_to_see_here))
Text(stringResource(R.string.loading))
}
} else {
LazyColumnForIndexed(
@@ -84,6 +81,11 @@ fun LobstersApp(
)
}
}
}
},
floatingActionButton = {
FloatingActionButton(onClick = { viewModel.refreshPosts() }) {
IconResource(resourceId = R.drawable.ic_refresh_24px)
}
},
)
}
@@ -24,16 +24,19 @@ class LobstersViewModel @ViewModelInject constructor(
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
when (throwable) {
// Swallow known network errors that can be recovered from.
is UnknownHostException, is SocketTimeoutException -> {}
is UnknownHostException, is SocketTimeoutException -> {
if (_posts.value.isEmpty()) {
viewModelScope.launch {
dao.loadPosts().collectLatest { _posts.value = it }
}
}
}
else -> throw throwable
}
}
val posts: StateFlow<List<LobstersPost>> get() = _posts
init {
viewModelScope.launch {
dao.loadPosts().collectLatest { _posts.value = it }
}
getMorePostsInternal(true)
}
@@ -41,6 +44,11 @@ class LobstersViewModel @ViewModelInject constructor(
getMorePostsInternal(false)
}
fun refreshPosts() {
apiPage = 1
getMorePostsInternal(true)
}
private fun getMorePostsInternal(firstLoad: Boolean) {
viewModelScope.launch(coroutineExceptionHandler) {
val newPosts = lobstersApi.getHottestPosts(apiPage)
@@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M10,7L8,7v4L4,11v2h4v4h2v-4h4v-2h-4L10,7zM20,18h-2L18,7.38L15,8.4L15,6.7L19.7,5h0.3v13z"
android:fillColor="#000000"/>
</vector>
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M17.65,6.35c-1.63,-1.63 -3.94,-2.57 -6.48,-2.31 -3.67,0.37 -6.69,3.35 -7.1,7.02C3.52,15.91 7.27,20 12,20c3.19,0 5.93,-1.87 7.21,-4.56 0.32,-0.67 -0.16,-1.44 -0.9,-1.44 -0.37,0 -0.72,0.2 -0.88,0.53 -1.13,2.43 -3.84,3.97 -6.8,3.31 -2.22,-0.49 -4.01,-2.3 -4.48,-4.52C5.31,9.44 8.26,6 12,6c1.66,0 3.14,0.69 4.22,1.78l-1.51,1.51c-0.63,0.63 -0.19,1.71 0.7,1.71H19c0.55,0 1,-0.45 1,-1V6.41c0,-0.89 -1.08,-1.34 -1.71,-0.71l-0.64,0.65z"/>
</vector>
+1 -1
View File
@@ -1,4 +1,4 @@
<resources>
<string name="app_name">lobste.rs</string>
<string name="nothing_to_see_here">Nothing to see here</string>
<string name="loading">Loading posts</string>
</resources>