Commit f8419f5c authored by Lucio Maciel's avatar Lucio Maciel

Use server settings on login screen

- in memory repository for settings for now (will move to DB)
- repository and interactors for Current server
parent aeb928ad
...@@ -3,6 +3,8 @@ package chat.rocket.android.authentication.login.presentation ...@@ -3,6 +3,8 @@ package chat.rocket.android.authentication.login.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.server.domain.*
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException import chat.rocket.common.RocketChatTwoFactorException
...@@ -13,11 +15,67 @@ import javax.inject.Inject ...@@ -13,11 +15,67 @@ import javax.inject.Inject
class LoginPresenter @Inject constructor(private val view: LoginView, class LoginPresenter @Inject constructor(private val view: LoginView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator) { private val navigator: AuthenticationNavigator,
@Inject lateinit var client: RocketChatClient private val settingsInteractor: GetSettingsInteractor,
private val serverInteractor: GetCurrentServerInteractor,
factory: RocketChatClientFactory) {
// TODO - we should validate the current server when opening the app, and have a nonnull get()
private val client: RocketChatClient = factory.create(serverInteractor.get()!!)
fun setup() {
val server = serverInteractor.get()
if (server == null) {
navigator.toServerScreen()
return
}
val settings = settingsInteractor.get(server)
if (settings == null) {
navigator.toServerScreen()
return
}
var hasSocial = false
if (settings.facebookEnabled()) {
view.enableLoginByFacebook()
hasSocial = true
}
if (settings.githubEnabled()) {
view.enableLoginByGithub()
hasSocial = true
}
if (settings.googleEnabled()) {
view.enableLoginByGoogle()
hasSocial = true
}
if (settings.linkedinEnabled()) {
view.enableLoginByLinkedin()
hasSocial = true
}
if (settings.meteorEnabled()) {
view.enableLoginByMeteor()
hasSocial = true
}
if (settings.twitterEnabled()) {
view.enableLoginByTwitter()
hasSocial = true
}
if (settings.gitlabEnabled()) {
view.enableLoginByGitlab()
hasSocial = true
}
view.showSignUpView(settings.registrationEnabled())
view.showOauthView(hasSocial)
}
fun authenticate(usernameOrEmail: String, password: String) { fun authenticate(usernameOrEmail: String, password: String) {
val server = serverInteractor.get()
when { when {
server == null -> {
navigator.toServerScreen()
}
usernameOrEmail.isBlank() -> { usernameOrEmail.isBlank() -> {
view.alertWrongUsernameOrEmail() view.alertWrongUsernameOrEmail()
} }
...@@ -35,7 +93,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -35,7 +93,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
} catch (rocketChatException: RocketChatException) { } catch (rocketChatException: RocketChatException) {
when (rocketChatException) { when (rocketChatException) {
is RocketChatTwoFactorException -> { is RocketChatTwoFactorException -> {
navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password) navigator.toTwoFA(usernameOrEmail, password)
} }
else -> { else -> {
val errorMessage = rocketChatException.message val errorMessage = rocketChatException.message
...@@ -56,6 +114,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -56,6 +114,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
} }
fun signup() { fun signup() {
navigator.toSignUp(navigator.currentServer!!) // TODO - remove the server parameter here, signup screen should use the interactor too
navigator.toSignUp()
} }
} }
\ No newline at end of file
...@@ -7,6 +7,7 @@ import android.os.Bundle ...@@ -7,6 +7,7 @@ import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.text.style.ClickableSpan import android.text.style.ClickableSpan
import android.view.* import android.view.*
import android.widget.ImageButton
import android.widget.ScrollView import android.widget.ScrollView
import android.widget.Toast import android.widget.Toast
import chat.rocket.android.R import chat.rocket.android.R
...@@ -41,24 +42,12 @@ class LoginFragment : Fragment(), LoginView { ...@@ -41,24 +42,12 @@ class LoginFragment : Fragment(), LoginView {
private var isGlobalLayoutListenerSetUp = false private var isGlobalLayoutListenerSetUp = false
companion object { companion object {
private const val SERVER_URL = "server_url" fun newInstance() = LoginFragment()
fun newInstance(url: String) = LoginFragment().apply {
arguments = Bundle(1).apply {
putString(SERVER_URL, url)
}
}
} }
// Todo remove
private lateinit var serverUrl: String
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this) AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// TODO - research a better way to initialize parameters on fragments.
serverUrl = arguments?.getString(SERVER_URL) ?: "https://open.rocket.chat"
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_log_in, container, false) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_log_in, container, false)
...@@ -72,14 +61,8 @@ class LoginFragment : Fragment(), LoginView { ...@@ -72,14 +61,8 @@ class LoginFragment : Fragment(), LoginView {
tintEditTextDrawableStart() tintEditTextDrawableStart()
} }
// TODO: THIS IS A PRESENTER CONCERN - REMOVE THAT ! presenter.setup()
// ------------------------------------------------------------------------------------------------------------------- showThreeSocialMethods()
showOauthView(true)
// Show the first three social account's ImageButton (REMARK: we must show at maximum *three* views)
enableLoginByFacebook()
enableLoginByGithub()
enableLoginByGoogle()
setupFabListener() setupFabListener()
...@@ -91,6 +74,17 @@ class LoginFragment : Fragment(), LoginView { ...@@ -91,6 +74,17 @@ class LoginFragment : Fragment(), LoginView {
button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email.textContent, text_password.textContent) } button_log_in.setOnClickListener { presenter.authenticate(text_username_or_email.textContent, text_password.textContent) }
} }
private fun showThreeSocialMethods() {
var count = 0
for (i in 0..social_accounts_container.childCount) {
val view = social_accounts_container.getChildAt(i) as? ImageButton ?: continue
if (view.isEnabled && count < 3) {
view.visibility = View.VISIBLE
count++
}
}
}
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
if (isGlobalLayoutListenerSetUp) { if (isGlobalLayoutListenerSetUp) {
...@@ -125,31 +119,31 @@ class LoginFragment : Fragment(), LoginView { ...@@ -125,31 +119,31 @@ class LoginFragment : Fragment(), LoginView {
} }
override fun enableLoginByFacebook() { override fun enableLoginByFacebook() {
button_facebook.setVisibility(true) button_facebook.isEnabled = true
} }
override fun enableLoginByGithub() { override fun enableLoginByGithub() {
button_github.setVisibility(true) button_github.isEnabled = true
} }
override fun enableLoginByGoogle() { override fun enableLoginByGoogle() {
button_google.setVisibility(true) button_google.isEnabled = true
} }
override fun enableLoginByLinkedin() { override fun enableLoginByLinkedin() {
button_linkedin.setVisibility(true) button_linkedin.isEnabled = true
} }
override fun enableLoginByMeteor() { override fun enableLoginByMeteor() {
button_meteor.setVisibility(true) button_meteor.isEnabled = true
} }
override fun enableLoginByTwitter() { override fun enableLoginByTwitter() {
button_twitter.setVisibility(true) button_twitter.isEnabled = true
} }
override fun enableLoginByGitlab() { override fun enableLoginByGitlab() {
button_gitlab.setVisibility(true) button_gitlab.isEnabled = true
} }
override fun showSignUpView(value: Boolean) { override fun showSignUpView(value: Boolean) {
...@@ -228,10 +222,10 @@ class LoginFragment : Fragment(), LoginView { ...@@ -228,10 +222,10 @@ class LoginFragment : Fragment(), LoginView {
private fun showRemainingSocialAccountsView() { private fun showRemainingSocialAccountsView() {
social_accounts_container.postDelayed({ social_accounts_container.postDelayed({
enableLoginByLinkedin() for (i in 0..social_accounts_container.childCount) {
enableLoginByMeteor() val view = social_accounts_container.getChildAt(i) as? ImageButton ?: continue
enableLoginByTwitter() if (view.isEnabled) view.visibility = View.VISIBLE
enableLoginByGitlab() }
}, 1000) }, 1000)
} }
......
...@@ -3,46 +3,35 @@ package chat.rocket.android.authentication.presentation ...@@ -3,46 +3,35 @@ package chat.rocket.android.authentication.presentation
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.chatrooms.ui.MainActivity
import chat.rocket.android.authentication.login.ui.LoginFragment import chat.rocket.android.authentication.login.ui.LoginFragment
import chat.rocket.android.authentication.signup.ui.SignupFragment import chat.rocket.android.authentication.signup.ui.SignupFragment
import chat.rocket.android.authentication.twofactor.ui.TwoFAFragment import chat.rocket.android.authentication.twofactor.ui.TwoFAFragment
import chat.rocket.android.authentication.ui.AuthenticationActivity import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.chatrooms.ui.MainActivity
import chat.rocket.android.util.addFragmentBackStack import chat.rocket.android.util.addFragmentBackStack
import chat.rocket.android.webview.webViewIntent import chat.rocket.android.webview.webViewIntent
class AuthenticationNavigator(internal val activity: AuthenticationActivity, internal val context: Context) { class AuthenticationNavigator(internal val activity: AuthenticationActivity, internal val context: Context) {
var currentServer: String? = null fun toLogin() {
fun toLogin(server: String) {
currentServer = server
activity.addFragmentBackStack("loginFragment", R.id.fragment_container) { activity.addFragmentBackStack("loginFragment", R.id.fragment_container) {
LoginFragment.newInstance(server) LoginFragment.newInstance()
} }
} }
fun toTwoFA(server: String, username: String, password: String) { fun toTwoFA(username: String, password: String) {
currentServer = server
activity.addFragmentBackStack("twoFAFragment", R.id.fragment_container) { activity.addFragmentBackStack("twoFAFragment", R.id.fragment_container) {
TwoFAFragment.newInstance(server, username, password) TwoFAFragment.newInstance(username, password)
} }
} }
fun toSignUp(server: String) { fun toSignUp() {
currentServer = server
activity.addFragmentBackStack("signupFragment", R.id.fragment_container) { activity.addFragmentBackStack("signupFragment", R.id.fragment_container) {
SignupFragment.newInstance(server) SignupFragment.newInstance()
} }
} }
fun toTermsOfService() { fun toWebPage(url: String) {
val webPageUrl = currentServer + "/terms-of-service" activity.startActivity(context.webViewIntent(url))
activity.startActivity(context.webViewIntent(webPageUrl))
}
fun toPrivacyPolicy() {
val webPageUrl = currentServer + "/privacy-policy"
activity.startActivity(context.webViewIntent(webPageUrl))
} }
fun toChatList() { fun toChatList() {
...@@ -52,4 +41,8 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity, int ...@@ -52,4 +41,8 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity, int
activity.startActivity(chatList) activity.startActivity(chatList)
activity.finish() activity.finish()
} }
fun toServerScreen() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
} }
...@@ -3,27 +3,56 @@ package chat.rocket.android.authentication.server.presentation ...@@ -3,27 +3,56 @@ package chat.rocket.android.authentication.server.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.server.domain.*
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.settings
import java.security.InvalidParameterException
import javax.inject.Inject import javax.inject.Inject
class ServerPresenter @Inject constructor(private val view: ServerView, class ServerPresenter @Inject constructor(private val view: ServerView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator) { private val navigator: AuthenticationNavigator,
@Inject lateinit var client: RocketChatClient private val serverInteractor: SaveCurrentServerInteractor,
private val settingsInteractor: SaveSettingsInteractor,
private val factory: RocketChatClientFactory) {
private var settingsFilter = arrayOf(SITE_URL, SITE_NAME,
FAVICON_512, USE_REALNAME, ALLOW_ROOM_NAME_SPECIAL_CHARS, FAVORITE_ROOMS,
ACCOUNT_LOGIN_FORM, ACCOUNT_GOOGLE, ACCOUNT_FACEBOOK, ACCOUNT_GITHUB, ACCOUNT_GITLAB,
ACCOUNT_LINKEDIN, ACCOUNT_METEOR, ACCOUNT_TWITTER, ACCOUNT_WORDPRESS, LDAP_ENABLE,
ACCOUNT_REGISTRATION, STORAGE_TYPE, HIDE_USER_JOIN, HIDE_USER_LEAVE, HIDE_TYPE_AU,
HIDE_MUTE_UNMUTE, HIDE_TYPE_RU, ACCOUNT_CUSTOM_FIELDS)
fun connect(server: String) { fun connect(server: String) {
launchUI(strategy) { var cli: RocketChatClient? = null
if (NetworkHelper.hasInternetAccess()) { try {
view.showLoading() cli = factory.create(server)
} catch (ex: InvalidParameterException) {
view.showMessage(ex.message!!)
}
cli?.let { client ->
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
// TODO - validate server URL and get server settings and info before going to Login screen try {
// client.connect(server) val settings = client.settings(*settingsFilter)
settingsInteractor.save(server, settings)
serverInteractor.save(server)
view.hideLoading() navigator.toLogin()
navigator.toLogin(server) } catch (ex: Exception) {
} else { ex.printStackTrace()
view.showNoInternetConnection() view.showMessage(ex.message!!)
} finally {
view.hideLoading()
}
} else {
view.showNoInternetConnection()
}
} }
} }
} }
......
...@@ -3,9 +3,10 @@ package chat.rocket.android.authentication.signup.presentation ...@@ -3,9 +3,10 @@ package chat.rocket.android.authentication.signup.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.signup import chat.rocket.core.internal.rest.signup
import timber.log.Timber import timber.log.Timber
...@@ -13,11 +14,16 @@ import javax.inject.Inject ...@@ -13,11 +14,16 @@ import javax.inject.Inject
class SignupPresenter @Inject constructor(private val view: SignupView, class SignupPresenter @Inject constructor(private val view: SignupView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator) { private val navigator: AuthenticationNavigator,
@Inject lateinit var client: RocketChatClient private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
fun signup(name: String, username: String, password: String, email: String) { fun signup(name: String, username: String, password: String, email: String) {
val server = serverInteractor.get()
when { when {
server == null -> {
navigator.toServerScreen()
}
name.isBlank() -> { name.isBlank() -> {
view.alertBlankName() view.alertBlankName()
} }
...@@ -31,6 +37,7 @@ class SignupPresenter @Inject constructor(private val view: SignupView, ...@@ -31,6 +37,7 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
view.alertBlankEmail() view.alertBlankEmail()
} }
else -> { else -> {
val client = factory.create(server)
launchUI(strategy) { launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) { if (NetworkHelper.hasInternetAccess()) {
view.showLoading() view.showLoading()
...@@ -59,10 +66,14 @@ class SignupPresenter @Inject constructor(private val view: SignupView, ...@@ -59,10 +66,14 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
} }
fun termsOfService() { fun termsOfService() {
navigator.toTermsOfService() serverInteractor.get()?.let {
navigator.toWebPage("/terms-of-service")
}
} }
fun privacyPolicy() { fun privacyPolicy() {
navigator.toPrivacyPolicy() serverInteractor.get()?.let {
navigator.toWebPage("/privacy-policy")
}
} }
} }
\ No newline at end of file
...@@ -32,25 +32,14 @@ class SignupFragment : Fragment(), SignupView { ...@@ -32,25 +32,14 @@ class SignupFragment : Fragment(), SignupView {
} }
} }
// TODO delete
lateinit var serverUrl: String
companion object { companion object {
private const val SERVER_URL = "server_url" fun newInstance() = SignupFragment()
fun newInstance(url: String) = SignupFragment().apply {
arguments = Bundle(1).apply {
putString(SERVER_URL, url)
}
}
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this) AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// TODO - research a better way to initialize parameters on fragments.
serverUrl = arguments?.getString(SERVER_URL) ?: "https://open.rocket.chat"
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_sign_up, container, false) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_sign_up, container, false)
......
...@@ -2,24 +2,30 @@ package chat.rocket.android.authentication.twofactor.presentation ...@@ -2,24 +2,30 @@ package chat.rocket.android.authentication.twofactor.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
class TwoFAPresenter @Inject constructor(private val view: TwoFAView, class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator) { private val navigator: AuthenticationNavigator,
@Inject lateinit var client: RocketChatClient private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
// TODO: If the usernameOrEmail and password was informed by the user on the previous screen, then we should pass only the pin, like this: fun authenticate(pin: EditText) // TODO: If the usernameOrEmail and password was informed by the user on the previous screen, then we should pass only the pin, like this: fun authenticate(pin: EditText)
fun authenticate(usernameOrEmail: String, password: String, twoFactorAuthenticationCode: String) { fun authenticate(usernameOrEmail: String, password: String, twoFactorAuthenticationCode: String) {
val server = serverInteractor.get()
if (twoFactorAuthenticationCode.isBlank()) { if (twoFactorAuthenticationCode.isBlank()) {
view.alertBlankTwoFactorAuthenticationCode() view.alertBlankTwoFactorAuthenticationCode()
} else if (server == null) {
navigator.toServerScreen()
} else { } else {
launchUI(strategy) { launchUI(strategy) {
val client = factory.create(server)
view.showLoading() view.showLoading()
try { try {
val token = client.login(usernameOrEmail, password, twoFactorAuthenticationCode) val token = client.login(usernameOrEmail, password, twoFactorAuthenticationCode)
...@@ -39,6 +45,6 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView, ...@@ -39,6 +45,6 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
} }
fun signup() { fun signup() {
navigator.toSignUp(navigator.currentServer!!) navigator.toSignUp()
} }
} }
\ No newline at end of file
...@@ -22,19 +22,16 @@ import javax.inject.Inject ...@@ -22,19 +22,16 @@ import javax.inject.Inject
class TwoFAFragment : Fragment(), TwoFAView { class TwoFAFragment : Fragment(), TwoFAView {
@Inject lateinit var presenter: TwoFAPresenter @Inject lateinit var presenter: TwoFAPresenter
@Inject lateinit var appContext: Context @Inject lateinit var appContext: Context
lateinit var serverUrl: String
lateinit var username: String lateinit var username: String
lateinit var password: String lateinit var password: String
companion object { companion object {
private const val SERVER_URL = "server_url"
private const val USERNAME = "username" private const val USERNAME = "username"
private const val PASSWORD = "password" private const val PASSWORD = "password"
fun newInstance(url: String, username: String, password: String) = TwoFAFragment().apply { fun newInstance(username: String, password: String) = TwoFAFragment().apply {
arguments = Bundle(1).apply { arguments = Bundle(1).apply {
putString(SERVER_URL, url)
putString(USERNAME, username) putString(USERNAME, username)
putString(PASSWORD, password) putString(PASSWORD, password)
} }
...@@ -46,7 +43,6 @@ class TwoFAFragment : Fragment(), TwoFAView { ...@@ -46,7 +43,6 @@ class TwoFAFragment : Fragment(), TwoFAView {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// TODO - research a better way to initialize parameters on fragments. // TODO - research a better way to initialize parameters on fragments.
serverUrl = arguments?.getString(SERVER_URL) ?: "https://open.rocket.chat"
username = arguments?.getString(USERNAME) ?: "" username = arguments?.getString(USERNAME) ?: ""
password = arguments?.getString(PASSWORD) ?: "" password = arguments?.getString(PASSWORD) ?: ""
} }
......
package chat.rocket.android.chatrooms.presentation package chat.rocket.android.chatrooms.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.chatRooms import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.model.ChatRoom import chat.rocket.core.model.ChatRoom
import javax.inject.Inject import javax.inject.Inject
class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, private val strategy: CancelStrategy) { class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
@Inject lateinit var client: RocketChatClient private val strategy: CancelStrategy,
private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
lateinit var client: RocketChatClient
fun chatRooms() { fun chatRooms() {
// TODO - check for current server
client = factory.create(serverInteractor.get()!!)
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
val chatRooms = client.chatRooms().update val chatRooms = client.chatRooms().update
......
...@@ -30,7 +30,7 @@ class ChatRoomsAdapter(private var dataSet: MutableList<ChatRoom>, private val c ...@@ -30,7 +30,7 @@ class ChatRoomsAdapter(private var dataSet: MutableList<ChatRoom>, private val c
if (chatRoom.type == RoomType.ONE_TO_ONE) { if (chatRoom.type == RoomType.ONE_TO_ONE) {
// TODO Check the best way to get the current server url. // TODO Check the best way to get the current server url.
val canonicalUrl = chatRoom.client.restUrl.toString() val canonicalUrl = chatRoom.client.url
holder.userAvatar.setImageURI(UrlHelper.getAvatarUrl(canonicalUrl, chatRoomName)) holder.userAvatar.setImageURI(UrlHelper.getAvatarUrl(canonicalUrl, chatRoomName))
holder.userAvatar.setVisibility(true) holder.userAvatar.setVisibility(true)
} else { } else {
......
...@@ -3,16 +3,20 @@ package chat.rocket.android.dagger.module ...@@ -3,16 +3,20 @@ package chat.rocket.android.dagger.module
import android.app.Application import android.app.Application
import android.arch.persistence.room.Room import android.arch.persistence.room.Room
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import chat.rocket.android.BuildConfig import chat.rocket.android.BuildConfig
import chat.rocket.android.app.RocketChatDatabase import chat.rocket.android.app.RocketChatDatabase
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.server.domain.CurrentServerRepository
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.infraestructure.MemorySettingsRepository
import chat.rocket.android.server.infraestructure.ServerDao import chat.rocket.android.server.infraestructure.ServerDao
import chat.rocket.android.server.infraestructure.SharedPrefsCurrentServerRepository
import chat.rocket.android.util.TimberLogger import chat.rocket.android.util.TimberLogger
import chat.rocket.common.util.PlatformLogger import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import okhttp3.HttpUrl
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton import javax.inject.Singleton
...@@ -29,8 +33,7 @@ class AppModule { ...@@ -29,8 +33,7 @@ class AppModule {
platformLogger = logger platformLogger = logger
// TODO remove // TODO remove
restUrl = HttpUrl.parse("https://open.rocket.chat")!! restUrl = "https://open.rocket.chat"
websocketUrl = "https://open.rocket.chat"
} }
} }
...@@ -84,4 +87,21 @@ class AppModule { ...@@ -84,4 +87,21 @@ class AppModule {
fun providePlatformLogger(): PlatformLogger { fun providePlatformLogger(): PlatformLogger {
return TimberLogger return TimberLogger
} }
@Provides
fun provideSharedPreferences(context: Application): SharedPreferences {
return context.getSharedPreferences("rocket.chat", Context.MODE_PRIVATE)
}
@Provides
@Singleton
fun provideCurrentServerRepository(prefs: SharedPreferences): CurrentServerRepository {
return SharedPrefsCurrentServerRepository(prefs)
}
@Provides
@Singleton
fun provideSettingsRepository(): SettingsRepository {
return MemorySettingsRepository()
}
} }
package chat.rocket.android.helper package chat.rocket.android.helper
import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.run import kotlinx.coroutines.experimental.withContext
import java.io.IOException import java.io.IOException
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.Socket import java.net.Socket
...@@ -15,7 +15,7 @@ object NetworkHelper { ...@@ -15,7 +15,7 @@ object NetworkHelper {
* *
* @return true if there is internet access, false otherwise. * @return true if there is internet access, false otherwise.
*/ */
suspend fun hasInternetAccess(): Boolean = run(CommonPool) { suspend fun hasInternetAccess(): Boolean = withContext(CommonPool) {
try { try {
val socket = Socket() val socket = Socket()
val inetSocketAddress = InetSocketAddress("8.8.8.8", 53) val inetSocketAddress = InetSocketAddress("8.8.8.8", 53)
......
package chat.rocket.android.server.domain
interface CurrentServerRepository {
fun save(url: String)
fun get(): String?
}
\ No newline at end of file
package chat.rocket.android.server.domain
import javax.inject.Inject
class GetCurrentServerInteractor @Inject constructor(private val repository: CurrentServerRepository) {
fun get(): String? = repository.get()
}
\ No newline at end of file
package chat.rocket.android.server.domain
import javax.inject.Inject
class GetSettingsInteractor @Inject constructor(private val repository: SettingsRepository) {
fun get(url: String) = repository.get(url)
}
\ No newline at end of file
package chat.rocket.android.server.domain
import javax.inject.Inject
class SaveCurrentServerInteractor @Inject constructor(private val repository: CurrentServerRepository) {
fun save(url: String) = repository.save(url)
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.core.model.Value
import javax.inject.Inject
class SaveSettingsInteractor @Inject constructor(private val repository: SettingsRepository) {
fun save(url: String, settings: Map<String, Value<Any>>) = repository.save(url, settings)
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.core.model.Value
interface SettingsRepository {
fun save(url: String, settings: Map<String, Value<Any>>)
fun get(url: String): Map<String, Value<Any>>?
}
fun Map<String, Value<Any>>.googleEnabled(): Boolean = (this[ACCOUNT_GOOGLE] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.facebookEnabled(): Boolean = (this[ACCOUNT_FACEBOOK] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.githubEnabled(): Boolean = (this[ACCOUNT_GITHUB] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.linkedinEnabled(): Boolean = (this[ACCOUNT_LINKEDIN] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.meteorEnabled(): Boolean = (this[ACCOUNT_METEOR] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.twitterEnabled(): Boolean = (this[ACCOUNT_TWITTER] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.gitlabEnabled(): Boolean = (this[ACCOUNT_GITLAB] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.wordpressEnabled(): Boolean = (this[ACCOUNT_WORDPRESS] as Value<Boolean>?)?.value == true
fun Map<String, Value<Any>>.registrationEnabled(): Boolean {
val value = this[ACCOUNT_REGISTRATION] as Value<String>?
return value?.value == "Public"
}
const val ACCOUNT_FACEBOOK = "Accounts_OAuth_Facebook"
const val ACCOUNT_GITHUB = "Accounts_OAuth_Github"
const val ACCOUNT_GITLAB = "Accounts_OAuth_Gitlab"
const val ACCOUNT_GOOGLE = "Accounts_OAuth_Google"
const val ACCOUNT_LINKEDIN = "Accounts_OAuth_Linkedin"
const val ACCOUNT_METEOR = "Accounts_OAuth_Meteor"
const val ACCOUNT_TWITTER = "Accounts_OAuth_Twitter"
const val ACCOUNT_WORDPRESS = "Accounts_OAuth_Wordpress"
const val ACCOUNT_REGISTRATION = "Accounts_RegistrationForm"
const val ACCOUNT_LOGIN_FORM = "Accounts_ShowFormLogin"
const val ACCOUNT_CUSTOM_FIELDS = "Accounts_CustomFields"
const val SITE_URL = "Site_Url"
const val SITE_NAME = "Site_Name"
const val FAVICON_512 = "Assets_favicon_512"
const val USE_REALNAME = "UI_Use_Real_Name"
const val ALLOW_ROOM_NAME_SPECIAL_CHARS = "UI_Allow_room_names_with_special_chars"
const val FAVORITE_ROOMS = "Favorite_Rooms"
const val LDAP_ENABLE = "LDAP_Enable"
const val STORAGE_TYPE = "FileUpload_Storage_Type"
const val HIDE_USER_JOIN = "Message_HideType_uj"
const val HIDE_USER_LEAVE = "Message_HideType_ul"
const val HIDE_TYPE_AU = "Message_HideType_au"
const val HIDE_TYPE_RU = "Message_HideType_ru"
const val HIDE_MUTE_UNMUTE = "Message_HideType_mute_unmute"
\ No newline at end of file
package chat.rocket.android.server.infraestructure
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.core.model.Value
class MemorySettingsRepository : SettingsRepository {
val cache = HashMap<String, Map<String, Value<Any>>>()
override fun save(url: String, settings: Map<String, Value<Any>>) {
cache.put(url, settings)
}
override fun get(url: String): Map<String, Value<Any>>? {
return cache[url]
}
}
\ No newline at end of file
package chat.rocket.android.server.infraestructure
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import okhttp3.OkHttpClient
import javax.inject.Inject
class RocketChatClientFactory @Inject constructor(val okHttpClient: OkHttpClient,
val repository: AuthTokenRepository,
val logger: PlatformLogger) {
private val cache = HashMap<String, RocketChatClient>()
fun create(url: String): RocketChatClient {
cache[url]?.let {
return it
}
val client = RocketChatClient.create {
httpClient = okHttpClient
restUrl = url
tokenRepository = repository
platformLogger = logger
}
cache.put(url, client)
return client
}
}
\ No newline at end of file
...@@ -4,7 +4,7 @@ import android.arch.persistence.room.Entity ...@@ -4,7 +4,7 @@ import android.arch.persistence.room.Entity
import android.arch.persistence.room.Index import android.arch.persistence.room.Index
import android.arch.persistence.room.PrimaryKey import android.arch.persistence.room.PrimaryKey
@Entity(tableName = "server", indices = arrayOf(Index(value = "host", unique = true))) @Entity(tableName = "server", indices = [(Index(value = ["host"], unique = true))])
data class ServerEntity( data class ServerEntity(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val id: Long, val id: Long,
......
package chat.rocket.android.server.infraestructure
import android.content.SharedPreferences
import chat.rocket.android.server.domain.CurrentServerRepository
class SharedPrefsCurrentServerRepository(private val preferences: SharedPreferences) : CurrentServerRepository {
private val CURRENT_SERVER = "current_server"
override fun save(url: String) {
preferences.edit().putString(CURRENT_SERVER, url).apply()
}
override fun get(): String? {
return preferences.getString(CURRENT_SERVER, null)
}
}
\ 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