Skip to content

Commit

Permalink
Adding, editing entities
Browse files Browse the repository at this point in the history
Scroll to
  • Loading branch information
CullyCross committed Nov 6, 2015
1 parent ea29a5f commit 217e5a5
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.cullycross.test4tabs.activities;

import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
Expand All @@ -17,14 +19,18 @@
import butterknife.OnClick;
import butterknife.OnPageChange;
import me.cullycross.test4tabs.R;
import me.cullycross.test4tabs.content.EntityContentProvider;
import me.cullycross.test4tabs.fragments.ContactsFragment;
import me.cullycross.test4tabs.fragments.DatabaseFragment;
import me.cullycross.test4tabs.fragments.EmailDialogFragment;
import me.cullycross.test4tabs.fragments.EntityDialogFragment;
import me.cullycross.test4tabs.fragments.PicturesFragment;
import me.cullycross.test4tabs.fragments.SinglePictureFragment;
import me.cullycross.test4tabs.pojos.SomeEntity;

public class FourTabsActivity extends AppCompatActivity
implements EmailDialogFragment.OnFragmentInteractionListener {
implements EmailDialogFragment.OnFragmentInteractionListener,
EntityDialogFragment.OnFragmentInteractionListener {

public static final int CONTACTS_POS = 0;
public static final int DATABASE_POS = 1;
Expand Down Expand Up @@ -67,6 +73,33 @@ public class FourTabsActivity extends AppCompatActivity
}
}

@Override public void onSubmit(int id, String title, String body, int row) {

final Object o = currentFragment();

if(!(o instanceof RowUpdateListener)) {
return;
}

if (id != DatabaseFragment.FLAG_NEW_ENTITY) {
final SomeEntity entity = SomeEntity.fromDatabase(this, id);
if (entity != null) {
entity.setName(title);
entity.setDescription(body);
final Uri uri = ContentUris.withAppendedId(EntityContentProvider.ENTITY_CONTENT_URI, id);
getContentResolver().update(uri, entity.toContentValues(), null, null);
} else {
Toast.makeText(FourTabsActivity.this, "Entity not found", Toast.LENGTH_SHORT).show();
}
} else {
final SomeEntity entity = new SomeEntity(title, body);
getContentResolver().insert(EntityContentProvider.ENTITY_CONTENT_URI,
entity.toContentValues());
}

((RowUpdateListener) o).onRowUpdate(row);
}

@OnPageChange(R.id.container) public void onPageSelected(int position) {
switch (position) {
case DATABASE_POS:
Expand All @@ -89,17 +122,25 @@ public class FourTabsActivity extends AppCompatActivity
}

@OnClick(R.id.fab) public void fabClick() {
Object o = mViewPager.getAdapter().instantiateItem(mViewPager, mViewPager.getCurrentItem());
final Object o = currentFragment();

if (o != null && o instanceof FabClickListener) {
((FabClickListener) o).onFabClick();
}
}

private Object currentFragment() {
return mViewPager.getAdapter().instantiateItem(mViewPager, mViewPager.getCurrentItem());
}

public interface FabClickListener {
void onFabClick();
}

public interface RowUpdateListener {
void onRowUpdate(int position);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public static final String CONTACTS = "Contacts";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import me.cullycross.test4tabs.R;
import me.cullycross.test4tabs.activities.FourTabsActivity;
import me.cullycross.test4tabs.fragments.EntityDialogFragment;
import me.cullycross.test4tabs.pojos.SomeEntity;

/**
Expand All @@ -18,29 +28,47 @@
*/
public class EntityCursorAdapter extends CursorRecyclerViewAdapter<EntityCursorAdapter.EntityItem> {

private static final SimpleDateFormat DATE_FORMAT =
new SimpleDateFormat("EEEE, MMMM dd yyyy", Locale.getDefault());

public EntityCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
}

@Override public EntityItem onCreateViewHolder(ViewGroup parent, int viewType) {

final View itemView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
.inflate(R.layout.recycler_view_item_entity, parent, false);

return new EntityItem(itemView);
}

@Override public void onBindViewHolder(EntityItem holder, Cursor cursor) {
final SomeEntity entity = SomeEntity.fromCursor(cursor);

holder.mLayout.setTag(entity.getId());
holder.mName.setText(entity.getName());
holder.mDescription.setText(entity.getDescription());
holder.mLastUpdated.setText(DATE_FORMAT.format(new Date(entity.getUpdatedMillis())));
}

class EntityItem extends RecyclerView.ViewHolder {

@Bind(R.id.entity_layout) LinearLayout mLayout;
@Bind(R.id.name) TextView mName;
@Bind(R.id.last_updated) TextView mLastUpdated;
@Bind(R.id.description) TextView mDescription;

public EntityItem(View view) {
super(view);
ButterKnife.bind(this, view);
}

@OnClick(R.id.entity_layout) void edit(View v) {
final FourTabsActivity ctx = ((FourTabsActivity) v.getContext());
EntityDialogFragment.newInstance(((int) v.getTag()), mName.getText().toString(),
mDescription.getText().toString(), getAdapterPosition())
.show(ctx.getSupportFragmentManager(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
import butterknife.Bind;
import butterknife.ButterKnife;
import me.cullycross.test4tabs.R;
import me.cullycross.test4tabs.activities.FourTabsActivity;
import me.cullycross.test4tabs.adapters.EntityCursorAdapter;
import me.cullycross.test4tabs.content.EntityContentProvider;

public class DatabaseFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public class DatabaseFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>, FourTabsActivity.FabClickListener,
FourTabsActivity.RowUpdateListener {

public static final int FLAG_NEW_ENTITY = -1;
public static final int FLAG_START = -2;

@Bind(R.id.recycler_view_entities) RecyclerView mRecyclerViewEntities;

private ProgressDialog mDialog;
private EntityCursorAdapter mAdapter;

private int mPositionToScroll = FLAG_START; // save here position to scroll after loader is restarted

public static DatabaseFragment newInstance() {
final DatabaseFragment fragment = new DatabaseFragment();
final Bundle args = new Bundle();
Expand All @@ -39,6 +47,11 @@ public DatabaseFragment() {
// Required empty public constructor
}

@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
}

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_database, container, false);
Expand All @@ -64,6 +77,14 @@ public DatabaseFragment() {
if (mDialog != null) {
mDialog.dismiss();
}

if(mPositionToScroll != FLAG_START) {
if(mPositionToScroll == FLAG_NEW_ENTITY) {
mRecyclerViewEntities.smoothScrollToPosition(mAdapter.getItemCount() - 1);
} else {
mRecyclerViewEntities.smoothScrollToPosition(mPositionToScroll);
}
}
}

@Override public void onLoaderReset(Loader<Cursor> loader) {
Expand All @@ -74,4 +95,26 @@ public DatabaseFragment() {
super.onDestroyView();
ButterKnife.unbind(this);
}
}

@Override public void onFabClick() {

EntityDialogFragment.newInstance(FLAG_NEW_ENTITY, null, null, FLAG_NEW_ENTITY)
.show(getActivity().getSupportFragmentManager(), null);
}

@Override public void onRowUpdate(int position) {
// bad decision, have to use notifyItemInserted/Changed (not implemented in this cursor adapter)
// only swapCursor with stableIds
restartLoader();
mPositionToScroll = position;
}

private void restartLoader() {
final Loader<Object> loader = getLoaderManager().getLoader(0);
if (loader != null && !loader.isReset()) {
getLoaderManager().restartLoader(0, null, DatabaseFragment.this);
} else {
getLoaderManager().initLoader(0, null, DatabaseFragment.this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

public class EmailDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {

@Bind(R.id.email_subject) TextView mSubject;
@Bind(R.id.email_body) TextView mBody;
@Bind(R.id.title) TextView mSubject;
@Bind(R.id.body) TextView mBody;

private static final String ARGS_EMAIL = "all_you_need_is_love";
private static final String ARGS_NAME = "what_s_in_your_head";
Expand Down Expand Up @@ -54,7 +54,7 @@ public EmailDialogFragment() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();

final View dialogView = inflater.inflate(R.layout.fragment_email_dialog, null);
final View dialogView = inflater.inflate(R.layout.fragment_title_body_dialog, null);

ButterKnife.bind(this, dialogView);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package me.cullycross.test4tabs.fragments;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.cullycross.test4tabs.R;

public class EntityDialogFragment extends DialogFragment
implements DialogInterface.OnClickListener {

@Bind(R.id.title) TextView mTitle;
@Bind(R.id.body) TextView mBody;

private static final String ARGS_ID = "when_life_brings_trouble";
private static final String ARGS_TITLE = "you_can_fight_or_run_away";
private static final String ARGS_BODY =
"stay_strong_through_the_struggle_that_s_what_life_is_about";

private static final String ARGS_ROW = "oh_baby_it_s_a_wild_world";

private OnFragmentInteractionListener mListener;

public static EntityDialogFragment newInstance(int id, String title, String body, int row) {
final EntityDialogFragment fragment = new EntityDialogFragment();
final Bundle args = new Bundle();

args.putInt(ARGS_ID, id);
args.putString(ARGS_TITLE, title);
args.putString(ARGS_BODY, body);

args.putInt(ARGS_ROW, row);

fragment.setArguments(args);
return fragment;
}

public EntityDialogFragment() {
// Required empty public constructor
}

@Override public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
activity.toString() + " must implement OnFragmentInteractionListener");
}
}

@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater inflater = getActivity().getLayoutInflater();

final View dialogView = inflater.inflate(R.layout.fragment_title_body_dialog, null);

ButterKnife.bind(this, dialogView);

if (getArguments() != null) {
mTitle.setText(getArguments().getString(ARGS_BODY));
mBody.setText(getArguments().getString(ARGS_BODY));
}

builder.setView(dialogView).setPositiveButton("Save", this).setNegativeButton("Cancel", null);
return builder.create();
}

@Override public void onDetach() {
super.onDetach();
mListener = null;
}

@Override public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
if (getArguments() != null) {
mListener.onSubmit(getArguments().getInt(ARGS_ID), mTitle.getText().toString(),
mBody.getText().toString(), getArguments().getInt(ARGS_ROW));
}
break;
}
}

public interface OnFragmentInteractionListener {
void onSubmit(int id, String title, String body, int row);
}
}
Loading

0 comments on commit 217e5a5

Please sign in to comment.