New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SwiftUI and some compose refactors #201
Merged
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
02dd01b
Update badges to touchlab-lab
brady-aiello 65ba498
Merge branch 'main' into main
brady-aiello 57768d3
Merge pull request #1 from touchlab/main
brady-aiello 781a293
[Issue-0000] Upgrade to Gradle 7
brady-aiello d6084a1
Update workflows to setup Java 11, update PR template
brady-aiello cc19c52
[Issue-0000] Add Jetpack Compose
brady-aiello dabf620
Update Compose dependencies to use 1.0.0-rc01
brady-aiello 5f77a19
Use derivedStateOf for isRefreshingState
brady-aiello 7c1dfaa
[Issue-0000] Alternative Loading state refactor
brady-aiello e4adcf7
Lint fix
brady-aiello 2685a43
Simple SwiftUI implementation
russhwolf 462e650
Compose reorg and lifecycle-aware flow
russhwolf a047aef
Compose preview
russhwolf b15e3a4
SwiftUI preview
russhwolf 0c7d485
Misc config adjustments and cleanup
russhwolf 51ce5ec
Make entire row clickable in SwiftUI
russhwolf 8eb1873
Bump to release versions
russhwolf 935848a
Revert badges
russhwolf c6c399e
Run ktlintFormat
russhwolf 8a2ba93
Bump to Compose 1.0.1 and Kotlin 1.5.21
russhwolf 66b102d
Remove extra derived state
russhwolf 6db990c
Oops
russhwolf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 11 additions & 82 deletions
93
app/src/main/java/co/touchlab/kampkit/android/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,30 @@ | ||
package co.touchlab.kampkit.android | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout | ||
import co.touchlab.kampkit.android.adapter.MainAdapter | ||
import co.touchlab.kampkit.android.databinding.ActivityMainBinding | ||
import co.touchlab.kampkit.db.Breed | ||
import co.touchlab.kampkit.models.DataState | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import co.touchlab.kampkit.android.ui.MainScreen | ||
import co.touchlab.kampkit.android.ui.theme.KaMPKitTheme | ||
import co.touchlab.kermit.Kermit | ||
import com.google.android.material.snackbar.Snackbar | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import org.koin.core.parameter.parametersOf | ||
|
||
class MainActivity : AppCompatActivity(), KoinComponent { | ||
class MainActivity : ComponentActivity(), KoinComponent { | ||
|
||
private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } | ||
private val log: Kermit by inject { parametersOf("MainActivity") } | ||
private val viewModel: BreedViewModel by viewModel() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(binding.root) | ||
|
||
val adapter = MainAdapter { viewModel.updateBreedFavorite(it) } | ||
val swipeRefresh: SwipeRefreshLayout = binding.swipeRefresh | ||
val recyclerView: RecyclerView = binding.breedList | ||
|
||
swipeRefresh.setOnRefreshListener { | ||
viewModel.refreshBreeds(true) | ||
} | ||
|
||
collectDataStateFlow( | ||
{ | ||
/* Display loading view */ | ||
swipeRefresh.isRefreshing = true | ||
}, | ||
{ breedList -> | ||
/* Display success view */ | ||
swipeRefresh.isRefreshing = false | ||
recyclerView.visibility = View.VISIBLE | ||
log.v { "View updating with ${breedList.size} breeds" } | ||
adapter.submitList(breedList) | ||
}, | ||
{ exception -> | ||
/* Display error view */ | ||
swipeRefresh.isRefreshing = false | ||
recyclerView.visibility = View.GONE | ||
log.e { "Displaying error: $exception" } | ||
Snackbar.make( | ||
binding.breedList, | ||
exception, | ||
Snackbar.LENGTH_SHORT | ||
).show() | ||
}, | ||
{ | ||
/* Display empty response view */ | ||
swipeRefresh.isRefreshing = false | ||
} | ||
) | ||
|
||
binding.breedList.adapter = adapter | ||
binding.breedList.layoutManager = LinearLayoutManager(this) | ||
|
||
viewModel.refreshBreeds() | ||
} | ||
|
||
private fun collectDataStateFlow( | ||
onLoading: () -> Unit, | ||
onSuccess: (List<Breed>) -> Unit, | ||
onError: (String) -> Unit, | ||
onEmpty: () -> Unit, | ||
) { | ||
lifecycleScope.launch { | ||
viewModel.breedStateFlow.collect { dataState -> | ||
when (dataState) { | ||
is DataState.Success -> { | ||
onSuccess(dataState.data.allItems) | ||
} | ||
is DataState.Error -> { | ||
onError(dataState.exception) | ||
} | ||
DataState.Empty -> { | ||
onEmpty() | ||
} | ||
DataState.Loading -> { | ||
onLoading() | ||
} | ||
} | ||
setContent { | ||
KaMPKitTheme { | ||
MainScreen(viewModel, log) | ||
} | ||
} | ||
if (viewModel.breedStateFlow.value.data == null) { | ||
viewModel.refreshBreeds() | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
app/src/main/java/co/touchlab/kampkit/android/adapter/MainAdapter.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AppCompatActivity
is aComponentActivity
; why the change?