Commit e1bf0dad authored by Lennart Bader's avatar Lennart Bader
Browse files

Fixed duplicate FragmentBug, added Search possibility

parent 7033f00b
......@@ -31,4 +31,5 @@ dependencies {
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.github.wangjiegulu:RapidFloatingActionButton:1.0.3'
}
......@@ -13,7 +13,7 @@ import de.mytfg.apps.vplan.fragments.FragmentHolderLogic;
public class FragmentHolder extends Fragment {
int resId;
FragmentHolderLogic logic;
private FragmentHolderLogic logic;
public static FragmentHolder newInstance(int layout) {
return FragmentHolder.newInstance(layout, null);
......@@ -30,7 +30,6 @@ public class FragmentHolder extends Fragment {
fragmentHolder.setArguments(args);
fragmentHolder.logic = logic;
return fragmentHolder;
}
public FragmentHolder() {
......@@ -47,4 +46,8 @@ public class FragmentHolder extends Fragment {
}
return view;
}
public FragmentHolderLogic getLogic() {
return logic;
}
}
......@@ -9,16 +9,13 @@ import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitelList = new ArrayList<>();
private final List<FragmentHolder> fragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
public void addFragment(Fragment fragment, String title) {
public void addFragment(FragmentHolder fragment) {
fragmentList.add(fragment);
fragmentTitelList.add(title);
}
@Override
......@@ -33,6 +30,6 @@ public class ViewPagerAdapter extends FragmentStatePagerAdapter {
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitelList.get(position);
return fragmentList.get(position).getLogic().getTabTitle();
}
}
......@@ -37,6 +37,7 @@ import java.util.UUID;
import javax.net.ssl.HttpsURLConnection;
import de.mytfg.apps.vplan.R;
import de.mytfg.apps.vplan.objects.User;
/**
* Wrapper for the MyTFG API. Handles Login tokens, authentication errors and result parsing.
......@@ -92,6 +93,12 @@ public class MyTFGApi {
}
public User getUser() {
User user = new User(context);
user.load(getUsername());
return user;
}
/**
* Generates the device ID passed to the API.
* @return The device ID.
......
......@@ -10,6 +10,8 @@ import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
......@@ -38,10 +40,11 @@ public class AboutFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_about, container, false);
MainActivity context = (MainActivity)this.getActivity();
setHasOptionsMenu(true);
context.getToolbarManager()
.clear()
.setTitle(getString(R.string.menutitle_about))
.setExpandable(true, true)
.setTabs(false);
.setExpandable(true, true);
ImageView github = (ImageView) view.findViewById(R.id.github);
github.setOnClickListener(new View.OnClickListener() {
......@@ -56,8 +59,10 @@ public class AboutFragment extends Fragment {
return view;
}
@Override
public void onResume() {
super.onResume();
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
}
......@@ -9,6 +9,8 @@ import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
......@@ -35,10 +37,11 @@ public class AccountFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_account, container, false);
final MainActivity context = (MainActivity)this.getActivity();
setHasOptionsMenu(true);
context.getToolbarManager()
.clear()
.setTitle(getString(R.string.menutitle_account))
.setExpandable(true, true)
.setTabs(false);
.setExpandable(true, true);
MyTFGApi api = new MyTFGApi(context);
String username = api.getUsername();
......@@ -76,6 +79,12 @@ public class AccountFragment extends Fragment {
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onResume() {
super.onResume();
......
......@@ -6,4 +6,6 @@ import android.view.View;
public interface FragmentHolderLogic {
void init(Context context, View view, Bundle args);
String getTabTitle();
}
......@@ -10,6 +10,8 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
......@@ -25,6 +27,7 @@ import de.mytfg.apps.vplan.activities.MainActivity;
import de.mytfg.apps.vplan.api.ApiCallback;
import de.mytfg.apps.vplan.api.ApiParams;
import de.mytfg.apps.vplan.api.MyTFGApi;
import de.mytfg.apps.vplan.objects.User;
public class LoginFragment extends Fragment {
private View view;
......@@ -38,11 +41,11 @@ public class LoginFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_login, container, false);
final MainActivity context = (MainActivity)this.getActivity();
setHasOptionsMenu(true);
context.getToolbarManager()
.clear()
.setTitle(getString(R.string.menutitle_login))
.setExpandable(true, true)
.setImage(R.mipmap.front)
.setTabs(false);
.setExpandable(true, true);
MyTFGApi api = new MyTFGApi(context);
EditText username = (EditText) view.findViewById(R.id.login_username);
......@@ -93,6 +96,11 @@ public class LoginFragment extends Fragment {
String device = result.getString("device");
long expire = result.getLong("expires");
api.saveLogin(uname, token, device, expire);
User user = new User(context);
JSONObject jsonuser = result.getJSONObject("user");
if (!user.load(jsonuser)) {
Log.e("LOGIN", "Could not load user during login");
}
context.getNavi().clear();
context.getNavi().navigate(new PlanFragment(), R.id.fragment_container);
} catch (JSONException ex) {
......@@ -126,6 +134,12 @@ public class LoginFragment extends Fragment {
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onResume() {
super.onResume();
......
......@@ -3,11 +3,14 @@ package de.mytfg.apps.vplan.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
......@@ -29,32 +32,45 @@ public class PlanFragment extends Fragment {
private Vplan todayPlan;
private Vplan tomorrowPlan;
private MainActivity context;
private ViewPagerAdapter viewPagerAdapter;
public PlanFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_plan, container, false);
MainActivity context = (MainActivity)this.getActivity();
final MainActivity context = (MainActivity)this.getActivity();
this.context = context;
context.getToolbarManager()
.clear(false)
.setTitle(getString(R.string.menutitle_plan))
.clearMenu()
.setExpandable(true, true)
.setTabs(true);
.setTabs(true)
.showFab()
.setTabOutscroll(true);
// Create logics
todayPlan = new Vplan(context, "today");
/*todayPlan = new Vplan(context, "today");
tomorrowPlan = new Vplan(context, "tomorrow");
today = new PlanLogic(todayPlan);
tomorrow = new PlanLogic(tomorrowPlan);
*/
setHasOptionsMenu(true);
FloatingActionButton fab = (FloatingActionButton) context.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.getToolbarManager().showSearchBar();
}
});
return view;
}
......@@ -63,12 +79,43 @@ public class PlanFragment extends Fragment {
menu.clear();
inflater.inflate(R.menu.plan_details_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
SearchView searchView = context.getToolbarManager().getSearchView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
today.filter(newText);
tomorrow.filter(newText);
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
today.resetFilter();
tomorrow.resetFilter();
return true;
}
});
}
private Vplan getTodayPlan() {
return todayPlan;
}
private Vplan getTomorrowPlan() {
return tomorrowPlan;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
TabLayout tabLayout = context.getToolbarManager().getTabs();
Vplan plan = tabLayout.getSelectedTabPosition() == 0 ? todayPlan : tomorrowPlan;
Vplan plan = tabLayout.getSelectedTabPosition() == 0 ? getTodayPlan() : getTomorrowPlan();
if (!plan.isLoaded()) {
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) context.findViewById(R.id.coordinator_layout);
Snackbar snackbar = Snackbar
......@@ -130,7 +177,7 @@ public class PlanFragment extends Fragment {
ViewPager viewPager = (ViewPager) view.findViewById(R.id.plan_pager);
// Create Pager elements if not existent
if (today == null || tomorrow == null || viewPagerAdapter == null) {
if (today == null || tomorrow == null) {
todayPlan = new Vplan(context, "today");
tomorrowPlan = new Vplan(context, "tomorrow");
......@@ -138,21 +185,21 @@ public class PlanFragment extends Fragment {
tomorrow = new PlanLogic(tomorrowPlan);
viewPagerAdapter = new ViewPagerAdapter(this.getChildFragmentManager());
Log.d("onResume", "Called on Resume");
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this.getChildFragmentManager());
viewPagerAdapter.addFragment(
FragmentHolder.newInstance(
R.layout.fragment_base_list,
today
),
getResources().getString(R.string.plan_today)
)
);
viewPagerAdapter.addFragment(
FragmentHolder.newInstance(
R.layout.fragment_base_list,
tomorrow
),
getResources().getString(R.string.plan_tomorrow)
)
);
viewPager.setAdapter(viewPagerAdapter);
}
......
......@@ -6,7 +6,10 @@ import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.TextViewCompat;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
......@@ -16,6 +19,7 @@ import de.mytfg.apps.vplan.R;
import de.mytfg.apps.vplan.activities.MainActivity;
import de.mytfg.apps.vplan.api.MyTFGApi;
import de.mytfg.apps.vplan.api.SuccessCallback;
import de.mytfg.apps.vplan.objects.User;
import de.mytfg.apps.vplan.objects.Vplan;
import de.mytfg.apps.vplan.toolbar.ToolbarManager;
......@@ -31,20 +35,37 @@ public class StartFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
MainActivity context = (MainActivity)this.getActivity();
setHasOptionsMenu(true);
context.getToolbarManager()
.clear()
.setTitle(getString(R.string.menutitle_home))
.setExpandable(true, true)
.setTabs(false);
.setExpandable(true, true);
MyTFGApi api = new MyTFGApi(context);
if (api.isLoggedIn()) {
User user = api.getUser();
TextView infotext = (TextView) view.findViewById(R.id.home_login_status);
infotext.setText(String.format(getString(R.string.home_status_loggedin), api.getUsername(), api.getExpireString()));
String text = String.format(
getString(R.string.home_welcome_text),
user.getFirstname(),
user.getLastname(),
user.getLevel(),
user.getUsername(),
user.getGrade(),
api.getExpireString()
);
infotext.setText(Html.fromHtml(text));
}
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onResume() {
super.onResume();
......
package de.mytfg.apps.vplan.logic;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
......@@ -28,7 +31,10 @@ public class PlanLogic implements FragmentHolderLogic {
private Context context;
private RecylcerPlanAdapter adapter;
private RecyclerView recyclerView;
private SwipeRefreshLayout refreshLayout;
private ProgressDialog dialog;
private String current_filter = null;
public PlanLogic(Vplan plan) {
this.plan = plan;
}
......@@ -37,6 +43,8 @@ public class PlanLogic implements FragmentHolderLogic {
public void init(final Context context, View view, Bundle args) {
this.context = context;
recyclerView = (RecyclerView) view.findViewById(R.id.base_recylcerview);
refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.base_refreshLayout);
adapter = new RecylcerPlanAdapter(context);
recyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1);
......@@ -47,55 +55,97 @@ public class PlanLogic implements FragmentHolderLogic {
if (this.plan.isLoaded()) {
display();
} else {
this.plan.load(new SuccessCallback() {
@Override
public void callback(boolean success) {
if (success) {
// Display results
display();
} else {
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) ((MainActivity) context).findViewById(R.id.coordinator_layout);
// Show snackbar since user input is invalid
int code = plan.getLastCode();
String message = context.getString(R.string.plan_failed);
switch (code) {
case -1:
message = context.getString(R.string.api_offline);
break;
case 403:
message = context.getString(R.string.api_noauth);
break;
default:
message = context.getString(R.string.api_serverfault);
break;
}
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
message,
Snackbar.LENGTH_LONG);
snackbar.show();
}
}
});
loadPlan(false);
}
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadPlan(true);
}
});
}
private void display() {
adapter = new RecylcerPlanAdapter(context);
recyclerView.setAdapter(adapter);
for (VplanEntry entry : this.plan.getEntries()) {
for (VplanEntry entry : this.plan.filter(this.current_filter)) {
adapter.addItem(entry);
}
adapter.notifyDataSetChanged();
//TabLayout.Tab tab = getTab();
ViewPager pager = (ViewPager)((MainActivity)context).findViewById(R.id.plan_pager);
((MainActivity)context).getToolbarManager().getTabs().setupWithViewPager(pager);/*
if (tab != null) {
//tab.setText("Test, sas - asd");
Log.d("TAB TEXT", tab.getText().toString());
Log.d("TAB TEXT LEN", tab.getText().toString().length() + "");
}*/
}
private void loadPlan(boolean reload) {
refreshLayout.setRefreshing(true);
this.plan.load(new SuccessCallback() {
@Override
public void callback(boolean success) {
refreshLayout.setRefreshing(false);
if (success) {
// Display results
display();
} else {
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) ((MainActivity) context).findViewById(R.id.coordinator_layout);
// Show snackbar since user input is invalid
int code = plan.getLastCode();
String message = context.getString(R.string.plan_failed);
switch (code) {
case -1:
message = context.getString(R.string.api_offline);
break;
case 403:
message = context.getString(R.string.api_noauth);
break;
default:
message = context.getString(R.string.api_serverfault);
break;
}
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
message,
Snackbar.LENGTH_LONG);
snackbar.show();
}
}
}, reload);
}
private TabLayout.Tab getTab() {
TabLayout tabLayout = ((MainActivity)context).getToolbarManager().getTabs();
TabLayout.Tab tab;
if (tabLayout == null) {
return null;
}
if (this.plan.getDay().equals("today")) {
tab = tabLayout.getTabAt(0);
} else {
tab = tabLayout.getTabAt(1);
}
if (tab != null) {
tab.setText(plan.getDayString());
}
return tab;
}
@Override
public String getTabTitle() {
return plan.getDayString();
}
public void filter(String filter) {
this.current_filter = filter;
this.display();
}
public void resetFilter() {
this.current_filter = null;
this.display();
}
}
......@@ -18,6 +18,7 @@ import android.transition.TransitionInflater;
import android.transition.TransitionSet;