Skip to content

Commit 315c8d9

Browse files
committed
Added matches count
Added tap to copy to clipboard for search labels
1 parent 15a078f commit 315c8d9

9 files changed

Lines changed: 71 additions & 24 deletions

File tree

BasicPlainTextReaderApp/BasicPlainTextReaderApp.Android/MainActivity.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ protected override void OnCreate(Bundle savedInstanceState)
3131

3232
if (Intent.ActionView.Equals(action) && !string.IsNullOrEmpty(type))
3333
{
34-
Android.Net.Uri fileUri = Intent.Data;
35-
if (fileUri != null)
34+
if (Intent.Data != null)
3635
{
3736
try
3837
{
@@ -52,7 +51,7 @@ protected override void OnCreate(Bundle savedInstanceState)
5251
}
5352
catch (Exception e)
5453
{
55-
data = new TextModel(e.ToString(), "error", "error", "error");
54+
data = new TextModel(e.ToString(), Intent.DataString, Intent.Type, Intent.Data.Path);
5655
}
5756
}
5857
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/ViewModels/SearchedViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace BasicPlainTextReaderApp.ViewModels
66
{
77
public class SearchedViewModel : BaseViewModel
88
{
9-
9+
public string _quantityFound;
10+
public string QuantityFound { get { return _quantityFound; } set { SetProperty(ref _quantityFound, value); } }
1011
}
1112
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/ViewModels/TextPageViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ public class TextPageViewModel : BaseViewModel
1010
{
1111
TextModel _model;
1212
public ToolbarItem InfoItem { get; set; }
13+
public ToolbarItem SearchItem { get; set; }
1314
public TextPageViewModel()
1415
{
15-
ShowPlaceholderText = true;
16-
ShowText = false;
17-
Title = "No text!";
1816
}
1917

2018
public TextModel TModel
@@ -29,12 +27,15 @@ public TextModel TModel
2927

3028
Title = value.DataPath;
3129
InfoItem.IsEnabled = true;
30+
SearchItem.IsEnabled = true;
3231
}
3332
else
3433
{
34+
Title = "No text!";
3535
ShowPlaceholderText = true;
3636
ShowText = false;
3737
InfoItem.IsEnabled = false;
38+
SearchItem.IsEnabled = false;
3839
}
3940
SetProperty(ref _model, value);
4041
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/AboutPage.xaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
<StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
3232
<Label Text="Basic Text Reader App" FontSize="Title"/>
3333
<Label Text="This app tries to read any file as plaint text! And it doesn't save any temporarily hidden file like other text editors do!" FontSize="16" Padding="0,0,0,0"/>
34-
<Label Text="This app uses work from:" FontSize="16" Padding="0,24,0,0"/>
35-
<Label Text="Icons made by Freepik from https://www.flaticon.com/authors/freepik" FontSize="16" Padding="0,7,0,0"/>
34+
<Label Text="Author" FontSize="Title"/>
35+
<Label Text="Made by @jonwolfdev"/>
3636

3737
<Button Margin="0,10,0,0" Text="Check out the GitHub Repository"
3838
Command="{Binding OpenWebCommand}"
3939
BackgroundColor="{StaticResource Primary}"
4040
TextColor="White" />
41+
<Label Text="This app uses work from:" FontSize="16" Padding="0,24,0,0"/>
42+
<Label Text="Icons made by Freepik from https://www.flaticon.com/authors/freepik" FontSize="16" Padding="0,7,0,0"/>
4143
</StackLayout>
4244
</ScrollView>
4345
</Grid>

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/SearchedTextPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<ContentPage.Content>
1717
<StackLayout>
18+
<Label Text="{Binding QuantityFound, StringFormat='Found {0} matches'}" />
1819
<Label Text="Tap one and it will be copied to clipboard" />
1920
<ScrollView x:Name="ScrollViewCtrl">
2021

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/SearchedTextPage.xaml.cs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Xamarin.Essentials;
99
using Xamarin.Forms;
10+
using Xamarin.Forms.Markup;
1011
using Xamarin.Forms.Xaml;
1112

1213
namespace BasicPlainTextReaderApp.Views
@@ -15,18 +16,21 @@ namespace BasicPlainTextReaderApp.Views
1516
public partial class SearchedTextPage : ContentPage
1617
{
1718
readonly SearchedViewModel _vm;
18-
TextModel _data;
19+
readonly TextModel _data;
20+
readonly List<TapGestureRecognizer> _gestures;
21+
1922
public SearchedTextPage(TextModel data, string search)
2023
{
2124
InitializeComponent();
2225
_vm = BindingContext as SearchedViewModel;
2326

24-
_vm.Title = search;
27+
_gestures = new List<TapGestureRecognizer>();
28+
2529
_data = data;
2630
var occurrences = Helper.SearchAllStrings(data.Text, search, Constants.SearchTextGetFromSides);
2731
ScrollViewCtrl.Content = CreateStackLayout(occurrences);
28-
29-
// await DisplayAlert("Copied to clipboard", "All displayed text here was copied to clipboard!", "Close");
32+
_vm.Title = search;
33+
_vm.QuantityFound = occurrences.Count.ToString();
3034
}
3135

3236
private async void SearchItem_Clicked(object sender, EventArgs e)
@@ -37,26 +41,66 @@ private async void SearchItem_Clicked(object sender, EventArgs e)
3741
return;
3842
}
3943

40-
_vm.Title = search;
4144
var occurrences = Helper.SearchAllStrings(_data.Text, search, Constants.SearchTextGetFromSides);
4245
ScrollViewCtrl.Content = CreateStackLayout(occurrences);
46+
_vm.Title = search;
47+
_vm.QuantityFound = occurrences.Count.ToString();
4348
}
4449

4550
private StackLayout CreateStackLayout(IReadOnlyList<string> occurrences)
4651
{
52+
if (_gestures.Count >= 1)
53+
{
54+
_gestures.ForEach(x => x.Tapped -= Gesture_Tapped);
55+
_gestures.Clear();
56+
}
57+
4758
var sl = new StackLayout();
4859

49-
foreach (var str in occurrences)
60+
if(occurrences.Count > 0)
5061
{
51-
sl.Children.Add(new Label()
62+
foreach (var str in occurrences)
5263
{
53-
Text = str,
64+
var label = new Label()
65+
{
66+
Text = str,
67+
Padding = new Thickness(2, 5, 2, 5),
68+
BackgroundColor = Color.Accent
69+
};
70+
var gesture = new TapGestureRecognizer();
71+
gesture.Tapped += Gesture_Tapped;
72+
_gestures.Add(gesture);
73+
label.GestureRecognizers.Add(gesture);
74+
75+
sl.Children.Add(label);
76+
}
77+
}
78+
else
79+
{
80+
var label = new Label()
81+
{
82+
Text = "Text was not found",
5483
Padding = new Thickness(2, 5, 2, 5),
55-
BackgroundColor = Color.Accent
56-
});
84+
HorizontalTextAlignment = TextAlignment.Center
85+
};
86+
87+
sl.Children.Add(label);
5788
}
5889

90+
5991
return sl;
6092
}
93+
94+
private async void Gesture_Tapped(object sender, EventArgs e)
95+
{
96+
if (sender is Label label)
97+
{
98+
if (string.IsNullOrEmpty(label.Text))
99+
return;
100+
101+
await Clipboard.SetTextAsync(label.Text);
102+
await DisplayAlert("Copied to clipboard", "Text was copied to clipboard!", "Close");
103+
}
104+
}
61105
}
62106
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/TextPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
<ContentPage.Content>
2121
<StackLayout>
22-
<Label Text="There is no text. Try opening a text file (txt) from the default file app with Basic Plain Text Reader app"
22+
<Label Text="There is no text. Try opening a text file (txt or any other type) from the default file app with Basic Plain Text Reader app"
2323
VerticalOptions="CenterAndExpand"
2424
HorizontalOptions="CenterAndExpand"
2525
IsVisible="{Binding ShowPlaceholderText}" Padding="10,10,10,10" d:IsVisible="false"/>
26-
<ScrollView IsVisible="{Binding ShowText}">
27-
<Label Text="{Binding Path=TModel.Text}" IsVisible="{Binding ShowText}" d:IsVisible="true" d:Text="Text file goes here" FontSize="Small" />
26+
<ScrollView IsVisible="{Binding ShowText}" InputTransparent="True">
27+
<Label Text="{Binding Path=TModel.Text}" IsVisible="{Binding ShowText}" d:IsVisible="true" d:Text="Text file goes here" Padding="2,2,2,2" FontSize="Small" />
2828
</ScrollView>
2929
</StackLayout>
3030

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/TextPage.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public TextPage()
2222

2323
_vm = BindingContext as TextPageViewModel;
2424
_vm.InfoItem = InfoItem;
25+
_vm.SearchItem = SearchItem;
2526
_vm.TModel = null;
2627

2728
MessagingCenter.Subscribe<TextModel>(this, "TextData", (data) =>
@@ -49,9 +50,7 @@ private async void SearchItem_Clicked(object sender, EventArgs e)
4950

5051
string search = await DisplayPromptAsync("Search for", "Enter the text to search for", "Search", "Cancel", "search for...");
5152
if (string.IsNullOrEmpty(search))
52-
{
5353
return;
54-
}
5554

5655
await Shell.Current.Navigation.PushAsync(new SearchedTextPage(_vm.TModel, search));
5756
}

resources/example.gif

629 KB
Loading

0 commit comments

Comments
 (0)