Implemented Canteen Plan
This commit is contained in:
@@ -20,10 +20,12 @@ import android.widget.TextView;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import de.sebse.fuplanner.fragments.CanteensFragment;
|
||||
import de.sebse.fuplanner.fragments.LoginFragment;
|
||||
import de.sebse.fuplanner.fragments.ModulesFragment;
|
||||
import de.sebse.fuplanner.fragments.ScheduleFragment;
|
||||
import de.sebse.fuplanner.fragments.StartupFragment;
|
||||
import de.sebse.fuplanner.fragments.canteen.DaySwitcherFragment;
|
||||
import de.sebse.fuplanner.fragments.moddetails.ModDetailFragment;
|
||||
import de.sebse.fuplanner.services.Canteen.CanteenBrowser;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
@@ -38,7 +40,8 @@ public class MainActivity extends AppCompatActivity
|
||||
implements MainAcitivityListener,
|
||||
NavigationView.OnNavigationItemSelectedListener,
|
||||
LoginFragment.OnLoginFragmentInteractionListener,
|
||||
ModulesFragment.OnModulesFragmentInteractionListener {
|
||||
ModulesFragment.OnModulesFragmentInteractionListener,
|
||||
CanteensFragment.OnCanteensFragmentInteractionListener {
|
||||
|
||||
private static final int FRAGMENT_NONE = -1;
|
||||
private static final int FRAGMENT_STARTUP = 0;
|
||||
@@ -46,7 +49,8 @@ public class MainActivity extends AppCompatActivity
|
||||
private static final int FRAGMENT_MODULES_DETAILS = 2;
|
||||
private static final int FRAGMENT_LOGIN = 3;
|
||||
private static final int FRAGMENT_SCHEDULE = 4;
|
||||
private static final int FRAGMENT_CANTEEN = 5;
|
||||
private static final int FRAGMENT_CANTEENS = 5;
|
||||
private static final int FRAGMENT_CANTEENS_DETAILS = 6;
|
||||
|
||||
private static final String ARG_FRAGMENT_PAGE = "fragment_page";
|
||||
private static final String ARG_FRAGMENT_STATUS = "fragment_status";
|
||||
@@ -165,6 +169,9 @@ public class MainActivity extends AppCompatActivity
|
||||
case R.id.nav_schedule:
|
||||
changeFragment(FRAGMENT_SCHEDULE);
|
||||
break;
|
||||
case R.id.nav_dining:
|
||||
changeFragment(FRAGMENT_CANTEENS);
|
||||
break;
|
||||
case R.id.nav_share:
|
||||
Intent sendIntent = new Intent();
|
||||
sendIntent.setAction(Intent.ACTION_SEND);
|
||||
@@ -188,7 +195,6 @@ public class MainActivity extends AppCompatActivity
|
||||
|
||||
int size = mNavigationView.getMenu().size();
|
||||
for (int k = 0; k < size; k++) {
|
||||
//mNavigationView.getMenu().getItem(k).setChecked(mNavigationView.getMenu().getItem(k).getOrder() == 101+itemPosition);
|
||||
mNavigationView.getMenu().getItem(k).setChecked(mNavigationView.getMenu().getItem(k) == item);
|
||||
}
|
||||
item.setChecked(true);
|
||||
@@ -291,6 +297,10 @@ public class MainActivity extends AppCompatActivity
|
||||
fragment = LoginFragment.newInstance();
|
||||
} else if (newFragment == FRAGMENT_SCHEDULE) {
|
||||
fragment = ScheduleFragment.newInstance();
|
||||
} else if (newFragment == FRAGMENT_CANTEENS) {
|
||||
fragment = CanteensFragment.newInstance();
|
||||
} else if (newFragment == FRAGMENT_CANTEENS_DETAILS) {
|
||||
fragment = DaySwitcherFragment.newInstance(Integer.parseInt(newData));
|
||||
} else { // FRAGMENT_STARTUP
|
||||
fragment = StartupFragment.newInstance();
|
||||
}
|
||||
@@ -354,6 +364,17 @@ public class MainActivity extends AppCompatActivity
|
||||
}
|
||||
}, log::e);
|
||||
}
|
||||
if (newFragment == FRAGMENT_CANTEENS_DETAILS) {
|
||||
getCanteenBrowser().getCanteens(success -> {
|
||||
int size = mNavigationView.getMenu().size();
|
||||
Canteen canteen = success.getCanteen(Integer.parseInt(newData));
|
||||
//noinspection ConstantConditions
|
||||
String title = canteen == null ? null : canteen.getName();
|
||||
for (int k = 0; k < size; k++) {
|
||||
mNavigationView.getMenu().getItem(k).setChecked(mNavigationView.getMenu().getItem(k).getTitle().equals(title));
|
||||
}
|
||||
}, log::e);
|
||||
}
|
||||
|
||||
this.fragmentPage = newFragment;
|
||||
this.fragmentData = newData;
|
||||
@@ -382,9 +403,10 @@ public class MainActivity extends AppCompatActivity
|
||||
private void afterAnyMenuInflate() {
|
||||
getCanteenBrowser().getCanteens(success -> {
|
||||
int i = 0;
|
||||
for (Canteen module : success) {
|
||||
MenuItem menuItem = mNavigationView.getMenu().add(Menu.NONE, Menu.NONE, 201 + i, module.getName());
|
||||
for (Canteen canteen: success) {
|
||||
MenuItem menuItem = mNavigationView.getMenu().add(Menu.NONE, Menu.NONE, 201 + i, canteen.getName());
|
||||
menuItem.setOnMenuItemClickListener(item -> {
|
||||
onCanteensFragmentInteraction(canteen.getId());
|
||||
return false;
|
||||
});
|
||||
i++;
|
||||
@@ -406,6 +428,10 @@ public class MainActivity extends AppCompatActivity
|
||||
changeFragment(FRAGMENT_MODULES_DETAILS, itemID);
|
||||
}
|
||||
|
||||
public void onCanteensFragmentInteraction(final int itemID) {
|
||||
changeFragment(FRAGMENT_CANTEENS_DETAILS, String.valueOf(itemID));
|
||||
}
|
||||
|
||||
public void onTitleTextChange(String newTitle) {
|
||||
setTitle(newTitle);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package de.sebse.fuplanner.fragments;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.fragments.CanteensFragment.OnCanteensFragmentInteractionListener;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteens;
|
||||
import de.sebse.fuplanner.services.KVV.types.Modules;
|
||||
import de.sebse.fuplanner.tools.ui.ItemViewHolder;
|
||||
|
||||
/**
|
||||
* {@link RecyclerView.Adapter} that can display a {@link Modules.Module} and makes a call to the
|
||||
* specified {@link OnCanteensFragmentInteractionListener}.
|
||||
*/
|
||||
public class CanteensAdapter extends RecyclerView.Adapter<ItemViewHolder> {
|
||||
|
||||
private Canteens mValues;
|
||||
private final OnCanteensFragmentInteractionListener mListener;
|
||||
|
||||
CanteensAdapter(OnCanteensFragmentInteractionListener listener) {
|
||||
mValues = null;
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
public void setCanteens(Canteens canteens) {
|
||||
mValues = canteens;
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.list_all_items, parent, false);
|
||||
return new ItemViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
|
||||
if (mValues == null)
|
||||
return;
|
||||
Canteen canteen = mValues.get(holder.getAdapterPosition());
|
||||
holder.mTitle.setText(canteen.getName());
|
||||
holder.mSubLeft.setText(canteen.getAddress());
|
||||
holder.mSubRight.setText(canteen.getCity());
|
||||
|
||||
holder.mView.setOnClickListener(v -> {
|
||||
if (null != mListener) {
|
||||
// Notify the active callbacks interface (the activity, if the
|
||||
// fragment is attached to one) that an item has been selected.
|
||||
mListener.onCanteensFragmentInteraction(canteen.getId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
if (mValues != null) {
|
||||
return mValues.size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package de.sebse.fuplanner.fragments;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import de.sebse.fuplanner.MainActivity;
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.services.Canteen.CanteenBrowser;
|
||||
import de.sebse.fuplanner.tools.MainAcitivityListener;
|
||||
import de.sebse.fuplanner.tools.logging.Logger;
|
||||
|
||||
/**
|
||||
* A fragment representing a list of Items.
|
||||
* <p/>
|
||||
* Activities containing this fragment MUST implement the {@link OnCanteensFragmentInteractionListener}
|
||||
* interface.
|
||||
*/
|
||||
public class CanteensFragment extends Fragment {
|
||||
private OnCanteensFragmentInteractionListener mListener;
|
||||
private Logger log = new Logger(this);
|
||||
private CanteensAdapter adapter;
|
||||
private SwipeRefreshLayout swipeLayout;
|
||||
|
||||
/**
|
||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||
* fragment (e.g. upon screen orientation changes).
|
||||
*/
|
||||
public CanteensFragment() {
|
||||
}
|
||||
|
||||
public static CanteensFragment newInstance() {
|
||||
CanteensFragment fragment = new CanteensFragment();
|
||||
Bundle args = new Bundle();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
View view = inflater.inflate(R.layout.fragment_modules_list, container, false);
|
||||
// Set the adapter
|
||||
Context context = view.getContext();
|
||||
RecyclerView recyclerView = view.findViewById(R.id.list);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(context));
|
||||
adapter = new CanteensAdapter(mListener);
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
// Getting SwipeContainerLayout
|
||||
swipeLayout = view.findViewById(R.id.swipe_container);
|
||||
// Adding Listener
|
||||
swipeLayout.setOnRefreshListener(() -> refresh(true));
|
||||
refresh(false);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void refresh(boolean forceRefresh) {
|
||||
if (getActivity() != null) {
|
||||
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
|
||||
browser.getCanteens(success -> {
|
||||
adapter.setCanteens(success);
|
||||
swipeLayout.setRefreshing(false);
|
||||
}, error -> {
|
||||
log.e(error.toString());
|
||||
swipeLayout.setRefreshing(false);
|
||||
}, forceRefresh);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
if (context instanceof OnCanteensFragmentInteractionListener) {
|
||||
mListener = (OnCanteensFragmentInteractionListener) context;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString()
|
||||
+ " must implement OnCanteensFragmentInteractionListener");
|
||||
}
|
||||
if (context instanceof MainAcitivityListener)
|
||||
((MainAcitivityListener) context).onTitleTextChange(R.string.canteens);
|
||||
else
|
||||
throw new RuntimeException(context.toString() + "must implement MainActivityListener");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetach() {
|
||||
super.onDetach();
|
||||
mListener = null;
|
||||
}
|
||||
|
||||
public interface OnCanteensFragmentInteractionListener {
|
||||
void onCanteensFragmentInteraction(int id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package de.sebse.fuplanner.fragments.canteen;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
import de.sebse.fuplanner.tools.DateUtils;
|
||||
import de.sebse.fuplanner.tools.logging.Logger;
|
||||
|
||||
class DaySwitcherAdapter extends FragmentStatePagerAdapter {
|
||||
private Canteen mCanteen = null;
|
||||
|
||||
DaySwitcherAdapter(FragmentManager fm) {
|
||||
super(fm);
|
||||
}
|
||||
|
||||
public void setModule(Canteen canteen) {
|
||||
mCanteen = canteen;
|
||||
this.setModule();
|
||||
}
|
||||
|
||||
public void setModule() {
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
|
||||
// Returns total number of pages
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mCanteen == null ? 0 : mCanteen.size();
|
||||
}
|
||||
|
||||
// Returns the fragment to display for that page
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
return MealFragment.newInstance(mCanteen.getId(), position);
|
||||
}
|
||||
|
||||
// Returns the page title for the top indicator
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return DateUtils.getModifiedDate(mCanteen.get(position).getCalendar().getTimeInMillis());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package de.sebse.fuplanner.fragments.canteen;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import de.sebse.fuplanner.MainActivity;
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.services.Canteen.CanteenBrowser;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
import de.sebse.fuplanner.tools.MainAcitivityListener;
|
||||
import de.sebse.fuplanner.tools.logging.Logger;
|
||||
import de.sebse.fuplanner.tools.network.NetworkCallback;
|
||||
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
* Activities that contain this fragment must implement the
|
||||
* {@link MainAcitivityListener} interface
|
||||
* to handle interaction events.
|
||||
* Use the {@link DaySwitcherFragment#newInstance} factory method to
|
||||
* create an instance of this fragment.
|
||||
*/
|
||||
public class DaySwitcherFragment extends Fragment implements DaySwitcherListener {
|
||||
private static final String ARG_POSITION = "canteenId";
|
||||
|
||||
// Parameters
|
||||
private int mCanteenId;
|
||||
|
||||
private MainAcitivityListener mListener;
|
||||
private Logger log = new Logger(this);
|
||||
private ViewPager mViewPager;
|
||||
private DaySwitcherAdapter adapterViewPager;
|
||||
|
||||
public DaySwitcherFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this factory method to create a new instance of
|
||||
* this fragment using the provided parameters.
|
||||
*
|
||||
* @param canteenId Canteen id in canteens list.
|
||||
* @return A new instance of fragment DaySwitcherFragment.
|
||||
*/
|
||||
public static Fragment newInstance(int canteenId) {
|
||||
DaySwitcherFragment fragment = new DaySwitcherFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(ARG_POSITION, canteenId);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (getArguments() != null) {
|
||||
mCanteenId = getArguments().getInt(ARG_POSITION);
|
||||
}
|
||||
if (mListener != null) {
|
||||
mListener.onTitleTextChange(R.string.canteens);
|
||||
mListener.getCanteenBrowser().getCanteens(success -> {
|
||||
Canteen canteen = success.getCanteen(mCanteenId);
|
||||
if (mListener != null && canteen != null)
|
||||
mListener.onTitleTextChange(canteen.getName());
|
||||
}, log::e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View v = inflater.inflate(R.layout.fragment_mod_detail, container, false);
|
||||
|
||||
mViewPager = v.findViewById(R.id.vpPager);
|
||||
adapterViewPager = new DaySwitcherAdapter(getChildFragmentManager());
|
||||
mViewPager.setAdapter(adapterViewPager);
|
||||
|
||||
refresh(false);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
if (context instanceof MainAcitivityListener) {
|
||||
mListener = (MainAcitivityListener) context;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString()
|
||||
+ " must implement MainActivityListener");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDetach() {
|
||||
super.onDetach();
|
||||
mListener = null;
|
||||
}
|
||||
|
||||
private void refresh(boolean forceRefresh) {
|
||||
refresh(forceRefresh, null, null);
|
||||
}
|
||||
|
||||
private void refresh(boolean forceRefresh, NetworkCallback<Canteen> callback, NetworkErrorCallback errorCallback) {
|
||||
if (getActivity() != null) {
|
||||
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
|
||||
browser.getCanteens(canteens -> {
|
||||
Canteen canteen = canteens.getCanteen(mCanteenId);
|
||||
adapterViewPager.setModule(canteen);
|
||||
browser.getCanteen(canteen, success -> {
|
||||
adapterViewPager.setModule();
|
||||
if (callback != null)
|
||||
callback.onResponse(success);
|
||||
}, error -> {
|
||||
log.e(error);
|
||||
if (errorCallback != null)
|
||||
errorCallback.onError(error);
|
||||
}, forceRefresh);
|
||||
}, error -> {
|
||||
log.e(error);
|
||||
if (errorCallback != null)
|
||||
errorCallback.onError(error);
|
||||
}, forceRefresh);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChildRefresh(NetworkCallback<Canteen> callback, NetworkErrorCallback errorCallback) {
|
||||
refresh(true, callback, errorCallback);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package de.sebse.fuplanner.fragments.canteen;
|
||||
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
import de.sebse.fuplanner.tools.network.NetworkCallback;
|
||||
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
||||
|
||||
public interface DaySwitcherListener {
|
||||
void onChildRefresh(NetworkCallback<Canteen> callback, NetworkErrorCallback errorCallback);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package de.sebse.fuplanner.fragments.canteen;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseExpandableListAdapter;
|
||||
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Day;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Meal;
|
||||
import de.sebse.fuplanner.services.KVV.types.Assignment;
|
||||
import de.sebse.fuplanner.services.KVV.types.Modules;
|
||||
import de.sebse.fuplanner.tools.DateUtils;
|
||||
import de.sebse.fuplanner.tools.ui.ItemViewHolder;
|
||||
import de.sebse.fuplanner.tools.ui.StringViewHolder;
|
||||
|
||||
public class MealAdapter extends BaseExpandableListAdapter {
|
||||
|
||||
private Day mDay = null;
|
||||
|
||||
@Override
|
||||
public String getChild(int groupPosition, int childPosititon) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\n\n");
|
||||
for (String s : this.getGroup(groupPosition).getNotes())
|
||||
{
|
||||
sb.append(s);
|
||||
sb.append("\n\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getChildId(int groupPosition, int childPosition) {
|
||||
return childPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getChildView(int groupPosition, final int childPosition,
|
||||
boolean isLastChild, View convertView, ViewGroup parent) {
|
||||
|
||||
final String childText = getChild(groupPosition, childPosition);
|
||||
|
||||
if (convertView == null) {
|
||||
convertView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.list_all_string, parent, false);
|
||||
}
|
||||
|
||||
StringViewHolder itemHolder = new StringViewHolder(convertView);
|
||||
itemHolder.mString.setText(childText);
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildrenCount(int groupPosition) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Meal getGroup(int groupPosition) {
|
||||
if (this.mDay != null)
|
||||
return this.mDay.get(groupPosition);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGroupCount() {
|
||||
if (this.mDay != null)
|
||||
return this.mDay.size();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getGroupId(int groupPosition) {
|
||||
return groupPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getGroupView(int groupPosition, boolean isExpanded,
|
||||
View convertView, ViewGroup parent) {
|
||||
Meal meal = getGroup(groupPosition);
|
||||
if (convertView == null) {
|
||||
convertView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.list_all_items, parent, false);
|
||||
}
|
||||
|
||||
ItemViewHolder itemHolder = new ItemViewHolder(convertView);
|
||||
itemHolder.mTitle.setText(meal.getName());
|
||||
itemHolder.mSubLeft.setText(meal.getCategory());
|
||||
itemHolder.mSubRight.setText(meal.getPriceStdnt()+"/"+meal.getPriceEmply()+"/"+meal.getPriceOther());
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasStableIds() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChildSelectable(int groupPosition, int childPosition) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setDay(Day day) {
|
||||
this.mDay = day;
|
||||
this.setDay();
|
||||
}
|
||||
|
||||
public void setDay() {
|
||||
this.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package de.sebse.fuplanner.fragments.canteen;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ExpandableListView;
|
||||
|
||||
import de.sebse.fuplanner.MainActivity;
|
||||
import de.sebse.fuplanner.R;
|
||||
import de.sebse.fuplanner.services.Canteen.CanteenBrowser;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Canteen;
|
||||
import de.sebse.fuplanner.services.Canteen.types.Day;
|
||||
import de.sebse.fuplanner.tools.logging.Logger;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
* Use the {@link MealFragment#newInstance} factory method to
|
||||
* create an instance of this fragment.
|
||||
*/
|
||||
public class MealFragment extends Fragment {
|
||||
private static final String ARG_POSITION = "canteenId";
|
||||
private static final String ARG_DAY_ID = "dayPosition";
|
||||
|
||||
private int mCanteenId;
|
||||
private int mDayPosition;
|
||||
private Logger log = new Logger(this);
|
||||
private MealAdapter adapter;
|
||||
private SwipeRefreshLayout swipeLayout;
|
||||
private DaySwitcherListener mListener;
|
||||
|
||||
|
||||
public MealFragment() {
|
||||
// Required empty public constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this factory method to create a new instance of
|
||||
* this fragment using the provided parameters.
|
||||
*
|
||||
* @param canteenId Item position in module list.
|
||||
* @param dayPosition Item position in module list.
|
||||
* @return A new instance of fragment ModDetailAnnounceFragment.
|
||||
*/
|
||||
public static MealFragment newInstance(int canteenId, int dayPosition) {
|
||||
MealFragment fragment = new MealFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(ARG_POSITION, canteenId);
|
||||
args.putInt(ARG_DAY_ID, dayPosition);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (getArguments() != null) {
|
||||
mCanteenId = getArguments().getInt(ARG_POSITION);
|
||||
mDayPosition = getArguments().getInt(ARG_DAY_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.fragment_expandable_list_view, container, false);
|
||||
// Set the adapter
|
||||
ExpandableListView expandableListView = view.findViewById(R.id.list);
|
||||
adapter = new MealAdapter();
|
||||
expandableListView.setAdapter(adapter);
|
||||
|
||||
// Getting SwipeContainerLayout
|
||||
swipeLayout = view.findViewById(R.id.swipe_container);
|
||||
// Adding Listener
|
||||
swipeLayout.setOnRefreshListener(() -> {
|
||||
mListener.onChildRefresh(callback -> refresh(true), error -> refresh(true));
|
||||
});
|
||||
refresh(false);
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
Fragment parentFragment = getParentFragment();
|
||||
if (parentFragment != null) {
|
||||
if (parentFragment instanceof DaySwitcherListener) {
|
||||
mListener = (DaySwitcherListener) parentFragment;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString()
|
||||
+ " must implement DaySwitcherListener");
|
||||
}
|
||||
} else log.w("No parent fragment!");
|
||||
}
|
||||
|
||||
private void refresh(boolean forceRefresh) {
|
||||
if (getActivity() != null) {
|
||||
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
|
||||
browser.getCanteens(canteens -> {
|
||||
Canteen canteen = canteens.getCanteen(mCanteenId);
|
||||
browser.getCanteen(canteen, success -> {
|
||||
Day day = success.get(mDayPosition);
|
||||
adapter.setDay(day);
|
||||
browser.getDay(day, success1 -> {
|
||||
adapter.setDay();
|
||||
swipeLayout.setRefreshing(false);
|
||||
}, error -> {
|
||||
swipeLayout.setRefreshing(false);
|
||||
log.e(error);
|
||||
}, forceRefresh);
|
||||
}, error -> {
|
||||
swipeLayout.setRefreshing(false);
|
||||
log.e(error);
|
||||
}, forceRefresh);
|
||||
}, error -> {
|
||||
swipeLayout.setRefreshing(false);
|
||||
log.e(error);
|
||||
}, forceRefresh);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public class CanteenBrowser extends HTTPService {
|
||||
return;
|
||||
}
|
||||
this.upgradeCanteens(success -> {
|
||||
log.d("updated canteen list");
|
||||
if (this.canteens == null)
|
||||
this.canteens = success;
|
||||
else
|
||||
@@ -110,6 +111,7 @@ public class CanteenBrowser extends HTTPService {
|
||||
return;
|
||||
}
|
||||
this.upgradeCanteen(canteen, success -> {
|
||||
log.d("updated canteen");
|
||||
canteen.update(success);
|
||||
this.save();
|
||||
callback.onResponse(canteen);
|
||||
@@ -157,6 +159,7 @@ public class CanteenBrowser extends HTTPService {
|
||||
return;
|
||||
}
|
||||
this.upgradeDay(day, success -> {
|
||||
log.d("updated day");
|
||||
day.update(success);
|
||||
this.save();
|
||||
callback.onResponse(day);
|
||||
|
||||
@@ -34,7 +34,8 @@ public class Canteen implements Serializable, Iterable<Day> {
|
||||
addDay(day);
|
||||
}
|
||||
|
||||
private void addDay(@NonNull Day day) {
|
||||
void addDay(@NonNull Day day) {
|
||||
if (this.list.contains(day)) return;
|
||||
if (!day.isClosed()) {
|
||||
this.list.add(day);
|
||||
}
|
||||
@@ -58,12 +59,8 @@ public class Canteen implements Serializable, Iterable<Day> {
|
||||
}
|
||||
|
||||
public void update(Canteen canteen) {
|
||||
SortedListDay oldList = this.list;
|
||||
// remove loaded meals when updating days of a canteen
|
||||
this.list = canteen.list;
|
||||
for (Day oldDay: oldList) {
|
||||
Day newDay = getDay(oldDay.getCalendar());
|
||||
if (newDay != null) newDay.update(oldDay);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Canteens implements Serializable, Iterable<Canteen> {
|
||||
private static final String FILE_NAME = "CanteensSaving";
|
||||
private SortedListCanteen list = new SortedListCanteen();
|
||||
|
||||
private Canteen getCanteen(int id) {
|
||||
public Canteen getCanteen(int id) {
|
||||
return this.list.getById(id);
|
||||
}
|
||||
|
||||
@@ -26,10 +26,11 @@ public class Canteens implements Serializable, Iterable<Canteen> {
|
||||
}
|
||||
|
||||
private void addCanteen(Canteen canteen) {
|
||||
if (this.list.contains(canteen)) return;
|
||||
this.list.add(canteen);
|
||||
}
|
||||
|
||||
private int size() {
|
||||
public int size() {
|
||||
return this.list.size();
|
||||
}
|
||||
|
||||
@@ -42,7 +43,11 @@ public class Canteens implements Serializable, Iterable<Canteen> {
|
||||
this.list = canteens.list;
|
||||
for (Canteen oldCanteen: oldList) {
|
||||
Canteen newCanteen = getCanteen(oldCanteen.getId());
|
||||
if (newCanteen != null) newCanteen.update(oldCanteen);
|
||||
if (newCanteen != null) {
|
||||
for (Day day: oldCanteen) {
|
||||
newCanteen.addDay(day);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ public class Day implements Serializable, Iterable<Meal> {
|
||||
|
||||
public void addMeal(int id, String name, String category, double priceStdnt, double priceEmply, double priceOther, String[] notes) {
|
||||
Meal meal = new Meal(id, name, category, priceStdnt, priceEmply, priceOther, notes);
|
||||
if (this.list.contains(meal)) return;
|
||||
this.list.add(meal);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package de.sebse.fuplanner.tools;
|
||||
|
||||
import android.support.annotation.StringRes;
|
||||
|
||||
import de.sebse.fuplanner.fragments.canteen.DaySwitcherFragment;
|
||||
import de.sebse.fuplanner.services.Canteen.CanteenBrowser;
|
||||
import de.sebse.fuplanner.services.GoogleAuth.GoogleAuth;
|
||||
import de.sebse.fuplanner.services.KVV.KVV;
|
||||
|
||||
@@ -17,4 +19,6 @@ public interface MainAcitivityListener {
|
||||
void loginTokenInvalid(boolean doPrecheck);
|
||||
|
||||
void refreshFailed(boolean isFailed);
|
||||
|
||||
CanteenBrowser getCanteenBrowser();
|
||||
}
|
||||
|
||||
@@ -45,6 +45,14 @@ public abstract class SortedList<T, I, F> implements Iterable<T>, Serializable {
|
||||
return internalList.size();
|
||||
}
|
||||
|
||||
public boolean contains(T o1) {
|
||||
for (T o2 : this.internalList) {
|
||||
if (compare(o1, o2) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
|
||||
@@ -8,12 +8,10 @@ android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/list"
|
||||
android:name="de.sebse.fuplanner.fragments.ModulesFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
|
||||
tools:context=".fragments.moddetails.ModDetailOverviewFragment"
|
||||
tools:listitem="@layout/list_all_caption" />
|
||||
</android.support.v4.widget.SwipeRefreshLayout>
|
||||
@@ -44,4 +44,5 @@
|
||||
<string name="app_url" translatable="false">https://play.google.com/store/apps/details?id=de.sebse.fuplanner</string>
|
||||
<string name="refresh">Refresh</string>
|
||||
<string name="go_to_today">Go to today</string>
|
||||
<string name="canteens">Canteens</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user