Skip to content

Commit

Permalink
Prepare app for username changes. Fixes #1048
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Sep 20, 2018
1 parent f18943e commit bc20ab8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions Habitica/res/layout/dialog_edittext_confirm_pw.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions Habitica/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -46,6 +47,8 @@ public String shopSpriteSuffix() {

public Integer maxChatLength() { return maxChatLength; }

public Boolean enableChangeUsername() { return enableChangeUsername; }

private void loadFromPreferences () {
String storedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
.getString(REMOTE_STRING_KEY, "");
Expand All @@ -70,6 +73,9 @@ private void parseConfig(String jsonString) {
if (obj.has("maxChatLength")) {
maxChatLength = obj.getInt("maxChatLength");
}
if (obj.has("enableChangeUsername")) {
enableChangeUsername = obj.getBoolean("enableChangeUsername");
}
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ 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
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
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bc20ab8

Please sign in to comment.