diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WhoToFollowActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WhoToFollowActivity.java index 3a2bd7313..a61757ad5 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/WhoToFollowActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/WhoToFollowActivity.java @@ -15,24 +15,36 @@ package fr.gouv.etalab.mastodon.activities; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.support.v4.content.ContextCompat; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; +import android.widget.TextView; import android.widget.Toast; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; - import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.WhoToFollowAsyncTask; +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Error; +import fr.gouv.etalab.mastodon.client.Entities.TrunkAccount; import fr.gouv.etalab.mastodon.drawers.WhoToFollowAccountsAdapter; import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface; +import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveWhoToFollowInterface; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; @@ -43,11 +55,18 @@ import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; * Display Who to follow accounts */ -public class WhoToFollowActivity extends BaseActivity implements OnRetrieveWhoToFollowInterface { +public class WhoToFollowActivity extends BaseActivity implements OnRetrieveWhoToFollowInterface, OnPostActionInterface, OnListActionInterface { private String item; + private List followedId; + private List accountListId; + private List toFollowdId; + private TextView progess_action; + private List trunkAccounts; + private RelativeLayout mainLoader; + private String listId, listTitle; @Override protected void onCreate(Bundle savedInstanceState) { @@ -69,17 +88,19 @@ public class WhoToFollowActivity extends BaseActivity implements OnRetrieveWhoTo } setContentView(R.layout.activity_who_to_follow); Toolbar toolbar = findViewById(R.id.toolbar); + progess_action = findViewById(R.id.progess_action); if( theme == THEME_BLACK) toolbar.setBackgroundColor(ContextCompat.getColor(WhoToFollowActivity.this, R.color.black)); setSupportActionBar(toolbar); if( getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); - RelativeLayout mainLoader = findViewById(R.id.loader); + mainLoader = findViewById(R.id.loader); Bundle b = getIntent().getExtras(); if(b != null){ item = b.getString("item"); } + followedId = new ArrayList<>(); String lastDateListNameRefresh = sharedpreferences.getString(Helper.LAST_DATE_LIST_NAME_REFRESH+item, null); Calendar cal = Calendar.getInstance(); cal.setTime(new Date( )); @@ -90,44 +111,157 @@ public class WhoToFollowActivity extends BaseActivity implements OnRetrieveWhoTo mainLoader.setVisibility(View.VISIBLE); }else { String lastList = sharedpreferences.getString(Helper.LAST_LIST_NAME + item, null); - List list = Helper.restoreArrayFromString(lastList); - displayResults(list); + List acctString = Helper.restoreArrayFromString(lastList); + if( acctString != null) { + trunkAccounts = new ArrayList<>(); + for (String acct : acctString) { + TrunkAccount trunkAccount = new TrunkAccount(); + trunkAccount.setAcct(acct); + trunkAccounts.add(trunkAccount); + } + } + displayResults(); } - setTitle(item); } - @Override - public void onRetrieveWhoToFollow(List list) { - if( list != null){ - SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.LAST_DATE_LIST_NAME_REFRESH + item, Helper.dateToString(new Date())); - editor.putString(Helper.LAST_LIST_NAME + item, Helper.arrayToStringStorage(list)); - editor.apply(); - } - displayResults(list); - } - - private void displayResults(List list){ - - RelativeLayout mainLoader = findViewById(R.id.loader); + private void displayResults(){ mainLoader.setVisibility(View.GONE); - if( list != null){ + WhoToFollowAccountsAdapter whoToFollowAccountsAdapter; + if( trunkAccounts != null){ ListView lv_list = findViewById(R.id.lv_list); - WhoToFollowAccountsAdapter whoToFollowAccountsAdapter = new WhoToFollowAccountsAdapter(WhoToFollowActivity.this, list); + whoToFollowAccountsAdapter = new WhoToFollowAccountsAdapter(WhoToFollowActivity.this, trunkAccounts); lv_list.setAdapter(whoToFollowAccountsAdapter); }else{ Toast.makeText(WhoToFollowActivity.this, R.string.toast_error, Toast.LENGTH_SHORT).show(); + return; } Button follow_accounts_select = findViewById(R.id.follow_accounts_select); follow_accounts_select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if(follow_accounts_select.getText().equals(getString(R.string.select_all))){ + follow_accounts_select.setText(R.string.unselect_all); + for(TrunkAccount trunkAccount: trunkAccounts){ + trunkAccount.setChecked(true); + } + whoToFollowAccountsAdapter.notifyDataSetChanged(); + }else { + follow_accounts_select.setText(R.string.select_all); + for(TrunkAccount trunkAccount: trunkAccounts){ + trunkAccount.setChecked(false); + } + whoToFollowAccountsAdapter.notifyDataSetChanged(); + } + } + }); + Button follow_accounts = findViewById(R.id.follow_accounts); + follow_accounts.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + follow_accounts.setEnabled(false); + follow_accounts_select.setEnabled(false); + progess_action.setVisibility(View.VISIBLE); + toFollowdId = new ArrayList<>(); + for(TrunkAccount trunkAccount: trunkAccounts){ + if( trunkAccount.isChecked()){ + toFollowdId.add(trunkAccount.getAcct()); + } + } + if(toFollowdId.size() > 0){ + Account account = new Account(); + String[] val = toFollowdId.get(0).split("@"); + progess_action.setText(getString(R.string.follow_trunk, toFollowdId.get(0))); + if( val.length > 1){ + account.setAcct(val[0]); + account.setInstance(val[1]); + new PostActionAsyncTask(WhoToFollowActivity.this, null, account, API.StatusAction.FOLLOW, WhoToFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } } }); } + + @Override + public void onRetrieveWhoToFollowList(List list) { + + } + + @Override + public void onRetrieveWhoToFollowAccount(List trunkAccounts) { + if( trunkAccounts != null){ + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.LAST_DATE_LIST_NAME_REFRESH + item, Helper.dateToString(new Date())); + List accounts = new ArrayList<>(); + for(TrunkAccount trunkAccount: trunkAccounts) + accounts.add(trunkAccount.getAcct()); + editor.putString(Helper.LAST_LIST_NAME + item, Helper.arrayToStringStorage(accounts)); + editor.apply(); + this.trunkAccounts = trunkAccounts; + } + displayResults(); + } + + @Override + public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) { + followedId.add(userId); + if( followedId != null && followedId.size() >= toFollowdId.size()) { + progess_action.setText(getString(R.string.create_list_trunk, item)); + new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.CREATE_LIST, null, null, null, item, WhoToFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + }else { + Account account = new Account(); + String[] val = toFollowdId.get(followedId.size()).split("@"); + progess_action.setText(getString(R.string.follow_trunk, toFollowdId.get(followedId.size()))); + if( val.length > 1){ + account.setAcct(val[0]); + account.setInstance(val[1]); + new PostActionAsyncTask(WhoToFollowActivity.this, null, account, API.StatusAction.FOLLOW, WhoToFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + } + } + + @Override + public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { + + List lists = apiResponse.getLists(); + if( lists!= null && lists.size() > 0 && actionType == ManageListsAsyncTask.action.CREATE_LIST){ + String[] accountsId = followedId.toArray(new String[0]); + progess_action.setText(R.string.add_account_list_trunk); + listId = lists.get(0).getId(); + listTitle = lists.get(0).getTitle(); + new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.ADD_USERS, new String[]{followedId.get(0)}, null, lists.get(0).getId(), null, WhoToFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + accountListId = new ArrayList<>(); + }else if(accountListId != null){ + + if( accountListId.size() >= followedId.size() -1) { + progess_action.setText(R.string.account_added_list_trunk); + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + Intent intent = new Intent(WhoToFollowActivity.this, ListActivity.class); + Bundle b = new Bundle(); + b.putString("id", listId); + b.putString("title", listTitle); + intent.putExtras(b); + startActivity(intent); + finish(); + } + + }, 1000); + + }else { + accountListId.add(followedId.get(accountListId.size())); + progess_action.setText(R.string.adding_account_list_trunk); + String userIdToAdd = followedId.get(accountListId.size()); + new ManageListsAsyncTask(WhoToFollowActivity.this, ManageListsAsyncTask.action.ADD_USERS, new String[]{userIdToAdd}, null, listId, null, WhoToFollowActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java index 89c6be953..6d3d25e01 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java @@ -16,7 +16,6 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; -import android.util.Log; import java.lang.ref.WeakReference; import java.util.List; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/WhoToFollowAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/WhoToFollowAsyncTask.java index 3118213a6..7c8e3007e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/WhoToFollowAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/WhoToFollowAsyncTask.java @@ -16,8 +16,10 @@ package fr.gouv.etalab.mastodon.asynctasks; import android.content.Context; import android.os.AsyncTask; import java.lang.ref.WeakReference; +import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.Entities.TrunkAccount; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveWhoToFollowInterface; @@ -52,7 +54,21 @@ public class WhoToFollowAsyncTask extends AsyncTask { @Override protected void onPostExecute(Void result) { - listener.onRetrieveWhoToFollow(response); + if( name == null) + listener.onRetrieveWhoToFollowList(response); + else { + List trunkAccounts = null; + if(response != null) { + trunkAccounts = new ArrayList<>(); + for (String res : response) { + TrunkAccount trunkAccount = new TrunkAccount(); + trunkAccount.setAcct(res); + trunkAccounts.add(trunkAccount); + } + } + listener.onRetrieveWhoToFollowAccount(trunkAccounts); + } + } } 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 2954e4850..cd612e762 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 @@ -17,8 +17,6 @@ package fr.gouv.etalab.mastodon.client; import android.content.Context; import android.content.SharedPreferences; -import com.google.gson.JsonArray; - import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TrunkAccount.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TrunkAccount.java new file mode 100644 index 000000000..a87b3b721 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/TrunkAccount.java @@ -0,0 +1,42 @@ +package fr.gouv.etalab.mastodon.client.Entities; +/* Copyright 2018 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ + +/** + * Created by Thomas on 11/09/2018. + * Adapter for Trunk accounts + */ +public class TrunkAccount { + + private String acct; + private boolean isChecked = false; + + public String getAcct() { + return acct; + } + + public void setAcct(String acct) { + this.acct = acct; + } + + + public boolean isChecked() { + return isChecked; + } + + public void setChecked(boolean checked) { + isChecked = checked; + } +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/WhoToFollowAccountsAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/WhoToFollowAccountsAdapter.java index a522d5452..3808e033b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/WhoToFollowAccountsAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/WhoToFollowAccountsAdapter.java @@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.drawers; import android.content.Context; +import android.support.design.widget.FloatingActionButton; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -25,6 +26,7 @@ import android.widget.TextView; import java.util.List; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.TrunkAccount; import fr.gouv.etalab.mastodon.helper.CrossActions; @@ -34,11 +36,11 @@ import fr.gouv.etalab.mastodon.helper.CrossActions; */ public class WhoToFollowAccountsAdapter extends BaseAdapter { - private List lists; + private List lists; private LayoutInflater layoutInflater; private Context context; - public WhoToFollowAccountsAdapter(Context context, List lists){ + public WhoToFollowAccountsAdapter(Context context, List lists){ this.lists = lists; layoutInflater = LayoutInflater.from(context); this.context = context; @@ -63,25 +65,35 @@ public class WhoToFollowAccountsAdapter extends BaseAdapter { @Override public View getView(final int position, View convertView, ViewGroup parent) { - String item = lists.get(position); + TrunkAccount trunkAccount = lists.get(position); final ViewHolder holder; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.drawer_who_to_follow_account, parent, false); holder = new ViewHolder(); + holder.account_to_follow_check = convertView.findViewById(R.id.account_to_follow_check); holder.account_to_follow = convertView.findViewById(R.id.account_to_follow); - holder.account_to_follow = convertView.findViewById(R.id.account_to_follow); + holder.account_to_follow_profile = convertView.findViewById(R.id.account_to_follow_profile); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } - holder.account_to_follow.setText(item); + holder.account_to_follow.setText(trunkAccount.getAcct()); + holder.account_to_follow_check.setChecked(trunkAccount.isChecked()); holder.account_to_follow.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + trunkAccount.setChecked(!trunkAccount.isChecked()); + notifyDataSetChanged(); + } + }); + + holder.account_to_follow_profile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Account account = new Account(); - String[] val = item.split("@"); + String[] val = trunkAccount.getAcct().split("@"); if( val.length > 1){ account.setAcct(val[0]); account.setInstance(val[1]); @@ -97,6 +109,7 @@ public class WhoToFollowAccountsAdapter extends BaseAdapter { private class ViewHolder { CheckBox account_to_follow_check; TextView account_to_follow; + FloatingActionButton account_to_follow_profile; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/WhoToFollowFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/WhoToFollowFragment.java index 4312c0bf0..47b0284da 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/WhoToFollowFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/WhoToFollowFragment.java @@ -20,7 +20,6 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -34,6 +33,7 @@ import java.util.List; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.asynctasks.WhoToFollowAsyncTask; +import fr.gouv.etalab.mastodon.client.Entities.TrunkAccount; import fr.gouv.etalab.mastodon.drawers.WhoToFollowAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveWhoToFollowInterface; @@ -49,13 +49,14 @@ public class WhoToFollowFragment extends Fragment implements OnRetrieveWhoToFoll private Context context; private View rootView; + private RelativeLayout mainLoader; @Override public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_who_to_follow, container, false); context = getContext(); - RelativeLayout mainLoader = rootView.findViewById(R.id.loader); + mainLoader = rootView.findViewById(R.id.loader); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String lastDateListRefresh = sharedpreferences.getString(Helper.LAST_DATE_LIST_REFRESH, null); Calendar cal = Calendar.getInstance(); @@ -86,22 +87,9 @@ public class WhoToFollowFragment extends Fragment implements OnRetrieveWhoToFoll this.context = context; } - @Override - public void onRetrieveWhoToFollow(List list) { - - if( list != null){ - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.LAST_DATE_LIST_REFRESH, Helper.dateToString(new Date())); - editor.putString(Helper.LAST_LIST, Helper.arrayToStringStorage(list)); - editor.apply(); - } - displayResults(list); - } private void displayResults(List list){ - RelativeLayout mainLoader = rootView.findViewById(R.id.loader); mainLoader.setVisibility(View.GONE); if( list != null){ ListView lv_list = rootView.findViewById(R.id.lv_list); @@ -112,4 +100,19 @@ public class WhoToFollowFragment extends Fragment implements OnRetrieveWhoToFoll } } + @Override + public void onRetrieveWhoToFollowList(List list) { + if( list != null){ + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.LAST_DATE_LIST_REFRESH, Helper.dateToString(new Date())); + editor.putString(Helper.LAST_LIST, Helper.arrayToStringStorage(list)); + editor.apply(); + } + displayResults(list); + } + + @Override + public void onRetrieveWhoToFollowAccount(List trunkAccounts) { + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveWhoToFollowInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveWhoToFollowInterface.java index 811b9b09b..f84e75d1b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveWhoToFollowInterface.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveWhoToFollowInterface.java @@ -17,10 +17,13 @@ package fr.gouv.etalab.mastodon.interfaces; import java.util.List; +import fr.gouv.etalab.mastodon.client.Entities.TrunkAccount; + /** * Created by Thomas on 10/09/2018. * Interface for who to follow */ public interface OnRetrieveWhoToFollowInterface { - void onRetrieveWhoToFollow(List list); + void onRetrieveWhoToFollowList(List list); + void onRetrieveWhoToFollowAccount(List trunkAccounts); } diff --git a/app/src/main/res/layout/activity_who_to_follow.xml b/app/src/main/res/layout/activity_who_to_follow.xml index 3e8b30658..3daea8f3f 100644 --- a/app/src/main/res/layout/activity_who_to_follow.xml +++ b/app/src/main/res/layout/activity_who_to_follow.xml @@ -92,23 +92,36 @@ android:indeterminate="true" /> -