Commit 1780c988 authored by Roger Barton's avatar Roger Barton
Browse files

Cleaner updating of events

parent 099f6dd4
Pipeline #4522 failed with stages
......@@ -12,17 +12,17 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:name=".core.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:name=".core.MyApplication"
android:theme="@style/AppThemeLight">
<activity
android:name=".core.SettingsActivity"
android:label=""
android:configChanges="layoutDirection|locale"
android:label=""
android:parentActivityName=".core.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
......@@ -39,7 +39,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".core.LoginActivity"
......@@ -58,60 +57,67 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="ch.amiv.android_app.core.MainActivity" />
</activity>
<activity android:name=".checkin.BarcodeIdActivity"
android:screenOrientation="portrait"
<activity
android:name=".checkin.BarcodeIdActivity"
android:configChanges="layoutDirection|locale"
android:parentActivityName=".core.MainActivity">
</activity>
android:parentActivityName=".core.MainActivity"
android:screenOrientation="portrait"></activity>
<!-- Checkin -->
<activity
android:name=".checkin.MainActivity"
android:label="@string/app_name_checkin"
android:screenOrientation="portrait"
android:theme="@style/CheckinTheme"></activity>
<activity
android:name=".checkin.ScanActivity"
android:label="@string/app_name_checkin"
android:theme="@style/CheckinTheme">
</activity>
<activity android:name=".checkin.ScanActivity"
android:screenOrientation="portrait"
android:theme="@style/CheckinTheme"
android:label="@string/app_name_checkin">
android:theme="@style/CheckinTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".checkin.MainActivity" />
</activity>
<activity android:name=".checkin.SettingsActivity"
android:screenOrientation="portrait"
<activity
android:name=".checkin.SettingsActivity"
android:label="@string/app_name_checkin"
android:screenOrientation="portrait"
android:theme="@style/CheckinTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".checkin.MainActivity" />
</activity>
<activity android:name=".checkin.MemberListActivity"
android:theme="@style/CheckinTheme"
android:label="@string/app_name_checkin">
<activity
android:name=".checkin.MemberListActivity"
android:label="@string/app_name_checkin"
android:theme="@style/CheckinTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/checkin_searchable"/>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/checkin_searchable" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".checkin.ScanActivity" />
</activity>
<activity android:name=".checkin.SearchMembersActivity"
android:theme="@style/CheckinTheme"
android:label="@string/app_name_checkin">
<activity
android:name=".checkin.SearchMembersActivity"
android:label="@string/app_name_checkin"
android:theme="@style/CheckinTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".checkin.MemberListActivity" />
</activity>
<!-- Demo -->
<activity android:name=".demo.MainActivity"
android:configChanges="layoutDirection|locale"
android:parentActivityName=".core.MainActivity"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
\ No newline at end of file
......@@ -13,8 +13,7 @@ import android.view.View;
import android.view.ViewGroup;
/**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat.
* This is used by the SettingsActivity
*/
public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
......
......@@ -376,6 +376,8 @@ public class EventDetailActivity extends AppCompatActivity {
if(volleyError.networkResponse.statusCode == 422) {
Snackbar.make(scrollView, R.string.already_registered, Snackbar.LENGTH_SHORT).show();
}
else if(volleyError.networkResponse.statusCode == 403)
Snackbar.make(scrollView, R.string.not_authorised, Snackbar.LENGTH_SHORT).show();
}
else
Log.e("request", "Request returned null response.");
......
package ch.amiv.android_app.core;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import ch.amiv.android_app.R;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link OnEventDetailFragmentListener} interface
* to handle interaction events.
* Use the {@link EventDetailFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class EventDetailFragment extends Fragment {
private static final String eventIndex_key = "eventIndex";
private int eventIndex;
private OnEventDetailFragmentListener mListener;
public EventDetailFragment() {}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param eventIndex_ The index in the EventInfos array of the event to display
* @return A new instance of fragment EventDetailFragment.
*/
public static EventDetailFragment newInstance(int eventIndex_) {
EventDetailFragment fragment = new EventDetailFragment();
Bundle args = new Bundle();
args.putInt(eventIndex_key, eventIndex_);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null)
eventIndex = getArguments().getInt(eventIndex_key);
else
eventIndex = 0;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView title = getView().findViewById(R.id.eventTitle);
TextView content = getView().findViewById(R.id.eventDetail);
final ImageView poster = getView().findViewById(R.id.eventPoster);
title.setText(Events.eventInfos.get(eventIndex).GetTitle(getResources()));
content.setText(Events.eventInfos.get(eventIndex).GetDescription(getResources()));
String posterUrl = "https://www.amiv.ethz.ch/sites/all/themes/amiv15/logo.png";
ImageRequest posterRequest = new ImageRequest(posterUrl,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
poster.setImageBitmap(bitmap);
}
}, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.RGB_565,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
poster.setImageResource(R.drawable.ic_error_white);
}
});
Requests.SendRequest(posterRequest, getContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.core_frag_event_detail, container, false);
}
//region Callbacks for Parent Activity
/**
* Call this when we have registered for the event in the fragment to notify the parent activity
*/
public void onRegisterEvent() {
if (mListener != null) {
mListener.onRegisteredForEvent(eventIndex);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
//Add the parent activity as a listener for our callback
if (context instanceof OnEventDetailFragmentListener)
mListener = (OnEventDetailFragmentListener) context;
else
throw new RuntimeException(context.toString() + " must implement OnEventDetailFragmentListener");
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnEventDetailFragmentListener {
// TODO: Update argument type and name
void onRegisteredForEvent(int eventIndex);
}
//endregion
}
......@@ -19,7 +19,7 @@ import java.util.List;
*/
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<List<EventInfo>>(4); //A list of lists which has been sorted according to the EventGroup configuration
public static List<List<EventInfo>> sortedEvents = new ArrayList<List<EventInfo>>(); //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
......@@ -39,18 +39,25 @@ public final class Events {
*/
public static void UpdateEventInfos(JSONArray json)
{
eventInfos.clear();
boolean isInitialising = eventInfos.size() == 0;
sortedEvents.clear();
for (int i = 0; i < json.length(); i++)
{
try {
eventInfos.add(new EventInfo(json.getJSONObject(i)));
//if we are not initialising, search for the event id and then update it, else add a new one to the list. This ensures we do not lose the signup data
JSONObject jsonEvent = json.getJSONObject(i);
EventInfo e = new EventInfo(jsonEvent);
if(e._id.isEmpty())
continue;
if(isInitialising || !UpdateSingleEvent(jsonEvent, e._id))
eventInfos.add(e);
} catch (JSONException e) {
e.printStackTrace();
}
}
//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;
......@@ -84,15 +91,19 @@ public final class Events {
}
/**
* Do not use this to create a single event, will update a given event with the id
* Will update a given event with the id
* @param json
* @param eventId
* @return true if the event was found and updated
*/
public static void UpdateSingleEvent(JSONObject json, @NonNull String eventId){
public static boolean UpdateSingleEvent(JSONObject json, @NonNull String eventId){
for (int i = 0; i < eventInfos.size(); i++){
if(eventInfos.get(i)._id.equalsIgnoreCase(eventId))
if(eventInfos.get(i)._id.equalsIgnoreCase(eventId)) {
eventInfos.get(i).UpdateEvent(json);
return true;
}
}
return false;
}
/**
......
......@@ -29,6 +29,9 @@ import java.util.Map;
import ch.amiv.android_app.R;
/**
* This handles logging into the api, getting a token with the provided username and password
*/
public class LoginActivity extends AppCompatActivity {
EditText userField;
......
......@@ -23,8 +23,12 @@ import android.widget.TextView;
import ch.amiv.android_app.R;
import ch.amiv.android_app.checkin.BarcodeIdActivity;
/**
* This is the first screen. features: drawer, pageview with bottom navigation bar and within each page a list view.
*/
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//region - ====Variables====
private NavigationView drawerNavigation;
private TextView drawer_title;
private TextView drawer_subtitle;
......@@ -71,6 +75,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
return false;
}
};
//endregion
//region Initialisation
@Override
......@@ -104,8 +109,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override
public void OnDataReceived() {
SetLoginUIDirty();
//Fetch the event list and then the signup information for the current user
//Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, null);
}
});
}
......@@ -223,6 +226,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
startActivity(intent);
}
/*private void StartDemoActivity() {
Intent intent = new Intent(this, ch.amiv.android_app.demo.MainActivity.class);
startActivity(intent);
}*/
/**
* Here we can interpret the result of the login/event detail activity, if the login was successful or not, then update accordingly
* @param requestCode
......@@ -298,8 +306,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
int id = item.getItemId();
if (id == R.id.nav_login) {
if (Settings.IsLoggedIn(getApplicationContext()))
if (Settings.IsLoggedIn(getApplicationContext())) {
LogoutUser();
return false;
}
else
StartLoginActivity();
}
......@@ -309,10 +319,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
StartBarcodeIdActivity();
else if (id == R.id.nav_settings)
StartSettingsActivity();
/*else if(id == R.id.nav_demo)
StartDemoActivity();*/
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
return false;
}
//endregion =====END OF DRAWER==================
......
package ch.amiv.android_app.demo;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import ch.amiv.android_app.R;
import ch.amiv.android_app.core.EventInfo;
import ch.amiv.android_app.core.Events;
import ch.amiv.android_app.core.Requests;
import ch.amiv.android_app.core.Settings;
import ch.amiv.android_app.core.UserInfo;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*UNCOMMENT WHEN READING
//To access details of the user use
String firstname = UserInfo.current.firstname;
//To access events use
EventInfo eventInfo = Events.sortedEvents.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.Requests class for examples. Basic structure is as below
//START of request
if(!Requests.CheckConnection(getApplicationContext())) {
//return;
}
//build url string
String url = Settings.API_URL + "my/url/extension";
//create our http request with the volley libary, we will override the methods to customise our request and set values
StringRequest request = new StringRequest(Request.Method.GET, url,null, null)
{
//====Adding data to be sent to the request====
@Override
public Map<String, String> getHeaders() throws AuthFailureError { //Here we add our data to the header, can also add the body in getParams, use Crl+O to see possible override functions
Map<String,String> headers = new HashMap<String, String>();
//This adds basic auth using our token if it exists, this can be retrieved from core.Settings
if(Settings.IsLoggedIn(getApplicationContext())) {
String credentials = Settings.GetToken(getApplicationContext()) + ":";
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Authorization", auth);
}
//Add other headers
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError { //Add key value pairs to the body of the request
Map<String,String> body = new HashMap<String, String>();
//Add body key values here using body.put(key, value);
return body;
}
//====Handling of the response of the request
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) { //this is called if we have received a reponse from the server with a successful status code, eg 2xx, not 4xx
if(response != null) {
Log.e("request", "fetch demo data status Code: " + response.statusCode);
try {
final JSONObject json = new JSONObject(new String(response.data)); //get data as json so we can use getString, optString, getJSONArray etc
//
Runnable runnable = new Runnable() {
@Override
public void run() {
//use the data to update values here, this is executed on the main thread
}
};
Handler handler = new Handler();
handler.post(runnable);
Log.e("request", new JSONObject(new String(response.data)).toString());
} catch (JSONException e) {
//Add your error handling here, usually a callback. This happens when we have received a json or other data which was not as expected
e.printStackTrace();
}
}
else {
//Add your error handling here, usually a callback
Log.e("request", "Request returned null response: fetch demo data");
}
return super.parseNetworkResponse(response);
}
@Override
protected VolleyError parseNetworkError(final VolleyError volleyError) { //This is called when we receive an error, either locally or the server returns an error status code, 4xx or 5xx
//Always check that we have a non null network response, it is often null when there is no internet
if(volleyError != null && volleyError.networkResponse != null)
Log.e("request", "status code: " + volleyError.networkResponse.statusCode + "\n" + new String(volleyError.networkResponse.data));
else
Log.e("request", "Request returned null response: fetch demo data");
return super.parseNetworkError(volleyError);
}
};
//send the request and check if it failed, this will use a request queue and handle internet connection issues
boolean hasSent = Requests.SendRequest(request, getApplicationContext());
//See the Requests class for real examples, using Crl+B can be useful to jump to the definition, Crl+F for searching the whole project for a function
//END of request
*/
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:alpha="0.4" android:color="?android:attr/colorForeground" />
<item android:alpha="1" android:color="?android:attr/colorForeground" />
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical">
<ImageView
android:id="@+id/eventPoster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_upload"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/eventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="@style/Header" />