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

Made Login Optional

parent aefc7723
......@@ -60,11 +60,6 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
MyTFGApi api = new MyTFGApi(context);
if (!api.isLoggedIn()) {
navi.clear();
navi.navigate(new LoginFragment(), R.id.fragment_container);
return true;
}
switch (item.getItemId()) {
default:
return false;
......@@ -73,8 +68,13 @@ public class MainActivity extends AppCompatActivity {
navi.navigate(new StartFragment(), R.id.fragment_container);
return true;
case R.id.mainmenu_plan:
// Login required to see VPlan
navi.clear();
navi.navigate(new PlanFragment(), R.id.fragment_container);
if (!api.isLoggedIn()) {
navi.navigate(new LoginFragment(), R.id.fragment_container);
} else {
navi.navigate(new PlanFragment(), R.id.fragment_container);
}
return true;
case R.id.mainmenu_vrr:
navi.clear();
......@@ -108,9 +108,11 @@ public class MainActivity extends AppCompatActivity {
Fragment fragment;
Settings settings = new Settings(context);
MyTFGApi api = new MyTFGApi(context);
if (!api.isLoggedIn()) {
if (!api.isLoggedIn() && !settings.getBool("first_login_hint")) {
settings.save("first_login_hint", true);
fragment = new LoginFragment();
} else {
if (savedInstanceState != null) {
......@@ -118,13 +120,7 @@ public class MainActivity extends AppCompatActivity {
"fragmentInstanceSaved");
} else {
// Read Landing Page from settings
Settings settings = new Settings(context);
int page = settings.getInt("landing_page");
String[] fragmentNames = getResources().getStringArray(R.array.settings_opt_landing_page_fragments);
String fragName = "StartFragment";
if (page >= 0 && page < fragmentNames.length) {
fragName = fragmentNames[page];
}
String fragName = settings.getString("landing_page");
try {
fragName = "de.mytfg.apps.vplan.fragments." + fragName;
Class c = Class.forName(fragName);
......
......@@ -8,6 +8,8 @@ import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.TextViewCompat;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
......@@ -16,6 +18,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -51,6 +54,9 @@ public class LoginFragment extends Fragment {
EditText username = (EditText) view.findViewById(R.id.login_username);
username.setText(api.getUsername());
TextView loginText = (TextView) view.findViewById(R.id.login_text);
loginText.setText(Html.fromHtml(getString(R.string.login_text)));
// Set Login Button listener
Button loginButton = (Button) view.findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
......
......@@ -28,6 +28,7 @@ import de.mytfg.apps.vplan.R;
import de.mytfg.apps.vplan.activities.MainActivity;
import de.mytfg.apps.vplan.adapters.FragmentHolder;
import de.mytfg.apps.vplan.adapters.ViewPagerAdapter;
import de.mytfg.apps.vplan.api.MyTFGApi;
import de.mytfg.apps.vplan.logic.PlanLogic;
import de.mytfg.apps.vplan.objects.Vplan;
import de.mytfg.apps.vplan.toolbar.ToolbarManager;
......@@ -52,6 +53,7 @@ public class PlanFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_plan, container, false);
final MainActivity context = (MainActivity)this.getActivity();
this.context = context;
context.getToolbarManager()
.clear(false)
......@@ -62,14 +64,6 @@ public class PlanFragment extends Fragment {
.showFab()
.setTabOutscroll(true);
// Create logics
/*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);
......
......@@ -10,6 +10,7 @@ import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
......@@ -62,50 +63,72 @@ public class SettingsFragment extends Fragment {
.setTitle(getString(R.string.menutitle_settings))
.setExpandable(true, true);
MyTFGApi api = new MyTFGApi(context);
String device = api.getStoredDevice();
long expirets = api.getExpire();
String expire = api.getExpireString();
final User user = api.getUser();
updateViews();
TextView username = (TextView) view.findViewById(R.id.account_name);
username.setText(user.getUsername());
TextView text = (TextView) view.findViewById(R.id.account_info_text);
text.setText(Html.fromHtml(
String.format(getString(R.string.account_info_text),
user.getFirstname(),
user.getLastname(),
user.getUsername(),
device,
expire,
user.getLevel(),
user.getGrade()
)
));
return view;
}
Button logoutbtn = (Button) view.findViewById(R.id.logout_button);
logoutbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyTFGApi api = new MyTFGApi(context);
api.logout(false);
private void updateViews() {
CardView details = (CardView) view.findViewById(R.id.account_loggedin);
CardView login = (CardView) view.findViewById(R.id.account_goto_login);
CardView additional = (CardView) view.findViewById(R.id.account_additional_card);
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) context.findViewById(R.id.coordinator_layout);
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
getString(R.string.account_logged_out),
Snackbar.LENGTH_LONG);
snackbar.show();
context.getNavi().clear();
context.getNavi().navigate(new LoginFragment(), R.id.fragment_container);
}
});
MyTFGApi api = new MyTFGApi(context);
if (api.isLoggedIn()) {
login.setVisibility(View.GONE);
details.setVisibility(View.VISIBLE);
additional.setVisibility(View.VISIBLE);
String device = api.getStoredDevice();
long expirets = api.getExpire();
String expire = api.getExpireString();
final User user = api.getUser();
updateSettings();
TextView username = (TextView) view.findViewById(R.id.account_name);
username.setText(user.getUsername());
TextView text = (TextView) view.findViewById(R.id.account_info_text);
text.setText(Html.fromHtml(
String.format(getString(R.string.account_info_text),
user.getFirstname(),
user.getLastname(),
user.getUsername(),
device,
expire,
user.getLevel(),
user.getGrade()
)
));
updateAdditionalClasses();
Button logoutbtn = (Button) view.findViewById(R.id.logout_button);
logoutbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyTFGApi api = new MyTFGApi(context);
api.logout(false);
return view;
CoordinatorLayout coordinatorLayout = (CoordinatorLayout) context.findViewById(R.id.coordinator_layout);
Snackbar snackbar = Snackbar
.make(coordinatorLayout,
getString(R.string.account_logged_out),
Snackbar.LENGTH_LONG);
snackbar.show();
updateViews();
}
});
updateAdditionalClasses();
} else {
login.setVisibility(View.VISIBLE);
details.setVisibility(View.GONE);
additional.setVisibility(View.GONE);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity)getActivity()).getNavi().navigate(new LoginFragment(), R.id.fragment_container);
}
});
}
updateSettings();
}
@Override
......@@ -224,7 +247,7 @@ public class SettingsFragment extends Fragment {
break;
default:
explanation.setText(Html.fromHtml(
String.format(getString(R.string.account_additional_explanation_pupil),
String.format(getString(R.string.account_additional_explanation_highlevel),
user.getLevel())
));
break;
......@@ -298,11 +321,21 @@ public class SettingsFragment extends Fragment {
}
});
landing.setSelection(settings.getInt("landing_page"));
String selection = settings.getString("landing_page");
int selectedId = 0;
String[] arr = getResources().getStringArray(R.array.settings_opt_landing_page_fragments);
for (int i = 0; i < arr.length; ++i) {
if (arr[i].equals(selection)) {
selectedId = i;
break;
}
}
landing.setSelection(selectedId);
landing.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
settings.save("landing_page", i);
settings.save("landing_page", getResources().getStringArray(R.array.settings_opt_landing_page_fragments)[i]);
}
@Override
......
......@@ -47,6 +47,12 @@ public class Settings {
return preferences.getString(option, "");
}
public void clear(String option) {
preferences.edit()
.remove(option)
.apply();
}
public int getInt(String option) {
return preferences.getInt(option, 0);
}
......
......@@ -32,11 +32,12 @@
android:textSize="@dimen/textTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
android:text="@string/login_title" />
<TextView
android:textSize="@dimen/textDefault"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/login_text"
android:text="@string/login_text" />
</LinearLayout>
......
......@@ -12,10 +12,56 @@
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cardview_spacing"
android:visibility="gone"
android:id="@+id/account_goto_login"
android:layout_marginBottom="@dimen/defaultPadding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="@dimen/cardview_spacing"
android:background="@color/colorPrimaryLight">
<ImageView
android:layout_width="60dp"
android:layout_gravity="start"
android:layout_height="60dp"
android:layout_marginEnd="20dp"
android:scaleType="fitEnd"
android:src="@mipmap/ic_mytfg_launcher_raw"
android:layout_marginRight="@dimen/cardview_default_radius" />
<TextView
android:textSize="@dimen/textTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:text="@string/login_action" />
<ImageView
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:scaleType="centerInside"
android:src="@drawable/ic_action_open_link"
android:layout_marginLeft="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cardview_spacing"
android:id="@+id/account_loggedin"
android:layout_marginBottom="@dimen/defaultPadding">
<LinearLayout
......@@ -53,6 +99,7 @@
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/account_additional_card"
android:padding="@dimen/cardview_spacing"
android:layout_marginBottom="@dimen/defaultPadding">
......
......@@ -33,8 +33,12 @@
<string name="api_vrr_error">Abfahrtszeiten konnten nicht geladen werden</string>
<!-- LOGIN -->
<string name="login_text">Um diese App und all ihre Funktionen nutzen zu können,
müssen Sie sich anmelden.</string>
<string name="login_text">
Für den Zugriff auf den Vertretungsplan ist eine Anmeldung erforderlich.&lt;br />
Sie können sich mit Ihrem MyTFG Account anmelden.&lt;br />
Eltern können den Zugang des Kindes nutzen.&lt;br />
Klassen von Geschwistern oder Freunden können anschließend in den Einstellungen ausgewählt werden.
</string>
<string name="login_action">Anmelden</string>
<string name="login_hint_username">Nutzername</string>
<string name="login_hint_password">Passwort</string>
......@@ -102,7 +106,7 @@
Sie haben die Möglichkeit, verschiedene Klassen hinzuzufügen, für die Sie ebenfalls Einträge erhalten wollen.
</string>
<string name="account_additional_explanation_highlevel">
Als %1$s erhalten Sie alle Einträge der Vertretungsplans.&lt;br/>
Als %1$s erhalten Sie alle Einträge des Vertretungsplans.&lt;br/>
Somit können Sie keine zusätzlichen Klassen wählen.
</string>
<string name="account_addional_classes">&lt;b>Zusätzliche Klassen&lt;/b>&lt;br/>%1$s</string>
......
......@@ -68,6 +68,7 @@
<string name="api_vrr_error">VRR Data could not be loaded</string>
<!-- LOGIN -->
<string name="login_title">MyTFG Login</string>
<string name="login_text">To use this App and all its features you have to login first.</string>
<string name="login_action">Login</string>
<string name="login_hint_username">Username</string>
......@@ -119,11 +120,13 @@
<string-array name="settings_opt_landing_page_entries">
<item>@string/menutitle_home</item>
<item>@string/menutitle_plan</item>
<item>@string/menutitle_vrr</item>
<item>@string/menutitle_about</item>
</string-array>
<string-array name="settings_opt_landing_page_fragments">
<item>StartFragment</item>
<item>PlanFragment</item>
<item>VrrFragment</item>
<item>AboutFragment</item>
</string-array>
......
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