Skip to content

Commit

Permalink
Merge branch 'trunk' into refactor-date-selector-usage-in-statsviewal…
Browse files Browse the repository at this point in the history
…lviewmodel
  • Loading branch information
irfano committed Jan 30, 2024
2 parents 0a04666 + f09894e commit 40a0457
Show file tree
Hide file tree
Showing 17 changed files with 309 additions and 31 deletions.
12 changes: 11 additions & 1 deletion WordPress/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,23 @@
<data
android:host="wordpress.com"
android:pathPattern="/media/.*"
android:scheme="http" />
android:scheme="https" />

<data
android:host="wordpress.com"
android:pathPattern="/domains/manage"
android:scheme="https" />

<data
android:host="wordpress.com"
android:pathPattern="/site-monitoring/.*"
android:scheme="https" />

<data
android:host="wordpress.com"
android:pathPattern="/site-monitoring/.*"
android:scheme="http" />

</intent-filter>
</activity-alias>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.wordpress.android.ui.mysite.menu.MenuActivity
import org.wordpress.android.ui.mysite.personalization.PersonalizationActivity
import org.wordpress.android.ui.quickstart.QuickStartEvent
import org.wordpress.android.ui.sitemonitor.SiteMonitorParentActivity
import org.wordpress.android.ui.sitemonitor.SiteMonitorType
import org.wordpress.android.util.ToastUtils
import org.wordpress.android.util.analytics.AnalyticsUtils
import javax.inject.Inject
Expand Down Expand Up @@ -163,5 +164,27 @@ class ActivityNavigator @Inject constructor() {
intent.putExtra(WordPress.SITE, site)
context.startActivity(intent)
}

fun openSiteMonitoringInNewStack(
context: Context,
site: SiteModel?,
siteMonitorType: SiteMonitorType = SiteMonitorType.METRICS
) {
if (site == null) {
ToastUtils.showToast(context, R.string.site_monitoring_cannot_be_started, ToastUtils.Duration.SHORT)
return
}
val props = mutableMapOf("site_monitoring_type" to siteMonitorType.name as Any)
AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.OPENED_SITE_MONITORING, site, props)
val taskStackBuilder = TaskStackBuilder.create(context)
val mainActivityIntent = getMainActivityInNewStack(context)
val intent = Intent(context, SiteMonitorParentActivity::class.java)
intent.putExtra(WordPress.SITE, site)
intent.putExtra(SiteMonitorParentActivity.ARG_SITE_MONITOR_TYPE_KEY, siteMonitorType)
taskStackBuilder
.addNextIntent(mainActivityIntent)
.addNextIntent(intent)
.startActivities()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.ShowS
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.StartCreateSiteFlow
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator.NavigateAction.ViewPostInReader
import org.wordpress.android.ui.sitecreation.misc.SiteCreationSource.DEEP_LINK
import org.wordpress.android.ui.sitemonitor.SiteMonitorType
import org.wordpress.android.ui.stats.StatsTimeframe
import org.wordpress.android.util.UriWrapper
import javax.inject.Inject
Expand Down Expand Up @@ -91,6 +92,11 @@ class DeepLinkNavigator
navigateAction.site
)
NavigateAction.DomainManagement -> ActivityLauncher.openDomainManagement(activity)
is NavigateAction.OpenSiteMonitoringForSite -> activityNavigator.openSiteMonitoringInNewStack(
activity,
navigateAction.site,
navigateAction.siteMonitorType
)
}
if (navigateAction != LoginForResult) {
activity.finish()
Expand Down Expand Up @@ -126,5 +132,7 @@ class DeepLinkNavigator
object OpenMedia : NavigateAction()
data class OpenMediaPickerForSite(val site: SiteModel) : NavigateAction()
object DomainManagement : NavigateAction()
data class OpenSiteMonitoringForSite(val site: SiteModel?, val siteMonitorType: SiteMonitorType) :
NavigateAction()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DeepLinkHandlers
mediaLinkHandler: MediaLinkHandler,
domainManagementLinkHandler: DomainManagementLinkHandler,
qrCodeMediaLinkHandler: QRCodeMediaLinkHandler,
siteMonitorLinkHandler: SiteMonitorLinkHandler
) {
private val handlers = listOf(
editorLinkHandler,
Expand All @@ -33,6 +34,7 @@ class DeepLinkHandlers
mediaLinkHandler,
domainManagementLinkHandler,
qrCodeMediaLinkHandler,
siteMonitorLinkHandler
)

private val _toast by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.wordpress.android.ui.deeplinks.handlers

import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.ui.deeplinks.DeepLinkNavigator
import org.wordpress.android.ui.deeplinks.DeepLinkUriUtils
import org.wordpress.android.ui.deeplinks.DeepLinkingIntentReceiverViewModel
import org.wordpress.android.ui.sitemonitor.SiteMonitorType
import org.wordpress.android.util.UriWrapper
import javax.inject.Inject

class SiteMonitorLinkHandler
@Inject constructor(private val deepLinkUriUtils: DeepLinkUriUtils) : DeepLinkHandler {
/**
* Returns true if the URI looks like `wordpress.com/SITE_MONITORING_PATH`
*/
override fun shouldHandleUrl(uri: UriWrapper): Boolean {
return (uri.host == DeepLinkingIntentReceiverViewModel.HOST_WORDPRESS_COM &&
uri.pathSegments.firstOrNull() == SITE_MONITORING_PATH) || uri.host == SITE_MONITORING_PATH
}

override fun buildNavigateAction(uri: UriWrapper): DeepLinkNavigator.NavigateAction {
val targetHost = extractHost(uri)
val site: SiteModel? = deepLinkUriUtils.hostToSite(targetHost)
val siteMonitorType = urlToType(uri.toString())
return DeepLinkNavigator.NavigateAction.OpenSiteMonitoringForSite(site, siteMonitorType)
}

private fun extractHost(uri: UriWrapper): String {
if (uri.pathSegments.size <= 1) return ""
return uri.pathSegments[1]
}

override fun stripUrl(uri: UriWrapper): String {
return buildString {
val offset = if (uri.host == SITE_MONITORING_PATH) {
append(DeepLinkingIntentReceiverViewModel.APPLINK_SCHEME)
0
} else {
append("${DeepLinkingIntentReceiverViewModel.HOST_WORDPRESS_COM}/")
1
}
append(SITE_MONITORING_PATH)
val pathSegments = uri.pathSegments
val size = pathSegments.size
val hasSiteUrl = if (size > offset + 1) pathSegments.getOrNull(offset + 1) != null else false
if (hasSiteUrl) {
append("/${DeepLinkingIntentReceiverViewModel.SITE_DOMAIN}")
}
}
}

private fun urlToType(url: String): SiteMonitorType {
return when {
url.contains(PHP_LOGS_PATTERN) -> SiteMonitorType.PHP_LOGS
url.contains(WEB_SERVER_LOGS_PATTERN) -> SiteMonitorType.WEB_SERVER_LOGS
else -> SiteMonitorType.METRICS
}
}

companion object {
private const val SITE_MONITORING_PATH = "site-monitoring"
private const val PHP_LOGS_PATTERN = "/php"
private const val WEB_SERVER_LOGS_PATTERN = "/web"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ import org.wordpress.android.ui.compose.components.MainTopAppBar
import org.wordpress.android.ui.compose.components.NavigationIcons
import org.wordpress.android.ui.compose.theme.AppTheme
import org.wordpress.android.util.extensions.getSerializableExtraCompat
import javax.inject.Inject

@AndroidEntryPoint
class SiteMonitorParentActivity: AppCompatActivity() {
@Inject
lateinit var siteMonitorUtils: SiteMonitorUtils

private var savedStateSparseArray = SparseArray<Fragment.SavedState>()
private var currentSelectItemId = 0

@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

siteMonitorUtils.trackActivityLaunched()

if (savedInstanceState != null) {
savedStateSparseArray = savedInstanceState.getSparseParcelableArray(
SAVED_STATE_CONTAINER_KEY
Expand Down Expand Up @@ -65,7 +72,13 @@ class SiteMonitorParentActivity: AppCompatActivity() {
return requireNotNull(intent.getSerializableExtraCompat(WordPress.SITE)) as SiteModel
}

private fun getInitialTab(): SiteMonitorType {
return intent?.getSerializableExtraCompat(ARG_SITE_MONITOR_TYPE_KEY) as SiteMonitorType?
?: SiteMonitorType.METRICS
}

companion object {
const val ARG_SITE_MONITOR_TYPE_KEY = "ARG_SITE_MONITOR_TYPE_KEY"
const val SAVED_STATE_CONTAINER_KEY = "ContainerKey"
const val SAVED_STATE_CURRENT_TAB_KEY = "CurrentTabKey"
}
Expand All @@ -90,7 +103,9 @@ class SiteMonitorParentActivity: AppCompatActivity() {
SiteMonitorTabNavigation(selectedTab) { selectedTab ->
val item = enumValues<SiteMonitorTabItem>().find {
it.route == selectedTab
} ?: SiteMonitorTabItem.Metrics
} ?: initialItem(getInitialTab())

siteMonitorUtils.trackTabLoaded(item.siteMonitorType)

SiteMonitorFragmentContainer(
modifier = Modifier.fillMaxSize(),
Expand All @@ -104,6 +119,12 @@ class SiteMonitorParentActivity: AppCompatActivity() {
}
}

private fun initialItem(type: SiteMonitorType): SiteMonitorTabItem {
return enumValues<SiteMonitorTabItem>().find {
it.siteMonitorType == type
} ?: SiteMonitorTabItem.Metrics
}

private fun getCommitFunction(
fragment : Fragment,
tag: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.wordpress.android.ui.sitemonitor

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.MaterialTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.TabRowDefaults
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.dp

@Composable
fun SiteMonitorTabHeader(navController: (String) -> Unit) {
Expand All @@ -25,16 +28,23 @@ fun SiteMonitorTabHeader(navController: (String) -> Unit) {
)
TabRow(
selectedTabIndex = selectedTabIndex,
containerColor = MaterialTheme.colorScheme.surface,
contentColor = MaterialTheme.colorScheme.onSurface,
containerColor = MaterialTheme.colors.surface,
contentColor = MaterialTheme.colors.onSurface,
indicator = { tabPositions ->
// Customizing the indicator color and style
TabRowDefaults.Indicator(
Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex]),
color = MaterialTheme.colors.onSurface,
height = 2.0.dp
)
}
) {
tabs.forEachIndexed { index, item ->
Tab(
text = {
Column (horizontalAlignment = Alignment.CenterHorizontally) {
Text(
text = stringResource(item.title),
fontSize = 12.sp,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.wordpress.android.ui.sitemonitor

import android.text.TextUtils
import android.util.Log
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -32,7 +31,6 @@ class SiteMonitorTabViewModel @Inject constructor(
val uiState: StateFlow<SiteMonitorUiState> = _uiState

fun start(type: SiteMonitorType, urlTemplate: String, site: SiteModel) {
Log.i("Track", "TheViewModel start with $urlTemplate and $type")
this.siteMonitorType = type
this.urlTemplate = urlTemplate
this.site = site
Expand Down Expand Up @@ -106,7 +104,6 @@ class SiteMonitorTabViewModel @Inject constructor(
}

fun onUrlLoaded() {
siteMonitorUtils.trackTabLoaded(siteMonitorType)
postUiState(SiteMonitorUiState.Loaded)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ data class SiteMonitorModel(
val url: String,
val addressToLoad: String
)
enum class SiteMonitorType {
METRICS,
PHP_LOGS,
WEB_SERVER_LOGS
enum class SiteMonitorType(val analyticsDescription: String) {
METRICS("metrics"),
PHP_LOGS("php_logs"),
WEB_SERVER_LOGS("server_logs")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.wordpress.android.ui.sitemonitor

import android.util.Log
import org.wordpress.android.WordPress
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.ui.WPWebViewActivity
Expand All @@ -26,22 +25,18 @@ class SiteMonitorUtils @Inject constructor(

fun sanitizeSiteUrl(url: String?) = url?.replace(Regex(HTTP_PATTERN), "") ?: ""

fun urlToType(url: String): SiteMonitorType {
return when {
url.contains(PHP_LOGS_PATTERN) -> SiteMonitorType.PHP_LOGS
url.contains(WEB_SERVER_LOGS_PATTERN) -> SiteMonitorType.WEB_SERVER_LOGS
else -> SiteMonitorType.METRICS
}
}

fun trackTabLoaded(siteMonitorType: SiteMonitorType) {
// todo: need to set this up properly with track events
Log.i(javaClass.simpleName, "track TabLoaded with $siteMonitorType")
analyticsTrackerWrapper.track(
AnalyticsTracker.Stat.SITE_MONITORING_TAB_SHOWN,
mapOf(
TAB_TRACK_KEY to siteMonitorType.analyticsDescription
))
}

companion object {
const val HTTP_PATTERN = "(https?://)"
const val PHP_LOGS_PATTERN = "/php"
const val WEB_SERVER_LOGS_PATTERN = "/web"
const val TAB_TRACK_KEY = "tab"
}
}
Loading

0 comments on commit 40a0457

Please sign in to comment.