This commit is contained in:
Phillip Hsu
2016-08-15 15:28:18 -07:00
parent b1f3c9c8c7
commit a7861a04c1
14 changed files with 170 additions and 32 deletions
@@ -1,14 +1,15 @@
package com.philliphsu.clock2;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
@@ -36,13 +37,14 @@ public class MainActivity extends BaseActivity {
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
private Drawable mAddItemDrawable;
// For delaying fab.show() on SCROLL_STATE_SETTLING
private final Handler mHandler = new Handler();
private boolean mScrollStateDragging;
private int mPageDragging = -1; // TOneverDO: initial value >= 0
private boolean mDraggingPastEndBoundaries;
// // For delaying fab.show() on SCROLL_STATE_SETTLING
// private final Handler mHandler = new Handler();
//
// private boolean mScrollStateDragging;
// private int mPageDragging = -1; // TOneverDO: initial value >= 0
// private boolean mDraggingPastEndBoundaries;
@Bind(R.id.container)
ViewPager mViewPager;
@@ -81,6 +83,8 @@ public class MainActivity extends BaseActivity {
// x-positions as each intermediate page is scrolled through.
// This is a visual optimization that ends the translation motion, immediately
// returning the FAB to its target position.
// TODO: The animation visibly skips to the end. We could interpolate
// intermediate x-positions if we cared to smooth it out.
mFab.setTranslationX(0);
} else {
// Initially, the FAB's translationX property is zero because, at its original
@@ -116,8 +120,7 @@ public class MainActivity extends BaseActivity {
@Override
public void onPageSelected(int position) {
if (position < mSectionsPagerAdapter.getCount() - 1) {
// TODO: Plus icon. Consider caching the Drawable in a member variable.
mFab.setImageResource(android.R.drawable.ic_dialog_email);
mFab.setImageDrawable(mAddItemDrawable);
}
}
// @Override
@@ -184,6 +187,10 @@ public class MainActivity extends BaseActivity {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// Using the resources is fine since tab icons will never change once they are set.
tabLayout.getTabAt(0).setIcon(R.drawable.ic_alarm_24dp);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_timer_24dp);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_stopwatch_24dp);
// TODO: @OnCLick instead.
mFab.setOnClickListener(new View.OnClickListener() {
@@ -203,6 +210,8 @@ public class MainActivity extends BaseActivity {
}
}
});
mAddItemDrawable = ContextCompat.getDrawable(this, R.drawable.ic_add_24dp);
}
@Override
@@ -319,18 +328,19 @@ public class MainActivity extends BaseActivity {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
// TODO: If you wish to have text labels for your tabs, then implement this method.
// @Override
// public CharSequence getPageTitle(int position) {
// switch (position) {
// case 0:
// return "SECTION 1";
// case 1:
// return "SECTION 2";
// case 2:
// return "SECTION 3";
// }
// return null;
// }
public Fragment getFragment(int position) {
return mFragments.get(position);
@@ -1,3 +1,19 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* 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.
*/
package com.philliphsu.clock2.stopwatch;
import android.annotation.TargetApi;
@@ -2,11 +2,13 @@ package com.philliphsu.clock2.stopwatch;
import android.animation.ObjectAnimator;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
@@ -45,6 +47,8 @@ public class StopwatchFragment extends RecyclerViewFragment<
private ObjectAnimator mProgressAnimator;
private SharedPreferences mPrefs;
private WeakReference<FloatingActionButton> mActivityFab;
private Drawable mStartDrawable;
private Drawable mPauseDrawable;
@Bind(R.id.chronometer) ChronometerWithMillis mChronometer;
@Bind(R.id.new_lap) FloatingActionButton mNewLapButton;
@@ -65,8 +69,13 @@ public class StopwatchFragment extends RecyclerViewFragment<
mPauseTime = mPrefs.getLong(KEY_PAUSE_TIME, 0);
Log.d(TAG, "mStartTime = " + mStartTime
+ ", mPauseTime = " + mPauseTime);
// TODO: Will these be kept alive after onDestroyView()? If not, we should move these to
// onCreateView() or any other callback that is guaranteed to be called.
mStartDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.ic_start_24dp);
mPauseDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_24dp);
}
// TODO: Restore progress bar animator on rotate
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -88,12 +97,25 @@ public class StopwatchFragment extends RecyclerViewFragment<
if (mPrefs.getBoolean(KEY_CHRONOMETER_RUNNING, false)) {
mChronometer.start();
}
// Hides the mini fabs prematurely, so when we actually select this tab
// they don't show at all before hiding.
// Hides the mini fabs prematurely, so when we actually display this tab
// they won't show even briefly at all before hiding.
updateButtonControls();
return view;
}
@Override
public void onResume() {
super.onResume();
// FAB icon change won't happen when selecting this tab from multiple tabs away,
// because isResumed() and isVisible() both return false, respectively from
// setUserVisibleHint() and from updateButtonControls() when called by onCreateView().
// TODO: Since this is only called to change the FAB icon, just allow duplicate
// code and manipulate the FAB directly.
// TODO: This has the side effect of prematurely changing the icon for the
// page directly before this page.
updateButtonControls();
}
/**
* If the user navigates away, this is the furthest point in the lifecycle
* this Fragment gets to. Here, the view hierarchy returned from onCreateView()
@@ -199,6 +221,7 @@ public class StopwatchFragment extends RecyclerViewFragment<
// We will get called again when we actually have this page selected, and by that time
// onCreateView() will have been called. Wait until we're resumed to call through.
if (isVisibleToUser && isResumed()) {
Log.d(TAG, "setUserVisibleHint called thru");
// At this point, the only thing this does is change the fab icon
// TODO: allow duplicate code and manipulate the fab icon directly?
// TODO: There is noticeable latency between showing this tab and
@@ -274,8 +297,8 @@ public class StopwatchFragment extends RecyclerViewFragment<
mNewLapButton.setVisibility(vis);
mStopButton.setVisibility(vis);
if (isVisible()) { // avoid changing the icon prematurely, esp. when we're not on this tab
// TODO: pause and start icon, resp.
mActivityFab.get().setImageResource(mChronometer.isRunning() ? 0 : 0);
Log.d(TAG, "Fab icon changing");
mActivityFab.get().setImageDrawable(mChronometer.isRunning() ? mPauseDrawable : mStartDrawable);
}
}
@@ -1,3 +1,19 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* 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.
*/
package com.philliphsu.clock2.timers;
import android.annotation.TargetApi;