Commit 3e9464dd authored by Roger Barton's avatar Roger Barton
Browse files

Can easily add a new event on its own, launch the EventDetailAcivity with an event reference

Can launch eventDetailActvity with either, index (from sorted or unsorted eventInfos) or the eventId.
Refinements in the Events class
parent d5fcbabf
Pipeline #8490 passed with stages
in 3 minutes and 4 seconds
......@@ -81,14 +81,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
instance = this;
setContentView(R.layout.core_main);
/*
//Use this to set a custom taskDescription in the app overview, ie when switching apps. Can set the icon, label and color of the bar
Resources r = getResources();
ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(r.getString(R.string.app_name),
BitmapFactory.decodeResource(getResources(), R.drawable.ic_amiv_logo_icon_white),
r.getColor(R.color.white));
this.setTaskDescription(taskDescription);*/
Toolbar toolbar = Util.SetupToolbar(this, false);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
......@@ -225,16 +217,16 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public void StartEventDetailActivity(int eventGroup, int eventIndex)
{
Intent intent = new Intent(this, EventDetailActivity.class);
intent.putExtra("eventGroup", eventGroup);
intent.putExtra("eventIndex", eventIndex);
intent.putExtra(EventDetailActivity.LauncherExtras.EVENT_GROUP, eventGroup);
intent.putExtra(EventDetailActivity.LauncherExtras.EVENT_INDEX, eventIndex);
startActivityForResult(intent, 0);
}
public void StartJobDetailActivity(int jobGroup, int jobIndex)
{
Intent intent = new Intent(this, JobDetailActivity.class);
intent.putExtra("jobGroup", jobGroup);
intent.putExtra("jobIndex", jobIndex);
intent.putExtra(JobDetailActivity.LauncherExtras.JOB_GROUP, jobGroup);
intent.putExtra(JobDetailActivity.LauncherExtras.JOB_INDEX, jobIndex);
startActivityForResult(intent, 0);
}
......
......@@ -22,7 +22,7 @@ public class MainActivity extends AppCompatActivity {
String firstname = UserInfo.current.firstname;
//To access events use
EventInfo eventInfo = Events.sortedEvents.get(0).get(1);//sorted into groups by date
EventInfo eventInfo = Events.sortedEventInfos.get(0).get(1);//sorted into groups by date
eventInfo = Events.eventInfos.get(0);//unsorted list
//To Send a request to the api or elsewhere see core.Request class for examples. Basic structure is as below
......
......@@ -43,29 +43,32 @@ import ch.amiv.android_app.core.LoginActivity;
import ch.amiv.android_app.core.Request;
import ch.amiv.android_app.core.Settings;
import ch.amiv.android_app.core.UserInfo;
import ch.amiv.android_app.util.PersistentStorage;
import ch.amiv.android_app.util.Util;
/**
* The activity/screen used for showing a selected event in detail.
* This mainly displays stored info about the event, eg description and also fetches more such as images. Also handles registering for and event and the possible outcomes
*
* To launch this activity, you need to provide an event to view. You need to provide this as an intent extra (use intent.putExtra()):
* - Provide eventGroup == -1, eventGroup == index in Events.eventInfos (unsorted list)
* - Provide eventGroup == group in Events.sortedEventInfos, eventGroup == index in Events.sortedEventInfos (sorted list)
* - Provide eventId == any valid event id
*
* If the event is not found, the activity finishes and returns to the calling activity.
*/
public class EventDetailActivity extends AppCompatActivity {
private int eventGroup = 0;
private int eventIndex = 0;
private EventInfo event(){ //Used to easily access the activities event
if(!hasEvent())
return null;
return Events.sortedEvents.get(eventGroup).get(eventIndex);
/**
* A constant class to easily set extras for launching the EventDetailActivity
*/
public static final class LauncherExtras {
public static final String EVENT_GROUP = "eventGroup";
public static final String EVENT_INDEX = "eventIndex";
public static final String EVENT_ID = "eventId";
public static final String LOAD_EVENTS = "loadEvents";
}
private boolean hasEvent(){
if(eventGroup >= Events.sortedEvents.size() || eventIndex >= Events.sortedEvents.get(eventGroup).size()){
Log.e("events", "EventDetailActivity given invalid event indexes, (group, index) = (" + eventGroup + ", " + eventIndex + "), with sortedEvents size of 1st dim: " + Events.sortedEvents.size());
return false;
}
return true;
}
private EventInfo event;
private ImageView posterImage;
private ImageView posterMask;
......@@ -89,7 +92,7 @@ public class EventDetailActivity extends AppCompatActivity {
@Override
public void OnDataReceived() {
SetUIDirty(true, false);
Request.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, cancelRefreshCallback, event()._id);
Request.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, cancelRefreshCallback, event._id);
LoadEventImage(true);
}
};
......@@ -104,9 +107,9 @@ public class EventDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
GetIntentData();
super.onCreate(savedInstanceState);
setContentView(R.layout.events_detail);
GetIntentData();
InitUI();
}
......@@ -145,7 +148,11 @@ public class EventDetailActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
//return to the calling activity with the set response in the intent, such as whether the login state has changed
setResult(RESULT_OK, responseIntent);
ReturnToCallingActivity(true);
}
private void ReturnToCallingActivity (boolean success){
setResult(success ? RESULT_OK : RESULT_CANCELED, responseIntent);
finish();
}
......@@ -153,13 +160,33 @@ public class EventDetailActivity extends AppCompatActivity {
* This will retrieve the eventIndexes to display, is only set when we originate from the MainActivity, where the int is added to the intent.
*/
private void GetIntentData (){
if(eventGroup == 0 && eventIndex == 0) {
Intent intent = getIntent();
if(intent.hasExtra("eventGroup") && intent.hasExtra("eventIndex")) {
eventGroup = intent.getIntExtra("eventGroup", 0);
eventIndex = intent.getIntExtra("eventIndex", 0);
}
Intent intent = getIntent();
if(intent.getBooleanExtra(LauncherExtras.LOAD_EVENTS, false))
PersistentStorage.LoadEvents(getApplicationContext());
if(intent.hasExtra(LauncherExtras.EVENT_GROUP) && intent.hasExtra(LauncherExtras.EVENT_INDEX))
{
int eventGroup = intent.getIntExtra(LauncherExtras.EVENT_GROUP, 0);
int eventIndex = intent.getIntExtra(LauncherExtras.EVENT_INDEX, 0);
if(eventGroup == -1)
event = Events.eventInfos.get(eventIndex);
else
event = Events.sortedEventInfos.get(eventGroup).get(eventIndex);
if (event == null)
Log.e("events", "invalid event index selected during InitUI(), (groupIndex, eventIndex): (" + eventGroup + "," + eventIndex + "), total event size" + Events.eventInfos.size() + ". Ensure that you are not clearing/overwriting the events list while viewing an event. Returning to calling activity...");
}
else if(intent.hasExtra(LauncherExtras.EVENT_ID))
{
event = Events.GetById(intent.getStringExtra(LauncherExtras.EVENT_ID));
if(event == null)
Log.e("events", "No event found from eventId=" + intent.getStringExtra(LauncherExtras.EVENT_ID) + " in intent, have you used intent.putStringExtra. Returning to calling activity...");
}
if(event == null)
ReturnToCallingActivity(false);
}
/**
......@@ -169,11 +196,7 @@ public class EventDetailActivity extends AppCompatActivity {
Util.SetupToolbar(this, true);
//Check that we have been given an event that exists else return to the calling activity
if(!hasEvent()) {
Log.e("events", "invalid event index selected during InitUI(), (groupIndex, eventIndex): (" + eventGroup + "," + eventIndex + "), total event size" + Events.eventInfos.size() + ". Ensure that you are not clearing/overwriting the events list while viewing an event.");
onBackPressed();
return;
}
if(event == null) return;
//Link up variables with UI elements from the layout xml
scrollView = findViewById(R.id.scrollView);
......@@ -230,16 +253,16 @@ public class EventDetailActivity extends AppCompatActivity {
}
private void OnSwipeRefreshed(){
if(!hasEvent())
return;
Request.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, cancelRefreshCallback, event()._id);
if(event == null) return;
Request.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, cancelRefreshCallback, event._id);
}
public void SetUIDirty(boolean isRefreshing, boolean signupUpdated)
{
if(!signupUpdated) {
((TextView) findViewById(R.id.eventTitle)).setText(event().GetTitle(getResources()));
((TextView) findViewById(R.id.eventDetail)).setText(event().GetDescription(getResources()));
((TextView) findViewById(R.id.eventTitle)).setText(event.GetTitle(getResources()));
((TextView) findViewById(R.id.eventDetail)).setText(event.GetDescription(getResources()));
LoadEventImage(isRefreshing);
AddRegisterInfos();
}
......@@ -253,7 +276,7 @@ public class EventDetailActivity extends AppCompatActivity {
private void LoadEventImage (boolean isRefreshing)
{
//Image loading and masking. the posterMask is a small arrow image but we use the layout margin to add some transparent 'padding' to the top of the scrollview
if(event().poster_url.isEmpty() || !Request.CheckConnection(getApplicationContext()))
if(event.poster_url.isEmpty() || !Request.CheckConnection(getApplicationContext()))
{
//Hide the image and mask if there is no poster linked with the event or we have no internet
if(!isRefreshing) {
......@@ -283,7 +306,7 @@ public class EventDetailActivity extends AppCompatActivity {
}
//Send a request for the image, note we can also use a NetworkImageView, but have less control then
ImageRequest posterRequest = new ImageRequest(event().GetPosterUrl(),
ImageRequest posterRequest = new ImageRequest(event.GetPosterUrl(),
new Response.Listener<Bitmap>() {
@Override
public void onResponse(final Bitmap bitmap) {
......@@ -332,7 +355,7 @@ public class EventDetailActivity extends AppCompatActivity {
LinearLayout linear = findViewById(R.id.register_details_list);
linear.removeAllViews();
ArrayList<String[]> infos = event().GetInfos(getResources());
ArrayList<String[]> infos = event.GetInfos(getResources());
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
for (int i = 0; i < infos.size(); i++) {
//Create a view from the xml and then add it as a child of the listview
......@@ -355,15 +378,13 @@ public class EventDetailActivity extends AppCompatActivity {
}
//Redirect to the login page first
if(!Settings.HasToken(getApplicationContext()) && !event().allow_email_signup){
if(!Settings.HasToken(getApplicationContext()) && !event.allow_email_signup){
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("cause", "register_event");
startActivityForResult(intent, 0);
return;
}
final int registerEventGroup = eventGroup; //declare final so it does not change
final int registerEventIndex = eventIndex;
String url = Settings.API_URL + "eventsignups";
StringRequest request = new StringRequest(com.android.volley.Request.Method.POST, url,null, null)
......@@ -376,7 +397,7 @@ public class EventDetailActivity extends AppCompatActivity {
try {
Log.e("request", new String(response.data));
JSONObject json = new JSONObject(new String(response.data));
event().AddSignup(json); //Register signup
event.AddSignup(json); //Register signup
//Fetch event signup object again for this event id
Request.FetchEventSignups(getApplicationContext(), new Request.OnDataReceivedCallback() {
......@@ -384,12 +405,12 @@ public class EventDetailActivity extends AppCompatActivity {
public void OnDataReceived() {
UpdateRegisterButton();
}
}, null, event()._id);
}, null, event._id);
//Interpret notification to show from the signup
int notification = 0;
if(event().accepted) {
if(event().confirmed)
if(event.accepted) {
if(event.confirmed)
notification = R.string.register_success;
else
notification = R.string.register_success_confirm_required;
......@@ -440,7 +461,7 @@ public class EventDetailActivity extends AppCompatActivity {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("event", event()._id);
params.put("event", event._id);
if(Settings.IsEmailOnlyLogin(getApplicationContext()))
params.put("email", UserInfo.current.email);
else
......@@ -467,19 +488,19 @@ public class EventDetailActivity extends AppCompatActivity {
*/
private void UpdateRegisterButton() {
if (event().IsSignedUp()) {
if (event.IsSignedUp()) {
registerButton.setEnabled(false);
registerButton.setText(R.string.already_registered);
return;
}
Date today = Calendar.getInstance().getTime();
if(event().time_register_start.before(today))
if(event.time_register_start.before(today))
{
if(event().time_register_end.after(today))
if(event.time_register_end.after(today))
{
registerButton.setEnabled(true);
if(Settings.IsEmailOnlyLogin(getApplicationContext()) && !event().allow_email_signup)
if(Settings.IsEmailOnlyLogin(getApplicationContext()) && !event.allow_email_signup)
registerButton.setText(R.string.requires_login);
else
registerButton.setText(R.string.register_title);
......
......@@ -2,6 +2,7 @@ package ch.amiv.android_app.events;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import org.json.JSONArray;
......@@ -19,17 +20,18 @@ import ch.amiv.android_app.util.PersistentStorage;
/**
* This is the central place for storing information about the events, events + signups.
* Attention! eventInfo and sortedEventInfos indexes may change, the events will allways be sorted according to the adDateComparator.
*/
public final class Events {
public static List<EventInfo> eventInfos = new ArrayList<EventInfo>(); //This is a list of ALL events as received from the api, we will not use this directly
public static List<List<EventInfo>> sortedEvents = new ArrayList<>(EventGroup.SIZE); //A list of lists which has been sorted according to the EventGroup configuration
public static List<List<EventInfo>> sortedEventInfos = new ArrayList<>(EventGroup.SIZE); //A list of lists which has been sorted according to the EventGroup configuration
public static boolean[] invertEventGroupSorting = new boolean[] {false, false, false, true}; //used to invert date sorting for the event groups
//Use this class to use the correct indexes for the event group for the sortedEvents list
//Use this class to use the correct indexes for the event group for the sortedEventInfos list
public static final class EventGroup {
public static final int SIZE = 4;
public static final int HIDDEN_EVENTS = 0;
public static final int ALL_EVENTS = 1;
public static final int CURRENT_EVENTS = 1;
public static final int CLOSED_EVENTS = 2;
public static final int PAST_EVENTS = 3;
}
......@@ -37,7 +39,15 @@ public final class Events {
//Defines for how many days after the ad start date the new tag is visible for
public static final int DAYS_NEW_TAG_ACTIVE = 3;
private static Comparator<EventInfo> adDateComparator = new Comparator<EventInfo>() {
@Override
public int compare(EventInfo a, EventInfo b) {
return b.time_advertising_start.compareTo(a.time_advertising_start);
}
};
/**
* This is the key function of this class. It converts a json for several events to java EventInfos
* Update the event infos with the data received from the api. This is just for updating information about the event NOT the signup
* @param json json array of the events.
*/
......@@ -69,54 +79,91 @@ public final class Events {
{
if(isInitialising){
for (int k = 0; k < EventGroup.SIZE; k++)
sortedEvents.add(new ArrayList<EventInfo>());
sortedEventInfos.add(new ArrayList<EventInfo>());
}
else {
for (int k = 0; k < sortedEvents.size(); k++)
sortedEvents.get(k).clear();
for (int k = 0; k < sortedEventInfos.size(); k++)
sortedEventInfos.get(k).clear();
}
//Sort list and update sorted list
if(!eventInfos.isEmpty()){
//sort so first elem has an advertising start date furthest in the future
Comparator<EventInfo> comparator;
comparator = new Comparator<EventInfo>() {
@Override
public int compare(EventInfo a, EventInfo b) {
return b.time_advertising_start.compareTo(a.time_advertising_start);
}
};
Collections.sort(eventInfos, comparator);
Collections.sort(eventInfos, adDateComparator);
Date today = Calendar.getInstance().getTime();
//fill in the sorted list according to the dates of the events
for (int i = 0; i < eventInfos.size(); i++){
if(eventInfos.get(i).time_advertising_start.after(today))
sortedEvents.get(EventGroup.HIDDEN_EVENTS).add(eventInfos.get(i));
else if(eventInfos.get(i).time_register_end.after(today))
sortedEvents.get(EventGroup.ALL_EVENTS).add(eventInfos.get(i));
else if(eventInfos.get(i).time_end.after(today))
sortedEvents.get(EventGroup.CLOSED_EVENTS).add(eventInfos.get(i));
else
sortedEvents.get(EventGroup.PAST_EVENTS).add(eventInfos.get(i));
AddEventToSorted(eventInfos.get(i), false);
}
}
}
/**
* Will add the event to the sorted array. Use AddEvent to add an event, this is just used to update the sortedEventInfos list accordingly
* @param sortAfterInsert Will resort the group after insertion, by date
*/
private static void AddEventToSorted(EventInfo eventInfo, boolean sortAfterInsert){
Date today = Calendar.getInstance().getTime();
int group = EventGroup.HIDDEN_EVENTS;
if(eventInfo.time_advertising_start.after(today)) //Determine which group the event is in, by date
group = EventGroup.HIDDEN_EVENTS;
else if(eventInfo.time_register_end.after(today))
group = EventGroup.CURRENT_EVENTS;
else if(eventInfo.time_end.after(today))
group = EventGroup.CLOSED_EVENTS;
else
group = EventGroup.PAST_EVENTS;
sortedEventInfos.get(group).add(eventInfo);
if(sortAfterInsert) {
Collections.sort(eventInfos, adDateComparator);
Collections.sort(sortedEventInfos.get(group), adDateComparator);
}
}
/**
* Will add a new event to the eventInfos and sortedEventInfos
* @return The UNsorted event index if the event does not exist, else -1 if it was found
*/
public static int AddEvent(JSONObject json){
EventInfo e = new EventInfo(json);
eventInfos.add(e);
AddEventToSorted(e, true);
return eventInfos.size() -1;
}
/**
* Will update a given event with the id
* @return true if the event was found and updated
*/
public static boolean UpdateSingleEvent(JSONObject json, @NonNull String eventId){
for (int i = 0; i < eventInfos.size(); i++){
if(eventInfos.get(i)._id.equalsIgnoreCase(eventId)) {
eventInfos.get(i).UpdateEvent(json);
return true;
}
EventInfo event = GetById(eventId);
if(event == null) return false;
event.UpdateEvent(json);
return true;
}
/**
* Note: Try to use the sorted/eventInfos lists to access by index. Only use this if you are having issues due to the indexes changing from the lists.
* @return The event with the corresponding _id. Returns null if the id was not found.
*/
public static EventInfo GetById(String id){
if(id == null || id.isEmpty()) return null;
for (EventInfo e : eventInfos) {
if(e._id.equalsIgnoreCase(id))
return e;
}
return false;
return null;
}
/**
......@@ -130,9 +177,9 @@ public final class Events {
try {
JSONObject signup = json.getJSONObject(i);
if(signup.has("event")) {
String event = signup.getString("event");
String eventId = signup.getString("event");
for (int j = 0; j < eventInfos.size(); j++) {
if (!hasUpdatedEvent[j] && event.equals(eventInfos.get(j)._id)) {
if (!hasUpdatedEvent[j] && eventId.equals(eventInfos.get(j)._id)) {
Events.eventInfos.get(j).AddSignup(signup);
hasUpdatedEvent[j] = true;
continue signupLoop;
......
......@@ -57,7 +57,7 @@ public class EventsListAdapter extends BaseRecyclerAdapter {
@Override
public void BuildDataset ()
{
if(Events.sortedEvents.size() == 0)
if(Events.sortedEventInfos.size() == 0)
return;
dataList.clear();
......@@ -71,12 +71,12 @@ public class EventsListAdapter extends BaseRecyclerAdapter {
//invert order on the specified groups
if(i >= Events.invertEventGroupSorting.length || !Events.invertEventGroupSorting[i]) {
for (int j = 0; j < Events.sortedEvents.get(i).size(); j++) {
for (int j = 0; j < Events.sortedEventInfos.get(i).size(); j++) {
dataList.add(new ListHelper.Pair(ViewType.EVENT, new int[]{i, j}));
}
}
else{
for (int j = Events.sortedEvents.get(i).size() -1; j >= 0; j--) {
for (int j = Events.sortedEventInfos.get(i).size() -1; j >= 0; j--) {
dataList.add(new ListHelper.Pair(ViewType.EVENT, new int[]{i, j}));
}
}
......@@ -150,7 +150,7 @@ public class EventsListAdapter extends BaseRecyclerAdapter {
int[] indexes = (int[])data;
final int eventGroup = indexes[0];
final int eventIndex = indexes[1];
final EventInfo e = Events.sortedEvents.get(eventGroup).get(eventIndex);
final EventInfo e = Events.sortedEventInfos.get(eventGroup).get(eventIndex);
final EventInfoHolder eventInfoHolder = (EventInfoHolder)holder;
eventInfoHolder.titleField.setText(e.GetTitle(activity.getResources()));
......@@ -160,7 +160,7 @@ public class EventsListAdapter extends BaseRecyclerAdapter {
Calendar cal = Calendar.getInstance();
cal.setTime(e.time_advertising_start);
cal.add(Calendar.DAY_OF_YEAR, Events.DAYS_NEW_TAG_ACTIVE);
if (eventGroup == Events.EventGroup.ALL_EVENTS && cal.getTime().after(Calendar.getInstance().getTime()))
if (eventGroup == Events.EventGroup.CURRENT_EVENTS && cal.getTime().after(Calendar.getInstance().getTime()))
eventInfoHolder.newTag.setVisibility(View.VISIBLE);
else
eventInfoHolder.newTag.setVisibility(View.GONE);
......
......@@ -32,6 +32,12 @@ import ch.amiv.android_app.util.Util;
* This mainly displays stored info about the job, eg description and also fetches more such as images. Also handles registering for and job and the possible outcomes
*/
public class JobDetailActivity extends AppCompatActivity {
public static final class LauncherExtras {
public static final String JOB_GROUP = "jobGroup";
public static final String JOB_INDEX = "jobIndex";
public static final String JOB_ID = "eventId";
public static final String LOAD_JOBS = "loadJobs";
}
private int jobGroup = 0;
private int jobIndex = 0;
......@@ -73,9 +79,9 @@ public class JobDetailActivity extends AppCompatActivity {
private void GetIntentData (){
if(jobGroup == 0 && jobIndex == 0) {
Intent intent = getIntent();
if(intent.hasExtra("jobGroup") && intent.hasExtra("jobIndex")) {
jobGroup = intent.getIntExtra("jobGroup", 0);
jobIndex = intent.getIntExtra("jobIndex", 0);
if(intent.hasExtra(JobDetailActivity.LauncherExtras.JOB_GROUP) && intent.hasExtra(LauncherExtras.JOB_INDEX)) {
jobGroup = intent.getIntExtra(JobDetailActivity.LauncherExtras.JOB_GROUP, 0);
jobIndex = intent.getIntExtra(LauncherExtras.JOB_INDEX, 0);
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment