diff --git a/app/src/main/java/uk/openvk/android/refresh/api/entities/Message.java b/app/src/main/java/uk/openvk/android/refresh/api/entities/Message.java index 94497dd..abd61b8 100644 --- a/app/src/main/java/uk/openvk/android/refresh/api/entities/Message.java +++ b/app/src/main/java/uk/openvk/android/refresh/api/entities/Message.java @@ -49,6 +49,9 @@ public Message(long id, boolean incoming, boolean error, long _timestamp, String timestamp_int = _timestamp; Date dt = new Date(TimeUnit.SECONDS.toMillis(_timestamp)); timestamp = new SimpleDateFormat("HH:mm").format(dt); + if(!incoming) { + type = 1; + } } public void getSendedId(String response) { diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AppActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AppActivity.java index dab62c8..06c3869 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AppActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AppActivity.java @@ -82,6 +82,7 @@ import uk.openvk.android.refresh.ui.core.fragments.app.settings.VideoSettingsFragment; import uk.openvk.android.refresh.ui.list.adapters.NewsfeedToolbarSpinnerAdapter; import uk.openvk.android.refresh.ui.list.items.ToolbarSpinnerItem; +import uk.openvk.android.refresh.ui.view.layouts.ProfileHeader; import uk.openvk.android.refresh.ui.wrappers.LocaleContextWrapper; public class AppActivity extends NetworkActivity { @@ -427,11 +428,9 @@ public void onClick(View v) { startQuickSearchActivity(); } }); - if(ovk_api.account == null || ovk_api.account.user == null) { - String profile_name = getResources().getString(R.string.loading); - ((TextView) header.findViewById(R.id.profile_name)).setText(profile_name); - header.findViewById(R.id.screen_name).setVisibility(View.GONE); - } + String profile_name = getResources().getString(R.string.loading); + ((TextView) header.findViewById(R.id.profile_name)).setText(profile_name); + header.findViewById(R.id.screen_name).setVisibility(View.GONE); @SuppressLint("CutPasteId") ShapeableImageView avatar = ((ShapeableImageView) ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0) .findViewById(R.id.profile_avatar)); @@ -664,6 +663,16 @@ public Fragment getSelectedFragment() { @SuppressLint("NotifyDataSetChanged") public void receiveState(int message, Bundle data) { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } // Handling OpenVK API and UI messages try { if (message == HandlerMessages.ACCOUNT_PROFILE_INFO) { @@ -671,6 +680,11 @@ public void receiveState(int message, Bundle data) { ovk_api.users.getAccountUser(ovk_api.wrapper, ovk_api.account.id); ovk_api.messages.getLongPollServer(ovk_api.wrapper); ovk_api.messages.getConversations(ovk_api.wrapper); + String profile_name = String.format("%s %s", + ovk_api.account.first_name, ovk_api.account.last_name); + ConstraintLayout header = (ConstraintLayout) + ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0); + ((TextView) header.findViewById(R.id.profile_name)).setText(profile_name); } else if (message == HandlerMessages.ACCOUNT_COUNTERS) { BottomNavigationView b_navView = findViewById(R.id.bottom_nav_view); int accentColor; @@ -731,12 +745,11 @@ public void receiveState(int message, Bundle data) { } } else if (message == HandlerMessages.USERS_GET_ALT) { ovk_api.account.user = ovk_api.users.getList().get(0); - ovk_api.account.user.downloadAvatar(downloadManager, "high", "account_avatar"); + ConstraintLayout header = (ConstraintLayout) + ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0); + ovk_api.account.user.downloadAvatar(ovk_api.dlman, "high", "account_avatar"); String profile_name = String.format("%s %s", ovk_api.account.first_name, ovk_api.account.last_name); - ((TextView) ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0) - .findViewById(R.id.profile_name)) - .setText(profile_name); if(ovk_api.account.user.screen_name != null && ovk_api.account.user.screen_name.length() > 0) { ((TextView) ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0) diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AuthActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AuthActivity.java index 2d354cf..22ed8a3 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AuthActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/AuthActivity.java @@ -179,6 +179,16 @@ public void signIn(String twofactor_code) { } public void receiveState(int message, Bundle data) { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if (message == HandlerMessages.AUTHORIZED) { SharedPreferences.Editor editor = instance_prefs.edit(); Authorization auth = new Authorization(data.getString("response")); diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ConversationActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ConversationActivity.java index 91db19d..b58640e 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ConversationActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ConversationActivity.java @@ -236,6 +236,16 @@ public void setAPIWrapper() { @SuppressLint("NotifyDataSetChanged") public void receiveState(int message, Bundle data) { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if (message == HandlerMessages.MESSAGES_GET_HISTORY) { messagesView = findViewById(R.id.messages_view); history = conversation.parseHistory(this, data.getString("response")); diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/FriendsIntentActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/FriendsIntentActivity.java index 5bea746..54cf07a 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/FriendsIntentActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/FriendsIntentActivity.java @@ -131,6 +131,16 @@ public void onClick(View view) { public void receiveState(int message, Bundle data) { try { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if (message == HandlerMessages.ACCOUNT_PROFILE_INFO) { if (args.startsWith("id")) { try { diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/GroupIntentActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/GroupIntentActivity.java index 81c2b10..be183c2 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/GroupIntentActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/GroupIntentActivity.java @@ -167,6 +167,16 @@ public void onClick(View view) { public void receiveState(int message, Bundle data) { try { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if (message == HandlerMessages.ACCOUNT_PROFILE_INFO) { if (args.startsWith("club")) { try { diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/MainSettingsActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/MainSettingsActivity.java index 4af5a32..4558c13 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/MainSettingsActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/MainSettingsActivity.java @@ -136,30 +136,30 @@ public void switchFragment(String tag) { if(selectedFragment == null) selectedFragment = getSupportFragmentManager() .findFragmentByTag("settings"); switch (tag) { - case "settings": + case "settings" -> { ft.hide(selectedFragment); selectedFragment = Objects.requireNonNull(fm.findFragmentByTag("settings")); ft.show(selectedFragment); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setTitle(R.string.nav_settings); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setNavigationIcon( R.drawable.ic_arrow_back); - break; - case "personalization": + } + case "personalization" -> { ft.hide(selectedFragment); selectedFragment = Objects.requireNonNull(fm.findFragmentByTag("personalization")); ft.show(selectedFragment); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setTitle(R.string.pref_personalization); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setNavigationIcon( R.drawable.ic_arrow_back); - break; - case "about_app": + } + case "about_app" -> { ft.hide(selectedFragment); selectedFragment = Objects.requireNonNull(fm.findFragmentByTag("about_app")); ft.show(selectedFragment); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setTitle(R.string.pref_about_app); ((MaterialToolbar) findViewById(R.id.app_toolbar)).setNavigationIcon( R.drawable.ic_arrow_back); - break; + } } ft.commit(); } diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/NewPostActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/NewPostActivity.java index 7a8b071..af94570 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/NewPostActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/NewPostActivity.java @@ -202,6 +202,16 @@ private void sendPost() { public void receiveState(int message, Bundle data) { try { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if(message == HandlerMessages.WALL_POST) { Toast.makeText(getApplicationContext(), getResources().getString(R.string.posted_successfully), Toast.LENGTH_LONG).show(); @@ -210,7 +220,7 @@ public void receiveState(int message, Bundle data) { Toast.makeText(getApplicationContext(), getResources().getString(R.string.posting_access_denied), Toast.LENGTH_LONG).show(); - } else { + } else if(message < 0){ Toast.makeText(getApplicationContext(), getResources().getString(R.string.posting_error), Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/PhotoViewerActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/PhotoViewerActivity.java index 6d79343..b6eeca6 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/PhotoViewerActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/PhotoViewerActivity.java @@ -175,6 +175,16 @@ private void loadPhoto() { @SuppressLint("UseCompatLoadingForDrawables") public void receiveState(int message, Bundle data) { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if(message == HandlerMessages.ACCESS_DENIED_MARSHMALLOW) { allowPermissionDialog(); } else if(message == HandlerMessages.ORIGINAL_PHOTO) { diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ProfileIntentActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ProfileIntentActivity.java index 4d993b8..66f0318 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ProfileIntentActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/ProfileIntentActivity.java @@ -170,6 +170,16 @@ public void onClick(View view) { public void receiveState(int message, Bundle data) { try { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if (message == HandlerMessages.ACCOUNT_PROFILE_INFO) { if (args.startsWith("id")) { MaterialToolbar appBar = findViewById(R.id.app_toolbar); diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/WallPostActivity.java b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/WallPostActivity.java index 2a6400c..7fc1285 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/WallPostActivity.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/core/activities/WallPostActivity.java @@ -92,6 +92,16 @@ private void setAPIWrapper() { } public void receiveState(int message, Bundle data) { + if(data.containsKey("address")) { + String activityName = data.getString("address"); + if(activityName == null) { + return; + } + boolean isCurrentActivity = activityName.equals(getLocalClassName()); + if(!isCurrentActivity) { + return; + } + } if(message == HandlerMessages.ACCOUNT_PROFILE_INFO) { ovk_api.account.parse(data.getString("response"), ovk_api.wrapper); setBottomPanel(); diff --git a/app/src/main/java/uk/openvk/android/refresh/ui/list/adapters/MessagesAdapter.java b/app/src/main/java/uk/openvk/android/refresh/ui/list/adapters/MessagesAdapter.java index 02e1c8e..2d3f7cf 100644 --- a/app/src/main/java/uk/openvk/android/refresh/ui/list/adapters/MessagesAdapter.java +++ b/app/src/main/java/uk/openvk/android/refresh/ui/list/adapters/MessagesAdapter.java @@ -3,6 +3,7 @@ import android.annotation.SuppressLint; import android.content.Context; import android.text.method.LinkMovementMethod; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,13 +24,14 @@ import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.RecyclerView; import uk.openvk.android.refresh.Global; +import uk.openvk.android.refresh.OvkApplication; import uk.openvk.android.refresh.R; import uk.openvk.android.refresh.api.entities.Message; import uk.openvk.android.refresh.ui.util.glide.GlideApp; public class MessagesAdapter extends RecyclerView.Adapter { - private Context ctx; - private ArrayList items; + private final Context ctx; + private final ArrayList items; public MessagesAdapter(Context context, ArrayList items, long peer_id) { this.ctx = context; @@ -83,85 +85,109 @@ public Holder(View view) { @SuppressLint({"SimpleDateFormat", "UseCompatLoadingForDrawables"}) void bind(final int position) { - final Message item = getItem(position); - if(item.type < 2) { - if(item.text.length() < 20) { - vertical_layout.setVisibility(View.GONE); - horizontal_layout.setVisibility(View.VISIBLE); - msg_text.setText(Global.formatLinksAsHtml(item.text)); - msg_text.setMovementMethod(LinkMovementMethod.getInstance()); - } else { - vertical_layout.setVisibility(View.VISIBLE); - horizontal_layout.setVisibility(View.GONE); - msg_text_2.setText(Global.formatLinksAsHtml(item.text)); - msg_text_2.setMovementMethod(LinkMovementMethod.getInstance()); - } - msg_timestamp.setText(item.timestamp); - if(item.type == 1) { - if(item.isError) { - ((ImageView) convertView.findViewById(R.id.error_image)) - .setVisibility(View.VISIBLE); - } else { - ((ImageView) convertView.findViewById(R.id.error_image)) - .setVisibility(View.GONE); - } - if(item.sending) { - ((ProgressBar) convertView.findViewById(R.id.sending_progress)) - .setVisibility(View.VISIBLE); + try { + final Message item = getItem(position); + assert item != null; + if (item.type < 2) { + if (item.text.length() < 20) { + vertical_layout.setVisibility(View.GONE); + horizontal_layout.setVisibility(View.VISIBLE); + msg_text.setText(Global.formatLinksAsHtml(item.text)); + msg_text.setMovementMethod(LinkMovementMethod.getInstance()); } else { - ((ProgressBar) convertView.findViewById(R.id.sending_progress)) - .setVisibility(View.GONE); + vertical_layout.setVisibility(View.VISIBLE); + horizontal_layout.setVisibility(View.GONE); + msg_text_2.setText(Global.formatLinksAsHtml(item.text)); + msg_text_2.setMovementMethod(LinkMovementMethod.getInstance()); } - } else { - if(getItem(position - 1).type != item.type) { - Global.setAvatarShape(ctx, convertView.findViewById(R.id.companion_avatar)); - ((ImageView) convertView.findViewById(R.id.companion_avatar)).setImageTintList(null); - GlideApp.with(ctx) - .load(String.format("%s/photos_cache/conversations_avatars/avatar_%s", - ctx.getCacheDir().getAbsolutePath(), item.id)) - .error(ctx.getResources().getDrawable(R.drawable.circular_avatar)) - .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true) - .dontAnimate().centerCrop() - .into((ImageView) convertView.findViewById(R.id.companion_avatar)); + msg_timestamp.setText(item.timestamp); + if (item.type == 1) { + if (item.isError) { + ((ImageView) convertView.findViewById(R.id.error_image)) + .setVisibility(View.VISIBLE); + } else { + ((ImageView) convertView.findViewById(R.id.error_image)) + .setVisibility(View.GONE); + } + if (item.sending) { + ((ProgressBar) convertView.findViewById(R.id.sending_progress)) + .setVisibility(View.VISIBLE); + } else { + ((ProgressBar) convertView.findViewById(R.id.sending_progress)) + .setVisibility(View.GONE); + } } else { - ((ImageView) convertView.findViewById(R.id.companion_avatar)) - .setVisibility(View.INVISIBLE); + if (Objects.requireNonNull(getItem(position - 1)).type != item.type) { + Global.setAvatarShape(ctx, convertView.findViewById(R.id.companion_avatar)); + ((ImageView) convertView.findViewById(R.id.companion_avatar)).setImageTintList(null); + GlideApp.with(ctx) + .load(String.format("%s/photos_cache/conversations_avatars/avatar_%s", + ctx.getCacheDir().getAbsolutePath(), item.id)) + .error(ctx.getResources().getDrawable(R.drawable.circular_avatar)) + .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true) + .dontAnimate().centerCrop() + .into((ImageView) convertView.findViewById(R.id.companion_avatar)); + } else { + ((ImageView) convertView.findViewById(R.id.companion_avatar)) + .setVisibility(View.INVISIBLE); + } } - } - CardView cardView; - boolean isDarkTheme = PreferenceManager.getDefaultSharedPreferences(ctx) - .getBoolean("dark_theme", false); - if(item.type == 0) { - cardView = ((CardView) convertView.findViewById(R.id.incoming_msg_layout)); - if (Global.checkMonet(ctx)) { - MonetCompat monet = MonetCompat.getInstance(); - cardView.setCardBackgroundColor( - Objects.requireNonNull(monet.getMonetColors().getAccent1().get(500)) - .toLinearSrgb().toSrgb().quantize8()); - } else { - if (isDarkTheme) { + CardView cardView; + boolean isDarkTheme = PreferenceManager.getDefaultSharedPreferences(ctx) + .getBoolean("dark_theme", false); + if (item.type == 0) { + cardView = ((CardView) convertView.findViewById(R.id.incoming_msg_layout)); + if (Global.checkMonet(ctx)) { + MonetCompat monet = MonetCompat.getInstance(); cardView.setCardBackgroundColor( - MaterialColors.getColor(convertView, androidx.appcompat.R.attr.colorPrimaryDark)); + Objects.requireNonNull(monet.getMonetColors().getAccent1().get(500)) + .toLinearSrgb().toSrgb().quantize8()); } else { - cardView.setCardBackgroundColor( - MaterialColors.getColor(convertView, androidx.appcompat.R.attr.colorAccent)); + if (isDarkTheme) { + cardView.setCardBackgroundColor( + MaterialColors.getColor(convertView, androidx.appcompat.R.attr.colorPrimaryDark)); + } else { + cardView.setCardBackgroundColor( + MaterialColors.getColor(convertView, androidx.appcompat.R.attr.colorAccent)); + } } } + } else { + msg_text.setTypeface(Global.getFlexibleTypeface(ctx, 500)); + msg_text.setText(Global.formatLinksAsHtml(item.text)); + msg_text.setMovementMethod(LinkMovementMethod.getInstance()); } - } else { - msg_text.setTypeface(Global.getFlexibleTypeface(ctx, 500)); - msg_text.setText(Global.formatLinksAsHtml(item.text)); - msg_text.setMovementMethod(LinkMovementMethod.getInstance()); + } catch (Exception | AssertionError ex) { + ex.printStackTrace(); } } } @Override public int getItemViewType(int position) { - return ((Message) getItem(position)).type; + try { + return ((Message) Objects.requireNonNull(getItem(position))).type; + } catch (Exception ex) { + return 0; + } } private Message getItem(int position) { - return items.get(position); + try { + if (position >= 0) { + return items.get(position); + } else { + Log.e(OvkApplication.APP_TAG, + String.format( + "[NewsfeedAdapter] Invalid position %s of %s", + position, + items.size() + ) + ); + return null; + } + } catch (Exception ex) { + return null; + } } }