Commit 374a6c4e authored by Niklas Fix's avatar Niklas Fix 🎓
Browse files

move fetch settings to login and remove from App.java

parent 71343379
package de.akamu.tudarmstadt; package de.akamu.tudarmstadt;
import android.app.Application; import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Log;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.yariksoffice.lingver.Lingver; import com.yariksoffice.lingver.Lingver;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import de.akamu.tudarmstadt.data.settings.SettingsDataSource;
import de.akamu.tudarmstadt.data.settings.SettingsDataSourceImpl;
import de.akamu.tudarmstadt.interactors.SettingsInteractor;
import de.akamu.tudarmstadt.model.MyObjectBox; import de.akamu.tudarmstadt.model.MyObjectBox;
import de.akamu.tudarmstadt.util.Constants; import de.akamu.tudarmstadt.util.Constants;
import de.akamu.tudarmstadt.util.SettingsPresenter;
import io.objectbox.BoxStore; import io.objectbox.BoxStore;
/** /**
...@@ -29,12 +20,11 @@ import io.objectbox.BoxStore; ...@@ -29,12 +20,11 @@ import io.objectbox.BoxStore;
* application state. It provides access to the ObjectBox database * application state. It provides access to the ObjectBox database
* and the global settings. * and the global settings.
*/ */
public class App extends Application implements SettingsDataSource.FetchSettingsCallback { public class App extends Application {
private final String TAG = "App"; private final String TAG = "App";
private BoxStore boxStore; private BoxStore boxStore;
SettingsPresenter settingsPresenter;
@Override @Override
public void onCreate() { public void onCreate() {
...@@ -51,8 +41,6 @@ public class App extends Application implements SettingsDataSource.FetchSettings ...@@ -51,8 +41,6 @@ public class App extends Application implements SettingsDataSource.FetchSettings
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} }
boxStore = MyObjectBox.builder().androidContext(App.this).build(); boxStore = MyObjectBox.builder().androidContext(App.this).build();
settingsPresenter = new SettingsPresenter(new SettingsInteractor(new SettingsDataSourceImpl()));
settingsPresenter.fetchSettings(this);
} }
/** /**
...@@ -61,14 +49,4 @@ public class App extends Application implements SettingsDataSource.FetchSettings ...@@ -61,14 +49,4 @@ public class App extends Application implements SettingsDataSource.FetchSettings
public BoxStore getBoxStore() { public BoxStore getBoxStore() {
return boxStore; return boxStore;
} }
@Override
public void onFetchSettingsSuccess(@NotNull JSONObject settings) {
System.out.println("Settings fetched successfully.");
}
@Override
public void onFetchSettingsFailed(@NotNull String reason) {
System.out.println("Fetching settings failed: " + reason);
}
} }
\ No newline at end of file
...@@ -17,6 +17,7 @@ import de.akamu.tudarmstadt.util.AppUserUtil ...@@ -17,6 +17,7 @@ import de.akamu.tudarmstadt.util.AppUserUtil
import de.akamu.tudarmstadt.util.Constants import de.akamu.tudarmstadt.util.Constants
import de.akamu.tudarmstadt.util.Extensions.Companion.toast import de.akamu.tudarmstadt.util.Extensions.Companion.toast
import kotlinx.android.synthetic.main.activity_login.* import kotlinx.android.synthetic.main.activity_login.*
import org.json.JSONObject
class LoginActivity : BaseActivity(), LoginContract.View { class LoginActivity : BaseActivity(), LoginContract.View {
...@@ -124,6 +125,14 @@ class LoginActivity : BaseActivity(), LoginContract.View { ...@@ -124,6 +125,14 @@ class LoginActivity : BaseActivity(), LoginContract.View {
} }
} }
override fun onFetchSettingsSuccess(settings: JSONObject) {
println("Settings fetched successfully.")
}
override fun onFetchSettingsFailed(reason: String) {
println("Fetching settings failed: $reason")
}
override fun handleUnauthorizedEvent() { override fun handleUnauthorizedEvent() {
//Don't handle unauthorized event //Don't handle unauthorized event
} }
......
...@@ -2,9 +2,11 @@ package de.akamu.tudarmstadt.features.login ...@@ -2,9 +2,11 @@ package de.akamu.tudarmstadt.features.login
import de.akamu.tudarmstadt.BasePresenter import de.akamu.tudarmstadt.BasePresenter
import de.akamu.tudarmstadt.BaseView import de.akamu.tudarmstadt.BaseView
import de.akamu.tudarmstadt.data.settings.SettingsDataSource
import de.akamu.tudarmstadt.features.login.LoginContract.Presenter import de.akamu.tudarmstadt.features.login.LoginContract.Presenter
import de.akamu.tudarmstadt.features.login.LoginContract.View import de.akamu.tudarmstadt.features.login.LoginContract.View
import de.akamu.tudarmstadt.model.User import de.akamu.tudarmstadt.model.User
import org.json.JSONObject
/** /**
* Specifies the contract between the [View] and the [Presenter] * Specifies the contract between the [View] and the [Presenter]
...@@ -37,6 +39,10 @@ interface LoginContract { ...@@ -37,6 +39,10 @@ interface LoginContract {
fun getLoginToken() : String fun getLoginToken() : String
fun getAppUser() : User? fun getAppUser() : User?
fun onFetchSettingsSuccess(settings: JSONObject)
fun onFetchSettingsFailed(reason: String)
} }
interface Presenter : BasePresenter { interface Presenter : BasePresenter {
...@@ -48,6 +54,9 @@ interface LoginContract { ...@@ -48,6 +54,9 @@ interface LoginContract {
fun attemptLogin(username: String, password: String) fun attemptLogin(username: String, password: String)
/** Start the [SignupActivity] **/ /** Start the [SignupActivity] **/
fun startSignup(); fun startSignup()
/** Fetches the settings for the app from server **/
fun fetchSettings()
} }
} }
\ No newline at end of file
...@@ -4,17 +4,22 @@ import androidx.preference.PreferenceManager ...@@ -4,17 +4,22 @@ import androidx.preference.PreferenceManager
import de.akamu.tudarmstadt.api.AkamuFirebase import de.akamu.tudarmstadt.api.AkamuFirebase
import de.akamu.tudarmstadt.api.V2API import de.akamu.tudarmstadt.api.V2API
import de.akamu.tudarmstadt.data.login.LoginDataSource import de.akamu.tudarmstadt.data.login.LoginDataSource
import de.akamu.tudarmstadt.data.settings.SettingsDataSource
import de.akamu.tudarmstadt.data.settings.SettingsDataSourceImpl
import de.akamu.tudarmstadt.interactors.SettingsInteractor
import de.akamu.tudarmstadt.model.User import de.akamu.tudarmstadt.model.User
import de.akamu.tudarmstadt.util.Constants import de.akamu.tudarmstadt.util.Constants
import org.json.JSONObject
class LoginPresenter(private val interactor: LoginInteractor, var view: LoginContract.View?) : LoginContract.Presenter { class LoginPresenter(private val interactor: LoginInteractor, var view: LoginContract.View?) :
LoginContract.Presenter {
init { init {
view?.presenter = this view?.presenter = this
} }
override fun autoLogin() { override fun autoLogin() {
if(view?.getLoginToken() != "" && view?.getAppUser() != null) { if (view?.getLoginToken() != "" && view?.getAppUser() != null) {
V2API.setToken(view?.getLoginToken()) V2API.setToken(view?.getLoginToken())
view?.isLoggedIn(true, view?.getAppUser()!!) view?.isLoggedIn(true, view?.getAppUser()!!)
} }
...@@ -24,23 +29,27 @@ class LoginPresenter(private val interactor: LoginInteractor, var view: LoginCon ...@@ -24,23 +29,27 @@ class LoginPresenter(private val interactor: LoginInteractor, var view: LoginCon
view?.showProgress(true) view?.showProgress(true)
AkamuFirebase.loadFirebaseToken(object : AkamuFirebase.GetFirebaseTokenCallback { AkamuFirebase.loadFirebaseToken(object : AkamuFirebase.GetFirebaseTokenCallback {
override fun onGetFirebaseTokenSuccess(firebaseToken: String) { override fun onGetFirebaseTokenSuccess(firebaseToken: String) {
interactor.login(username, password, firebaseToken, object: LoginDataSource.LoginCallback { interactor.login(
override fun onLoginSuccess(appUser: User, loginToken: String) { username,
view?.saveLoginToken(loginToken) password,
view?.isLoggedIn(true, appUser) firebaseToken,
view?.showProgress(false) object : LoginDataSource.LoginCallback {
} override fun onLoginSuccess(appUser: User, loginToken: String) {
view?.saveLoginToken(loginToken)
override fun onLoginFailed(message: String) { view?.isLoggedIn(true, appUser)
view?.showLoginFailed() view?.showProgress(false)
view?.showProgress(false) }
}
override fun onLoginFailed(message: String) {
override fun onLoginCancelled() { view?.showLoginFailed()
view?.showLoginCancelled() view?.showProgress(false)
view?.showProgress(false) }
}
}) override fun onLoginCancelled() {
view?.showLoginCancelled()
view?.showProgress(false)
}
})
} }
override fun onGetFirebaseTokenFail(error: String) { override fun onGetFirebaseTokenFail(error: String) {
...@@ -50,6 +59,20 @@ class LoginPresenter(private val interactor: LoginInteractor, var view: LoginCon ...@@ -50,6 +59,20 @@ class LoginPresenter(private val interactor: LoginInteractor, var view: LoginCon
}) })
} }
override fun fetchSettings() {
val settingsInteractor = SettingsInteractor(SettingsDataSourceImpl())
settingsInteractor.fetchSettings(object : SettingsDataSource.FetchSettingsCallback {
override fun onFetchSettingsSuccess(settings: JSONObject) {
view?.onFetchSettingsSuccess(settings)
}
override fun onFetchSettingsFailed(reason: String) {
view?.onFetchSettingsFailed(reason)
}
})
}
override fun startSignup() { override fun startSignup() {
view?.navigateToSignup() view?.navigateToSignup()
} }
......
package de.akamu.tudarmstadt.util
import de.akamu.tudarmstadt.data.settings.SettingsDataSource
import de.akamu.tudarmstadt.interactors.SettingsInteractor
import org.json.JSONObject
class SettingsPresenter (var settingsInteractor: SettingsInteractor) {
fun fetchSettings(callback: SettingsDataSource.FetchSettingsCallback) {
settingsInteractor.fetchSettings(object : SettingsDataSource.FetchSettingsCallback {
override fun onFetchSettingsSuccess(settings: JSONObject) {
callback.onFetchSettingsSuccess(settings)
}
override fun onFetchSettingsFailed(reason: String) {
callback.onFetchSettingsFailed(reason)
}
})
}
}
\ No newline at end of file
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