Skip to content

Commit

Permalink
refactor example, removed copyright from xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarikx committed Aug 25, 2016
1 parent 59eaa92 commit b333313
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 232 deletions.
16 changes: 1 addition & 15 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Yaroslav Heriatovych.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yheriatovych.reductor.example">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.*;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.yheriatovych.reductor.Cancelable;
import com.yheriatovych.reductor.Store;
Expand All @@ -17,12 +18,10 @@
import com.yheriatovych.reductor.example.reducers.NotesListReducerImpl;
import com.yheriatovych.reductor.example.reducers.utils.UndoableReducer;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends AppCompatActivity {


Store<AppState> store;
private AtomicInteger idGenerator = new AtomicInteger();
private Cancelable mCancelable;
Expand All @@ -34,35 +33,23 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
setupSpinner(spinner);

setupSpinner(spinner, store);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

final TodoAdapter adapter = new TodoAdapter();
final TodoAdapter adapter = new TodoAdapter(store.getState().notes(),
note -> store.dispatch(NotesListReducerImpl.ActionCreator.toggle(note.id)));

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
new ItemTouchHelper(new NoteTouchCallback(position -> {
Note note = store.getState().notes().get(position);
store.dispatch(NotesListReducerImpl.ActionCreator.remove(note.id));
})).attachToRecyclerView(recyclerView);

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
Note note = store.getState().notes().get(position);
store.dispatch(NotesListReducerImpl.ActionCreator.remove(note.id));
}

@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
}).attachToRecyclerView(recyclerView);

adapter.setHasStableIds(true);
recyclerView.setAdapter(adapter);

mCancelable = store.subscribe(state -> {
adapter.setNotes(Utils.getFilteredNotes(state));
spinner.setSelection(state.filter().ordinal());
Expand All @@ -77,7 +64,7 @@ public boolean isItemViewSwipeEnabled() {
});
}

private void setupSpinner(Spinner spinner) {
private void setupSpinner(Spinner spinner, Store<AppState> store) {
SpinnerAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, NotesFilter.values());
spinner.setAdapter(adapter);
spinner.setSelection(0);
Expand All @@ -96,7 +83,7 @@ public void onNothingSelected(AdapterView<?> adapterView) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add("Undo")
menu.add(R.string.undo)
.setIcon(R.drawable.ic_undo_24dp)
.setOnMenuItemClickListener(menuItem -> {
store.dispatch(UndoableReducer.pop());
Expand All @@ -112,72 +99,4 @@ protected void onDestroy() {
super.onDestroy();
}

class TodoAdapter extends RecyclerView.Adapter<NoteViewHolder> {
private List<Note> mNotes = store.getState().notes();

@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NoteViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_item, parent, false));
}

@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
final Note note = mNotes.get(position);
holder.content.setText(note.note);
holder.content.setChecked(note.checked);
holder.itemView.setOnClickListener(view -> store.dispatch(NotesListReducerImpl.ActionCreator.toggle(note.id)));

holder.itemView.setOnLongClickListener(v -> {
store.dispatch(NotesListReducerImpl.ActionCreator.remove(note.id));
return true;
});
}

@Override
public int getItemCount() {
return mNotes.size();
}

@Override
public long getItemId(int position) {
return mNotes.get(position).id;
}

public void setNotes(List<Note> notes) {
List<Note> oldNotes = mNotes;
mNotes = notes;
DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return oldNotes.size();
}

@Override
public int getNewListSize() {
return notes.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldNotes.get(oldItemPosition).id == notes.get(newItemPosition).id;
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldNotes.get(oldItemPosition).equals(notes.get(newItemPosition));
}
}, false).dispatchUpdatesTo(this);
}
}

static class NoteViewHolder extends RecyclerView.ViewHolder {

public CheckBox content;

public NoteViewHolder(View itemView) {
super(itemView);
content = (CheckBox) itemView;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yheriatovych.reductor.example;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import rx.functions.Action1;

class NoteTouchCallback extends ItemTouchHelper.SimpleCallback {
private final Action1<Integer> onDismissed;
public NoteTouchCallback(Action1<Integer> onDismissed) {
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.onDismissed = onDismissed;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
onDismissed.call(position);
}

@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.yheriatovych.reductor.example;

import android.support.v7.util.DiffUtil;
import com.yheriatovych.reductor.example.model.Note;

import java.util.List;

class NotesDiffCallback extends DiffUtil.Callback {
private final List<Note> oldNotes;
private final List<Note> newNotes;

public NotesDiffCallback(List<Note> oldNotes, List<Note> newNotes) {
this.oldNotes = oldNotes;
this.newNotes = newNotes;
}

@Override
public int getOldListSize() {
return oldNotes.size();
}

@Override
public int getNewListSize() {
return newNotes.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldNotes.get(oldItemPosition).id == newNotes.get(newItemPosition).id;
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldNotes.get(oldItemPosition).equals(newNotes.get(newItemPosition));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.yheriatovych.reductor.example;

import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import com.yheriatovych.reductor.example.model.Note;
import rx.functions.Action1;

import java.util.List;

class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.NoteViewHolder> {
private List<Note> notes;
private final Action1<Note> onClickListener;

public TodoAdapter(List<Note> notes, Action1<Note> onClickListener) {
this.notes = notes;
this.onClickListener = onClickListener;
}

@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NoteViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_item, parent, false));
}

@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
final Note note = notes.get(position);
holder.content.setText(note.note);
holder.content.setChecked(note.checked);
holder.itemView.setOnClickListener(view -> onClickListener.call(note));
}

@Override
public int getItemCount() {
return notes.size();
}

@Override
public long getItemId(int position) {
return notes.get(position).id;
}

public void setNotes(List<Note> notes) {
List<Note> oldNotes = this.notes;
this.notes = notes;
DiffUtil.calculateDiff(new NotesDiffCallback(oldNotes, notes), false).dispatchUpdatesTo(this);
}

static class NoteViewHolder extends RecyclerView.ViewHolder {

public CheckBox content;

public NoteViewHolder(View itemView) {
super(itemView);
content = (CheckBox) itemView;
}
}

}
16 changes: 1 addition & 15 deletions example/src/main/res/drawable/ic_undo_24dp.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
<!--
~ Copyright 2016 Yaroslav Heriatovych.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->


<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
Expand Down
19 changes: 2 additions & 17 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Yaroslav Heriatovych.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
Expand All @@ -28,7 +13,7 @@

<TextView
android:id="@+id/show_text"
android:text="Show: "
android:text="@string/show_prefix"
android:gravity="center_vertical"
android:layout_alignBottom="@+id/spinner"
android:layout_alignParentTop="true"
Expand All @@ -47,7 +32,7 @@
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:id="@+id/add"
android:text="Add"
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_alignTop="@+id/note_edit_text"
android:layout_height="wrap_content"/>
Expand Down
16 changes: 1 addition & 15 deletions example/src/main/res/layout/layout_item.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Yaroslav Heriatovych.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->


<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
Expand Down
Loading

0 comments on commit b333313

Please sign in to comment.