Commit f50e924b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Merge branch 'develop-2.x' into notifications

parents 049de2ba 226e069b
......@@ -4,9 +4,12 @@ import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.*
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.registerPushToken
......@@ -16,11 +19,67 @@ import javax.inject.Inject
class LoginPresenter @Inject constructor(private val view: LoginView,
private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator,
private val localRepository: LocalRepository) {
@Inject lateinit var client: RocketChatClient
private val localRepository: LocalRepository,
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) {
val server = serverInteractor.get()
when {
server == null -> {
navigator.toServerScreen()
}
usernameOrEmail.isBlank() -> {
view.alertWrongUsernameOrEmail()
}
......@@ -37,14 +96,16 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
registerPushToken()
navigator.toChatList()
} catch (exception: RocketChatException) {
if (exception is RocketChatTwoFactorException) {
navigator.toTwoFA(usernameOrEmail, password)
} else {
val message = exception.message
if (message != null) {
view.showMessage(message)
} else {
view.showGenericErrorMessage()
when (exception) {
is RocketChatTwoFactorException -> {
navigator.toTwoFA(usernameOrEmail, password)
}
else -> {
exception.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
}
}
}
......@@ -66,7 +127,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
client.registerPushToken(it)
}
// TODO: Schedule push token registering when it comes up null
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.style.ClickableSpan
import android.view.*
import android.widget.ImageButton
import android.view.inputmethod.InputMethodManager
import android.widget.ScrollView
import android.widget.Toast
......@@ -27,7 +28,6 @@ class LoginFragment : Fragment(), LoginView {
@Inject lateinit var presenter: LoginPresenter
@Inject lateinit var appContext: Context // TODO we really need it? Check alternatives...
/*
private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener {
if (KeyboardHelper.isSoftKeyboardShown(scroll_view.rootView)) {
showSignUpView(false)
......@@ -42,7 +42,7 @@ class LoginFragment : Fragment(), LoginView {
}
}
private var isGlobalLayoutListenerSetUp = false
*/
companion object {
fun newInstance() = LoginFragment()
}
......@@ -67,29 +67,28 @@ class LoginFragment : Fragment(), LoginView {
tintEditTextDrawableStart()
}
presenter.setup()
showThreeSocialMethods()
button_log_in.setOnClickListener {
presenter.authenticate(text_username_or_email.textContent, text_password.textContent)
}
/*
// TODO: THIS IS A PRESENTER CONCERN - REMOVE THAT ! WE SHOULD GET THE SERVER SETTINGS!
// -------------------------------------------------------------------------------------------------------------------
showOauthView(true)
// Show the first three social account's ImageButton (REMARK: we must show at maximum *three* views)
enableLoginByFacebook()
enableLoginByGithub()
enableLoginByGoogle()
setupFabListener()
// Just an example: if the server allow the new users registration then show the respective interface.
setupSignUpListener()
showSignUpView(true)
// -------------------------------------------------------------------------------------------------------------------
*/
}
/*
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() {
super.onDestroyView()
if (isGlobalLayoutListenerSetUp) {
......@@ -97,58 +96,58 @@ class LoginFragment : Fragment(), LoginView {
isGlobalLayoutListenerSetUp = false
}
}
*/
override fun showOauthView(value: Boolean) {
// if (value) {
// social_accounts_container.setVisibility(true)
// button_fab.setVisibility(true)
//
// // We need to setup the layout to hide and show the oauth interface when the soft keyboard is shown
// // (means that the user touched the text_username_or_email or text_password EditText to fill that respective fields).
// if (!isGlobalLayoutListenerSetUp) {
// scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
// isGlobalLayoutListenerSetUp = true
// }
// } else {
// social_accounts_container.setVisibility(false)
// button_fab.setVisibility(false)
// }
if (value) {
social_accounts_container.setVisibility(true)
button_fab.setVisibility(true)
// We need to setup the layout to hide and show the oauth interface when the soft keyboard is shown
// (means that the user touched the text_username_or_email or text_password EditText to fill that respective fields).
if (!isGlobalLayoutListenerSetUp) {
scroll_view.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
isGlobalLayoutListenerSetUp = true
}
} else {
social_accounts_container.setVisibility(false)
button_fab.setVisibility(false)
}
}
override fun setupFabListener() {
// button_fab.setOnClickListener({
// button_fab.hide()
// showRemainingSocialAccountsView()
// scrollToBottom()
// })
button_fab.setOnClickListener({
button_fab.hide()
showRemainingSocialAccountsView()
scrollToBottom()
})
}
override fun enableLoginByFacebook() {
button_facebook.setVisibility(true)
button_facebook.isEnabled = true
}
override fun enableLoginByGithub() {
button_github.setVisibility(true)
button_github.isEnabled = true
}
override fun enableLoginByGoogle() {
button_google.setVisibility(true)
button_google.isEnabled = true
}
override fun enableLoginByLinkedin() {
button_linkedin.setVisibility(true)
button_linkedin.isEnabled = true
}
override fun enableLoginByMeteor() {
button_meteor.setVisibility(true)
button_meteor.isEnabled = true
}
override fun enableLoginByTwitter() {
button_twitter.setVisibility(true)
button_twitter.isEnabled = true
}
override fun enableLoginByGitlab() {
button_gitlab.setVisibility(true)
button_gitlab.isEnabled = true
}
override fun showSignUpView(value: Boolean) = text_new_to_rocket_chat.setVisibility(value)
......@@ -195,7 +194,7 @@ class LoginFragment : Fragment(), LoginView {
DrawableHelper.compoundDrawables(arrayOf(text_username_or_email, text_password), drawables)
}
}
/*
private fun showLoginButton(value: Boolean) {
button_log_in.setVisibility(value)
}
......@@ -212,26 +211,26 @@ class LoginFragment : Fragment(), LoginView {
TextHelper.addLink(text_new_to_rocket_chat, arrayOf(signUp), arrayOf(signUpListener))
}
*/
private fun enableUserInput(value: Boolean) {
button_log_in.isEnabled = value
text_username_or_email.isEnabled = value
text_password.isEnabled = value
// if (isEditTextEmpty()) {
// showSignUpView(value)
// showOauthView(value)
// }
if (isEditTextEmpty()) {
showSignUpView(value)
showOauthView(value)
}
}
/*
// Returns true if *all* EditTexts are empty.
private fun isEditTextEmpty(): Boolean = text_username_or_email.textContent.isBlank() && text_password.textContent.isEmpty()
private fun showRemainingSocialAccountsView() {
social_accounts_container.postDelayed({
enableLoginByLinkedin()
enableLoginByMeteor()
enableLoginByTwitter()
enableLoginByGitlab()
for (i in 0..social_accounts_container.childCount) {
val view = social_accounts_container.getChildAt(i) as? ImageButton ?: continue
if (view.isEnabled) view.visibility = View.VISIBLE
}
}, 1000)
}
......@@ -240,5 +239,4 @@ class LoginFragment : Fragment(), LoginView {
scroll_view.fullScroll(ScrollView.FOCUS_DOWN)
}, 1250)
}
*/
}
\ No newline at end of file
......@@ -3,31 +3,24 @@ package chat.rocket.android.authentication.presentation
import android.content.Context
import android.content.Intent
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.signup.ui.SignupFragment
import chat.rocket.android.authentication.twofactor.ui.TwoFAFragment
import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.chatrooms.ui.MainActivity
import chat.rocket.android.util.addFragmentBackStack
import chat.rocket.android.webview.webViewIntent
class AuthenticationNavigator(internal val activity: AuthenticationActivity, internal val context: Context) {
lateinit var server: String
lateinit var usernameOrEmail: String
lateinit var password: String
fun toLogin(server: String) {
this.server = server
fun toLogin() {
activity.addFragmentBackStack("loginFragment", R.id.fragment_container) {
LoginFragment.newInstance()
}
}
fun toTwoFA(usernameOrEmail: String, password: String) {
this.usernameOrEmail = usernameOrEmail
this.password = password
fun toTwoFA(username: String, password: String) {
activity.addFragmentBackStack("twoFAFragment", R.id.fragment_container) {
TwoFAFragment.newInstance()
TwoFAFragment.newInstance(username, password)
}
}
......@@ -37,16 +30,8 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity, int
}
}
fun toTermsOfService() {
val webPageUrl = server + "/terms-of-service" // TODO Move to UrlHelper
activity.startActivity(context.webViewIntent(webPageUrl))
activity.overridePendingTransition(R.anim.slide_up, R.anim.hold)
}
fun toPrivacyPolicy() {
val webPageUrl = server + "/privacy-policy" // TODO Move to UrlHelper
activity.startActivity(context.webViewIntent(webPageUrl))
activity.overridePendingTransition(R.anim.slide_up, R.anim.hold)
fun toWebPage(url: String) {
activity.startActivity(context.webViewIntent(url))
}
fun toChatList() {
......@@ -56,4 +41,8 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity, int
activity.startActivity(chatList)
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
import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
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.core.RocketChatClient
import chat.rocket.core.internal.rest.settings
import java.security.InvalidParameterException
import javax.inject.Inject
class ServerPresenter @Inject constructor(private val view: ServerView,
private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator) {
@Inject lateinit var client: RocketChatClient
private val navigator: AuthenticationNavigator,
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) {
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
var cli: RocketChatClient? = null
try {
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
//client.connect(server)
navigator.toLogin(server)
try {
val settings = client.settings(*settingsFilter)
settingsInteractor.save(server, settings)
serverInteractor.save(server)
view.hideLoading()
} else {
view.showNoInternetConnection()
navigator.toLogin()
} catch (ex: Exception) {
ex.printStackTrace()
view.showMessage(ex.message!!)
} finally {
view.hideLoading()
}
} else {
view.showNoInternetConnection()
}
}
}
}
......
......@@ -4,6 +4,8 @@ import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
......@@ -15,11 +17,18 @@ import javax.inject.Inject
class SignupPresenter @Inject constructor(private val view: SignupView,
private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator,
private val localRepository: LocalRepository) {
@Inject lateinit var client: RocketChatClient
private val localRepository: LocalRepository,
private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
private val client: RocketChatClient = factory.create(serverInteractor.get()!!)
fun signup(name: String, username: String, password: String, email: String) {
val server = serverInteractor.get()
when {
server == null -> {
navigator.toServerScreen()
}
name.isBlank() -> {
view.alertBlankName()
}
......@@ -33,6 +42,7 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
view.alertBlankEmail()
}
else -> {
val client = factory.create(server)
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
......@@ -61,11 +71,15 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
}
fun termsOfService() {
navigator.toTermsOfService()
serverInteractor.get()?.let {
navigator.toWebPage("/terms-of-service")
}
}
fun privacyPolicy() {
navigator.toPrivacyPolicy()
serverInteractor.get()?.let {
navigator.toWebPage("/privacy-policy")
}
}
private suspend fun registerPushToken() {
......
......@@ -4,6 +4,8 @@ import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatAuthException
import chat.rocket.common.RocketChatException
......@@ -15,19 +17,29 @@ import javax.inject.Inject
class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator,
private val localRepository: LocalRepository) {
@Inject lateinit var client: RocketChatClient
private val localRepository: LocalRepository,
private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
fun authenticate(twoFactorAuthenticationCode: String) {
private val client: RocketChatClient = factory.create(serverInteractor.get()!!)
// 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) {
val server = serverInteractor.get()
if (twoFactorAuthenticationCode.isBlank()) {
view.alertBlankTwoFactorAuthenticationCode()
} else if (server == null) {
navigator.toServerScreen()
} else {
launchUI(strategy) {
val client = factory.create(server)
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
try {
client.login(navigator.usernameOrEmail, navigator.password, twoFactorAuthenticationCode) // TODO This function returns a user token so should we save it?
// The token is saved via the client TokenProvider
client.login(usernameOrEmail, password, twoFactorAuthenticationCode)
registerPushToken()
navigator.toChatList()
} catch (exception: RocketChatException) {
......@@ -42,7 +54,6 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
}
}
}
view.hideLoading()
} else {
view.showNoInternetConnection()
......
......@@ -22,15 +22,29 @@ import javax.inject.Inject
class TwoFAFragment : Fragment(), TwoFAView {
@Inject lateinit var presenter: TwoFAPresenter
@Inject lateinit var appContext: Context // TODO we really need it? Check alternatives...
lateinit var username: String
lateinit var password: String
// TODO - we could create an in memory repository to save username and password.
companion object {
fun newInstance() = TwoFAFragment()
private const val USERNAME = "username"
private const val PASSWORD = "password"
fun newInstance(username: String, password: String) = TwoFAFragment().apply {
arguments = Bundle(1).apply {
putString(USERNAME, username)
putString(PASSWORD, password)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
// TODO - research a better way to initialize parameters on fragments.
username = arguments?.getString(USERNAME) ?: ""
password = arguments?.getString(PASSWORD) ?: ""
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_two_fa, container, false)
......@@ -51,8 +65,10 @@ class TwoFAFragment : Fragment(), TwoFAView {
}
override fun alertBlankTwoFactorAuthenticationCode() {
AnimationHelper.vibrateSmartPhone(appContext)
AnimationHelper.shakeView(text_two_factor_auth)
activity?.let {
AnimationHelper.vibrateSmartPhone(it)
AnimationHelper.shakeView(text_two_factor_auth)
}
}
override fun alertInvalidTwoFactorAuthenticationCode() = showMessage(getString(R.string.msg_invalid_2fa_code))
......@@ -89,7 +105,7 @@ class TwoFAFragment : Fragment(), TwoFAView {
private fun setupOnClickListener() {
button_log_in.setOnClickListener {
presenter.authenticate(text_two_factor_auth.textContent)
presenter.authenticate(username, password, text_two_factor_auth.textContent)
}
}
}
\ No newline at end of file
package chat.rocket.android.chatrooms.presentation
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.core.RocketChatClient
import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.model.ChatRoom
import javax.inject.Inject
class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, private val strategy: CancelStrategy) {
@Inject lateinit var client: RocketChatClient
class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
private val strategy: CancelStrategy,
private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory) {
lateinit var client: RocketChatClient
fun chatRooms() {
// TODO - check for current server
client = factory.create(serverInteractor.get()!!)
launchUI(strategy) {
view.showLoading()
val chatRooms = client.chatRooms().update
......
......@@ -5,12 +5,15 @@ import android.arch.persistence.room.Room
import android.content.Context
import android.content.SharedPreferences
import chat.rocket.android.BuildConfig
import chat.rocket.android.app.RocketChatApplication
import chat.rocket.android.app.RocketChatDatabase
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPreferencesRepository
import chat.rocket.android.infrastructure.SharedPrefsLocalRepository
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.SharedPrefsCurrentServerRepository
import chat.rocket.android.util.TimberLogger
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
......@@ -32,7 +35,6 @@ class AppModule {
platformLogger = logger
// TODO remove
// TODO: From where should we get the url here?
restUrl = "https://open.rocket.chat"
}
}
......@@ -89,14 +91,25 @@ class AppModule {
}
@Provides
@Singleton
fun provideSharedPreferences(context: Context): SharedPreferences {
fun provideSharedPreferences(context: Application): SharedPreferences {
return context.getSharedPreferences("rocket.chat", Context.MODE_PRIVATE)
}
@Provides
@Singleton
fun provideSharedPreferencesRepository(preferences: SharedPreferences): LocalRepository {
return SharedPreferencesRepository(preferences)
fun provideSharedPreferencesRepository(prefs: SharedPreferences): LocalRepository {
return SharedPrefsLocalRepository(prefs)
}
@Provides
@Singleton
fun provideCurrentServerRepository(prefs: SharedPreferences): CurrentServerRepository {
return SharedPrefsCurrentServerRepository(prefs)
}
@Provides
@Singleton
fun provideSettingsRepository(): SettingsRepository {
return MemorySettingsRepository()
}
}
package chat.rocket.android.helper
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.run
import kotlinx.coroutines.experimental.withContext
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
......@@ -15,7 +15,7 @@ object NetworkHelper {
*
* @return true if there is internet access, false otherwise.
*/
suspend fun hasInternetAccess(): Boolean = run(CommonPool) {
suspend fun hasInternetAccess(): Boolean = withContext(CommonPool) {
try {
val socket = Socket()
val inetSocketAddress = InetSocketAddress("8.8.8.8", 53)
......
......@@ -2,7 +2,7 @@ package chat.rocket.android.infrastructure
import android.content.SharedPreferences
class SharedPreferencesRepository(private val preferences: SharedPreferences) : LocalRepository {
class SharedPrefsLocalRepository(private val preferences: SharedPreferences) : LocalRepository {
override fun save(key: String, value: String?) {
preferences.edit().putString(key, value).apply()
......
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
import android.arch.persistence.room.Index
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(
@PrimaryKey(autoGenerate = true)
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