Updated LoginFragment and ModulesFragment (not runnable)
This commit is contained in:
@@ -48,7 +48,6 @@ import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
|||||||
public class MainActivity extends AppCompatActivity
|
public class MainActivity extends AppCompatActivity
|
||||||
implements MainActivityListener, KVVListener,
|
implements MainActivityListener, KVVListener,
|
||||||
NavigationView.OnNavigationItemSelectedListener,
|
NavigationView.OnNavigationItemSelectedListener,
|
||||||
LoginFragment.OnLoginFragmentInteractionListener,
|
|
||||||
ModulesFragment.OnModulesFragmentInteractionListener,
|
ModulesFragment.OnModulesFragmentInteractionListener,
|
||||||
CanteensFragment.OnCanteensFragmentInteractionListener {
|
CanteensFragment.OnCanteensFragmentInteractionListener {
|
||||||
|
|
||||||
@@ -102,7 +101,10 @@ public class MainActivity extends AppCompatActivity
|
|||||||
mNavigationView.setNavigationItemSelectedListener(this);
|
mNavigationView.setNavigationItemSelectedListener(this);
|
||||||
mFragmentManager = getSupportFragmentManager();
|
mFragmentManager = getSupportFragmentManager();
|
||||||
|
|
||||||
getNewKVV().account().doOfflineLogin();
|
if (!getNewKVV().account().restoreOnlineLogin()) {
|
||||||
|
desiredPage = FRAGMENT_LOGIN;
|
||||||
|
desiredData = "";
|
||||||
|
}
|
||||||
updateNavigation();
|
updateNavigation();
|
||||||
changeFragment(desiredPage, desiredData);
|
changeFragment(desiredPage, desiredData);
|
||||||
}
|
}
|
||||||
@@ -492,12 +494,6 @@ public class MainActivity extends AppCompatActivity
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode) {
|
|
||||||
int fragment = getDefaultFragmentAfterLogin();
|
|
||||||
toLoginState(loginToken.getFullName(), loginToken.getEmail(), fragment, "", onlineMode);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onModulesFragmentInteraction(final String itemID) {
|
public void onModulesFragmentInteraction(final String itemID) {
|
||||||
changeFragment(FRAGMENT_MODULES_DETAILS, itemID);
|
changeFragment(FRAGMENT_MODULES_DETAILS, itemID);
|
||||||
|
|||||||
@@ -70,8 +70,8 @@ public class CanteensFragment extends Fragment {
|
|||||||
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
|
CanteenBrowser browser = ((MainActivity) getActivity()).getCanteenBrowser();
|
||||||
browser.getCanteens(success -> {
|
browser.getCanteens(success -> {
|
||||||
adapter.setCanteens(success);
|
adapter.setCanteens(success);
|
||||||
if (mMainActivityListener != null)
|
//if (mMainActivityListener != null)
|
||||||
mMainActivityListener.refreshNavigation();
|
// mMainActivityListener.refreshNavigation();
|
||||||
swipeLayout.setRefreshing(false);
|
swipeLayout.setRefreshing(false);
|
||||||
}, error -> {
|
}, error -> {
|
||||||
log.e(error.toString());
|
log.e(error.toString());
|
||||||
|
|||||||
@@ -9,31 +9,21 @@ import android.view.ViewGroup;
|
|||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import de.sebse.fuplanner.MainActivity;
|
|
||||||
import de.sebse.fuplanner.R;
|
import de.sebse.fuplanner.R;
|
||||||
import de.sebse.fuplanner.services.GoogleAuth.GoogleAuth;
|
|
||||||
import de.sebse.fuplanner.services.KVV.KVV;
|
|
||||||
import de.sebse.fuplanner.services.KVV.types.LoginToken;
|
|
||||||
import de.sebse.fuplanner.services.KVV.types.Modules;
|
|
||||||
import de.sebse.fuplanner.tools.MainActivityListener;
|
import de.sebse.fuplanner.tools.MainActivityListener;
|
||||||
import de.sebse.fuplanner.tools.logging.Logger;
|
import de.sebse.fuplanner.tools.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple {@link Fragment} subclass.
|
* A simple {@link Fragment} subclass.
|
||||||
* Activities that contain this fragment must implement the
|
|
||||||
* {@link LoginFragment.OnLoginFragmentInteractionListener} interface
|
|
||||||
* to handle interaction events.
|
|
||||||
* Use the {@link LoginFragment#newInstance} factory method to
|
* Use the {@link LoginFragment#newInstance} factory method to
|
||||||
* create an instance of this fragment.
|
* create an instance of this fragment.
|
||||||
*/
|
*/
|
||||||
public class LoginFragment extends Fragment {
|
public class LoginFragment extends Fragment {
|
||||||
private OnLoginFragmentInteractionListener mListener;
|
|
||||||
private final Logger log = new Logger(this);
|
private final Logger log = new Logger(this);
|
||||||
private MainActivityListener mActivityListener;
|
@Nullable private MainActivityListener mActivityListener;
|
||||||
|
|
||||||
public LoginFragment() {
|
public LoginFragment() {
|
||||||
// Required empty public constructor
|
// Required empty public constructor
|
||||||
@@ -57,21 +47,11 @@ public class LoginFragment extends Fragment {
|
|||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
// Inflate the layout for this fragment
|
// Inflate the layout for this fragment
|
||||||
View v = inflater.inflate(R.layout.fragment_login, container, false);
|
View v = inflater.inflate(R.layout.fragment_login, container, false);
|
||||||
try {
|
if (mActivityListener != null && mActivityListener.getNewKVV().modules().list().stored()) {
|
||||||
Context context = getContext();
|
Button offline_btn = v.findViewById(R.id.btn_offline);
|
||||||
if (context != null) {
|
offline_btn.setVisibility(View.VISIBLE);
|
||||||
Modules modules = Modules.load(context);
|
offline_btn.setText(v.getResources().getString(R.string.enter_offline_mode, mActivityListener.getNewKVV().modules().list().getUsername()));
|
||||||
if (modules != null) {
|
offline_btn.setOnClickListener(v1 -> mActivityListener.getNewKVV().account().doOfflineLogin());
|
||||||
Button offline_btn = v.findViewById(R.id.btn_offline);
|
|
||||||
offline_btn.setVisibility(View.VISIBLE);
|
|
||||||
offline_btn.setText(v.getResources().getString(R.string.enter_offline_mode, modules.getToken().getUsername()));
|
|
||||||
offline_btn.setOnClickListener(v1 -> mListener.onLoginFragmentInteraction(modules.getToken(), false));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
View btn_login = v.findViewById(R.id.btn_login);
|
View btn_login = v.findViewById(R.id.btn_login);
|
||||||
@@ -84,40 +64,29 @@ public class LoginFragment extends Fragment {
|
|||||||
|
|
||||||
EditText input_usr = ((View) view.getParent()).findViewById(R.id.input_username);
|
EditText input_usr = ((View) view.getParent()).findViewById(R.id.input_username);
|
||||||
EditText input_pwd = ((View) view.getParent()).findViewById(R.id.input_password);
|
EditText input_pwd = ((View) view.getParent()).findViewById(R.id.input_password);
|
||||||
if (input_usr != null) {
|
|
||||||
if (input_pwd != null) {
|
String username = input_usr.getText().toString();
|
||||||
if (LoginFragment.this.getActivity() == null) {
|
String password = input_pwd.getText().toString();
|
||||||
log.e("Login fragment has no activity!");
|
|
||||||
return;
|
mActivityListener.getNewKVV().account().doOnlineLogin(username, password, success -> {
|
||||||
|
progressDialog.dismiss();
|
||||||
|
mActivityListener.getGoogleAuth().setLoginState(username, password);
|
||||||
|
input_usr.setError(null);
|
||||||
|
input_pwd.setError(null);
|
||||||
|
}, error -> {
|
||||||
|
progressDialog.dismiss();
|
||||||
|
// Invalid password
|
||||||
|
if (mActivityListener != null) {
|
||||||
|
if (error.getCode() == 100131) {
|
||||||
|
mActivityListener.showToast(R.string.invalid_credentials);
|
||||||
|
input_usr.setError(input_usr.getResources().getString(R.string.invalid_credentials));
|
||||||
|
input_pwd.setError(input_pwd.getResources().getString(R.string.invalid_credentials));
|
||||||
|
} else {
|
||||||
|
mActivityListener.showToast(v.getResources().getString(R.string.error_occurred_code, error.getCode()));
|
||||||
}
|
}
|
||||||
String username = input_usr.getText().toString();
|
|
||||||
String password = input_pwd.getText().toString();
|
|
||||||
KVV kvv = ((MainActivity) getActivity()).getKVV();
|
|
||||||
GoogleAuth gauth = ((MainActivity) getActivity()).getGoogleAuth();
|
|
||||||
kvv.login(username, password, success -> {
|
|
||||||
progressDialog.dismiss();
|
|
||||||
gauth.setLoginState(username, password);
|
|
||||||
if (mListener != null) {
|
|
||||||
input_usr.setError(null);
|
|
||||||
input_pwd.setError(null);
|
|
||||||
mListener.onLoginFragmentInteraction(success, true);
|
|
||||||
}
|
|
||||||
}, error -> {
|
|
||||||
progressDialog.dismiss();
|
|
||||||
// Invalid password
|
|
||||||
if (mActivityListener != null) {
|
|
||||||
if (error.getCode() == 100131) {
|
|
||||||
mActivityListener.showToast(R.string.invalid_credentials);
|
|
||||||
input_usr.setError(input_usr.getResources().getString(R.string.invalid_credentials));
|
|
||||||
input_pwd.setError(input_pwd.getResources().getString(R.string.invalid_credentials));
|
|
||||||
} else {
|
|
||||||
mActivityListener.showToast(v.getResources().getString(R.string.error_occurred_code, error.getCode()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.e("Error on KVV login!", error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
log.e("Error on KVV login!", error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
@@ -128,12 +97,6 @@ public class LoginFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
if (context instanceof OnLoginFragmentInteractionListener) {
|
|
||||||
mListener = (OnLoginFragmentInteractionListener) context;
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException(context.toString()
|
|
||||||
+ " must implement OnLoginFragmentInteractionListener");
|
|
||||||
}
|
|
||||||
if (context instanceof MainActivityListener) {
|
if (context instanceof MainActivityListener) {
|
||||||
mActivityListener = (MainActivityListener) context;
|
mActivityListener = (MainActivityListener) context;
|
||||||
mActivityListener.onTitleTextChange(R.string.log_in);
|
mActivityListener.onTitleTextChange(R.string.log_in);
|
||||||
@@ -144,20 +107,6 @@ public class LoginFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onDetach() {
|
public void onDetach() {
|
||||||
super.onDetach();
|
super.onDetach();
|
||||||
mListener = null;
|
mActivityListener = 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 OnLoginFragmentInteractionListener {
|
|
||||||
void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import de.sebse.fuplanner.R;
|
import de.sebse.fuplanner.R;
|
||||||
import de.sebse.fuplanner.fragments.ModulesFragment.OnModulesFragmentInteractionListener;
|
import de.sebse.fuplanner.fragments.ModulesFragment.OnModulesFragmentInteractionListener;
|
||||||
import de.sebse.fuplanner.services.KVV.types.Modules;
|
import de.sebse.fuplanner.services.NewKVV.types.Modules;
|
||||||
import de.sebse.fuplanner.tools.ui.ItemViewHolder;
|
import de.sebse.fuplanner.tools.ui.ItemViewHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
@@ -28,7 +29,7 @@ public class ModulesFragment extends Fragment {
|
|||||||
private final Logger log = new Logger(this);
|
private final Logger log = new Logger(this);
|
||||||
private ModulesAdapter adapter;
|
private ModulesAdapter adapter;
|
||||||
private SwipeRefreshLayout swipeLayout;
|
private SwipeRefreshLayout swipeLayout;
|
||||||
private MainActivityListener mMainActivityListener;
|
@Nullable private MainActivityListener mMainActivityListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||||
@@ -66,12 +67,11 @@ public class ModulesFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void refresh(boolean forceRefresh) {
|
private void refresh(boolean forceRefresh) {
|
||||||
if (getActivity() != null) {
|
if (mMainActivityListener != null) {
|
||||||
KVV kvv = ((MainActivity) getActivity()).getKVV();
|
mMainActivityListener.getNewKVV().modules().list().recv(success -> {
|
||||||
kvv.getModuleList(success -> {
|
|
||||||
adapter.setModules(success);
|
adapter.setModules(success);
|
||||||
if (mMainActivityListener != null)
|
//if (mMainActivityListener != null)
|
||||||
mMainActivityListener.refreshNavigation();
|
// mMainActivityListener.refreshNavigation();
|
||||||
swipeLayout.setRefreshing(false);
|
swipeLayout.setRefreshing(false);
|
||||||
}, error -> {
|
}, error -> {
|
||||||
log.e(error.toString());
|
log.e(error.toString());
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package de.sebse.fuplanner.services.NewKVV;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@@ -39,10 +40,10 @@ public class KVVLogin extends HTTPService {
|
|||||||
}
|
}
|
||||||
mLoginPending = true;
|
mLoginPending = true;
|
||||||
doLogin(username, password, token -> {
|
doLogin(username, password, token -> {
|
||||||
testLoginToken(token2 -> {
|
testLoginToken(token, token2 -> {
|
||||||
setToken(token, true);
|
setToken(token2, true);
|
||||||
mLoginPending = false;
|
mLoginPending = false;
|
||||||
callback.onResponse(token);
|
callback.onResponse(token2);
|
||||||
}, error -> {
|
}, error -> {
|
||||||
mLoginPending = false;
|
mLoginPending = false;
|
||||||
errorCallback.onError(error);
|
errorCallback.onError(error);
|
||||||
@@ -53,13 +54,21 @@ public class KVVLogin extends HTTPService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean restoreOnlineLogin() {
|
||||||
|
return restoreLogin(true);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean doOfflineLogin() {
|
public boolean doOfflineLogin() {
|
||||||
|
return restoreLogin(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean restoreLogin(boolean enteringOnlineMode) {
|
||||||
if (mLoginPending || mToken != null)
|
if (mLoginPending || mToken != null)
|
||||||
return false;
|
return false;
|
||||||
mLoginPending = true;
|
mLoginPending = true;
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
try {
|
try {
|
||||||
result = setToken(LoginToken.load(getContext()), false);
|
result = setToken(LoginToken.load(getContext()), enteringOnlineMode);
|
||||||
} catch (FileNotFoundException ignored) {
|
} catch (FileNotFoundException ignored) {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -110,12 +119,16 @@ public class KVVLogin extends HTTPService {
|
|||||||
return isLoggedIn() && mOnlineMode;
|
return isLoggedIn() && mOnlineMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLoginToken(NetworkCallback<LoginToken> callback, NetworkErrorCallback errorCallback) {
|
void testLoginToken(NetworkCallback<LoginToken> callback, NetworkErrorCallback errorCallback) {
|
||||||
if (mToken == null) {
|
if (mToken == null) {
|
||||||
errorCallback.onError(new NetworkError(100173, -1, "Not logged in!"));
|
errorCallback.onError(new NetworkError(100173, -1, "Not logged in!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
get(String.format("https://kvv.imp.fu-berlin.de/direct/profile/%s.json", mToken.getUsername()), mToken.getCookies(), response -> {
|
testLoginToken(mToken, callback, errorCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testLoginToken(@NotNull LoginToken token, NetworkCallback<LoginToken> callback, NetworkErrorCallback errorCallback) {
|
||||||
|
get(String.format("https://kvv.imp.fu-berlin.de/direct/profile/%s.json", token.getUsername()), token.getCookies(), response -> {
|
||||||
String body = response.getParsed();
|
String body = response.getParsed();
|
||||||
if (body == null) {
|
if (body == null) {
|
||||||
errorCallback.onError(new NetworkError(100172, 403, "Testing login failed!"));
|
errorCallback.onError(new NetworkError(100172, 403, "Testing login failed!"));
|
||||||
@@ -125,8 +138,8 @@ public class KVVLogin extends HTTPService {
|
|||||||
JSONObject json = new JSONObject(body);
|
JSONObject json = new JSONObject(body);
|
||||||
String displayName = json.getString("displayName");
|
String displayName = json.getString("displayName");
|
||||||
String email = json.getString("email");
|
String email = json.getString("email");
|
||||||
mToken.setAdditionals(displayName, email);
|
token.setAdditionals(displayName, email);
|
||||||
callback.onResponse(mToken);
|
callback.onResponse(token);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
errorCallback.onError(new NetworkError(100171, 403, "Cannot parse profile!"));
|
errorCallback.onError(new NetworkError(100171, 403, "Cannot parse profile!"));
|
||||||
}
|
}
|
||||||
@@ -150,7 +163,7 @@ public class KVVLogin extends HTTPService {
|
|||||||
|
|
||||||
private boolean handleCallbacks() {
|
private boolean handleCallbacks() {
|
||||||
if (mToken != null) {
|
if (mToken != null) {
|
||||||
mListener.handleLogin(mToken, false);
|
mListener.handleLogin(mToken, mOnlineMode);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
mListener.handleLogout();
|
mListener.handleLogout();
|
||||||
@@ -169,7 +182,7 @@ public class KVVLogin extends HTTPService {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mOnlineMode = !enteringOnlineMode;
|
mOnlineMode = enteringOnlineMode;
|
||||||
return handleCallbacks();
|
return handleCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,27 +24,41 @@ import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
|
|||||||
import static de.sebse.fuplanner.services.NewKVV.ModulesPart.RETRY_COUNT;
|
import static de.sebse.fuplanner.services.NewKVV.ModulesPart.RETRY_COUNT;
|
||||||
|
|
||||||
public class KVVModulesList extends HTTPService {
|
public class KVVModulesList extends HTTPService {
|
||||||
protected final KVVLogin login;
|
private final KVVLogin mLogin;
|
||||||
@Nullable private Modules modules;
|
@Nullable private Modules mModules;
|
||||||
private NewAsyncQueue queue = new NewAsyncQueue();
|
private NewAsyncQueue mQueue = new NewAsyncQueue();
|
||||||
|
|
||||||
KVVModulesList(KVVLogin login, Context context) {
|
KVVModulesList(KVVLogin login, Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.login = login;
|
this.mLogin = login;
|
||||||
restore();
|
restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean stored() {
|
||||||
|
return mModules != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getUsername() {
|
||||||
|
if (mModules != null) {
|
||||||
|
return mModules.getUsername();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void find(String moduleID, NetworkCallback<Modules.Module> moduleNetworkCallback, NetworkErrorCallback errorCallback) {
|
public void find(String moduleID, NetworkCallback<Modules.Module> moduleNetworkCallback, NetworkErrorCallback errorCallback) {
|
||||||
find(moduleID, moduleNetworkCallback, errorCallback, RETRY_COUNT);
|
find(moduleID, moduleNetworkCallback, errorCallback, RETRY_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void find(String moduleID, NetworkCallback<Modules.Module> moduleNetworkCallback, NetworkErrorCallback errorCallback, int retries) {
|
private void find(String moduleID, NetworkCallback<Modules.Module> moduleNetworkCallback, NetworkErrorCallback errorCallback, int retries) {
|
||||||
|
if (mModules != null && mLogin.getLoginToken() != null && mLogin.getLoginToken().isSameUser(mModules.getUsername()))
|
||||||
|
delete();
|
||||||
if (retries < 0) {
|
if (retries < 0) {
|
||||||
errorCallback.onError(new NetworkError(101107, -1, "Too many retries!"));
|
errorCallback.onError(new NetworkError(101107, -1, "Too many retries!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.modules != null) {
|
if (this.mModules != null) {
|
||||||
Modules.Module module = this.modules.get(moduleID);
|
Modules.Module module = this.mModules.get(moduleID);
|
||||||
if (module != null) {
|
if (module != null) {
|
||||||
moduleNetworkCallback.onResponse(module);
|
moduleNetworkCallback.onResponse(module);
|
||||||
return;
|
return;
|
||||||
@@ -54,9 +68,9 @@ public class KVVModulesList extends HTTPService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void store() {
|
void store() {
|
||||||
if (this.modules != null) {
|
if (this.mModules != null) {
|
||||||
try {
|
try {
|
||||||
this.modules.save(getContext());
|
this.mModules.save(getContext());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -65,7 +79,7 @@ public class KVVModulesList extends HTTPService {
|
|||||||
|
|
||||||
private void restore() {
|
private void restore() {
|
||||||
try {
|
try {
|
||||||
this.modules = Modules.load(getContext());
|
this.mModules = Modules.load(getContext());
|
||||||
} catch (FileNotFoundException ignored) {
|
} catch (FileNotFoundException ignored) {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -74,51 +88,64 @@ public class KVVModulesList extends HTTPService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recv(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback) {
|
private void delete() {
|
||||||
recv(callback, errorCallback, false, RETRY_COUNT);
|
if (this.mModules != null) {
|
||||||
|
this.mModules.delete(getContext());
|
||||||
|
this.mModules = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recv(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback, boolean forceRefresh, final int retries) {
|
public void recv(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback) {
|
||||||
queue.add(() -> {
|
recv(callback, errorCallback, false);
|
||||||
if (this.modules != null && !forceRefresh) {
|
}
|
||||||
callback.onResponse(this.modules);
|
|
||||||
queue.next();
|
public void recv(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback, boolean forceRefresh) {
|
||||||
|
recv(callback, errorCallback, forceRefresh, RETRY_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recv(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback, boolean forceRefresh, final int retries) {
|
||||||
|
if (mModules != null && mLogin.getLoginToken() != null && mLogin.getLoginToken().isSameUser(mModules.getUsername()))
|
||||||
|
delete();
|
||||||
|
mQueue.add(() -> {
|
||||||
|
if (this.mModules != null && !forceRefresh) {
|
||||||
|
callback.onResponse(this.mModules);
|
||||||
|
mQueue.next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.upgrade(success -> {
|
this.upgrade(success -> {
|
||||||
if (this.modules == null)
|
if (this.mModules == null)
|
||||||
this.modules = success;
|
this.mModules = success;
|
||||||
else
|
else
|
||||||
this.modules.updateList(success);
|
this.mModules.updateList(success);
|
||||||
callback.onResponse(this.modules);
|
callback.onResponse(this.mModules);
|
||||||
queue.next();
|
mQueue.next();
|
||||||
}, error -> {
|
}, error -> {
|
||||||
if (retries > 0 && (error.getHttpStatus() == 401 || error.getHttpStatus() == 403)) {
|
if (retries > 0 && (error.getHttpStatus() == 401 || error.getHttpStatus() == 403)) {
|
||||||
login.refreshLogin(success -> {
|
mLogin.refreshLogin(success -> {
|
||||||
recv(callback, errorCallback, forceRefresh, retries-1);
|
recv(callback, errorCallback, forceRefresh, retries-1);
|
||||||
queue.next();
|
mQueue.next();
|
||||||
}, errorCallback);
|
}, errorCallback);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
errorCallback.onError(error);
|
errorCallback.onError(error);
|
||||||
queue.next();
|
mQueue.next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void upgrade(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback) {
|
private void upgrade(final NetworkCallback<Modules> callback, final NetworkErrorCallback errorCallback) {
|
||||||
if (!login.isInOnlineMode()) {
|
if (!mLogin.isInOnlineMode()) {
|
||||||
errorCallback.onError(new NetworkError(101105, 500, "Currently running in offline mode!"));
|
errorCallback.onError(new NetworkError(101105, 500, "Currently running in offline mode!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// https://file.io/71sa2V
|
// https://file.io/71sa2V
|
||||||
get("https://kvv.imp.fu-berlin.de/direct/site.json", login.getLoginToken().getCookies(), response -> {
|
get("https://kvv.imp.fu-berlin.de/direct/site.json", mLogin.getLoginToken().getCookies(), response -> {
|
||||||
String body = response.getParsed();
|
String body = response.getParsed();
|
||||||
if (body == null) {
|
if (body == null) {
|
||||||
errorCallback.onError(new NetworkError(101101, 403, "No module list retrieved!"));
|
errorCallback.onError(new NetworkError(101101, 403, "No module list retrieved!"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Modules modules = new Modules(login.getLoginToken());
|
Modules modules = new Modules(mLogin.getLoginToken().getUsername());
|
||||||
try {
|
try {
|
||||||
JSONObject json = new JSONObject(body);
|
JSONObject json = new JSONObject(body);
|
||||||
JSONArray sites = json.getJSONArray("site_collection");
|
JSONArray sites = json.getJSONArray("site_collection");
|
||||||
@@ -153,7 +180,7 @@ public class KVVModulesList extends HTTPService {
|
|||||||
}
|
}
|
||||||
// Empty module *may be* because token is invalid -> check
|
// Empty module *may be* because token is invalid -> check
|
||||||
if (modules.size() == 0)
|
if (modules.size() == 0)
|
||||||
login.testLoginToken(token -> callback.onResponse(modules), errorCallback);
|
mLogin.testLoginToken(token -> callback.onResponse(modules), errorCallback);
|
||||||
else
|
else
|
||||||
callback.onResponse(modules);
|
callback.onResponse(modules);
|
||||||
}, error -> errorCallback.onError(new NetworkError(101104, error.networkResponse.statusCode, "Cannot get module list!")));
|
}, error -> errorCallback.onError(new NetworkError(101104, error.networkResponse.statusCode, "Cannot get module list!")));
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ public class LoginToken implements Serializable {
|
|||||||
return cookies;
|
return cookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSameUser(LoginToken token) {
|
public boolean isSameUser(String username) {
|
||||||
return token != null && this.getUsername().equals(token.getUsername());
|
return this.getUsername().equals(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ import androidx.annotation.Nullable;
|
|||||||
|
|
||||||
public class Modules implements Iterable<Modules.Module>, Serializable {
|
public class Modules implements Iterable<Modules.Module>, Serializable {
|
||||||
private SortedListModule list;
|
private SortedListModule list;
|
||||||
private final LoginToken token;
|
private final String mUsername;
|
||||||
//private transient Logger log = new Logger(this);
|
//private transient Logger log = new Logger(this);
|
||||||
private static final String FILE_NAME = "ModuleListSaveFile";
|
private static final String FILE_NAME = "ModuleListSaveFile";
|
||||||
|
|
||||||
public Modules(LoginToken loginToken) {
|
public Modules(String username) {
|
||||||
this.token = loginToken;
|
this.mUsername = username;
|
||||||
this.list = new SortedListModule();
|
this.list = new SortedListModule();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +84,8 @@ public class Modules implements Iterable<Modules.Module>, Serializable {
|
|||||||
context.deleteFile(FILE_NAME);
|
context.deleteFile(FILE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginToken getToken() {
|
public String getUsername() {
|
||||||
return token;
|
return mUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateList(Modules modules) {
|
public void updateList(Modules modules) {
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ public interface MainActivityListener {
|
|||||||
|
|
||||||
KVV getKVV();
|
KVV getKVV();
|
||||||
|
|
||||||
|
de.sebse.fuplanner.services.NewKVV.KVV getNewKVV();
|
||||||
|
|
||||||
GoogleAuth getGoogleAuth();
|
GoogleAuth getGoogleAuth();
|
||||||
|
|
||||||
void loginTokenInvalid(boolean doLoginCheck);
|
void loginTokenInvalid(boolean doLoginCheck);
|
||||||
|
|||||||
Reference in New Issue
Block a user