update notebook title

This commit is contained in:
xingxing
2017-02-23 00:10:12 +08:00
parent a7e78d9c48
commit 58eb523332
7 changed files with 110 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.ToastUtils;
import java.util.ArrayList;
import java.util.List;
@@ -113,14 +114,16 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
String notebookId = notebook.getNotebookId();
boolean isSuper = isSuper(notebookId);
boolean isSuperOrRoot = isSuper | mStack.isEmpty();
boolean hasChild = hasChild(notebookId);
final boolean hasChild = hasChild(notebookId);
holder.placeholder.setVisibility(isSuperOrRoot ? View.GONE : View.VISIBLE);
holder.navigator.setVisibility(mCanOpenEmpty | hasChild ? View.VISIBLE : View.INVISIBLE);
holder.navigator.setImageResource(isSuper ? R.drawable.ic_expanding : R.drawable.ic_expandable);
holder.navigator.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO: arrow animation
if (!hasChild) {
return;
}
if (isSuper(notebook.getNotebookId())) {
listUpper();
} else {
@@ -132,11 +135,19 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
@Override
public void onClick(View v) {
if (mListener != null) {
listChild(notebook);
mListener.onClickedNotebook(notebook);
}
}
});
holder.titleTv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mListener != null) {
mListener.onEditNotebook(notebook);
}
return true;
}
});
}
private boolean isSuper(String notebookId) {
@@ -197,6 +208,8 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
void onClickedNotebook(Notebook notebook);
void onClickedAddNotebook(String parentNotebookId);
void onEditNotebook(Notebook notebook);
}
static class NotebookHolder extends RecyclerView.ViewHolder {

View File

@@ -19,4 +19,9 @@ public interface NotebookApi {
@POST("notebook/addNotebook")
Call<Notebook> addNotebook(@Query("title") String title, @Query("parentNotebookId") String parentId);
@POST("notebook/updateNotebook")
Call<Notebook> updateNotebook(@Query("notebookId") String notebookId, @Query("title") String title,
@Query("parentNotebookId") String parentId, @Query("seq") int seq, @Query("usn") int usn);
}

View File

@@ -36,4 +36,24 @@ public class NotebookService {
Notebook notebook = AppDataBase.getNotebookByLocalId(notebookLocalId);
return notebook != null ? notebook.getTitle() : "";
}
public static Notebook updateNotebook(String title, Notebook notebook) {
Notebook newNotebook = RetrofitUtils.excute(ApiProvider.getInstance().getNotebookApi().
updateNotebook(notebook.getNotebookId(), title, notebook.getParentNotebookId(), notebook.getSeq(), notebook.getUsn()));
if (newNotebook == null) {
throw new IllegalStateException("Network error");
}
if (newNotebook.isOk()) {
Account account = AccountService.getCurrent();
if (notebook.getUsn() - account.getNotebookUsn() == 1) {
account.setNotebookUsn(notebook.getUsn());
account.save();
}
newNotebook.setId(notebook.getId());
newNotebook.update();
return notebook;
} else {
throw new IllegalStateException(notebook.getMsg());
}
}
}

View File

@@ -43,6 +43,7 @@ import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NotebookService;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.utils.OpenUtils;
import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.AlphabetDrawable;
import org.houxg.leamonax.widget.TriangleView;
@@ -312,6 +313,25 @@ public class Navigation {
})
.show();
}
@Override
public void onEditNotebook(final Notebook notebook) {
View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_sigle_edittext, null);
final EditText mEdit = (EditText) view.findViewById(R.id.edit);
mEdit.setText(notebook.getTitle());
mEdit.setSelection(notebook.getTitle().length());
new AlertDialog.Builder(mActivity)
.setTitle(R.string.update_notebook_title)
.setView(view)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
updateNotebook(mEdit.getText().toString(), notebook);
}
})
.show();
}
});
mNotebookRv.setAdapter(mNotebookAdapter);
mNotebookAdapter.refresh();
@@ -323,7 +343,47 @@ public class Navigation {
});
}
private void updateNotebook(final String title, final Notebook notebook) {
if (TextUtils.isEmpty(title)) {
ToastUtils.show(mActivity, R.string.toast_notebook_title_not_empty);
return;
}
Observable.create(
new Observable.OnSubscribe<Notebook>() {
@Override
public void call(Subscriber<? super Notebook> subscriber) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(NotebookService.updateNotebook(title, notebook));
subscriber.onCompleted();
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Notebook>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Notebook isSucceed) {
mNotebookAdapter.refresh();
}
});
}
private void addNotebook(final String title, final String parentNotebookId) {
if (TextUtils.isEmpty(title)) {
ToastUtils.show(mActivity, R.string.toast_notebook_title_not_empty);
return;
}
Observable.create(
new Observable.OnSubscribe<Notebook>() {
@Override

View File

@@ -75,6 +75,11 @@ public class DialogUtils {
public void onClickedAddNotebook(String parentNotebookId) {
}
@Override
public void onEditNotebook(Notebook notebook) {
}
});
dialog.show();

View File

@@ -108,4 +108,6 @@
<string name="host_example">登录接口地址将会是:\n%s/api/login</string>
<string name="my_blog">我的博客</string>
<string name="explore">蚂蚁笔记探索</string>
<string name="toast_notebook_title_not_empty">笔记本的名称不能为空</string>
<string name="update_notebook_title">修改笔记本的名称</string>
</resources>

View File

@@ -110,4 +110,6 @@
<string name="host_example">For example, login api will be:\n%s/api/login</string>
<string name="my_blog">My Blog</string>
<string name="explore">Leanote Explore</string>
<string name="toast_notebook_title_not_empty">the notebook title can\'t empty</string>
<string name="update_notebook_title">Update notebook title</string>
</resources>