Skip to content

Commit

Permalink
feat: implement location feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bilal-arikan committed Jun 12, 2021
1 parent 05e8a61 commit af3da2c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Runtime/Example/ApiExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
public class ApiExample : MonoBehaviour
{
public int ShowMaxResultAmount = 15;
public string location = "us-en";
[Space]
public InputField SearchInput;
public Button SearchButton;
public LayoutGroup ResultsLayout;
Expand Down Expand Up @@ -40,20 +42,20 @@ void SendExample()
SearchButton.interactable = false;
lastPageNo = 1;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback);
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}

void NextPage()
{
lastPageNo++;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback);
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}
void PreviousPage()
{
lastPageNo--;
ClearResults();
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, OnSearchCallback);
ImagesApi.Search(SearchInput.text, SafeSearch.Off, lastPageNo, location, OnSearchCallback);
}

void ClearResults(){
Expand Down
1 change: 1 addition & 0 deletions Runtime/Example/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ShowMaxResultAmount: 24
location: us-en
SearchInput: {fileID: 313931728}
SearchButton: {fileID: 1956213752}
ResultsLayout: {fileID: 322455885}
Expand Down
22 changes: 13 additions & 9 deletions Runtime/Scripts/ImagesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ public class ImagesApi : MonoBehaviour


public static void Search(string text, int pageNo, Action<ImageSearchResult> onCompleted)
=> Search(text, SafeSearch.Moderate, pageNo, onCompleted);
public static void Search(string text, SafeSearch safeSearch, int pageNo, Action<ImageSearchResult> onCompleted)
=> Search(text, SafeSearch.Moderate, pageNo, "us-en", onCompleted);
public static void Search(string text, SafeSearch safeSearch, int pageNo, string location, Action<ImageSearchResult> onCompleted)
{
if (!instance)
{
instance = new GameObject("DuckDuckGoAPI").AddComponent<Arikan.Duckduckgo.Api.ImagesApi>();
}
instance.SearchFromInstance(text, safeSearch, pageNo, onCompleted);
if(string.IsNullOrWhiteSpace(location))
{
location = "us-en";
}
instance.SearchFromInstance(text, safeSearch, pageNo, location, onCompleted);
}


Expand All @@ -51,11 +55,11 @@ private void Awake()
instance = this;
DontDestroyOnLoad(gameObject);
}
private void SearchFromInstance(string text, SafeSearch safeSearch, int pageNo, Action<ImageSearchResult> onCompleted)
private void SearchFromInstance(string text, SafeSearch safeSearch, int pageNo, string location, Action<ImageSearchResult> onCompleted)
{
StartCoroutine(SearchCoRo(text, safeSearch, pageNo, onCompleted));
StartCoroutine(SearchCoRo(text, safeSearch, pageNo, location, onCompleted));
}
private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo, Action<ImageSearchResult> onCompleted)
private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo, string location, Action<ImageSearchResult> onCompleted)
{
string token = lastSearch.Value;
if (lastSearch.Key != keyword)
Expand All @@ -66,7 +70,7 @@ private IEnumerator SearchCoRo(string keyword, SafeSearch safeSearch, int pageNo
}

// Debug.Log("SrcOb:" + currentToken);
yield return RequestSearchResult(keyword, token, safeSearch, pageNo, onCompleted);
yield return RequestSearchResult(keyword, token, safeSearch, pageNo, location, onCompleted);
}

private IEnumerator RequestToken(string keyword, SafeSearch safeSearch, Action<string> tokenCallback)
Expand Down Expand Up @@ -96,11 +100,11 @@ private IEnumerator RequestToken(string keyword, SafeSearch safeSearch, Action<s
tokenCallback.Invoke(match.Groups[1].Value);
}

private IEnumerator RequestSearchResult(string keyword, string token, SafeSearch safeSearch, int pageNo, Action<ImageSearchResult> callback)
private IEnumerator RequestSearchResult(string keyword, string token, SafeSearch safeSearch, int pageNo, string location, Action<ImageSearchResult> callback)
{
pageNo = Mathf.Clamp(pageNo, 1, int.MaxValue);
Dictionary<string, string> parameters = new Dictionary<string, string>(){
{"l", "us-en"},
{"l", location},
{"o", "json"},
{"q", keyword},
{"vqd", token},
Expand Down

0 comments on commit af3da2c

Please sign in to comment.