Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
enzevalos_iphone
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
enzevalos
enzevalos_iphone
Commits
7bfa9975
Commit
7bfa9975
authored
5 years ago
by
lazarog98
Browse files
Options
Downloads
Patches
Plain Diff
#238
refactor ListViewController search
parent
bf410e23
No related branches found
No related tags found
1 merge request
!27
Resolve "Improve searching"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
enzevalos_iphone/ListViewController.swift
+49
-57
49 additions, 57 deletions
enzevalos_iphone/ListViewController.swift
enzevalos_iphone/SearchHelper.swift
+25
-1
25 additions, 1 deletion
enzevalos_iphone/SearchHelper.swift
with
74 additions
and
58 deletions
enzevalos_iphone/ListViewController.swift
+
49
−
57
View file @
7bfa9975
...
...
@@ -68,6 +68,9 @@ class ListViewController: UITableViewController {
}
}
var
loading
=
false
private
let
searchDelay
=
0.5
private
var
searchTimer
:
Timer
?
override
func
viewWillAppear
(
_
animated
:
Bool
)
{
tableView
.
reloadData
()
...
...
@@ -105,63 +108,53 @@ class ListViewController: UITableViewController {
deinit
{
print
(
"===============|| ListViewController deinitialized ||==============="
)
}
func
filterContentForSearchText
(
_
searchText
:
String
,
scope
:
Int
=
0
)
{
func
startSearch
(
searchText
:
String
,
scope
:
Int
=
0
)
{
if
#available
(
iOS
10.0
,
*
)
{
if
let
searchBarTimer
:
Timer
=
self
.
searchTimer
{
searchBarTimer
.
invalidate
()
}
self
.
searchTimer
=
Timer
.
scheduledTimer
(
withTimeInterval
:
searchDelay
,
repeats
:
false
,
block
:
{
_
in
self
.
filterContentForSearchText
(
searchText
,
scope
:
scope
)
})
}
}
/**
- parameters:
- searchText: user input string to look for
- scope : 0 = subject: 1 = body: 2 = cc + to: 3 = all
*/
private
func
filterContentForSearchText
(
_
searchText
:
String
,
scope
:
Int
=
0
)
{
filteredMails
=
contact
!.
mails
.
filter
{
mail
in
var
returnValue
=
false
switch
scope
{
case
0
:
if
let
subject
=
mail
.
subject
{
returnValue
=
subject
.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
case
1
:
if
!
returnValue
&&
mail
.
decryptedBody
!=
nil
{
returnValue
=
mail
.
decryptedBody
!.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
else
if
!
returnValue
&&
mail
.
body
!=
nil
{
returnValue
=
mail
.
body
!.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
case
2
:
if
!
returnValue
&&
mail
.
cc
?
.
count
>
0
{
if
let
result
=
mail
.
cc
?
.
contains
(
where
:
{
cc
->
Bool
in
if
let
mail
=
cc
as?
MailAddress
{
return
mail
.
mailAddress
.
contains
(
searchText
.
lowercased
())
}
return
false
})
{
returnValue
=
result
}
}
if
!
returnValue
&&
mail
.
getReceivers
()
.
count
>
1
{
returnValue
=
mail
.
getReceivers
()
.
contains
(
where
:
{
rec
->
Bool
in
return
rec
.
mailAddress
.
contains
(
searchText
.
lowercased
())
})
}
default
:
if
let
subject
=
mail
.
subject
{
returnValue
=
subject
.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
if
!
returnValue
&&
mail
.
decryptedBody
!=
nil
{
returnValue
=
mail
.
decryptedBody
!.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
else
if
!
returnValue
&&
mail
.
body
!=
nil
&&
!
mail
.
isEncrypted
{
returnValue
=
mail
.
body
!.
lowercased
()
.
contains
(
searchText
.
lowercased
())
}
if
!
returnValue
&&
mail
.
cc
?
.
count
>
0
{
if
let
res
=
mail
.
cc
?
.
contains
(
where
:
{
cc
->
Bool
in
if
let
mail
=
cc
as?
MailAddress
{
return
mail
.
mailAddress
.
contains
(
searchText
.
lowercased
())
}
return
false
})
{
returnValue
=
res
}
}
if
!
returnValue
&&
mail
.
getReceivers
()
.
count
>
1
{
returnValue
=
mail
.
getReceivers
()
.
contains
(
where
:
{
rec
->
Bool
in
return
rec
.
mailAddress
.
contains
(
searchText
.
lowercased
())
})
// a big string that we search through, contains everything from the scopes we are searching in
var
str
=
""
if
scope
==
0
||
scope
==
3
{
str
.
append
(
mail
.
subject
??
""
)
}
if
scope
==
1
||
scope
==
3
{
if
let
decryptedBody
=
mail
.
decryptedBody
{
str
.
append
(
decryptedBody
)
}
else
if
let
body
=
mail
.
body
{
str
.
append
(
body
)
}
}
return
returnValue
if
scope
==
2
||
scope
==
3
{
var
receivers
:
[
MailAddress
]
=
[]
receivers
.
append
(
contentsOf
:
mail
.
getReceivers
())
receivers
.
append
(
contentsOf
:
mail
.
getCCs
())
// build a string of all email addresses to input in the search function
receivers
.
map
({
addr
->
String
in
return
addr
.
mailAddress
})
.
forEach
({
addr
in
str
.
append
(
addr
)
})
}
return
containsSearchTerms
(
content
:
str
,
searchText
:
searchText
)
}
tableView
.
reloadData
()
...
...
@@ -259,13 +252,12 @@ class ListViewController: UITableViewController {
extension
ListViewController
:
UISearchResultsUpdating
{
func
updateSearchResults
(
for
searchController
:
UISearchController
)
{
let
searchBar
=
searchController
.
searchBar
let
_
=
searchBar
.
scopeButtonTitles
!
[
searchBar
.
selectedScopeButtonIndex
]
filterContentForSearchText
(
searchController
.
searchBar
.
text
!
,
scope
:
searchBar
.
selectedScopeButtonIndex
)
startSearch
(
searchText
:
searchController
.
searchBar
.
text
!
,
scope
:
searchBar
.
selectedScopeButtonIndex
)
}
}
extension
ListViewController
:
UISearchBarDelegate
{
func
searchBar
(
_
searchBar
:
UISearchBar
,
selectedScopeButtonIndexDidChange
selectedScope
:
Int
)
{
filterContentForS
earchText
(
searchBar
.
text
!
,
scope
:
selectedScope
)
startSearch
(
s
earchText
:
searchBar
.
text
!
,
scope
:
selectedScope
)
}
}
This diff is collapsed.
Click to expand it.
enzevalos_iphone/SearchHelper.swift
+
25
−
1
View file @
7bfa9975
...
...
@@ -7,6 +7,11 @@
//
import
Foundation
/**
A collection of helper methods that are used for the different search bars
*/
/**
Function to be used to find mails that contain the search terms. All terms (separated by spaces) need to be contained in the search text.
- parameters:
...
...
@@ -23,7 +28,26 @@ func containsSearchTerms ( content : String?, searchText: String) -> Bool
{
return
false
///Case Mail has no body/subject
}
let
terms
=
searchText
.
lowercased
()
.
components
(
separatedBy
:
" "
)
var
longterms
:
[
String
]
=
[]
var
terms
:
[
String
]
=
[]
//Break String into substrings separated by quoatation marks
longterms
=
searchText
.
components
(
separatedBy
:
"
\"
"
)
var
i
=
0
//even elements will be outside the quotation marks and need to be separated again
while
(
i
<
longterms
.
count
)
{
if
i
%
2
==
0
{
terms
.
append
(
contentsOf
:
longterms
[
i
]
.
lowercased
()
.
components
(
separatedBy
:
" "
))
}
else
{
terms
.
append
(
longterms
[
i
])
}
i
+=
1
}
var
found
=
true
for
t
in
terms
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment