Skip to content

Commit

Permalink
Current Status: ArticleDetailActivity and ArticleDetailFragment restr…
Browse files Browse the repository at this point in the history
…uctured based on the new layout design

Manifest updated with new App Theme
Removed irrelevant code from ArticleDetailActivity java code
Removed irrelevant code from ArticleDetailFragment java code
New style added to styles.xml
  • Loading branch information
thatsabhi22 committed May 30, 2020
1 parent 5df72e2 commit c3f1f20
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 249 deletions.
1 change: 1 addition & 0 deletions XYZReader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.palette:palette:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
Expand Down
2 changes: 1 addition & 1 deletion XYZReader/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Bacon">
android:theme="@style/AppTheme">

<activity
android:name=".ui.ArticleListActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import androidx.legacy.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;

import androidx.appcompat.app.AppCompatActivity;
import androidx.legacy.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.xyzreader.R;
import com.example.xyzreader.data.ArticleLoader;
Expand All @@ -30,13 +30,9 @@ public class ArticleDetailActivity extends AppCompatActivity
private long mStartId;

private long mSelectedItemId;
private int mSelectedItemUpButtonFloor = Integer.MAX_VALUE;
private int mTopInset;

private ViewPager mPager;
private MyPagerAdapter mPagerAdapter;
// private View mUpButtonContainer;
// private View mUpButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -57,47 +53,20 @@ protected void onCreate(Bundle savedInstanceState) {
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
mPager.setPageMarginDrawable(new ColorDrawable(0x22000000));

// mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
// @Override
// public void onPageScrollStateChanged(int state) {
// super.onPageScrollStateChanged(state);
// mUpButton.animate()
// .alpha((state == ViewPager.SCROLL_STATE_IDLE) ? 1f : 0f)
// .setDuration(300);
// }
//
// @Override
// public void onPageSelected(int position) {
// if (mCursor != null) {
// mCursor.moveToPosition(position);
// }
// mSelectedItemId = mCursor.getLong(ArticleLoader.Query._ID);
// updateUpButtonPosition();
// }
// });

// mUpButtonContainer = findViewById(R.id.up_container);

// mUpButton = findViewById(R.id.action_up);
// mUpButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// onSupportNavigateUp();
// }
// });

// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// mUpButtonContainer.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
// @Override
// public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
// view.onApplyWindowInsets(windowInsets);
// mTopInset = windowInsets.getSystemWindowInsetTop();
// mUpButtonContainer.setTranslationY(mTopInset);
// updateUpButtonPosition();
// return windowInsets;
// }
// });
// }
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}

@Override
public void onPageSelected(int position) {
if (mCursor != null) {
mCursor.moveToPosition(position);
}
mSelectedItemId = mCursor.getLong(ArticleLoader.Query._ID);
}
});

