| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Query profiles by name or address |
| | 7 | | /// ENS query (by name) use a partial match checking if any ENS contains the input string. |
| | 8 | | /// </summary> |
| | 9 | | public class UsersSearcher : IDisposable |
| | 10 | | { |
| | 11 | | private readonly IUsersSearchBridge bridge; |
| | 12 | |
|
| | 13 | | private Promise<UserProfileModel[]> searchPrommise; |
| | 14 | | private string currentSearchInput; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Query profiles of users who their owned names contains string "name" |
| | 18 | | /// If "name" is an address it will return the profile for that address |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="name">name or address</param> |
| | 21 | | /// <param name="maxResults">max results for the query</param> |
| | 22 | | /// <returns>Profiles or null (if no profile found) promise</returns> |
| | 23 | | public Promise<UserProfileModel[]> SearchUser(string name, int maxResults) |
| | 24 | | { |
| 6 | 25 | | searchPrommise?.Dispose(); |
| 6 | 26 | | searchPrommise = new Promise<UserProfileModel[]>(); |
| 6 | 27 | | currentSearchInput = name; |
| 6 | 28 | | WebInterface.SearchENSOwner(name, maxResults); |
| 6 | 29 | | return searchPrommise; |
| | 30 | | } |
| | 31 | |
|
| 18 | 32 | | public UsersSearcher() : this(UsersSearchBridge.i) { } |
| | 33 | |
|
| 12 | 34 | | public UsersSearcher(IUsersSearchBridge bridge) |
| | 35 | | { |
| 12 | 36 | | this.bridge = bridge; |
| 12 | 37 | | if (bridge != null) |
| 12 | 38 | | bridge.OnSearchResult += OnSearchResult; |
| 12 | 39 | | } |
| | 40 | |
|
| | 41 | | public void Dispose() |
| | 42 | | { |
| 3 | 43 | | if (bridge != null) |
| 3 | 44 | | bridge.OnSearchResult -= OnSearchResult; |
| 3 | 45 | | searchPrommise?.Dispose(); |
| 3 | 46 | | } |
| | 47 | |
|
| | 48 | | private void OnSearchResult(string searchInput, UserProfileModel[] profiles) |
| | 49 | | { |
| 5 | 50 | | if (searchInput == currentSearchInput) |
| | 51 | | { |
| 3 | 52 | | searchPrommise?.Resolve(profiles); |
| | 53 | | } |
| 5 | 54 | | } |
| | 55 | | } |