Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.anko.design.snackbar
import org.koin.androidx.viewmodel.ext.android.viewModel

const val PLAY_STORE_BUILD_FLAVOR = "playStore"
const val FDROID_BUILD_FLAVOR = "fdroid"

class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.fossasia.openevent.general.di

import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.Response
import org.fossasia.openevent.general.BuildConfig
import org.fossasia.openevent.general.data.Preference
import org.fossasia.openevent.general.settings.API_URL

class HostSelectionInterceptor(private val preference: Preference) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
var original = chain.request()
val httpUrl = preference.getString(API_URL)?.toHttpUrlOrNull()
if (original.url.host == BuildConfig.DEFAULT_BASE_URL.toHttpUrlOrNull()?.host && httpUrl != null) {
val newUrl =
original.url.newBuilder()
.scheme(httpUrl.scheme)
.host(httpUrl.host)
.port(httpUrl.port)
.build()
original = original.newBuilder()
.url(newUrl)
.build()
}
return chain.proceed(original)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ val viewModelModule = module {
viewModel { AboutEventViewModel(get(), get()) }
viewModel { EventFAQViewModel(get(), get()) }
viewModel { FavoriteEventsViewModel(get(), get(), get()) }
viewModel { SettingsViewModel(get()) }
viewModel { SettingsViewModel(get(), get()) }
viewModel { OrderCompletedViewModel(get(), get(), get(), get()) }
viewModel { OrdersUnderUserViewModel(get(), get(), get(), get(), get()) }
viewModel { OrderDetailsViewModel(get(), get(), get(), get()) }
Expand Down Expand Up @@ -286,6 +286,7 @@ val networkModule = module {
val builder = OkHttpClient().newBuilder()
.connectTimeout(connectTimeout.toLong(), TimeUnit.SECONDS)
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
.addInterceptor(HostSelectionInterceptor(get()))
.addInterceptor(RequestAuthenticator(get()))
.addNetworkInterceptor(StethoInterceptor())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem
import android.webkit.URLUtil
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.navigation.fragment.findNavController
import androidx.lifecycle.Observer
import androidx.navigation.fragment.navArgs
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceManager
import androidx.preference.PreferenceFragmentCompat
import kotlinx.android.synthetic.main.dialog_api_configuration.view.urlTextInputLayout
import kotlinx.android.synthetic.main.dialog_api_configuration.view.urlEditText
import kotlinx.android.synthetic.main.dialog_api_configuration.view.urlCheckBox
import org.jetbrains.anko.design.snackbar
import kotlinx.android.synthetic.main.dialog_change_password.view.oldPassword
import kotlinx.android.synthetic.main.dialog_change_password.view.newPassword
import kotlinx.android.synthetic.main.dialog_change_password.view.confirmNewPassword
import kotlinx.android.synthetic.main.dialog_change_password.view.textInputLayoutNewPassword
import kotlinx.android.synthetic.main.dialog_change_password.view.textInputLayoutConfirmNewPassword
import org.fossasia.openevent.general.BuildConfig
import org.fossasia.openevent.general.FDROID_BUILD_FLAVOR
import org.fossasia.openevent.general.PLAY_STORE_BUILD_FLAVOR
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.auth.MINIMUM_PASSWORD_LENGTH
Expand Down Expand Up @@ -80,6 +87,12 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceChangeListener {
profileViewModel.isLoggedIn()
preferenceScreen.findPreference<Preference>(getString(R.string.key_timezone_switch))?.isVisible =
profileViewModel.isLoggedIn()

preferenceScreen.findPreference<PreferenceCategory>(getString(R.string.key_server_configuration))
?.isVisible = BuildConfig.FLAVOR == FDROID_BUILD_FLAVOR

preferenceScreen.findPreference<Preference>(getString(R.string.key_api_url))?.title =
settingsViewModel.getApiUrl()
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
Expand All @@ -99,6 +112,9 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceChangeListener {
}
})

if (preference?.key == getString(R.string.key_api_url)) {
showChangeApiDialog()
}
if (preference?.key == getString(R.string.key_visit_website)) {
// Goes to website
Utils.openUrl(requireContext(), WEBSITE_LINK)
Expand Down Expand Up @@ -146,6 +162,46 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceChangeListener {
return false
}

private fun showChangeApiDialog() {
val layout = layoutInflater.inflate(R.layout.dialog_api_configuration, null)
layout.urlCheckBox.text = BuildConfig.DEFAULT_BASE_URL

val dialog = AlertDialog.Builder(requireContext())
.setView(layout)
.setPositiveButton(getString(R.string.change)) { _, _ ->
val url = if (layout.urlCheckBox.isChecked) BuildConfig.DEFAULT_BASE_URL
else layout.urlEditText.text.toString()
if (url === settingsViewModel.getApiUrl()) return@setPositiveButton
settingsViewModel.changeApiUrl(url)
view?.snackbar("API URL changed to $url")
findNavController().popBackStack(R.id.eventsFragment, false)
}
.setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.cancel() }
.setCancelable(false)
.show()
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false

layout.urlCheckBox.setOnCheckedChangeListener { _, isChecked ->
layout.urlTextInputLayout.isVisible = !isChecked
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = isChecked
}

layout.urlEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
val url = s.toString()
val isValidUrl = (URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)) &&
URLUtil.isValidUrl(url)
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = isValidUrl ||
layout.urlCheckBox.isChecked
if (!isValidUrl) layout.urlEditText.error = getString(R.string.invalid_url)
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { /*Implement here*/ }

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { /*Implement here*/ }
})
}

private fun startAppPlayStore(packageName: String) {
try {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(settingsViewModel.getMarketAppLink(packageName))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import org.fossasia.openevent.general.BuildConfig
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers
import org.fossasia.openevent.general.auth.AuthService
import org.fossasia.openevent.general.common.SingleLiveEvent
import org.fossasia.openevent.general.data.Preference
import timber.log.Timber

class SettingsViewModel(private val authService: AuthService) : ViewModel() {
const val API_URL = "apiUrl"

class SettingsViewModel(
private val authService: AuthService,
private val preference: Preference
) : ViewModel() {

private val compositeDisposable = CompositeDisposable()
private val mutableSnackBar = SingleLiveEvent<String>()
Expand Down Expand Up @@ -53,6 +60,15 @@ class SettingsViewModel(private val authService: AuthService) : ViewModel() {
})
}

fun getApiUrl(): String {
return preference.getString(API_URL) ?: BuildConfig.DEFAULT_BASE_URL
}

fun changeApiUrl(url: String) {
preference.putString(API_URL, url)
logout()
}

override fun onCleared() {
super.onCleared()
compositeDisposable.clear()
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/dialog_api_configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/urlCheckBox"
android:layout_margin="@dimen/layout_margin_large"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:id="@+id/urlTextInputLayout"
android:layout_margin="@dimen/layout_margin_large"
android:hint="@string/other_url"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/urlEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@
<string name="visit_website">Visit Website</string>
<string name="key_visit_website">Visit_Website</string>
<string name="not_logged_in">No account logged in</string>
<string name="server_configuration">Server configuration</string>
<string name="key_server_configuration">server_configuration</string>
<string name="api_url">Api url</string>
<string name="key_api_url">api_url</string>
<string name="other_url">Other url</string>
<string name="invalid_url">Invalid url</string>

<!--settings profile dialog-->
<string name="message">Are you sure you want to log out?</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
android:title="@string/server_configuration"
android:key="@string/key_server_configuration"
app:iconSpaceReserved="false">

<Preference
android:title="@string/api_url"
android:key="@string/key_api_url"
app:iconSpaceReserved="false"/>

</PreferenceCategory>

<PreferenceCategory
android:title="@string/logged_in_account"
android:key="@string/key_account"
Expand Down