Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
Merge pull request #131 from HackIllinois/feature/homeclock-redesign
Browse files Browse the repository at this point in the history
Invalidate Homeclock views when resuming
  • Loading branch information
kevinhinterlong authored Feb 23, 2018
2 parents bb791d7 + cccd922 commit def18b2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 64 deletions.
20 changes: 6 additions & 14 deletions app/src/main/java/org/hackillinois/android/ui/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.hackillinois.android.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
Expand All @@ -12,13 +11,9 @@
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import org.hackillinois.android.R;
import org.hackillinois.android.api.response.location.LocationResponse;
import org.hackillinois.android.helper.Settings;
import org.hackillinois.android.helper.Utils;
import org.hackillinois.android.ui.base.BaseActivity;
import org.hackillinois.android.ui.modules.announcement.AnnouncementFragment;
Expand All @@ -29,9 +24,6 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

public class MainActivity extends BaseActivity {
Expand Down Expand Up @@ -74,6 +66,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
fragmentManager = getSupportFragmentManager();

if (savedInstanceState == null) {
fragmentManager.beginTransaction()
.remove(profileFragment)
.remove(announcementFragment)
.remove(scheduleFragment)
.remove(homeFragment)
.commitNow();
fragmentManager.beginTransaction()
.add(R.id.content_frame, profileFragment)
.hide(profileFragment)
Expand Down Expand Up @@ -130,12 +128,6 @@ protected void onNewIntent(Intent intent) {
}
}

@Override
protected void onResumeFragments() {
super.onResumeFragments();
homeFragment.sync();
}

private void swapFragments(Fragment newFragment) {
// Delay newFragment swapping for increased fluidity (we wait for drawer to close)
new Handler().postDelayed(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.annimon.stream.Optional;

import org.hackillinois.android.R;
import org.hackillinois.android.api.response.location.LocationResponse;
import org.hackillinois.android.api.response.login.LoginResponse;
import org.hackillinois.android.helper.Settings;
import org.hackillinois.android.helper.Utils;
Expand Down Expand Up @@ -44,14 +43,14 @@ public class SplashActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_splash);
ButterKnife.bind(this);

Settings settings = Settings.get();

Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

Optional<DateTime> lastAuth = settings.getLastAuth();
Timber.d("User last auth: %s", lastAuth.map(DateTime::toString).orElse("Never"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hackillinois.android.ui.modules.home;

import android.content.Context;
import android.os.CountDownTimer;

import com.airbnb.lottie.LottieAnimationView;
Expand All @@ -15,24 +14,23 @@
import timber.log.Timber;

public class HomeClock {

public interface OnFinishListener {

void onFinish(int timerIndex);
}

private CountDownTimer timer;
private static final float SECONDS_SPEED_RATIO = 2f;//1f / (2.0f);

private static final String COUNTDOWN_60_JSON = "countdown-60.json";
private static final String COUNTDOWN_24_JSON = "countdown-24.json";
private OnFinishListener onFinishListener;
private List<DateTime> activeTimers;

private List<DateTime> activeTimers;
private int finishedTimers;

private final LottieAnimationView secondAnimation;

private final LottieAnimationView minuteAnimation;
private final LottieAnimationView hourAnimation;
private final LottieAnimationView daysAnimation;

public HomeClock(
LottieAnimationView secondAnimation,
LottieAnimationView minuteAnimation,
Expand Down Expand Up @@ -72,6 +70,22 @@ public void setCountDownTo(OnFinishListener onFinishListener, List<DateTime> dat
playNext();
}

public void onPause() {
if(timer != null) {
timer.cancel();
}
}

public void onResume() {
secondAnimation.invalidate();
minuteAnimation.invalidate();
hourAnimation.invalidate();
daysAnimation.invalidate();
if(timer != null) {
timer.start();
}
}

private void playNext() {
if (activeTimers.size() > 0) {
DateTime nextTimer = activeTimers.get(0);
Expand All @@ -85,42 +99,36 @@ private void setCountdownView(DateTime time) {
return;
}

Context context = secondAnimation.getContext();

try {
Period diff = new Period(DateTime.now(), time);
secondAnimation.setFrame(numberToFrame(diff.getSeconds()));
minuteAnimation.setFrame(numberToFrame(diff.getMinutes()));
hourAnimation.setFrame(numberToFrame(diff.getHours()));
daysAnimation.setFrame(numberToFrame(diff.getDays() + 7 * diff.getWeeks()));

long millisUntilFinish = time.getMillis() - DateTime.now().getMillis();
CountDownTimer timer = new CountDownTimer(millisUntilFinish, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Period diff = new Period(DateTime.now(), time);
int seconds = diff.getSeconds();
int hours = diff.getHours();
int minutes = diff.getMinutes();
int days = diff.getDays() + 7 * diff.getWeeks();
tickSecond(seconds, minutes, hours, days);
}
Period diff = new Period(DateTime.now(), time);
secondAnimation.setFrame(numberToFrame(diff.getSeconds()));
minuteAnimation.setFrame(numberToFrame(diff.getMinutes()));
hourAnimation.setFrame(numberToFrame(diff.getHours()));
daysAnimation.setFrame(numberToFrame(diff.getDays() + 7 * diff.getWeeks()));

long millisUntilFinish = time.getMillis() - DateTime.now().getMillis();
timer = new CountDownTimer(millisUntilFinish, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Period diff = new Period(DateTime.now(), time);
int seconds = diff.getSeconds();
int hours = diff.getHours();
int minutes = diff.getMinutes();
int days = diff.getDays() + 7 * diff.getWeeks();
tickSecond(seconds, minutes, hours, days);
}

@Override
public void onFinish() {
if (onFinishListener != null) {
++finishedTimers;
onFinishListener.onFinish(finishedTimers);
if (activeTimers.size() > 0) {
activeTimers.remove(0);
playNext();
}
@Override
public void onFinish() {
if (onFinishListener != null) {
++finishedTimers;
onFinishListener.onFinish(finishedTimers);
if (activeTimers.size() > 0) {
activeTimers.remove(0);
playNext();
}
}
}.start();
} catch (Exception e) {
Timber.wtf(e, "Couldn't play animation");
}
}
}.start();
}

private static int lastSecond = 0;
Expand All @@ -144,7 +152,6 @@ private void tickSecond(int seconds, int minutes, int hours, int days) {
}

private void tickMinute(int seconds, int minutes, int hours, int days) {
Timber.d("Tick minute at %s", DateTime.now());
if (minutes == 0) {
tickHour(seconds, minutes, hours, days);
minuteAnimation.setFrame(numberToFrame(60));
Expand All @@ -159,7 +166,6 @@ private void tickMinute(int seconds, int minutes, int hours, int days) {
}

private void tickHour(int seconds, int minutes, int hours, int days) {
Timber.d("Tick hour at %s", DateTime.now());
if (hours == 0) {
tickDay(seconds, minutes, hours, days);
hourAnimation.setFrame(numberToFrame(24));
Expand All @@ -174,7 +180,6 @@ private void tickHour(int seconds, int minutes, int hours, int days) {
}

private void tickDay(int seconds, int minutes, int hours, int days) {
Timber.d("Tick day at %s", DateTime.now());
if (days == 0) {
daysAnimation.setFrame(numberToFrame(60));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import timber.log.Timber;

public class HomeFragment extends BaseFragment implements HomeClock.OnFinishListener {
@BindView(R.id.second_animation) LottieAnimationView seconds;
Expand All @@ -43,6 +44,12 @@ public class HomeFragment extends BaseFragment implements HomeClock.OnFinishList
@BindView(R.id.countdownTitle) TextView countdownTitle;
@BindView(R.id.empty_view) View emptyView;

private static final List<DateTime> EVENT_TIMERS = Arrays.asList(
Settings.EVENT_START_TIME,
Settings.HACKING_START_TIME,
Settings.HACKING_END_TIME,
Settings.EVENT_END_TIME
);
private static final int[] TITLE_IDS = new int[]{
R.string.hackillinois_starts_in,
R.string.hacking_starts_in,
Expand All @@ -67,6 +74,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
swipeRefresh.setOnRefreshListener(this::fetchEvents);
Utils.attachHackIllinoisRefreshView(swipeRefresh, inflater);

clock = new HomeClock(seconds, minutes, hours, days);
clock.setCountDownTo(this, EVENT_TIMERS);
sync();

//set our adapters to the RecyclerView
Expand Down Expand Up @@ -104,9 +113,18 @@ public void stopRefreshing() {

public void sync() {
fetchEvents();
clock = new HomeClock(seconds, minutes, hours, days);
List<DateTime> eventTimers = Arrays.asList(Settings.EVENT_START_TIME, Settings.HACKING_START_TIME, Settings.HACKING_END_TIME, Settings.EVENT_END_TIME);
clock.setCountDownTo(this, eventTimers);
}

@Override
public void onPause() {
super.onPause();
clock.onPause();
}

@Override
public void onResume() {
super.onResume();
clock.onResume();
}

public void fetchEvents() {
Expand Down

0 comments on commit def18b2

Please sign in to comment.