Skip to content
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

[Feature Request] Synchronization #598

Closed
xVemu opened this issue Jan 16, 2024 · 3 comments
Closed

[Feature Request] Synchronization #598

xVemu opened this issue Jan 16, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@xVemu
Copy link

xVemu commented Jan 16, 2024

Is your feature request related to a problem? Please describe.
I'm trying to:

  1. Display data from local room DB.
  2. Display Refresh Indicator.
  3. Download, display and store data from network.

Does this package support such a feature?

@xVemu xVemu added the enhancement New feature or request label Jan 16, 2024
@github-project-automation github-project-automation bot moved this to 🆕 Triage in Store Roadmap Jan 16, 2024
@github-project-automation github-project-automation bot moved this from 🆕 Triage to ✅ Done in Store Roadmap Jan 16, 2024
@matt-ramotar
Copy link
Collaborator

Hey there, yes Store supports that. Also check out the current site for an overview of Fetcher, SourceOfTruth, and StoreBuilder. For now, here's a simple getting started:

Create Your Fetcher

suspend fun makeNetworkRequest(args: Args): Data {
  TODO()
}

val fetcher = Fetcher.of<Args, Data> { args ->
  flowOf(makeNetworkRequest(args))
}

Create Your SourceOfTruth

val sourceOfTruth = SourceOfTruth.of<Args, Data, Data>(
    reader = {args: Args ->
        flowOf(db.get(args))
    },
    writer = { (args: Args, data: Data) ->
        db.put(args, data)
    }
)

Build Your Store with StoreBuilder

val storeBuilder = StoreBuilder.from(
    fetcher = fetcher,
    sourceOfTruth = sourceOfTruth
)

val store = storeBuilder.build()

Stream Store Response

// In your ViewModel

val request = StoreReadRequest.fresh(args)
val stream = store.stream(request).collect { response: StoreReadResponse ->
  when (response) {
    is StoreReadResponse.Loading -> setState { Loading }
    is StoreReadResponse.Data -> setState { Data(response.data) }
    is StoreReadResponse.Error -> setState { Error }
  }
}

Display Refresh Indicator When Loading

// In your View

val state = viewModel.collectAsState()
when (state.value) {
  is Loading -> displayLoadingView()
  is Data -> displayLoadedView()
  is Error -> displayErrorView()
}

@xVemu
Copy link
Author

xVemu commented Jan 19, 2024

Display Refresh Indicator When Loading

// In your View

val state = viewModel.collectAsState()
when (state.value) {
  is Loading -> displayLoadingView()
  is Data -> displayLoadedView()
  is Error -> displayErrorView()
}

But when state is a type of Loading, I lose access to data, right?

@matt-ramotar
Copy link
Collaborator

Store's loading response doesn't include data. If I were solving that, my view model would be responsible for merging isRefreshing and lastData into one view state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

3 participants