From c7fe26167ded1a3eea3061d45b15f8c8b894006f Mon Sep 17 00:00:00 2001 From: stom79 Date: Thu, 14 Dec 2017 16:14:24 +0100 Subject: [PATCH] List activity --- app/src/main/AndroidManifest.xml | 5 + .../mastodon/activities/BaseMainActivity.java | 2 +- .../mastodon/activities/ListActivity.java | 94 +++++++++++++++++++ .../etalab/mastodon/drawers/ListAdapter.java | 11 ++- .../fragments/DisplayListsFragment.java | 35 +++++-- app/src/main/res/layout/activity_list.xml | 80 ++++++++++++++++ app/src/main/res/layout/fragment_list.xml | 8 -- 7 files changed, 217 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/activities/ListActivity.java create mode 100644 app/src/main/res/layout/activity_list.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7ccbfee59..640671e6d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -138,6 +138,11 @@ android:configChanges="orientation|screenSize" android:label="@string/app_name" /> + . */ +package fr.gouv.etalab.mastodon.activities; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.support.v7.widget.RecyclerView; +import android.view.MenuItem; +import android.view.View; +import android.widget.RelativeLayout; +import android.widget.Toast; + + +import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface; + + +/** + * Created by Thomas on 14/12/2017. + * Display content of a list, also help to manage it + */ + +public class ListActivity extends BaseActivity implements OnListActionInterface { + + + private RecyclerView lv_status; + private RelativeLayout loader; + private String title, listId; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + if( theme == Helper.THEME_LIGHT){ + setTheme(R.style.AppTheme); + }else { + setTheme(R.style.AppThemeDark); + } + setContentView(R.layout.activity_list); + + loader = findViewById(R.id.loader); + lv_status = findViewById(R.id.lv_status); + + Bundle b = getIntent().getExtras(); + if(b != null){ + title = b.getString("title"); + listId = b.getString("id"); + }else{ + Toast.makeText(this,R.string.toast_error_search,Toast.LENGTH_LONG).show(); + } + if( getSupportActionBar() != null) + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + setTitle(title); + loader.setVisibility(View.VISIBLE); + lv_status.setVisibility(View.GONE); + + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + + @Override + public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { + + } +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ListAdapter.java index 1c70afc80..ee30880f0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ListAdapter.java @@ -17,8 +17,10 @@ package fr.gouv.etalab.mastodon.drawers; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; @@ -31,6 +33,7 @@ import android.widget.TextView; import java.util.List; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.ListActivity; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -100,10 +103,14 @@ public class ListAdapter extends BaseAdapter { holder.search_container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + Intent intent = new Intent(context, ListActivity.class); + Bundle b = new Bundle(); + b.putString("id", list.getId()); + b.putString("title", list.getTitle()); + intent.putExtras(b); + context.startActivity(intent); } }); - final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); holder.search_container.setOnLongClickListener(new View.OnLongClickListener() { @Override 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 293ca812a..d9450dbe0 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 @@ -17,6 +17,7 @@ package fr.gouv.etalab.mastodon.fragments; import android.annotation.SuppressLint; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; @@ -40,6 +41,8 @@ import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.ListActivity; +import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.drawers.ListAdapter; @@ -65,6 +68,7 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf private ListView lv_lists; private RelativeLayout textviewNoAction; private FloatingActionButton add_new; + private ListAdapter listAdapter; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -81,12 +85,17 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf no_action_text = rootView.findViewById(R.id.no_action_text); mainLoader = rootView.findViewById(R.id.loader); RelativeLayout nextElementLoader = rootView.findViewById(R.id.loading_next_items); - add_new = rootView.findViewById(R.id.add_new); mainLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.GONE); - + lists = new ArrayList<>(); + listAdapter = new ListAdapter(context, 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); + try { + add_new = ((MainActivity) context).findViewById(R.id.add_new); + }catch (Exception ignored){} + if( add_new != null) add_new.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -111,6 +120,8 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf dialog.dismiss(); } }); + + AlertDialog alertDialog = dialogBuilder.create(); alertDialog.setTitle(getString(R.string.action_lists_create)); alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @@ -165,17 +176,27 @@ public class DisplayListsFragment extends Fragment implements OnListActionInterf } 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); + listAdapter.notifyDataSetChanged(); } else { no_action_text.setVisibility(View.VISIBLE); } }else if( actionType == ManageListsAsyncTask.action.CREATE_LIST){ - + if (apiResponse.getLists() != null && apiResponse.getLists().size() > 0) { + String listId = apiResponse.getLists().get(0).getId(); + String title = apiResponse.getLists().get(0).getTitle(); + Intent intent = new Intent(context, ListActivity.class); + Bundle b = new Bundle(); + b.putString("id", listId); + b.putString("title", title); + intent.putExtras(b); + context.startActivity(intent); + this.lists.add(0, apiResponse.getLists().get(0)); + listAdapter.notifyDataSetChanged(); + }else{ + Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); + } } } } diff --git a/app/src/main/res/layout/activity_list.xml b/app/src/main/res/layout/activity_list.xml new file mode 100644 index 000000000..a721a5805 --- /dev/null +++ b/app/src/main/res/layout/activity_list.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + \ 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 85f313d83..3425f6435 100644 --- a/app/src/main/res/layout/fragment_list.xml +++ b/app/src/main/res/layout/fragment_list.xml @@ -73,13 +73,5 @@ android:layout_height="match_parent" android:indeterminate="true" /> -