Commit d373acde authored by Toni Beier's avatar Toni Beier

Merge branch 'feature_#27_timetable_swipe-to-refresh-course-list' into 'develop'

Kursübersicht mit Swipe-Geste aktualisieren

Closes ASIST/openasist4android_tu_chemnitz#18 and #27

See merge request OpenASiST/openasist4android_core!19
parents c9b720eb 2f9381da
......@@ -25,7 +25,22 @@ public class ASiSTModuleManager {
public void register(AbstractAsistModule module){
modules.add(module);
}
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass, T defaultValue ) {
T resultModule = defaultValue;
for ( AbstractAsistModule registeredModule: this.modules ) {
if( moduleClass.isInstance( registeredModule ) ) {
defaultValue = (T)registeredModule;
break;
}
}
return resultModule;
}
public <T extends AbstractAsistModule> T getModule( Class<T> moduleClass) {
return this.getModule(moduleClass, null);
}
public List<AbstractAsistModule> getModules() {
return modules;
}
......
......@@ -75,6 +75,10 @@ public class EnvironmentManager {
setEnvironmentString(context, TimetableModule.PREF_KEY_SHORTCODE, code);
}
public boolean containsCourseShortCode(Context context) {
return containsEnvironmentKey(context, TimetableModule.PREF_KEY_SHORTCODE);
}
public static boolean updateOnStartUp(Context context){
return getEnvironmentBoolean(context, KEY_UPDATE_ON_STARTUP, true);
}
......@@ -83,6 +87,18 @@ public class EnvironmentManager {
setEnvironmentBoolean(context, KEY_UPDATE_ON_STARTUP, value);
}
/**
* Return if a environment variable key exists
*
* @param context The context
* @param key The key that is search
* @return Is the key available
*/
public static boolean containsEnvironmentKey(final Context context, final String key) {
final SharedPreferences environment = context.getSharedPreferences(SHAREDPREFERENCE_ENVIRONMENT, Context.MODE_PRIVATE);
return environment.contains(key);
}
/**
* Speichert einen String in den Umgebungsvariablen 'key'
* @param context
......
......@@ -12,5 +12,5 @@ public interface CourseParseCallback {
public void onSuccess();
public void onManagedError(CourseList parsed);
public void onCoursesParsed(CourseList parsedCourses);
}
......@@ -42,9 +42,9 @@ class TimetableCallback implements ParserCallback<CourseList> {
}
@Override
public void onManagedError(CourseList parsed) {
public void onManagedError(CourseList parsedCourses) {
if (pc != null) {
pc.onManagedError(parsed);
pc.onCoursesParsed(parsedCourses);
} else if (pg != null) {
pg.dismiss();
}
......
......@@ -8,7 +8,11 @@ import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
......@@ -19,6 +23,7 @@ import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
import java.util.List;
......@@ -30,6 +35,7 @@ import de.bps.asist.gui.AbstractASiSTFragment;
import de.bps.asist.gui.code.GenericAddActivity;
import de.bps.asist.gui.dialog.ASiSTDialog;
import de.bps.asist.module.timetable.model.Course;
import de.bps.asist.module.timetable.model.CourseList;
import de.bps.asist.module.timetable.model.SelectionTreeNode;
/**
......@@ -83,6 +89,64 @@ public class TimetableFragment extends AbstractASiSTFragment {
DatabaseManager<SelectionTreeNode> selectionDB = new DatabaseManager<>(getActivity(), SelectionTreeNode.class);
hasSelectionTree = selectionDB.hasItems();
final SwipeRefreshLayout refreshTimeTableSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_timetable_swipe_layout);
refreshTimeTableSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
if (EnvironmentManager.getInstance().containsCourseShortCode(getContext())) {
TimetableModule.updateByShortCode(getContext(), EnvironmentManager.getInstance().getCourseShortCode(getContext()), new CourseParseCallback() {
@Override
public void onError(Exception e) {
Log.d("timetable_update", "Error while Downloading/Parsing timetable", e);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getContext(), R.string.module_timetable_course_update_failure, Toast.LENGTH_LONG).show();
}
});
refreshTimeTableSwipeLayout.setRefreshing(false);
}
@Override
public void onSuccess() {
refreshTimeTableSwipeLayout.setRefreshing(false);
}
@Override
public void onCoursesParsed(final CourseList parsedCourses) {
// Refresh UI in onCoursesParsed Method, because in Success run after Database update.
// So UI and Database are updating "parallel".
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drawTable(parsedCourses.getCourse());
}
});
refreshTimeTableSwipeLayout.setRefreshing(false);
}
}, null);
} else {
TextView toastContent = new TextView(getContext());
toastContent.setText(R.string.module_timetable_course_code_not_found);
toastContent.setGravity(Gravity.CENTER);
Toast courseCodeNotFoundToast = new Toast(getContext());
courseCodeNotFoundToast.setView(toastContent);
courseCodeNotFoundToast.setDuration(Toast.LENGTH_LONG);
courseCodeNotFoundToast.show();
refreshTimeTableSwipeLayout.setRefreshing(false);
}
}
});
}
});
return view;
}
......
......@@ -67,15 +67,15 @@ public class UICallback implements CourseParseCallback {
}
@Override
public void onManagedError(CourseList result){
if(result.getCode() == null){
public void onCoursesParsed(CourseList parsedCourses){
if(parsedCourses.getCode() == null){
return;
}
FragmentManager fm = context.getSupportFragmentManager();
String errorReason = result.getMessage();
String code = "Code:" + result.getCode();
String uri = "Uri:" + result.getUri();
String status = "Status:" + result.getStatus();
String errorReason = parsedCourses.getMessage();
String code = "Code:" + parsedCourses.getCode();
String uri = "Uri:" + parsedCourses.getUri();
String status = "Status:" + parsedCourses.getStatus();
ASiSTDialog dialog;
if(BuildConfig.DEBUG) {
dialog = new ASiSTDialog(translate(R.string.timetable_import_error_message), errorReason + "\n\n" + code + "\n\n" + uri + "\n\n" + status, true, false);
......
......@@ -46,10 +46,15 @@
/>
</LinearLayout>
<ListView
android:id="@+id/courses_overview"
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh_timetable_swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent">
<ListView
android:id="@+id/courses_overview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
\ No newline at end of file
......@@ -198,6 +198,8 @@
<string name="generic_list_showmore">Mehr anzeigen</string>
<!-- timetable -->
<string name="module_timetable_course_code_not_found" formatted="false">Aktualisierung konnte nicht durchgeführt werden.\nImportieren Sie zuerst Ihren Stundenplan.</string>
<string name="module_timetable_course_update_failure" formatted="false">Während der Aktualisierung des Stundplanes ist ein Fehler aufgetreten</string>
<!-- Feedback Module -->
<string name="module_feedback_name">Vorlesungs-Feedback</string>
......
Markdown is supported
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