Commit ae034eed authored by Mathilde BALLOUHEY's avatar Mathilde BALLOUHEY
Browse files

Ajout de la recherche de Gifs en Asynchrone

parent 9bfb6f77
Pipeline #23312 failed with stages
in 2 minutes and 37 seconds
package fr.iutnantes.miar.dndgenerator;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
......@@ -36,10 +38,12 @@ public class DisplayGIFActivity extends AppCompatActivity {
private static String TAG = "DisplayGIFActivity :: ";
private static final String GIF_LIST = "gifList";
private boolean m_searchOnChange = true;
private int m_nbResult;
private Context m_context = this;
private DnDSheet m_sheet;
private List<GIFObject> m_listGif;
private ListGifAdapter m_adapter;
private AsyncTaskGetGifList m_asyncTaskGetGifList;
private TextView lbl_displayGif_name, lbl_displayGif_tag;
private EditText txt_displayGif_nbResult, txt_displayGif_recherche;
private ImageView img_displayGif_gif;
......@@ -66,6 +70,9 @@ public class DisplayGIFActivity extends AppCompatActivity {
m_sheet = (DnDSheet) v_bundle.getSerializable(DisplayResultActivity.SHEET);
setDataList(savedInstanceState, checkNbResult());
if(savedInstanceState != null) {
savedInstanceState.clear();
}
m_adapter = new ListGifAdapter(m_listGif, this);
rclr_displayGif_relatedGif.setAdapter(m_adapter);
......@@ -105,7 +112,11 @@ public class DisplayGIFActivity extends AppCompatActivity {
if(m_listGif == null || !m_searchOnChange) {
return;
}
doSearch();
if(m_asyncTaskGetGifList != null) {
m_asyncTaskGetGifList.cancel(true);
}
m_asyncTaskGetGifList = new AsyncTaskGetGifList();
m_asyncTaskGetGifList.execute((Nullable) null);
}
});
......@@ -122,7 +133,11 @@ public class DisplayGIFActivity extends AppCompatActivity {
@Override
public void afterTextChanged(Editable editable) {
doSearch();
if(m_asyncTaskGetGifList != null) {
m_asyncTaskGetGifList.cancel(true);
}
m_asyncTaskGetGifList = new AsyncTaskGetGifList();
m_asyncTaskGetGifList.execute((Nullable) null);
}
});
......@@ -140,18 +155,30 @@ public class DisplayGIFActivity extends AppCompatActivity {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
m_searchOnChange = true;
doSearch();
if(m_asyncTaskGetGifList != null) {
m_asyncTaskGetGifList.cancel(true);
}
m_asyncTaskGetGifList = new AsyncTaskGetGifList();
m_asyncTaskGetGifList.execute((Nullable) null);
}
});
swtch_displayGif_isNSFW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
doSearch();
if(m_asyncTaskGetGifList != null) {
m_asyncTaskGetGifList.cancel(true);
}
m_asyncTaskGetGifList = new AsyncTaskGetGifList();
m_asyncTaskGetGifList.execute((Nullable) null);
}
});
if(txt_displayGif_recherche.getText().toString().isEmpty()){
doSearch();
if(m_asyncTaskGetGifList != null) {
m_asyncTaskGetGifList.cancel(true);
}
m_asyncTaskGetGifList = new AsyncTaskGetGifList();
m_asyncTaskGetGifList.execute((Nullable) null);
}
}
private int checkNbResult() {
......@@ -178,7 +205,7 @@ public class DisplayGIFActivity extends AppCompatActivity {
}
private void doSearch() {
int v_nbResult = checkNbResult();
int v_nbResult = m_nbResult;
String v_recherche = m_sheet.searchGIF;
Log.d(TAG, v_recherche);
TenorArgs v_tenorArgs = TenorArgs.GIF_SAFE;
......@@ -193,7 +220,6 @@ public class DisplayGIFActivity extends AppCompatActivity {
v_recherche, v_tenorArgs.fork(v_nbResult, m_sheet.gif));
m_listGif.clear();
m_listGif.addAll(v_newGifs);
m_adapter.notifyDataSetChanged();
}
}
......@@ -202,4 +228,24 @@ public class DisplayGIFActivity extends AppCompatActivity {
outState.putSerializable(GIF_LIST, (Serializable) m_listGif);
super.onSaveInstanceState(outState);
}
private class AsyncTaskGetGifList extends AsyncTask<Nullable, String, Nullable> {
@Override
protected void onPreExecute() {
super.onPreExecute();
m_nbResult = checkNbResult();
}
@Override
protected Nullable doInBackground(Nullable... nullables) {
doSearch();
return null;
}
@Override
protected void onPostExecute(Nullable nullable) {
super.onPostExecute(nullable);
m_adapter.notifyDataSetChanged();
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment