From 84741c1f1bba2722754fd17a2141dbb1850076e9 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Sun, 16 Apr 2017 18:51:09 -0400 Subject: [PATCH] Adds a partial profile editor (non-working). --- app/src/main/AndroidManifest.xml | 1 + .../tusky/EditProfileActivity.java | 130 ++++++++++++++++++ .../keylesspalace/tusky/LoginActivity.java | 20 --- .../com/keylesspalace/tusky/MainActivity.java | 9 +- .../main/res/layout/activity_edit_profile.xml | 62 +++++++++ .../main/res/menu/edit_profile_toolbar.xml | 9 ++ app/src/main/res/values/strings.xml | 8 ++ 7 files changed, 213 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.java create mode 100644 app/src/main/res/layout/activity_edit_profile.xml create mode 100644 app/src/main/res/menu/edit_profile_toolbar.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 62955811..99e67606 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -59,6 +59,7 @@ + diff --git a/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.java b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.java new file mode 100644 index 00000000..5475a0d4 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.java @@ -0,0 +1,130 @@ +package com.keylesspalace.tusky; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.widget.Toolbar; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Button; +import android.widget.EditText; + +import com.keylesspalace.tusky.entity.Account; + +import butterknife.BindView; +import butterknife.ButterKnife; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class EditProfileActivity extends BaseActivity { + private static final String TAG = "EditProfileActivity"; + + @BindView(R.id.edit_profile_display_name) EditText displayNameEditText; + @BindView(R.id.edit_profile_note) EditText noteEditText; + @BindView(R.id.edit_profile_avatar) Button avatarButton; + @BindView(R.id.edit_profile_header) Button headerButton; + + private String priorDisplayName; + private String priorNote; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_edit_profile); + ButterKnife.bind(this); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setTitle(null); + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowHomeEnabled(true); + } + + if (savedInstanceState != null) { + priorDisplayName = savedInstanceState.getString("priorDisplayName"); + priorNote = savedInstanceState.getString("priorNote"); + } else { + priorDisplayName = null; + priorNote = null; + } + + mastodonAPI.accountVerifyCredentials().enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (!response.isSuccessful()) { + onAccountVerifyCredentialsFailed(); + return; + } + Account me = response.body(); + priorDisplayName = me.getDisplayName(); + priorNote = me.note.toString(); + displayNameEditText.setText(priorDisplayName); + noteEditText.setText(HtmlUtils.fromHtml(priorNote)); + } + + @Override + public void onFailure(Call call, Throwable t) { + onAccountVerifyCredentialsFailed(); + } + }); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + outState.putString("priorDisplayName", priorDisplayName); + outState.putString("priorNote", priorNote); + super.onSaveInstanceState(outState); + } + + private void onAccountVerifyCredentialsFailed() { + Log.e(TAG, "The account failed to load."); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.edit_profile_toolbar, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + onBackPressed(); + return true; + } + case R.id.action_save: { + save(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + + private void save() { + String newDisplayName = displayNameEditText.getText().toString(); + if (newDisplayName.isEmpty()) { + displayNameEditText.setError(getString(R.string.error_empty)); + return; + } + if (priorDisplayName != null && priorDisplayName.equals(newDisplayName)) { + // If it's not any different, don't patch it. + newDisplayName = null; + } + + String newNote = HtmlUtils.toHtml(noteEditText.getText()); + if (newNote.isEmpty()) { + noteEditText.setError(getString(R.string.error_empty)); + return; + } + if (priorNote != null && priorNote.equals(newNote)) { + // If it's not any different, don't patch it. + newNote = null; + } + + mastodonAPI.accountUpdateCredentials(newDisplayName, newNote, null, null); + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java index e4695dd7..b1996d19 100644 --- a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java @@ -110,26 +110,6 @@ public class LoginActivity extends AppCompatActivity { textView.setMovementMethod(LinkMovementMethod.getInstance()); } }); - - // Apply any updates needed. - int versionCode = 1; - try { - versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; - } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, "The app version was not found. " + e.getMessage()); - } - if (preferences.getInt("lastUpdateVersion", 0) != versionCode) { - SharedPreferences.Editor editor = preferences.edit(); - if (versionCode == 14) { - /* This version switches the order of scheme and host in the OAuth redirect URI. - * But to fix it requires forcing the app to re-authenticate with servers. So, clear - * out the stored client id/secret pairs. The only other things that are lost are - * "rememberedVisibility", "loggedInUsername", and "loggedInAccountId". */ - editor.clear(); - } - editor.putInt("lastUpdateVersion", versionCode); - editor.apply(); - } } @Override diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 43415269..bd4326ed 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -261,7 +261,7 @@ public class MainActivity extends BaseActivity { .withHasStableIds(true) .withSelectedItem(-1) .addDrawerItems( - new PrimaryDrawerItem().withIdentifier(0).withName(R.string.action_view_profile).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person), + new PrimaryDrawerItem().withIdentifier(0).withName(getString(R.string.action_edit_profile)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_person), new PrimaryDrawerItem().withIdentifier(1).withName(getString(R.string.action_view_favourites)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_star), new PrimaryDrawerItem().withIdentifier(2).withName(getString(R.string.action_view_blocks)).withSelectable(false).withIcon(GoogleMaterial.Icon.gmd_block), new DividerDrawerItem(), @@ -275,11 +275,8 @@ public class MainActivity extends BaseActivity { long drawerItemIdentifier = drawerItem.getIdentifier(); if (drawerItemIdentifier == 0) { - if (loggedInAccountId != null) { - Intent intent = new Intent(MainActivity.this, AccountActivity.class); - intent.putExtra("id", loggedInAccountId); - startActivity(intent); - } + Intent intent = new Intent(MainActivity.this, EditProfileActivity.class); + startActivity(intent); } else if (drawerItemIdentifier == 1) { Intent intent = new Intent(MainActivity.this, FavouritesActivity.class); startActivity(intent); diff --git a/app/src/main/res/layout/activity_edit_profile.xml b/app/src/main/res/layout/activity_edit_profile.xml new file mode 100644 index 00000000..d861a91d --- /dev/null +++ b/app/src/main/res/layout/activity_edit_profile.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + +