Prepare app for username changes. Fixes #1048

This commit is contained in:
Phillip Thelen 2018-09-20 12:07:43 +02:00
parent f18943eabe
commit bc20ab80e0
9 changed files with 49 additions and 8 deletions

View file

@ -8,6 +8,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/passwordTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password"

View file

@ -692,6 +692,7 @@
<string name="change_password">Change Password</string>
<string name="change_email">Change Email Address</string>
<string name="change_login_name">Change Login Name</string>
<string name="change_username">Change Username</string>
<string name="change">Change</string>
<string name="character_level">Character Level</string>
<string name="auto_allocate_points">Auto Allocate Points</string>

View file

@ -232,6 +232,7 @@ interface ApiClient {
fun sendPasswordResetEmail(email: String): Flowable<Void>
fun updateLoginName(newLoginName: String, password: String): Flowable<Void>
fun updateUsername(newLoginName: String): Flowable<Void>
fun updateEmail(newEmail: String, password: String): Flowable<Void>

View file

@ -63,7 +63,7 @@ interface UserRepository : BaseRepository {
fun sendPasswordResetEmail(email: String): Flowable<Void>
fun updateLoginName(newLoginName: String, password: String): Flowable<Void>
fun updateLoginName(newLoginName: String, password: String? = null): Flowable<Void>
fun updateEmail(newEmail: String, password: String): Flowable<Void>
fun updatePassword(newPassword: String, oldPassword: String, oldPasswordConfirmation: String): Flowable<Void>

View file

@ -653,6 +653,12 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
return apiService.updateLoginName(updateObject).compose(configureApiCallObserver())
}
override fun updateUsername(newLoginName: String): Flowable<Void> {
val updateObject = HashMap<String, String>()
updateObject["username"] = newLoginName
return apiService.updateLoginName(updateObject).compose(configureApiCallObserver())
}
override fun updateEmail(newEmail: String, password: String): Flowable<Void> {
val updateObject = HashMap<String, String>()
updateObject["newEmail"] = newEmail

View file

@ -205,8 +205,13 @@ class UserRepositoryImpl(localRepository: UserLocalRepository, apiClient: ApiCli
override fun sendPasswordResetEmail(email: String): Flowable<Void> =
apiClient.sendPasswordResetEmail(email)
override fun updateLoginName(newLoginName: String, password: String): Flowable<Void> =
override fun updateLoginName(newLoginName: String, password: String?): Flowable<Void> {
return if (password != null) {
apiClient.updateLoginName(newLoginName, password)
} else {
apiClient.updateUsername(newLoginName)
}
}
override fun updateEmail(newEmail: String, password: String): Flowable<Void> =
apiClient.updateEmail(newEmail, password)

View file

@ -24,6 +24,7 @@ public class RemoteConfigManager {
private Boolean enableNewShops = false;
private String shopSpriteSuffix = "";
private Integer maxChatLength = 3000;
private Boolean enableChangeUsername = false;
private String REMOTE_STRING_KEY = "remote-string";
public RemoteConfigManager(Context context) {
@ -46,6 +47,8 @@ public class RemoteConfigManager {
public Integer maxChatLength() { return maxChatLength; }
public Boolean enableChangeUsername() { return enableChangeUsername; }
private void loadFromPreferences () {
String storedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
.getString(REMOTE_STRING_KEY, "");
@ -70,6 +73,9 @@ public class RemoteConfigManager {
if (obj.has("maxChatLength")) {
maxChatLength = obj.getInt("maxChatLength");
}
if (obj.has("enableChangeUsername")) {
enableChangeUsername = obj.getBoolean("enableChangeUsername");
}
} catch (JSONException e) {
e.printStackTrace();
}

View file

@ -125,6 +125,7 @@ class LoginActivity : BaseActivity(), Consumer<UserAuthResponse> {
}
override fun onCreate(savedInstanceState: Bundle?) {
FacebookSdk.sdkInitialize(this.applicationContext)
super.onCreate(savedInstanceState)
supportActionBar?.hide()
//Set default values to avoid null-responses when requesting unedited settings
@ -248,9 +249,9 @@ class LoginActivity : BaseActivity(), Consumer<UserAuthResponse> {
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
callbackManager.onActivityResult(requestCode, resultCode, intent)
val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent)
super.onActivityResult(requestCode, resultCode, data)
callbackManager.onActivityResult(requestCode, resultCode, data)
val scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
if (scanResult != null) {
try {
Log.d("scanresult", scanResult.contents)

View file

@ -10,8 +10,10 @@ import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.support.v7.preference.Preference
import android.text.InputType
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.habitrpg.android.habitica.HabiticaApplication
import com.habitrpg.android.habitica.HabiticaBaseApplication
@ -19,14 +21,19 @@ import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand
import com.habitrpg.android.habitica.extensions.layoutInflater
import com.habitrpg.android.habitica.extensions.notNull
import com.habitrpg.android.habitica.helpers.RemoteConfigManager
import com.habitrpg.android.habitica.helpers.RxErrorHandler
import com.habitrpg.android.habitica.models.user.User
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionDetailsView
import io.reactivex.functions.Consumer
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject
class AuthenticationPreferenceFragment: BasePreferencesFragment() {
@Inject
lateinit var configManager: RemoteConfigManager
override var user: User? = null
set(value) {
field = value
@ -37,6 +44,10 @@ class AuthenticationPreferenceFragment: BasePreferencesFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
HabiticaBaseApplication.component?.inject(this)
super.onCreate(savedInstanceState)
if (configManager.enableChangeUsername()) {
findPreference("login_name").title = context?.getString(R.string.username)
}
}
private fun updateUserFields() {
@ -109,11 +120,20 @@ class AuthenticationPreferenceFragment: BasePreferencesFragment() {
val loginNameEditText = view?.findViewById<EditText>(R.id.editText)
loginNameEditText?.setText(user?.authentication?.localAuthentication?.username)
val passwordEditText = view?.findViewById<EditText>(R.id.passwordEditText)
if (configManager.enableChangeUsername()) {
passwordEditText?.visibility = View.GONE
val passwordTitleTextView = view?.findViewById<TextView>(R.id.passwordTitleTextView)
passwordTitleTextView?.visibility = View.GONE
}
context.notNull { context ->
val dialog = AlertDialog.Builder(context)
var builder = AlertDialog.Builder(context)
builder = if (configManager.enableChangeUsername()) {
builder.setTitle(R.string.change_username)
} else {
builder.setTitle(R.string.change_login_name)
}
.setTitle(R.string.change_login_name)
.setPositiveButton(R.string.change) { thisDialog, _ ->
val dialog = builder.setPositiveButton(R.string.change) { thisDialog, _ ->
thisDialog.dismiss()
userRepository.updateLoginName(loginNameEditText?.text.toString(), passwordEditText?.text.toString())
.subscribe(Consumer {