Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material3.Text
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.runtime.livedata.observeAsState
import androidx.fragment.app.viewModels
import com.woocommerce.android.R
import com.woocommerce.android.ui.base.TopLevelFragment
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.compose.composeView
import com.woocommerce.android.ui.main.AppBarStatus
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -24,13 +24,12 @@ class BookingListFragment : TopLevelFragment() {
return
}

private val viewModel: BookingListViewModel by viewModels()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
WooThemeWithBackground {
Text("Empty Booking List screen: WIP")
}
return composeView {
viewModel.state.observeAsState().value?.let { state ->
Text("Empty Booking List screen: WIP")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.woocommerce.android.ui.bookings

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.asLiveData
import com.woocommerce.android.R
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event
import com.woocommerce.android.viewmodel.ScopedViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsStore
import org.wordpress.android.fluxc.persistence.entity.BookingEntity
import javax.inject.Inject

@HiltViewModel
class BookingListViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val bookingsStore: BookingsStore,
private val selectedSite: SelectedSite,
) : ScopedViewModel(savedStateHandle) {
private val isLoading = MutableStateFlow(false)

val state = combine(
bookingsStore.observeBookings(selectedSite.get()),
isLoading
) { bookings, loading ->
State(
bookings = bookings,
isLoading = loading,
onRefresh = { fetchBookings() }
)
}.asLiveData()

init {
launch { fetchBookings() }
}

fun fetchBookings() {
if (isLoading.value) return
launch {
isLoading.value = true
val result = bookingsStore.fetchBookings(selectedSite.get())
if (result.isError) {
// Surface a generic error
triggerEvent(Event.ShowSnackbar(R.string.error_generic))
}
isLoading.value = false
}
}

data class State(
val bookings: List<BookingEntity>, // To be replaced with Ui model
val isLoading: Boolean,
val onRefresh: () -> Unit,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class WCWPAPIEndpoint {

private static final String WC_PREFIX_ADMIN = "wc-admin";

private static final String WC_PREFIX_V2_BOOKINGS = "wc-bookings/v2";

private final String mEndpoint;

public WCWPAPIEndpoint(String endpoint) {
Expand Down Expand Up @@ -72,4 +74,9 @@ public String getPathWcTelemetry() {
public String getPathWcAdmin() {
return "/" + WC_PREFIX_ADMIN + mEndpoint;
}

public String getPathV2Bookings() {
return "/" + WC_PREFIX_V2_BOOKINGS + mEndpoint;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3955,4 +3955,4 @@
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '2d180f4d4c2ccaf6305a746959d1289d')"
]
}
}
}
Loading