Skip to content

Commit

Permalink
Merge pull request #105 from ZeusWPI/cacheAndDate
Browse files Browse the repository at this point in the history
Improve cache and date
  • Loading branch information
niknetniko authored Sep 30, 2016
2 parents a0729ca + fea99ac commit 7d49c69
Show file tree
Hide file tree
Showing 131 changed files with 1,486 additions and 2,081 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ dependencies {
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.timehop.stickyheadersrecyclerview:library:0.4.3@aar'
compile 'net.danlew:android.joda:2.9.3.1'
compile 'com.jakewharton.threetenabp:threetenabp:1.0.4'
compile 'com.google.android.gms:play-services-analytics:9.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/be/ugent/zeus/hydra/HydraApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.jakewharton.threetenabp.AndroidThreeTen;
import net.danlew.android.joda.JodaTimeAndroid;

/**
* The Hydra application.
Expand All @@ -23,7 +22,6 @@ public class HydraApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JodaTimeAndroid.init(this);
AndroidThreeTen.init(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import be.ugent.zeus.hydra.models.association.Activity;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.format.DateTimeFormatter;

/**
* Activity to show details of an association's event.
Expand All @@ -31,6 +30,8 @@ public class ActivityDetailActivity extends ToolbarActivity implements View.OnCl

public static final String PARCEL_EVENT = "eventParcelable";

private static final DateTimeFormatter formatHour = DateTimeFormatter.ofPattern("HH:mm");
private static final DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("E d MMM H:mm");
private static final String GENT = "51.3,3.44";

//The data
Expand Down Expand Up @@ -75,21 +76,18 @@ protected void onCreate(Bundle savedInstanceState) {
}

if(event.getStart() != null) {
DateTimeFormatter startTimeFormatter = DateTimeFormat.forPattern("E d MMM H:mm");

DateTime start = new DateTime(event.getStart());
LocalDateTime start = event.getLocalStart();
if (event.getEnd() != null) {
DateTime end = new DateTime(event.getEnd());
if (start.dayOfYear() == end.dayOfYear() || start.plusHours(12).isAfter(end)) {
LocalDateTime end = event.getLocalEnd();
if (start.getDayOfYear() == end.getDayOfYear() || start.plusHours(12).isAfter(end)) {
// Use format day month start time - end time
DateTimeFormatter endTimeFormatter = DateTimeFormat.forPattern("H:mm");
date.setText(String.format("%s - %s", startTimeFormatter.print(start), endTimeFormatter.print(end)));
date.setText(String.format("%s - %s", start.format(formatHour), end.format(formatHour)));
} else {
// Use format with two dates
date.setText(String.format("%s - %s",startTimeFormatter.print(start), startTimeFormatter.print(end)));
date.setText(String.format("%s - %s", start.format(fullFormatter), end.format(fullFormatter)));
}
} else {
date.setText(startTimeFormatter.print(start));
date.setText(start.format(fullFormatter));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

if(article.getDate() != null) {
date.setText(DateUtils.relativeDateString(article.getDate(), date.getContext()));
date.setText(DateUtils.relativeDateTimeString(article.getDate(), date.getContext()));
}

if(article.getContent() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {

String category = StringUtils.capitaliseFirst(article.getCategory());
if(article.getPubDate() != null) {
date.setText(DateUtils.relativeDateString(article.getPubDate(), date.getContext()) + " - " + category);
date.setText(DateUtils.relativeDateTimeString(article.getPubDate(), date.getContext()) + " - " + category);
} else {
date.setText(category);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
package be.ugent.zeus.hydra.activities.common;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.cache.CachedAsyncTaskLoader;
import be.ugent.zeus.hydra.loader.ErrorLoaderCallback;
import be.ugent.zeus.hydra.loader.ThrowableEither;
import be.ugent.zeus.hydra.caching.CacheRequest;
import be.ugent.zeus.hydra.loaders.LoaderCallbackHandler;
import be.ugent.zeus.hydra.loaders.RequestAsyncTaskLoader;
import be.ugent.zeus.hydra.loaders.ThrowableEither;
import be.ugent.zeus.hydra.requests.common.SimpleCacheRequest;

import java.io.Serializable;

/**
* Activity that uses the {@link CachedAsyncTaskLoader}.
* Activity that loads {@link be.ugent.zeus.hydra.caching.CacheRequest} using a loader.
*
* @author Niko Strijbol
*/
public abstract class LoaderToolbarActivity<D extends Serializable> extends ToolbarActivity implements ErrorLoaderCallback<D, D> {
public abstract class LoaderToolbarActivity<D extends Serializable> extends ToolbarActivity implements LoaderCallbackHandler.LoaderCallback<D>, LoaderCallbackHandler.ProgressbarListener {

private static final String TAG = "LoaderToolbarActivity";

protected LoaderCallbackHandler<D> loaderHandler = new LoaderCallbackHandler<>(this, this);
// ID of the loader.
private static final int LOADER = 0;
protected boolean shouldRenew = false;
//The progress bar. Is used if not null.
protected ProgressBar progressBar;
Expand All @@ -40,71 +43,27 @@ public abstract class LoaderToolbarActivity<D extends Serializable> extends Tool
public void setContentView(@LayoutRes int layoutResID) {
super.setContentView(layoutResID);
progressBar = $(R.id.progress_bar);

}

/**
* Hide the progress bar.
*/
private void hideProgressBar() {
progressBar.setVisibility(View.GONE);
}

/**
* Called when a previously created loader is being reset, and thus making its data unavailable. The application
* should at this point remove any references it has to the Loader's data.
*
* @param loader The Loader that is being reset.
*/
@Override
public void onLoaderReset(Loader<ThrowableEither<D>> loader) {
loader.reset();
public LoaderManager getTheLoaderManager() {
return getSupportLoaderManager();
}

/**
* Called when a previously created loader has finished its load.
*
* @param loader The Loader that has finished.
* @param data The data generated by the Loader.
*/
@Override
public void onLoadFinished(Loader<ThrowableEither<D>> loader, ThrowableEither<D> data) {
if(data.hasError()) {
receiveError(data.getError());
} else {
receiveData(data.getData());
}
hideProgressBar();
public void hideProgressBar() {
progressBar.setVisibility(View.GONE);
}

/**
* Instantiate and return a new Loader for the given ID.
*
* @param id The ID whose loader is to be created.
* @param args Any arguments supplied by the caller.
*
* @return Return a new Loader instance that is ready to start loading.
*/
@Override
public Loader<ThrowableEither<D>> onCreateLoader(int id, Bundle args) {
return new CachedAsyncTaskLoader<>(getRequest(), getApplicationContext(), shouldRenew);
public void showProgressBar() {
progressBar.setVisibility(View.VISIBLE);
}

/**
* Start the loader.
*/
protected void startLoader() {
// Start the data loader.
getSupportLoaderManager().initLoader(LOADER, null, this);
@Override
public Loader<ThrowableEither<D>> getLoader() {
return new RequestAsyncTaskLoader<>(new SimpleCacheRequest<>(this, getRequest(), shouldRenew), this);
}

/**
* Restart the loader.
*/
protected void restartLoader() {
// Start the data loader.
getSupportLoaderManager().restartLoader(LOADER, null, this);
}
protected abstract CacheRequest<D> getRequest();

/**
* When the request has failed.
Expand All @@ -128,7 +87,7 @@ public void onClick(View v) {
protected void refresh() {
Toast.makeText(getApplicationContext(), R.string.begin_refresh, Toast.LENGTH_SHORT).show();
shouldRenew = true;
restartLoader();
loaderHandler.restartLoader();
shouldRenew = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import be.ugent.zeus.hydra.activities.common.ToolbarActivity;
import be.ugent.zeus.hydra.models.minerva.AgendaItem;
import be.ugent.zeus.hydra.utils.html.Utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.threeten.bp.format.DateTimeFormatter;

/**
* @author Niko Strijbol
Expand All @@ -27,7 +24,7 @@ public class AgendaActivity extends ToolbarActivity {
public static final String ARG_AGENDA_ITEM = "argAgendaItem";

private AgendaItem agendaItem;
private static final DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm", new Locale("nl"));
private static final DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -56,13 +53,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
location.setText(agendaItem.getLocation());
}

String start = format.format(agendaItem.getStartDate());
String end = format.format(agendaItem.getEndDate());
TextView startTime = $(R.id.agenda_time_start);
//TODO: deta
startTime.setText(start);
TextView endTime = $(R.id.agenda_time_end);
endTime.setText(end);

startTime.setText(agendaItem.getStartDate().format(format));
endTime.setText(agendaItem.getEndDate().format(format));

TextView course = $(R.id.agenda_course);
if(TextUtils.isEmpty(agendaItem.getCourse().getTitle())) {
Expand Down Expand Up @@ -96,8 +91,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
private void addToCalendar() {
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, agendaItem.getStartDate().getTime())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, agendaItem.getEndDate().getTime())
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, agendaItem.getStartDate().toInstant().toEpochMilli())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, agendaItem.getEndDate().toInstant().toEpochMilli())
.putExtra(CalendarContract.Events.TITLE, agendaItem.getTitle())
.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import be.ugent.zeus.hydra.utils.DateUtils;
import be.ugent.zeus.hydra.utils.html.PicassoImageGetter;
import be.ugent.zeus.hydra.utils.html.Utils;

import java.util.Date;
import org.threeten.bp.ZonedDateTime;

/**
* Show a Minerva announcement.
Expand Down Expand Up @@ -59,7 +58,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

if(announcement.getDate() != null) {
date.setText(DateUtils.relativeDateString(announcement.getDate(), date.getContext()));
date.setText(DateUtils.relativeDateTimeString(announcement.getDate(), date.getContext()));
}

if(announcement.getContent() != null) {
Expand Down Expand Up @@ -101,7 +100,7 @@ protected void onStart() {
//Set the read date if needed
if(!announcement.isRead()) {
read = true;
announcement.setRead(new Date());
announcement.setRead(ZonedDateTime.now());
}
setResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import be.ugent.zeus.hydra.minerva.auth.models.BearerToken;
import be.ugent.zeus.hydra.minerva.auth.models.GrantInformation;
import be.ugent.zeus.hydra.requests.common.Request;
import be.ugent.zeus.hydra.requests.common.RequestFailureException;
import be.ugent.zeus.hydra.requests.exceptions.RequestFailureException;
import be.ugent.zeus.hydra.requests.minerva.UserInfoRequest;
import be.ugent.zeus.hydra.utils.customtabs.ActivityHelper;
import be.ugent.zeus.hydra.utils.customtabs.CustomTabsHelper;
import org.joda.time.DateTime;
import org.threeten.bp.LocalDateTime;

/**
* An activity to prompt the user to authorise our access to the account.
Expand Down Expand Up @@ -175,8 +175,8 @@ protected Intent doInBackground(String... strings) {
manager.setPassword(account, result.getRefreshToken());
}

DateTime expiration = DateTime.now().plusSeconds(result.getExpiresIn());
manager.setUserData(account, MinervaAuthenticator.EXP_DATE, MinervaAuthenticator.formatter.print(expiration));
LocalDateTime expiration = LocalDateTime.now().plusSeconds(result.getExpiresIn());
manager.setUserData(account, MinervaAuthenticator.EXP_DATE, expiration.format(MinervaAuthenticator.formatter));
manager.setAuthToken(account, authType, result.getAccessToken());

//Make intent for return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import be.ugent.zeus.hydra.R;
import be.ugent.zeus.hydra.activities.common.LoaderToolbarActivity;
import be.ugent.zeus.hydra.cache.CacheRequest;
import be.ugent.zeus.hydra.caching.CacheRequest;
import be.ugent.zeus.hydra.models.association.Association;
import be.ugent.zeus.hydra.models.association.Associations;
import be.ugent.zeus.hydra.recyclerview.adapters.MultiSelectListAdapter;
Expand Down Expand Up @@ -61,7 +61,7 @@ public String getDisplayValue(Association element) {
scroller.setRecyclerView(recyclerView);

searchView.setOnQueryTextListener(adapter);
startLoader();
loaderHandler.startLoader();
}

@Override
Expand Down Expand Up @@ -100,7 +100,7 @@ public void receiveData(@NonNull Associations data) {
}

@Override
public CacheRequest<Associations, Associations> getRequest() {
public CacheRequest<Associations> getRequest() {
return new AssociationsRequest();
}

Expand Down
Loading

0 comments on commit 7d49c69

Please sign in to comment.