diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManageListsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManageListsAsyncTask.java index 19d0a2b79..dbba63e49 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManageListsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/ManageListsAsyncTask.java @@ -16,11 +16,13 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; +import android.util.Log; import java.lang.ref.WeakReference; import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface; @@ -64,7 +66,7 @@ public class ManageListsAsyncTask extends AsyncTask { @Override protected Void doInBackground(Void... params) { - + Log.v(Helper.TAG,"apiAction: " + apiAction); if(apiAction == action.GET_LIST){ apiResponse = new API(contextReference.get()).getLists(); }else if(apiAction == action.GET_LIST_ACCOUNT){ @@ -85,7 +87,8 @@ public class ManageListsAsyncTask extends AsyncTask { @Override protected void onPostExecute(Void result) { - listener.onActionDone(apiResponse, statusCode); + Log.v(Helper.TAG,"onPostExecute: " + apiResponse); + listener.onActionDone(this.apiAction, apiResponse, statusCode); } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 3af22b618..1f4c6d553 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -1153,11 +1153,9 @@ public class API { public APIResponse getLists(){ List lists = new ArrayList<>(); - fr.gouv.etalab.mastodon.client.Entities.List list; try { String response = new HttpsConnection().get(getAbsoluteUrl("/lists"), 60, null, prefKeyOauthTokenT); - list = parseList(new JSONObject(response)); - lists.add(list); + lists = parseLists(new JSONArray(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); }catch (Exception e) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayListsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayListsFragment.java index 98336ff1c..293ca812a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayListsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayListsFragment.java @@ -14,15 +14,23 @@ package fr.gouv.etalab.mastodon.fragments; * You should have received a copy of the GNU General Public License along with Mastalab; if not, * see . */ +import android.annotation.SuppressLint; import android.content.Context; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; +import android.support.v7.app.AlertDialog; +import android.text.InputFilter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; @@ -37,6 +45,9 @@ import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.drawers.ListAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface; +import fr.gouv.etalab.mastodon.sqlite.SearchDAO; + +import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; /** @@ -48,9 +59,12 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf private Context context; private AsyncTask asyncTask; - private ListAdapter listAdapter; private List lists; private TextView no_action_text; + private RelativeLayout mainLoader; + private ListView lv_lists; + private RelativeLayout textviewNoAction; + private FloatingActionButton add_new; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -62,18 +76,57 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf lists = new ArrayList<>(); - ListView lv_lists = rootView.findViewById(R.id.lv_lists); + lv_lists = rootView.findViewById(R.id.lv_lists); + textviewNoAction = rootView.findViewById(R.id.no_action); no_action_text = rootView.findViewById(R.id.no_action_text); - RelativeLayout mainLoader = rootView.findViewById(R.id.loader); + mainLoader = rootView.findViewById(R.id.loader); RelativeLayout nextElementLoader = rootView.findViewById(R.id.loading_next_items); - RelativeLayout textviewNoAction = rootView.findViewById(R.id.no_action); + add_new = rootView.findViewById(R.id.add_new); mainLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.GONE); - listAdapter = new ListAdapter(context, this.lists, textviewNoAction); - lv_lists.setAdapter(listAdapter); no_action_text.setVisibility(View.GONE); asyncTask = new ManageListsAsyncTask(context, ManageListsAsyncTask.action.GET_LIST, null, null, null, null, DisplayListsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + add_new.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); + LayoutInflater inflater = getLayoutInflater(); + @SuppressLint("InflateParams") View dialogView = inflater.inflate(R.layout.add_list, null); + dialogBuilder.setView(dialogView); + final EditText editText = dialogView.findViewById(R.id.add_list); + editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(255)}); + dialogBuilder.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if( editText.getText() != null && editText.getText().toString().trim().length() > 0 ) + new ManageListsAsyncTask(context, ManageListsAsyncTask.action.CREATE_LIST, null, null, null, editText.getText().toString().trim(), DisplayListsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + dialog.dismiss(); + add_new.setEnabled(false); + } + }); + dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.setTitle(getString(R.string.action_lists_create)); + alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialogInterface) { + //Hide keyboard + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + assert imm != null; + imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); + } + }); + if( alertDialog.getWindow() != null ) + alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); + alertDialog.show(); + } + }); return rootView; } @@ -100,7 +153,9 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf @Override - public void onActionDone(APIResponse apiResponse, int statusCode) { + public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { + mainLoader.setVisibility(View.GONE); + add_new.setEnabled(true); if( apiResponse.getError() != null){ final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); @@ -108,11 +163,19 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); return; } - if( this.lists != null && this.lists.size() > 0) { - this.lists = apiResponse.getLists(); - listAdapter.notifyDataSetChanged(); - }else { - no_action_text.setVisibility(View.VISIBLE); + if( actionType == ManageListsAsyncTask.action.GET_LIST) { + if (apiResponse.getLists() != null && apiResponse.getLists().size() > 0) { + ; + this.lists = new ArrayList<>(); + this.lists.addAll(apiResponse.getLists()); + ListAdapter listAdapter = new ListAdapter(context, this.lists, textviewNoAction); + lv_lists.setAdapter(listAdapter); + + } else { + no_action_text.setVisibility(View.VISIBLE); + } + }else if( actionType == ManageListsAsyncTask.action.CREATE_LIST){ + } } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnListActionInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnListActionInterface.java index ee8b75a4d..1f2db4818 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnListActionInterface.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnListActionInterface.java @@ -15,6 +15,7 @@ package fr.gouv.etalab.mastodon.interfaces; +import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; /** @@ -22,5 +23,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse; * Interface when actions have been done with lists */ public interface OnListActionInterface { - void onActionDone(APIResponse apiResponse, int statusCode); + void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode); } diff --git a/app/src/main/res/layout/add_list.xml b/app/src/main/res/layout/add_list.xml new file mode 100644 index 000000000..957c8ca58 --- /dev/null +++ b/app/src/main/res/layout/add_list.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_list.xml b/app/src/main/res/layout/fragment_list.xml index 1e29b51c6..85f313d83 100644 --- a/app/src/main/res/layout/fragment_list.xml +++ b/app/src/main/res/layout/fragment_list.xml @@ -15,8 +15,8 @@ You should have received a copy of the GNU General Public License along with Mastalab; if not, see . --> - +