Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Model
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule

Public Class CountListView
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        'SetNavitemCaptionCount("RegistryTasks_ListView_Not_Completed", "@RegistryTask_ListView_Not_Completed")
        'SetNavitemCaptionCount("CRM_Leads_Activity_Task_ListView", "CRM_Leads_Activity_Task_ListView")
    End Sub
    Sub SetNavitemCaptionCount(_ListView As String, _NavigationItem As String)
        Dim ListViewModel As IModelListView = Application.FindModelView(_ListView)
        Dim count As Long = TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.Evaluate(ListViewModel.ModelClass.TypeInfo.Type, New AggregateOperand("", Aggregate.Count), CriteriaOperator.Parse(ListViewModel.Criteria))
        Dim navItem As ChoiceActionItem = Application.MainWindow.GetController(Of ShowNavigationItemController).ShowNavigationItemAction.Items.Find(_NavigationItem, ChoiceActionItemFindType.Recursive, ChoiceActionItemFindTarget.Any)
        Dim sArray() As String
        If navItem IsNot Nothing Then
            sArray = Split(navItem.Caption, "[")
            If UBound(sArray) > 0 Then
                navItem.Caption = Replace(sArray(0).ToString, "[", "").Trim + String.Format(" [{0}]", count)
            Else
                navItem.Caption += String.Format(" [{0}]", count)
            End If

        End If
    End Sub
End Class
