From fce573f1eca899f4a28811583b4dfbc072a871a0 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sat, 15 Apr 2017 19:44:29 +0200 Subject: [PATCH] Update thread view after sent reply (closes #74) --- .../keylesspalace/tusky/ComposeActivity.java | 2 ++ .../com/keylesspalace/tusky/MainActivity.java | 14 +++++++++++++- .../com/keylesspalace/tusky/SFragment.java | 19 ++++++++++++++++--- .../keylesspalace/tusky/TimelineFragment.java | 8 ++++++++ .../tusky/TimelinePagerAdapter.java | 15 +++++++++++++++ .../tusky/ViewThreadFragment.java | 6 ++++++ 6 files changed, 60 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 5be683a2..cfd1eb6f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -101,6 +101,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag private static final int MEDIA_PICK_RESULT = 1; private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1; private static final int MEDIA_SIZE_UNKNOWN = -1; + private static final int COMPOSE_SUCCESS = -1; private String inReplyToId; private EditText textEditor; @@ -816,6 +817,7 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFrag private void onSendSuccess() { Snackbar bar = Snackbar.make(findViewById(R.id.activity_compose), getString(R.string.confirmation_send), Snackbar.LENGTH_SHORT); bar.show(); + setResult(COMPOSE_SUCCESS); finish(); } diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 43415269..8a2b7bc6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -66,6 +66,7 @@ import retrofit2.Response; public class MainActivity extends BaseActivity { private static final String TAG = "MainActivity"; // logging tag and Volley request tag + protected static int COMPOSE_RESULT = 1; private String loggedInAccountId; private String loggedInAccountUsername; @@ -99,7 +100,7 @@ public class MainActivity extends BaseActivity { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), ComposeActivity.class); - startActivity(intent); + startActivityForResult(intent, COMPOSE_RESULT); } }); @@ -474,6 +475,17 @@ public class MainActivity extends BaseActivity { Log.e(TAG, "Failed to fetch user info. " + exception.getMessage()); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) { + TimelinePagerAdapter adapter = (TimelinePagerAdapter) viewPager.getAdapter(); + if (adapter.getCurrentFragment() instanceof SFragment) { + ((SFragment) adapter.getCurrentFragment()).onSuccessfulStatus(); + } + } + super.onActivityResult(requestCode, resultCode, data); + } + @Override public void onBackPressed() { if(drawer != null && drawer.isDrawerOpen()) { diff --git a/app/src/main/java/com/keylesspalace/tusky/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/SFragment.java index f18f07f0..54d897a0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/SFragment.java @@ -44,9 +44,10 @@ import retrofit2.Callback; * adapters. I feel like the profile pages and thread viewer, which I haven't made yet, will also * overlap functionality. So, I'm momentarily leaving it and hopefully working on those will clear * up what needs to be where. */ -public class SFragment extends BaseFragment { +public abstract class SFragment extends BaseFragment { protected String loggedInAccountId; protected String loggedInUsername; + protected static int COMPOSE_RESULT = 1; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -79,11 +80,23 @@ public class SFragment extends BaseFragment { intent.putExtra("reply_visibility", replyVisibility); intent.putExtra("content_warning", contentWarning); intent.putExtra("mentioned_usernames", mentionedUsernames.toArray(new String[0])); - startActivity(intent); + startActivityForResult(intent, COMPOSE_RESULT); + } + + public void onSuccessfulStatus() { + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == COMPOSE_RESULT && resultCode == ComposeActivity.RESULT_OK) { + onSuccessfulStatus(); + } else { + super.onActivityResult(requestCode, resultCode, data); + } } protected void reblog(final Status status, final boolean reblog, - final RecyclerView.Adapter adapter, final int position) { + final RecyclerView.Adapter adapter, final int position) { String id = status.getActionableId(); Callback cb = new Callback() { diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java index 4f1b6e20..825411e8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java @@ -299,6 +299,14 @@ public class TimelineFragment extends SFragment implements } } + @Override + public void onSuccessfulStatus() { + if (kind == Kind.HOME || kind == Kind.PUBLIC_FEDERATED || kind == Kind.PUBLIC_LOCAL) { + onRefresh(); + } + super.onSuccessfulStatus(); + } + public void onReply(int position) { super.reply(adapter.getItem(position)); } diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelinePagerAdapter.java b/app/src/main/java/com/keylesspalace/tusky/TimelinePagerAdapter.java index af9bc639..2e81adc3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelinePagerAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelinePagerAdapter.java @@ -18,12 +18,27 @@ package com.keylesspalace.tusky; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; +import android.view.ViewGroup; class TimelinePagerAdapter extends FragmentPagerAdapter { + private Fragment currentFragment; + TimelinePagerAdapter(FragmentManager manager) { super(manager); } + public Fragment getCurrentFragment() { + return currentFragment; + } + + @Override + public void setPrimaryItem(ViewGroup container, int position, Object object) { + if (getCurrentFragment() != object) { + currentFragment = ((Fragment) object); + } + super.setPrimaryItem(container, position, object); + } + @Override public Fragment getItem(int i) { switch (i) { diff --git a/app/src/main/java/com/keylesspalace/tusky/ViewThreadFragment.java b/app/src/main/java/com/keylesspalace/tusky/ViewThreadFragment.java index 67952669..39baf8ee 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ViewThreadFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/ViewThreadFragment.java @@ -155,6 +155,12 @@ public class ViewThreadFragment extends SFragment implements sendThreadRequest(thisThreadsStatusId); } + @Override + public void onSuccessfulStatus() { + onRefresh(); + super.onSuccessfulStatus(); + } + public void onReply(int position) { super.reply(adapter.getItem(position)); }