Skip to content

Commit 15a078f

Browse files
committed
Added scrollview
Added search text page and view model Added search string helper method and its unit tests Added another appshell when app is opened through open with
1 parent 296b04b commit 15a078f

15 files changed

Lines changed: 413 additions & 14 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
11+
<PackageReference Include="xunit" Version="2.4.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\BasicPlainTextReaderApp\BasicPlainTextReaderApp\BasicPlainTextReaderApp.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Xunit;
5+
6+
namespace BasicPlainTextReaderApp.UnitTests
7+
{
8+
public class HelperUnitTests
9+
{
10+
[Fact]
11+
public void Test_SearchAllStrings_ShouldReturnAllMatchingTexts()
12+
{
13+
//Arrange
14+
string text = "Text to be searched ñ\r\nAnother line t l";
15+
string search = "t";
16+
int getFromSides = 4;
17+
//Act
18+
var list = Helper.SearchAllStrings(text, search, getFromSides);
19+
20+
//Assert
21+
Assert.Equal(5, list.Count);
22+
Assert.Equal("Text to b", list[0]);
23+
Assert.Equal("Text to b", list[1]);
24+
Assert.Equal("ext to be", list[2]);
25+
Assert.Equal("\nAnother ", list[3]);
26+
Assert.Equal("ine t l", list[4]);
27+
}
28+
29+
[Fact]
30+
public void Test_SearchAllStrings_ShouldReturnSameText()
31+
{
32+
//Arrange
33+
string text = "Teñxt";
34+
string search = "teñxt";
35+
int getFromSides = 6;
36+
//Act
37+
var list = Helper.SearchAllStrings(text, search, getFromSides);
38+
39+
//Assert
40+
Assert.Single(list);
41+
Assert.Equal("Teñxt", list[0]);
42+
}
43+
44+
[Fact]
45+
public void Test_SearchAllStrings_ShouldExactEndOfText()
46+
{
47+
//Arrange
48+
string text = "12345t12345";
49+
string search = "t";
50+
int getFromSides = 5;
51+
//Act
52+
var list = Helper.SearchAllStrings(text, search, getFromSides);
53+
54+
//Assert
55+
Assert.Single(list);
56+
Assert.Equal("12345t12345", list[0]);
57+
}
58+
59+
/// <summary>
60+
/// This tests the case edge where `getFromSides` is exact where string ends
61+
/// </summary>
62+
[Fact]
63+
public void Test_SearchAllStrings_ExactFromGetSidesOnEnd()
64+
{
65+
//Arrange
66+
string text = "12345t123456";
67+
string search = "t";
68+
int getFromSides = 5;
69+
//Act
70+
var list = Helper.SearchAllStrings(text, search, getFromSides);
71+
72+
//Assert
73+
Assert.Single(list);
74+
Assert.Equal("12345t12345", list[0]);
75+
}
76+
}
77+
}

BasicPlainTextReaderApp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicPlainTextReaderApp", "
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.Library", "BasicPlainTextReaderApp.Library\BasicPlainTextReaderApp.Library.csproj", "{F889341B-08D1-4416-A989-D0E03D434B11}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.UnitTests", "BasicPlainTextReaderApp.UnitTests\BasicPlainTextReaderApp.UnitTests.csproj", "{CCA174D5-E76D-4A84-B434-99B45252426D}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +33,10 @@ Global
3133
{F889341B-08D1-4416-A989-D0E03D434B11}.Debug|Any CPU.Build.0 = Debug|Any CPU
3234
{F889341B-08D1-4416-A989-D0E03D434B11}.Release|Any CPU.ActiveCfg = Release|Any CPU
3335
{F889341B-08D1-4416-A989-D0E03D434B11}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{CCA174D5-E76D-4A84-B434-99B45252426D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{CCA174D5-E76D-4A84-B434-99B45252426D}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{CCA174D5-E76D-4A84-B434-99B45252426D}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{CCA174D5-E76D-4A84-B434-99B45252426D}.Release|Any CPU.Build.0 = Release|Any CPU
3440
EndGlobalSection
3541
GlobalSection(SolutionProperties) = preSolution
3642
HideSolutionNode = FALSE

BasicPlainTextReaderApp/BasicPlainTextReaderApp/App.xaml.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,36 @@ namespace BasicPlainTextReaderApp
88
{
99
public partial class App : Application
1010
{
11-
readonly TextModel _data;
1211
public App(TextModel data = null)
1312
{
1413
InitializeComponent();
1514

16-
_data = data;
17-
MainPage = new AppShell(this);
15+
if (data == null)
16+
{
17+
MainPage = new AppShell(this);
18+
}
19+
else
20+
{
21+
MainPage = new AppShellOpenWith(this, data);
22+
}
1823
}
1924

20-
public void GoToCurrentTextPage()
25+
public void GoToTextPage()
26+
{
27+
var page = new TextPage();
28+
Shell.Current.Navigation.PushAsync(page);
29+
Shell.Current.FlyoutIsPresented = false;
30+
}
31+
public void GoToAboutPage()
2132
{
22-
var page = new TextPage(_data);
33+
var page = new AboutPage();
2334
Shell.Current.Navigation.PushAsync(page);
2435
Shell.Current.FlyoutIsPresented = false;
2536
}
2637

2738
protected override void OnStart()
2839
{
29-
if(_data != null)
30-
{
31-
GoToCurrentTextPage();
32-
}
40+
3341
}
3442

3543
protected override void OnSleep()
@@ -38,6 +46,11 @@ protected override void OnSleep()
3846

3947
protected override void OnResume()
4048
{
49+
//TODO: not sure if this is needed
50+
//if (MainPage is AppShellOpenWith shell)
51+
//{
52+
// shell.SetTextData();
53+
//}
4154
}
4255
}
4356
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/AppShell.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public AppShell(App parent)
1919

2020
private void OnMenuItemClicked(object sender, EventArgs e)
2121
{
22-
_parent.GoToCurrentTextPage();
22+
_parent.GoToTextPage();
2323
}
2424
}
2525
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:BasicPlainTextReaderApp.Views"
5+
Title="BasicPlainTextReaderApp"
6+
x:Class="BasicPlainTextReaderApp.AppShellOpenWith">
7+
8+
<Shell.Resources>
9+
<ResourceDictionary>
10+
<Style x:Key="BaseStyle" TargetType="Element">
11+
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
12+
<Setter Property="Shell.ForegroundColor" Value="White" />
13+
<Setter Property="Shell.TitleColor" Value="White" />
14+
<Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
15+
<Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
16+
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
17+
<Setter Property="Shell.TabBarForegroundColor" Value="White"/>
18+
<Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
19+
<Setter Property="Shell.TabBarTitleColor" Value="White"/>
20+
</Style>
21+
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
22+
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
23+
24+
<Style Class="FlyoutItemLabelStyle" TargetType="Label">
25+
<Setter Property="TextColor" Value="{StaticResource Primary}"></Setter>
26+
</Style>
27+
<Style Class="FlyoutItemLayoutStyle" TargetType="Layout" ApplyToDerivedTypes="True">
28+
<Setter Property="BackgroundColor" Value="{StaticResource SelectedItem}"></Setter>
29+
<Setter Property="VisualStateManager.VisualStateGroups">
30+
<VisualStateGroupList>
31+
<VisualStateGroup x:Name="CommonStates">
32+
<VisualState x:Name="Normal">
33+
<VisualState.Setters>
34+
<Setter Property="BackgroundColor" Value="{StaticResource SelectedItem}" />
35+
<Setter TargetName="FlyoutItemLabel" Property="Label.TextColor" Value="{StaticResource Primary}" />
36+
</VisualState.Setters>
37+
</VisualState>
38+
<VisualState x:Name="Selected">
39+
<VisualState.Setters>
40+
<Setter Property="BackgroundColor" Value="{StaticResource SelectedItem}" />
41+
</VisualState.Setters>
42+
</VisualState>
43+
</VisualStateGroup>
44+
</VisualStateGroupList>
45+
</Setter>
46+
</Style>
47+
48+
</ResourceDictionary>
49+
</Shell.Resources>
50+
51+
52+
<MenuItem Text="About" StyleClass="MenuItemLayoutStyle" Clicked="OnMenuItemClicked" IconImageSource="tab_about.png">
53+
</MenuItem>
54+
55+
<FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
56+
<ShellContent Title="Current text" Icon="tab_feed.png" Route="TextPage" ContentTemplate="{DataTemplate local:TextPage}" x:Name="TextPageShellContent" />
57+
</FlyoutItem>
58+
59+
60+
</Shell>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using BasicPlainTextReaderApp.Library;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
using Xamarin.Forms;
9+
using Xamarin.Forms.Xaml;
10+
11+
namespace BasicPlainTextReaderApp
12+
{
13+
public partial class AppShellOpenWith : Xamarin.Forms.Shell
14+
{
15+
readonly App _parent;
16+
readonly TextModel _data;
17+
bool _sentData;
18+
public AppShellOpenWith(App parent, TextModel data)
19+
{
20+
InitializeComponent();
21+
_parent = parent;
22+
_data = data;
23+
Shell.SetTabBarIsVisible(this, false);
24+
}
25+
26+
protected override void OnNavigated(ShellNavigatedEventArgs args)
27+
{
28+
base.OnNavigated(args);
29+
if (_sentData)
30+
return;
31+
_sentData = true;
32+
33+
MessagingCenter.Send<TextModel>(_data, "TextData");
34+
}
35+
public void SetTextData()
36+
{
37+
_sentData = false;
38+
}
39+
private void OnMenuItemClicked(object sender, EventArgs e)
40+
{
41+
_parent.GoToAboutPage();
42+
}
43+
}
44+
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/BasicPlainTextReaderApp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23+
<EmbeddedResource Update="AppShellOpenWith.xaml">
24+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
25+
</EmbeddedResource>
26+
<EmbeddedResource Update="Views\SearchedTextPage.xaml">
27+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
28+
</EmbeddedResource>
2329
<EmbeddedResource Update="Views\TextPage.xaml">
2430
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2531
</EmbeddedResource>

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ namespace BasicPlainTextReaderApp
77
public static class Constants
88
{
99
public const string AboutUrl = "https://github.com/jonwolfdev/AndroidBasicPlainTextReaderApp";
10+
public const int SearchTextGetFromSides = 25;
1011
}
1112
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace BasicPlainTextReaderApp
6+
{
7+
public class Helper
8+
{
9+
/// <summary>
10+
///
11+
/// </summary>
12+
/// <param name="text"></param>
13+
/// <param name="search"></param>
14+
/// <param name="getFromSides">From the position of search, it will get more text from its sides</param>
15+
/// <returns></returns>
16+
public static List<string> SearchAllStrings(string text, string search, int getFromSides)
17+
{
18+
var list = new List<string>();
19+
20+
if (string.IsNullOrEmpty(search))
21+
return list;
22+
23+
if (string.IsNullOrEmpty(text))
24+
return list;
25+
26+
if (getFromSides <= 0)
27+
throw new ArgumentOutOfRangeException(nameof(getFromSides), "Cannot be less than 1");
28+
29+
int index = 0;
30+
do
31+
{
32+
index = text.IndexOf(search, index, StringComparison.OrdinalIgnoreCase);
33+
34+
if (index == -1)
35+
break;
36+
37+
int startAt = index - getFromSides;
38+
if (startAt < 0)
39+
startAt = 0;
40+
41+
int endAt = (getFromSides * 2) + 1;
42+
if (startAt + 1 + endAt > text.Length)
43+
endAt = text.Length - startAt;
44+
45+
list.Add(text.Substring(startAt, endAt));
46+
index++;
47+
} while (index > 0);
48+
49+
return list;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)