if (savedInstanceState == null) {
if (getIntent() != null && getIntent().getData() != null) {
Expand Down Expand Up @@ -139,18 +108,6 @@ public void onLoaderReset(Loader<Cursor> cursorLoader) {
mPagerAdapter.notifyDataSetChanged();
}

public void onUpButtonFloorChanged(long itemId, ArticleDetailFragment fragment) {
if (itemId == mSelectedItemId) {
mSelectedItemUpButtonFloor = fragment.getUpButtonFloor();
updateUpButtonPosition();
}
}

private void updateUpButtonPosition() {
// int upButtonNormalBottom = mTopInset + mUpButton.getHeight();
// mUpButton.setTranslationY(Math.min(mSelectedItemUpButtonFloor - upButtonNormalBottom, 0));
}

private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
Expand All @@ -159,11 +116,6 @@ public MyPagerAdapter(FragmentManager fm) {
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
ArticleDetailFragment fragment = (ArticleDetailFragment) object;
if (fragment != null) {
mSelectedItemUpButtonFloor = fragment.getUpButtonFloor();
updateUpButtonPosition();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import android.os.Bundle;
import androidx.core.app.ShareCompat;
import androidx.palette.graphics.Palette;
import android.text.Html;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ShareCompat;
import androidx.palette.graphics.Palette;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.xyzreader.R;
import com.example.xyzreader.data.ArticleLoader;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* A fragment representing a single Article detail screen. This fragment is
Expand All @@ -41,9 +42,8 @@
*/
public class ArticleDetailFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = "ArticleDetailFragment";

public static final String ARG_ITEM_ID = "item_id";
private static final String TAG = "ArticleDetailFragment";
private static final float PARALLAX_FACTOR = 1.25f;

private Cursor mCursor;
Expand All @@ -65,7 +65,9 @@ public class ArticleDetailFragment extends Fragment implements
// Use default locale format
private SimpleDateFormat outputFormat = new SimpleDateFormat();
// Most time functions can only handle 1902 - 2037
private GregorianCalendar START_OF_EPOCH = new GregorianCalendar(2,1,1);
private GregorianCalendar START_OF_EPOCH = new GregorianCalendar(2, 1, 1);

private CollapsingToolbarLayout collapsingToolbarLayout;

/**
* Mandatory empty constructor for the fragment manager to instantiate the
Expand Down Expand Up @@ -113,7 +115,7 @@ public void onActivityCreated(Bundle savedInstanceState) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_article_detail, container, false);
mDrawInsetsFrameLayout = (DrawInsetsFrameLayout)
mRootView.findViewById(R.id.draw_insets_frame_layout);
Expand All @@ -124,21 +126,7 @@ public void onInsetsChanged(Rect insets) {
}
});

mScrollView = (ObservableScrollView) mRootView.findViewById(R.id.scrollview);
mScrollView.setCallbacks(new ObservableScrollView.Callbacks() {
@Override
public void onScrollChanged() {
mScrollY = mScrollView.getScrollY();
getActivityCast().onUpButtonFloorChanged(mItemId, ArticleDetailFragment.this);
mPhotoContainerView.setTranslationY((int) (mScrollY - mScrollY / PARALLAX_FACTOR));
updateStatusBar();
}
});

mPhotoView = (ImageView) mRootView.findViewById(R.id.photo);
mPhotoContainerView = mRootView.findViewById(R.id.photo_container);

mStatusBarColorDrawable = new ColorDrawable(0);

mRootView.findViewById(R.id.share_fab).setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -151,37 +139,13 @@ public void onClick(View view) {
});

bindViews();
updateStatusBar();
return mRootView;
}

private void updateStatusBar() {
int color = 0;
if (mPhotoView != null && mTopInset != 0 && mScrollY > 0) {
float f = progress(mScrollY,
mStatusBarFullOpacityBottom - mTopInset * 3,
mStatusBarFullOpacityBottom - mTopInset);
color = Color.argb((int) (255 * f),
(int) (Color.red(mMutedColor) * 0.9),
(int) (Color.green(mMutedColor) * 0.9),
(int) (Color.blue(mMutedColor) * 0.9));
}
mStatusBarColorDrawable.setColor(color);
mDrawInsetsFrameLayout.setInsetBackground(mStatusBarColorDrawable);
}
Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbarDetail);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

static float progress(float v, float min, float max) {
return constrain((v - min) / (max - min), 0, 1);
}
collapsingToolbarLayout = (CollapsingToolbarLayout) mRootView.findViewById(R.id.collapsing_toolbar_layout);

static float constrain(float val, float min, float max) {
if (val < min) {
return min;
} else if (val > max) {
return max;
} else {
return val;
}
return mRootView;
}

private Date parsePublishedDate() {
Expand All @@ -200,9 +164,7 @@ private void bindViews() {
return;
}

TextView titleView = (TextView) mRootView.findViewById(R.id.article_title);
TextView bylineView = (TextView) mRootView.findViewById(R.id.article_byline);
bylineView.setMovementMethod(new LinkMovementMethod());
TextView bodyView = (TextView) mRootView.findViewById(R.id.article_body);


Expand All @@ -212,7 +174,9 @@ private void bindViews() {
mRootView.setAlpha(0);
mRootView.setVisibility(View.VISIBLE);
mRootView.animate().alpha(1);
titleView.setText(mCursor.getString(ArticleLoader.Query.TITLE));

collapsingToolbarLayout.setTitle(mCursor.getString(ArticleLoader.Query.TITLE));

Date publishedDate = parsePublishedDate();
if (!publishedDate.before(START_OF_EPOCH.getTime())) {
bylineView.setText(Html.fromHtml(
Expand All @@ -228,7 +192,7 @@ private void bindViews() {
// If date is before 1902, just show the string
bylineView.setText(Html.fromHtml(
outputFormat.format(publishedDate) + " by <font color='#ffffff'>"
+ mCursor.getString(ArticleLoader.Query.AUTHOR)
+ mCursor.getString(ArticleLoader.Query.AUTHOR)
+ "</font>"));

}
Expand All @@ -242,9 +206,6 @@ public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
Palette p = Palette.generate(bitmap, 12);
mMutedColor = p.getDarkMutedColor(0xFF333333);
mPhotoView.setImageBitmap(imageContainer.getBitmap());
mRootView.findViewById(R.id.meta_bar)
.setBackgroundColor(mMutedColor);
updateStatusBar();
}
}

Expand All @@ -255,9 +216,6 @@ public void onErrorResponse(VolleyError volleyError) {
});
} else {
mRootView.setVisibility(View.GONE);
titleView.setText("N/A");
bylineView.setText("N/A" );
bodyView.setText("N/A");
}
}

Expand Down Expand Up @@ -290,15 +248,4 @@ public void onLoaderReset(Loader<Cursor> cursorLoader) {
mCursor = null;
bindViews();
}

public int getUpButtonFloor() {
if (mPhotoContainerView == null || mPhotoView.getHeight() == 0) {
return Integer.MAX_VALUE;
}

// account for parallax
return mIsCard
? (int) mPhotoContainerView.getTranslationY() + mPhotoView.getHeight() - mScrollY
: mPhotoView.getHeight() - mScrollY;
}
}
Loading

0 comments on commit c3f1f20

Please sign in to comment.