code refactoring

- extract DashDividerDecoration class
- keep showing app name on main activity’s title
- show notebook’s title on list item
This commit is contained in:
houxg
2016-11-24 14:01:57 +08:00
parent 6d5dbeade8
commit 5cd27a4a13
8 changed files with 111 additions and 90 deletions

View File

@@ -34,14 +34,14 @@
android:label="@string/preview" />
<activity
android:name=".ui.MainActivity"
android:label="@string/recent_notes" />
android:label="@string/app_name" />
<activity android:name=".ui.SignInActivity" />
<activity
android:name=".ui.SettingsActivity"
android:label="@string/settings" />
<activity android:name=".ui.AboutActivity"
android:label="@string/about"/>
<activity
android:name=".ui.AboutActivity"
android:label="@string/about" />
<service
android:name=".background.NoteSyncService"

View File

@@ -30,6 +30,8 @@ import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import rx.Observable;
import rx.Subscriber;
public class NoteService {
@@ -426,18 +428,29 @@ public class NoteService {
return ApiProvider.getInstance().getNoteApi().update(requestBodyMap, fileBodies);
}
public static void deleteNote(Note note) {
if (TextUtils.isEmpty(note.getNoteId())) {
AppDataBase.deleteNoteByLocalId(note.getId());
} else {
UpdateRe response = RetrofitUtils.excuteWithException(deleteNote(note.getNoteId(), note.getUsn()));
if (response.isOk()) {
AppDataBase.deleteNoteByLocalId(note.getId());
updateUsnIfNeed(response.getUsn());
} else {
throw new IllegalStateException(response.getMsg());
}
}
public static Observable<Void> deleteNote(final Note note) {
return Observable.create(
new Observable.OnSubscribe<Void>() {
@Override
public void call(Subscriber<? super Void> subscriber) {
if (!subscriber.isUnsubscribed()) {
if (TextUtils.isEmpty(note.getNoteId())) {
AppDataBase.deleteNoteByLocalId(note.getId());
} else {
UpdateRe response = RetrofitUtils.excuteWithException(
ApiProvider.getInstance().getNoteApi().delete(note.getNoteId(), note.getUsn()));
if (response.isOk()) {
AppDataBase.deleteNoteByLocalId(note.getId());
updateUsnIfNeed(response.getUsn());
} else {
throw new IllegalStateException(response.getMsg());
}
}
subscriber.onNext(null);
subscriber.onCompleted();
}
}
});
}
private static void updateUsnIfNeed(int newUsn) {

View File

@@ -3,6 +3,7 @@ package org.houxg.leamonax.service;
import android.util.Log;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.network.ApiProvider;
@@ -29,4 +30,9 @@ public class NotebookService {
throw new IllegalStateException(notebook.getMsg());
}
}
public static String getTitle(long notebookLocalId) {
Notebook notebook = AppDataBase.getNotebookByLocalId(notebookLocalId);
return notebook != null ? notebook.getTitle() : "";
}
}

View File

@@ -51,7 +51,7 @@ import rx.schedulers.Schedulers;
public class MainActivity extends BaseActivity implements NotebookAdapter.NotebookAdapterListener {
private static final String EXT_SHOULD_RELOAD = "ext_should_reload";
private static final String TAG_NOTE_FRAGMENT = "note_fragment";
private static final String TAG_NOTE_FRAGMENT = "tag_note_fragment";
NoteFragment mNoteFragment;
@@ -129,6 +129,15 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void fetchInfo() {
AccountService.getInfo(AccountService.getCurrent().getUserId())
.subscribeOn(Schedulers.io())
@@ -169,7 +178,6 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
public void onClickedNotebook(Notebook notebook) {
mNoteFragment.loadNoteFromLocal(notebook.getId());
mDrawerLayout.closeDrawer(GravityCompat.START, true);
setTitle(notebook.getTitle());
}
@Override
@@ -238,7 +246,6 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
void showRecentNote() {
mNoteFragment.loadNoteFromLocal(NoteFragment.RECENT_NOTES);
mDrawerLayout.closeDrawer(GravityCompat.START, true);
setTitle(getString(R.string.recent_notes));
}
@OnClick(R.id.rl_about)

View File

@@ -4,14 +4,8 @@ package org.houxg.leamonax.ui;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v13.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
@@ -36,6 +30,7 @@ import org.houxg.leamonax.service.NoteService;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.utils.NetworkUtils;
import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.DashDividerDecoration;
import java.util.Collections;
import java.util.List;
@@ -43,9 +38,7 @@ import java.util.Locale;
import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
@@ -206,17 +199,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
}
private void deleteNote(final Note note) {
Observable.create(
new Observable.OnSubscribe<Void>() {
@Override
public void call(Subscriber<? super Void> subscriber) {
if (!subscriber.isUnsubscribed()) {
NoteService.deleteNote(note);
subscriber.onNext(null);
subscriber.onCompleted();
}
}
})
NoteService.deleteNote(note)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Void>() {
@@ -248,46 +231,4 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
}
}
}
private static class DashDividerDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path mPath;
public DashDividerDecoration(int color, int dashGap, int dashWidth, int height) {
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(height);
mPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));
mPath = new Path();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
final int strokeWidth = (int) mPaint.getStrokeWidth();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin +
Math.round(ViewCompat.getTranslationY(child));
int offsetY = top + strokeWidth / 2;
mPath.reset();
mPath.moveTo(left, offsetY);
mPath.lineTo(right, offsetY);
c.drawPath(mPath, mPaint);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth());
}
}
}

View File

@@ -0,0 +1,53 @@
package org.houxg.leamonax.widget;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.v13.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class DashDividerDecoration extends RecyclerView.ItemDecoration {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path mPath;
public DashDividerDecoration(int color, int dashGap, int dashWidth, int height) {
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(height);
mPaint.setPathEffect(new DashPathEffect(new float[]{dashWidth, dashGap}, 0));
mPath = new Path();
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
final int strokeWidth = (int) mPaint.getStrokeWidth();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin +
Math.round(ViewCompat.getTranslationY(child));
int offsetY = top + strokeWidth / 2;
mPath.reset();
mPath.moveTo(left, offsetY);
mPath.lineTo(right, offsetY);
c.drawPath(mPath, mPaint);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth());
}
}

View File

@@ -22,15 +22,14 @@
android:orientation="horizontal">
<TextView
android:id="@+id/tv_notebook"
android:id="@+id/tv_dirty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:fontFamily="sans-serif-light"
android:textColor="@color/colorAccent"
android:textSize="12sp"
android:visibility="gone"
tools:text="Gradle" />
android:text="@string/changed"
android:textColor="#FFA726"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_update_time"
@@ -42,14 +41,14 @@
tools:text="9-18 23:23" />
<TextView
android:id="@+id/tv_dirty"
android:id="@+id/tv_notebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:fontFamily="sans-serif-light"
android:text="@string/changed"
android:textColor="#FFA726"
android:textSize="12sp" />
android:fontFamily="sans-serif-medium"
android:textColor="@color/hint_text_light"
android:textSize="12sp"
tools:text="Gradle" />
</LinearLayout>

View File

@@ -7,6 +7,8 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:actionMenuTextColor">@color/menu_text</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
</style>
<style name="SettingsStatus">