Skip to content

Commit

Permalink
Merge pull request #10677 from woocommerce/issue/10666-see-report-hub
Browse files Browse the repository at this point in the history
Add see report button in Analytic Hub
  • Loading branch information
atorresveiga authored Feb 7, 2024
2 parents 1b7105a + 9557832 commit 5bf254e
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import kotlinx.coroutines.launch
import java.util.Date

@AndroidEntryPoint
class AnalyticsHubFragment :
BaseFragment(R.layout.fragment_analytics) {
class AnalyticsHubFragment : BaseFragment(R.layout.fragment_analytics) {
companion object {
const val KEY_DATE_RANGE_SELECTOR_RESULT = "key_order_status_result"
const val DATE_PICKER_FRAGMENT_TAG = "DateRangePicker"
Expand Down Expand Up @@ -110,6 +109,9 @@ class AnalyticsHubFragment :
private fun bind(view: View) {
_binding = FragmentAnalyticsBinding.bind(view)
binding.analyticsDateSelectorCard.setOnClickListener { viewModel.onDateRangeSelectorClick() }
binding.analyticsOrdersCard.onSeeReportClickListener = { url -> viewModel.onSeeReport(url) }
binding.analyticsRevenueCard.onSeeReportClickListener = { url -> viewModel.onSeeReport(url) }
binding.analyticsProductsCard.onSeeReportClickListener = { url -> viewModel.onSeeReport(url) }
}

private fun handleStateChange(viewState: AnalyticsViewState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.woocommerce.android.model.OrdersStat
import com.woocommerce.android.model.ProductsStat
import com.woocommerce.android.model.RevenueStat
import com.woocommerce.android.model.SessionStat
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.analytics.hub.RefreshIndicator.NotShowIndicator
import com.woocommerce.android.ui.analytics.hub.RefreshIndicator.ShowIndicator
import com.woocommerce.android.ui.analytics.hub.daterangeselector.AnalyticsHubDateRangeSelectorViewState
Expand Down Expand Up @@ -70,6 +71,8 @@ class AnalyticsHubViewModel @Inject constructor(
private val feedbackRepository: FeedbackRepository,
private val tracker: AnalyticsTrackerWrapper,
private val dateUtils: DateUtils,
private val selectedSite: SelectedSite,
private val getReportUrl: GetReportUrl,
savedState: SavedStateHandle
) : ScopedViewModel(savedState) {

Expand Down Expand Up @@ -132,6 +135,17 @@ class AnalyticsHubViewModel @Inject constructor(
rangeSelectionState.value = selectionType.generateLocalizedSelectionData()
}

fun onSeeReport(url: String) {
selectedSite.getOrNull()?.let { site ->
val event = if (site.isWpComStore) {
AnalyticsViewEvent.OpenWPComWebView(url)
} else {
AnalyticsViewEvent.OpenUrl(url)
}
triggerEvent(event)
} ?: triggerEvent(AnalyticsViewEvent.OpenUrl(url))
}

fun onCustomRangeSelected(startDate: Date, endDate: Date) {
rangeSelectionState.value = SelectionType.CUSTOM.generateLocalizedSelectionData(
startDate = startDate,
Expand Down Expand Up @@ -310,7 +324,8 @@ class AnalyticsHubViewModel @Inject constructor(
stats.conversionRate,
null,
listOf()
)
),
reportUrl = null
)

private fun buildRevenueDataViewState(revenueStat: RevenueStat) =
Expand All @@ -328,6 +343,10 @@ class AnalyticsHubViewModel @Inject constructor(
if (revenueStat.netDelta is DeltaPercentage.Value) revenueStat.netDelta.value else null,
revenueStat.netRevenueByInterval.map { it.toFloat() }
),
reportUrl = getReportUrl(
selection = ranges,
card = ReportCard.Revenue
)
)

private fun buildOrdersDataViewState(ordersStats: OrdersStat) =
Expand All @@ -352,6 +371,10 @@ class AnalyticsHubViewModel @Inject constructor(
null
},
ordersStats.avgOrderValueByInterval.map { it.toFloat() }
),
reportUrl = getReportUrl(
selection = ranges,
card = ReportCard.Orders
)
)

Expand Down Expand Up @@ -379,7 +402,11 @@ class AnalyticsHubViewModel @Inject constructor(
),
index != products.size - 1
)
}
},
reportUrl = getReportUrl(
selection = ranges,
card = ReportCard.Products
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.woocommerce.android.ui.analytics.hub

import com.woocommerce.android.extensions.adminUrlOrDefault
import com.woocommerce.android.extensions.formatToYYYYmmDD
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.analytics.ranges.StatsTimeRangeSelection
import javax.inject.Inject

class GetReportUrl @Inject constructor(
private val selectedSite: SelectedSite
) {
operator fun invoke(
selection: StatsTimeRangeSelection,
card: ReportCard
): String? {
return selectedSite.getOrNull()?.let { siteModel ->
val adminURL = siteModel.adminUrlOrDefault
val page = "page=wc-admin"
val path = when (card) {
ReportCard.Revenue -> "path=%2Fanalytics%2Frevenue"
ReportCard.Orders -> "path=%2Fanalytics%2Forders"
ReportCard.Products -> "path=%2Fanalytics%2Fproducts"
}
val period = getPeriod(selection)
val compare = "compare=previous_period"
"${adminURL}admin.php?$page&$path&$period&$compare"
}
}

private fun getPeriod(selection: StatsTimeRangeSelection): String {
return when (selection.selectionType) {
StatsTimeRangeSelection.SelectionType.TODAY -> "period=today"
StatsTimeRangeSelection.SelectionType.YESTERDAY -> "period=yesterday"
StatsTimeRangeSelection.SelectionType.LAST_WEEK -> "period=last_week"
StatsTimeRangeSelection.SelectionType.LAST_MONTH -> "period=last_month"
StatsTimeRangeSelection.SelectionType.LAST_QUARTER -> "period=last_quarter"
StatsTimeRangeSelection.SelectionType.LAST_YEAR -> "period=last_year"
StatsTimeRangeSelection.SelectionType.WEEK_TO_DATE -> "period=week"
StatsTimeRangeSelection.SelectionType.MONTH_TO_DATE -> "period=month"
StatsTimeRangeSelection.SelectionType.QUARTER_TO_DATE -> "period=quarter"
StatsTimeRangeSelection.SelectionType.YEAR_TO_DATE -> "period=year"
StatsTimeRangeSelection.SelectionType.CUSTOM -> {
val startDate = selection.currentRange.start.formatToYYYYmmDD()
val endDate = selection.currentRange.end.formatToYYYYmmDD()
"period=custom&after=$startDate&before=$endDate"
}
}
}
}

enum class ReportCard { Revenue, Orders, Products }
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.woocommerce.android.ui.analytics.hub.informationcard.AnalyticsHubInfo
import com.woocommerce.android.ui.analytics.hub.informationcard.AnalyticsHubInformationViewState.NoDataState
import com.woocommerce.android.widgets.SkeletonView

typealias SeeReportClickListener = (url: String) -> Unit

class AnalyticsHubInformationCardView @JvmOverloads constructor(
ctx: Context,
attrs: AttributeSet? = null,
Expand All @@ -21,6 +23,8 @@ class AnalyticsHubInformationCardView @JvmOverloads constructor(
val binding = AnalyticsInformationCardViewBinding.inflate(LayoutInflater.from(ctx), this)
private var skeletonView = SkeletonView()

var onSeeReportClickListener: SeeReportClickListener? = null

internal fun updateInformation(viewState: AnalyticsHubInformationViewState) {
visibility = if (viewState is HiddenState) View.GONE else View.VISIBLE
when (viewState) {
Expand Down Expand Up @@ -49,6 +53,14 @@ class AnalyticsHubInformationCardView @JvmOverloads constructor(
binding.leftAnalyticsSection.visibility = VISIBLE
binding.rightAnalyticsSection.visibility = VISIBLE
binding.noDataText.visibility = GONE
if (viewState.reportUrl != null) {
binding.reportGroup.visibility = VISIBLE
binding.reportText.setOnClickListener {
onSeeReportClickListener?.let { it(viewState.reportUrl) }
}
} else {
binding.reportGroup.visibility = GONE
}
visibility = VISIBLE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sealed class AnalyticsHubInformationViewState {
data class DataViewState(
val title: String,
val leftSection: AnalyticsHubInformationSectionViewState,
val rightSection: AnalyticsHubInformationSectionViewState
val rightSection: AnalyticsHubInformationSectionViewState,
val reportUrl: String?
) : AnalyticsHubInformationViewState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.card.MaterialCardView
import com.woocommerce.android.R
import com.woocommerce.android.databinding.AnalyticsListCardViewBinding
import com.woocommerce.android.ui.analytics.hub.informationcard.SeeReportClickListener
import com.woocommerce.android.ui.analytics.hub.listcard.AnalyticsHubListViewState.DataViewState
import com.woocommerce.android.ui.analytics.hub.listcard.AnalyticsHubListViewState.LoadingViewState
import com.woocommerce.android.ui.analytics.hub.listcard.AnalyticsHubListViewState.NoDataState
Expand All @@ -26,6 +27,7 @@ class AnalyticsHubListCardView @JvmOverloads constructor(
) : MaterialCardView(ctx, attrs, defStyleAttr) {
val binding = AnalyticsListCardViewBinding.inflate(LayoutInflater.from(ctx), this)
private var skeletonView = SkeletonView()
var onSeeReportClickListener: SeeReportClickListener? = null

internal fun updateInformation(viewState: AnalyticsHubListViewState) {
when (viewState) {
Expand Down Expand Up @@ -77,6 +79,14 @@ class AnalyticsHubListCardView @JvmOverloads constructor(
binding.analyticsListLeftHeader.visibility = VISIBLE
binding.analyticsListRightHeader.visibility = VISIBLE
binding.noDataText.visibility = GONE
if (viewState.reportUrl != null) {
binding.reportGroup.visibility = VISIBLE
binding.reportText.setOnClickListener {
onSeeReportClickListener?.let { it(viewState.reportUrl) }
}
} else {
binding.reportGroup.visibility = GONE
}
}

private fun setNoDataViewState(viewState: NoDataState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ sealed class AnalyticsHubListViewState {
val delta: Int?,
val listLeftHeader: String,
val listRightHeader: String,
val items: List<AnalyticsHubListCardItemViewState>
val items: List<AnalyticsHubListCardItemViewState>,
val reportUrl: String?
) : AnalyticsHubListViewState() {
val sign: String
get() = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.woocommerce.android.ui.analytics.hub.sync

import com.woocommerce.android.extensions.adminUrlOrDefault
import com.woocommerce.android.extensions.formatToYYYYmmDDhhmmss
import com.woocommerce.android.model.DeltaPercentage
import com.woocommerce.android.model.OrdersStat
Expand Down Expand Up @@ -196,10 +195,6 @@ class AnalyticsRepository @Inject constructor(
)
}

fun getRevenueAdminPanelUrl() = getAdminPanelUrl() + ANALYTICS_REVENUE_PATH
fun getOrdersAdminPanelUrl() = getAdminPanelUrl() + ANALYTICS_ORDERS_PATH
fun getProductsAdminPanelUrl() = getAdminPanelUrl() + ANALYTICS_PRODUCTS_PATH

private suspend fun getCurrentPeriodStats(
rangeSelection: StatsTimeRangeSelection,
fetchStrategy: FetchStrategy
Expand Down Expand Up @@ -321,13 +316,8 @@ class AnalyticsRepository @Inject constructor(
}

private fun getCurrencyCode() = wooCommerceStore.getSiteSettings(selectedSite.get())?.currencyCode
private fun getAdminPanelUrl() = selectedSite.getIfExists()?.adminUrlOrDefault

companion object {
const val ANALYTICS_REVENUE_PATH = "admin.php?page=wc-admin&path=%2Fanalytics%2Frevenue"
const val ANALYTICS_ORDERS_PATH = "admin.php?page=wc-admin&path=%2Fanalytics%2Forders"
const val ANALYTICS_PRODUCTS_PATH = "admin.php?page=wc-admin&path=%2Fanalytics%2Fproducts"

const val ZERO_VALUE = 0.0
const val MINUS_ONE = -1
const val ONE_H_PERCENT = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />

<com.google.android.material.divider.MaterialDivider
android:id="@+id/reportDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@+id/leftAnalyticsSection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/reportText"
style="@style/Woo.Card.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:textSize="@dimen/text_minor_125"
android:textStyle="bold"
android:text="@string/see_report"
tools:visibility="visible"
app:layout_constraintTop_toBottomOf="@+id/reportDivider"
app:layout_constraintStart_toStartOf="parent"
android:padding="@dimen/major_100"
android:background="?attr/selectableItemBackground"
/>
<ImageView
android:id="@+id/reportIcon"
android:layout_width="@dimen/major_200"
android:layout_height="@dimen/major_200"
android:src="@drawable/ic_arrow_right"
app:layout_constraintTop_toTopOf="@+id/reportText"
app:layout_constraintBottom_toBottomOf="@+id/reportText"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="@dimen/minor_100"/>

<androidx.constraintlayout.widget.Group
android:id="@+id/reportGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="reportDivider,reportText,reportIcon"
android:visibility="gone"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
</merge>

42 changes: 42 additions & 0 deletions WooCommerce/src/main/res/layout/analytics_list_card_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,48 @@
tools:itemCount="6"
tools:listitem="@layout/analytics_list_card_item_view"/>

<com.google.android.material.divider.MaterialDivider
android:id="@+id/reportDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@+id/analyticsItemsList"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/reportText"
style="@style/Woo.Card.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:textSize="@dimen/text_minor_125"
android:textStyle="bold"
android:text="@string/see_report"
tools:visibility="visible"
app:layout_constraintTop_toBottomOf="@+id/reportDivider"
app:layout_constraintStart_toStartOf="parent"
android:padding="@dimen/major_100"
android:background="?attr/selectableItemBackground"
/>
<ImageView
android:id="@+id/reportIcon"
android:layout_width="@dimen/major_200"
android:layout_height="@dimen/major_200"
android:src="@drawable/ic_arrow_right"
app:layout_constraintTop_toTopOf="@+id/reportText"
app:layout_constraintBottom_toBottomOf="@+id/reportText"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="@dimen/minor_100"/>

<androidx.constraintlayout.widget.Group
android:id="@+id/reportGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="reportDivider,reportText,reportIcon"
android:visibility="gone"
/>


</androidx.constraintlayout.widget.ConstraintLayout>
</merge>

1 change: 1 addition & 0 deletions WooCommerce/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3999,5 +3999,6 @@
<string name="order_configuration_product_selection">Select product %s</string>
<string name="order_configuration_product_selection_control">Product selection</string>
<string name="disabled">Disabled</string>
<string name="see_report">See Report</string>

</resources>
Loading

0 comments on commit 5bf254e

Please sign in to comment.