Custom Blocks
Okay, maybe all of this still isn't quite enough for what you need. Or maybe it works, but you are building such complicated entity commands and SQL queries in Lava that it's just not performant enough. Maybe you are just far more familiar with C# so you'd rather go that way. Possibly you want to build a plugin that provides a new block type with some settings for the admin to configure and then it magically does what it does.
Let's start with the most basic block configuration:
1
[DisplayName( "My Custom Block" )]
2
[Category( "Rock Solid Church > Mobile" )]
3
[Description( "A custom block to do magical things." )]
4
[IconCssClass( "fa fa-square" )]
5
public class MyCustomBlock : RockMobileBlockType
6
{
7
#region IRockMobileBlockType Implementation
8
​
9
int IRockMobileBlockType.RequiredMobileAbiVersion => 1;
10
​
11
string IRockMobileBlockType.MobileBlockType => "Rock.Mobile.Blocks.Content";
12
​
13
object IRockMobileBlockType.GetMobileConfigurationValues()
14
{
15
string content = "<Label Text=\"Hello World\" />";
16
​
17
return new Rock.Mobile.Common.Blocks.Content.Configuration
18
{
19
Content = content
20
}
21
}
22
​
23
#endregion
24
}
Copied!
So here is what we got. We are specifying that in order for this block to render, the mobile app needs to support ABI version 1. Next we are specifying the CLR class name on the mobile app that will handle the rendering, we are going to use the Rock.Mobile.Blocks.Content block so that we can update things. This block will render a simple text label that says Hello World.
But that is rather boring. Instead, lets have it render out something a bit more expressive.
1
object IRockMobileBlockType.GetMobileConfigurationValues()
2
{
3
string content = @"
4
<StackLayout>
5
<Rock:FormGroup Title=""Search"">
6
<Rock:FormField>
7
<Rock:TextBox x:Name=""SearchTerm"" />
8
</Rock:FormField>
9
</Rock:FormGroup>
10
​
11
<Button Text=""Search"" StyleClass=""btn, btn-primary"" Command=""{Binding Callback}"">
12
<Button.CommandParameter>
13
<Rock:CallbackParameters Name="":Search"">
14
<Rock:Parameter Name=""Term"" Value=""{Binding Source={x:Reference SearchTerm}, Path=Text}"" />
15
</Rock:CallbackParameters>
16
</Button.CommandParameter>
17
</Button>
18
</StackLayout>
19
";
20
​
21
return new Rock.Mobile.Common.Blocks.Content.Configuration
22
{
23
Content = content
24
}
25
}
Copied!
If you look very carefully, you'll notice that we prefixed the action command Search with a :. Here is the difference. Without the : prefix, the mobile block will call a single method:
1
[BlockAction]
2
public string GetCallbackContent( string command, Dictionary<string, object> parameters)
Copied!
That is perfectly fine and you can do it that way if you want. However, with the : prefix then it calls a method named for the Name and passes the parameters as actual method parameters. So in our case, our search method would be:
1
[BlockAction]
2
public string Search( string term )
Copied!
Either will achieve the same result, but the latter form might be easier to read in your code - especially if you have multiple commands being handled. The return type of these methods is a simple string that contains the XAML code used to render the UI. Let's use the latter form and build our search results:
1
[BlockAction]
2
public object Search( string term )
3
{
4
var searchClient = IndexContainer.GetActiveComponent();
5
var searchEntities = new List<int> { EntityTypeCache.Get( SystemGuid.EntityType.GROUP.AsGuid() ) };
6
var results = searchClient.Search( term,
7
SearchType.Wildcard,
8
searchEntities,
9
"GroupTypeName^Serving Team",
10
5,
11
0,
12
out int totalResults );
13
​
14
var sb = new StringBuilder();
15
sb.AppendLine( "<StackLayout>" );
16
foreach ( var result in results )
17
{
18
var button = [email protected]"
19
<Button Text=""{result.DocumentName}""
20
StyleClass=""btn, btn-primary""
21
Command=""{{Binding Callback}}"">
22
<Button.CommandParameter>
23
<Rock:CallbackParameters Name="":ShowGroup"">
24
<Rock:Parameter Name=""GroupId"" Value=""{result.Id}"" />
25
</Rock:CallbackParameters>
26
</Button.CommandParameter>
27
</Button>";
28
sb.AppendLine( button );
29
}
30
sb.AppendLine( "</StackLayout>" );
31
​
32
return new Rock.Mobile.Common.Blocks.Content.CallbackResponse
33
{
34
Content = sb.ToString()
35
};
36
}
Copied!
So we use the Universal Search component to get a list of all matching serving team groups. Then we loop over those results and build a stack layout of buttons. Each button will trigger the :ShowGroup command with a different GroupId value. A handler for such a command might look like:
1
[BlockAction]
2
public object ShowGroup( int groupId )
3
{
4
return new Rock.Mobile.Common.Blocks.Content.CallbackResponse
5
{
6
Content = "<Rock:NotificationBox NotificationType=""Error"" Text=""Not Implemented"" />"
7
};
8
}
Copied!
This is a simple handler that just displays a notification on the screen stating it hasn't been implemented yet. Obviously you will want to have to actually do something. For example, you might have it display some information about the group and then display two buttons: Cancel and Join.

Dynamic Initial Content

What we described above will leave you with a block whose initial content is static, that is it never changes unless the admin deploys a new application bundle. That might work for you, but you probably want to change that content based on query string parameters and such. Let's modify our block so that it instructs the mobile application to be dynamic.
1
[DisplayName( "My Custom Block" )]
2
[Category( "Rock Solid Church > Mobile" )]
3
[Description( "A custom block to do magical things." )]
4
[IconCssClass( "fa fa-square" )]
5
public class MyCustomBlock : RockMobileBlockType
6
{
7
#region IRockMobileBlockType Implementation
8
​
9
int IRockMobileBlockType.RequiredMobileAbiVersion => 1;
10
​
11
string IRockMobileBlockType.MobileBlockType => "Rock.Mobile.Blocks.Content";
12
​
13
object IRockMobileBlockType.GetMobileConfigurationValues()
14
{
15
return new Rock.Mobile.Common.Blocks.Content.Configuration
16
{
17
Content = null,
18
DynamicContent = true
19
}
20
}
21
​
22
#endregion
23
​
24
[BlockAction]
25
public object GetInitialContent()
26
{
27
return new Rock.Mobile.Common.Blocks.Content.CallbackResponse
28
{
29
Content = "<Label Text=\"Hello World\" />"
30
};
31
}
32
}
Copied!
Last modified 2yr ago
Copy link