Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Fbr 561 create reading list #208

Merged
merged 3 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ public boolean postTitleToReadingList(String accessToken, String readingListId,
return responseCode[0] == HttpURLConnection.HTTP_OK;
}

public boolean postReadingList(String accessToken, String readingListName, String description) throws Exception {

String url = String.format("https://%s/v2/lists/?api_key=%s",HOST_NAME, API_KEY);

HttpsURLConnection urlConnection = createHttpsUrlConnection(url, POST_REQUESTE_METHOD);
setAccessToken(accessToken, urlConnection);

LinkedHashMap<String, String> formParameters = new LinkedHashMap<>();
formParameters.put("name", readingListName);
formParameters.put("description", readingListName);
formParameters.put("access", "private");
writeFormParameters(formParameters, urlConnection);

int[] responseCode = new int[1];
final String rawResponseWithReadingLists = requestData(urlConnection, responseCode);

return responseCode[0] == HttpURLConnection.HTTP_OK;
}




private JSONObject getBooksForReadingList(String accessToken, int readingListId) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package org.geometerplus.android.fbreader.benetech;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.accessibility.VoiceableDialog;
import org.benetech.android.R;
import org.geometerplus.android.fbreader.library.AbstractSQLiteBooksDatabase;
import org.geometerplus.android.fbreader.library.SQLiteBooksDatabase;
import org.geometerplus.android.fbreader.network.ReadingListApiManager;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.SyncReadingListsWithBookshareAction;
import org.geometerplus.fbreader.fbreader.SyncReadingListsWithBookshareActionObserver;
import org.geometerplus.fbreader.library.ReadingList;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.ui.android.util.SortUtil;

import java.util.ArrayList;
Expand All @@ -34,6 +50,8 @@ public class BookshareReadingListsFragment extends ListFragment implements SortU
private static final String READINGLIST_TAG = "ReadingListFragment";
private ReadingListFragment myReadingListFragment = null;

private Dialog createDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -52,7 +70,24 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
if(getActivity() instanceof MyBooksActivity){
((MyBooksActivity)getActivity()).onBookshareReadingListsFragmentAppeared();
}
return super.onCreateView(inflater, container, savedInstanceState);
ViewGroup root = (ViewGroup)super.onCreateView(inflater, container, savedInstanceState);

FloatingActionButton mButton = new FloatingActionButton(getActivity());
mButton.setImageResource(R.drawable.ic_add_white_24dp);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.RIGHT;

DisplayMetrics dm = getResources().getDisplayMetrics();
int dpInPx = Math.round(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, dm));

params.bottomMargin = dpInPx;
params.rightMargin = dpInPx;
root.addView(mButton, params);

mButton.setOnClickListener(buttonListener);
return root;
}

@Override
Expand Down Expand Up @@ -140,6 +175,73 @@ private void replaceFragment(Fragment fragment, boolean addToBackStack) {
getFragmentManager().executePendingTransactions();
}

private void createReadingList(String name){
ReadingListApiManager.createReadingList(getActivity(), name, apiListener);
}

private final ReadingListApiManager.ReadinglistAPIListener apiListener = new ReadingListApiManager.ReadinglistAPIListener() {
@Override
public void onAPICallResult(Bundle results) {
ZLApplication.Instance().doAction(ActionCode.SYNC_WITH_BOOKSHARE, SyncReadingListsWithBookshareAction.SyncType.SILENT_STARTUP);
SyncReadingListsWithBookshareActionObserver.getInstance().notifyRelevantBooklistOpened(getActivity());
if(createDialog.isShowing()){
createDialog.dismiss();
}
}

@Override
public void onAPICallError(Bundle results) {
if(createDialog.isShowing()){
createDialog.dismiss();
showErrorMessage(getString(R.string.create_new_readinglist_error));
}
}
};

private void showCreateListDialog(){
final Dialog dialog = new Dialog(getActivity());
createDialog = dialog;
dialog.setContentView(R.layout.bookshare_dialog_with_progress);
final EditText dialog_search_term = (EditText)dialog.findViewById(R.id.bookshare_dialog_search_edit_txt);
TextView dialog_search_title = (TextView)dialog.findViewById(R.id.bookshare_dialog_search_txt);
Button positiveButton = (Button)dialog.findViewById(R.id.bookshare_dialog_btn_ok);
Button dialog_cancel = (Button) dialog.findViewById(R.id.bookshare_dialog_btn_cancel);
TextView dialog_example_text = (TextView)dialog.findViewById(R.id.bookshare_dialog_search_example);
final ProgressBar bar = (ProgressBar)dialog.findViewById(R.id.progressbar);
dialog.setTitle(R.string.create_new_readinglist_title);
dialog_search_title.setText(R.string.create_new_readinglist_message);
dialog_example_text.setVisibility(View.GONE);
dialog_search_term.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
createReadingList(dialog_search_term.getText().toString());
return true;
}
return false;
}
});

positiveButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
createReadingList(dialog_search_term.getText().toString());
bar.setVisibility(View.VISIBLE);
}
});

dialog_cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}

private void showErrorMessage(final CharSequence text) {
final VoiceableDialog finishedDialog = new VoiceableDialog(getActivity());
finishedDialog.popup(text.toString(), 5000);
}



private class ReadingListsAdapter extends ArrayAdapter<ReadingListsItem> {
public ReadingListsAdapter(Context context, List<ReadingListsItem> items) {
Expand Down Expand Up @@ -170,6 +272,13 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}

private View.OnClickListener buttonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
showCreateListDialog();
}
};

private static class ViewHolder {
public TextView readingListNameTextView;
public TextView readingListBooksCountTextView;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.geometerplus.android.fbreader.network;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import org.bookshare.net.BookshareHttpOauth2Client;
import org.geometerplus.android.fbreader.benetech.AddToReadingListDialogActivity;
import org.geometerplus.android.fbreader.network.bookshare.Bookshare_Webservice_Login;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
/**
* Created by avanticatechnologies on 1/3/17.
*/

public class ReadingListApiManager {

public static final String RESULT_SUCCESS = "";

private String accessToken = null;
private AsyncTask pendingTask = null;
private Context context;

private static final ReadingListApiManager instance = new ReadingListApiManager();


static public void createReadingList(Context context, String readingListName, ReadinglistAPIListener listener){
instance._createReadingList(context, readingListName, listener);
}
public void _createReadingList(Context context, String readingListName, ReadinglistAPIListener listener){
this.context = context;
CreateReadingListTask createTask = new CreateReadingListTask();
createTask.readingListName = readingListName;
createTask.listener = listener;
runTask(createTask);

}

private void runTask(AsyncTask task){
if(accessToken == null){
pendingTask = task;
new GetAuthenticatedTokenTask().execute();
}
else {
task.execute();
}
}

private void setToken(String token){
accessToken = token;
if(pendingTask != null){
runTask(pendingTask);
pendingTask = null;
}
}

private class CreateReadingListTask extends AsyncTask<Object, Void, Boolean> {
public String readingListName;
public ReadinglistAPIListener listener;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

protected Boolean doInBackground(Object... params) {
try {
BookshareHttpOauth2Client client = new BookshareHttpOauth2Client();
return client.postReadingList(accessToken, readingListName, "");
} catch (Exception e) {
Log.e(getClass().getSimpleName(), e.getMessage(), e);
return false;
}
}

protected void onPostExecute(Boolean result) {
Bundle results = new Bundle();
results.putString(RESULT_SUCCESS, result.toString());
if(result){
listener.onAPICallResult(results);
}
else {
listener.onAPICallError(results);
}
}
}

private class GetAuthenticatedTokenTask extends AsyncTask<Object, Void, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

protected String doInBackground(Object... params) {
try {
SharedPreferences login_preference = PreferenceManager.getDefaultSharedPreferences(context);
String username = login_preference.getString(Bookshare_Webservice_Login.USER, "");
String password = login_preference.getString(Bookshare_Webservice_Login.PASSWORD, "");

BookshareHttpOauth2Client client = new BookshareHttpOauth2Client();
HttpsURLConnection urlConnection = client.createBookshareApiUrlConnection(username, password);

String response = client.requestData(urlConnection);
JSONObject jsonResponse = new JSONObject(response);
String accessToken = jsonResponse.getString(BookshareHttpOauth2Client.ACCESS_TOKEN_CODE);

return accessToken;
} catch (Exception e) {
Log.e(getClass().getSimpleName(), e.getMessage(), e);
return null;
}
}

protected void onPostExecute(String result) {
setToken(result);
}
}

public interface ReadinglistAPIListener {
void onAPICallResult(Bundle results);
void onAPICallError(Bundle results);
}
}
Binary file added FBReader/src/main/res/drawable/ic_add_white_24dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading