1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
package ml.docilealligator.infinityforreddit.activities;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.transition.AutoTransition;
import androidx.transition.TransitionManager;
import com.evernote.android.state.State;
import com.google.android.material.snackbar.Snackbar;
import com.livefront.bridge.Bridge;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.adapters.PrivateMessagesDetailRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.LoadUserData;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
import ml.docilealligator.infinityforreddit.databinding.ActivityViewPrivateMessagesBinding;
import ml.docilealligator.infinityforreddit.events.PassPrivateMessageEvent;
import ml.docilealligator.infinityforreddit.events.PassPrivateMessageIndexEvent;
import ml.docilealligator.infinityforreddit.events.RepliedToPrivateMessageEvent;
import ml.docilealligator.infinityforreddit.message.Message;
import ml.docilealligator.infinityforreddit.message.ReadMessage;
import ml.docilealligator.infinityforreddit.message.ReplyMessage;
import retrofit2.Retrofit;
public class ViewPrivateMessagesActivity extends BaseActivity implements ActivityToolbarInterface {
public static final String EXTRA_PRIVATE_MESSAGE_INDEX = "EPM";
public static final String EXTRA_MESSAGE_POSITION = "EMP";
private static final String USER_AVATAR_STATE = "UAS";
@Inject
@Named("no_oauth")
Retrofit mRetrofit;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private LinearLayoutManagerBugFixed mLinearLayoutManager;
private PrivateMessagesDetailRecyclerViewAdapter mAdapter;
@State
Message privateMessage;
@State
Message replyTo;
private String mUserAvatar;
private ArrayList<ProvideUserAvatarCallback> mProvideUserAvatarCallbacks;
private boolean isLoadingUserAvatar = false;
private boolean isSendingMessage = false;
private int mSecondaryTextColor;
private int mSendMessageIconColor;
private ActivityViewPrivateMessagesBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this);
setImmersiveModeNotApplicable();
super.onCreate(savedInstanceState);
binding = ActivityViewPrivateMessagesBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Bridge.restoreInstanceState(this, savedInstanceState);
EventBus.getDefault().register(this);
applyCustomTheme();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) {
addOnOffsetChangedListener(binding.appbarLayoutViewPrivateMessagesActivity);
}
setSupportActionBar(binding.toolbarViewPrivateMessagesActivity);
setToolbarGoToTop(binding.toolbarViewPrivateMessagesActivity);
mProvideUserAvatarCallbacks = new ArrayList<>();
if (savedInstanceState != null) {
mUserAvatar = savedInstanceState.getString(USER_AVATAR_STATE);
if (privateMessage == null) {
EventBus.getDefault().post(new PassPrivateMessageIndexEvent(getIntent().getIntExtra(EXTRA_PRIVATE_MESSAGE_INDEX, -1)));
} else {
bindView();
}
} else {
EventBus.getDefault().post(new PassPrivateMessageIndexEvent(getIntent().getIntExtra(EXTRA_PRIVATE_MESSAGE_INDEX, -1)));
}
}
private void bindView() {
if (privateMessage != null) {
if (privateMessage.getAuthor().equals(accountName)) {
setTitle(privateMessage.getDestination());
binding.toolbarViewPrivateMessagesActivity.setOnClickListener(view -> {
if (privateMessage.isDestinationDeleted()) {
return;
}
Intent intent = new Intent(this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, privateMessage.getDestination());
startActivity(intent);
});
} else {
setTitle(privateMessage.getAuthor());
binding.toolbarViewPrivateMessagesActivity.setOnClickListener(view -> {
if (privateMessage.isAuthorDeleted()) {
return;
}
Intent intent = new Intent(this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, privateMessage.getAuthor());
startActivity(intent);
});
}
}
mAdapter = new PrivateMessagesDetailRecyclerViewAdapter(this, mSharedPreferences,
getResources().getConfiguration().locale, privateMessage, accountName, mCustomThemeWrapper);
mLinearLayoutManager = new LinearLayoutManagerBugFixed(this);
mLinearLayoutManager.setStackFromEnd(true);
binding.recyclerViewViewPrivateMessagesActivity.setLayoutManager(mLinearLayoutManager);
binding.recyclerViewViewPrivateMessagesActivity.setAdapter(mAdapter);
goToBottom();
binding.sendImageViewViewPrivateMessagesActivity.setOnClickListener(view -> {
if (!isSendingMessage) {
if (!binding.editTextViewPrivateMessagesActivity.getText().toString().equals("")) {
//Send Message
if (privateMessage != null) {
ArrayList<Message> replies = privateMessage.getReplies();
if (replyTo == null) {
replyTo = privateMessage;
}
isSendingMessage = true;
binding.sendImageViewViewPrivateMessagesActivity.setColorFilter(mSecondaryTextColor, android.graphics.PorterDuff.Mode.SRC_IN);
ReplyMessage.replyMessage(binding.editTextViewPrivateMessagesActivity.getText().toString(), replyTo.getFullname(),
getResources().getConfiguration().locale, mOauthRetrofit, accessToken,
new ReplyMessage.ReplyMessageListener() {
@Override
public void replyMessageSuccess(Message message) {
if (mAdapter != null) {
mAdapter.addReply(message);
}
goToBottom();
binding.editTextViewPrivateMessagesActivity.setText("");
binding.sendImageViewViewPrivateMessagesActivity.setColorFilter(mSendMessageIconColor, android.graphics.PorterDuff.Mode.SRC_IN);
isSendingMessage = false;
EventBus.getDefault().post(new RepliedToPrivateMessageEvent(message, getIntent().getIntExtra(EXTRA_MESSAGE_POSITION, -1)));
}
@Override
public void replyMessageFailed(String errorMessage) {
if (errorMessage != null && !errorMessage.equals("")) {
Snackbar.make(binding.getRoot(), errorMessage, Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(binding.getRoot(), R.string.reply_message_failed, Snackbar.LENGTH_LONG).show();
}
binding.sendImageViewViewPrivateMessagesActivity.setColorFilter(mSendMessageIconColor, android.graphics.PorterDuff.Mode.SRC_IN);
isSendingMessage = false;
}
});
StringBuilder fullnames = new StringBuilder();
if (privateMessage.isNew()) {
fullnames.append(privateMessage.getFullname()).append(",");
}
if (replies != null && !replies.isEmpty()) {
for (Message m : replies) {
if (m.isNew()) {
fullnames.append(m).append(",");
}
}
}
if (fullnames.length() > 0) {
fullnames.deleteCharAt(fullnames.length() - 1);
ReadMessage.readMessage(mOauthRetrofit, accessToken, fullnames.toString(),
new ReadMessage.ReadMessageListener() {
@Override
public void readSuccess() {}
@Override
public void readFailed() {}
});
}
}
}
}
});
}
public void fetchUserAvatar(String username, ProvideUserAvatarCallback provideUserAvatarCallback) {
if (mUserAvatar == null) {
mProvideUserAvatarCallbacks.add(provideUserAvatarCallback);
if (!isLoadingUserAvatar) {
LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase,
username, mRetrofit, iconImageUrl -> {
isLoadingUserAvatar = false;
mUserAvatar = iconImageUrl == null ? "" : iconImageUrl;
for (ProvideUserAvatarCallback provideUserAvatarCallbackInArrayList : mProvideUserAvatarCallbacks) {
provideUserAvatarCallbackInArrayList.fetchAvatarSuccess(iconImageUrl);
}
mProvideUserAvatarCallbacks.clear();
});
}
} else {
provideUserAvatarCallback.fetchAvatarSuccess(mUserAvatar);
}
}
public void delayTransition() {
TransitionManager.beginDelayedTransition(binding.recyclerViewViewPrivateMessagesActivity, new AutoTransition());
}
private void goToBottom() {
if (mLinearLayoutManager != null && mAdapter != null) {
mLinearLayoutManager.scrollToPositionWithOffset(mAdapter.getItemCount() - 1, 0);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(USER_AVATAR_STATE, mUserAvatar);
Bridge.saveInstanceState(this, outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
Bridge.clear(this);
}
@Override
public SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences;
}
@Override
public SharedPreferences getCurrentAccountSharedPreferences() {
return mCurrentAccountSharedPreferences;
}
@Override
public CustomThemeWrapper getCustomThemeWrapper() {
return mCustomThemeWrapper;
}
@Override
protected void applyCustomTheme() {
binding.getRoot().setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutViewPrivateMessagesActivity,
null, binding.toolbarViewPrivateMessagesActivity);
binding.cardViewViewPrivateMessagesActivity.setCardBackgroundColor(mCustomThemeWrapper.getFilledCardViewBackgroundColor());
binding.editTextViewPrivateMessagesActivity.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
mSecondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor();
binding.editTextViewPrivateMessagesActivity.setHintTextColor(mSecondaryTextColor);
mSendMessageIconColor = mCustomThemeWrapper.getSendMessageIconColor();
binding.sendImageViewViewPrivateMessagesActivity.setColorFilter(mSendMessageIconColor, android.graphics.PorterDuff.Mode.SRC_IN);
if (typeface != null) {
binding.editTextViewPrivateMessagesActivity.setTypeface(typeface);
}
}
@Override
public void onLongPress() {
if (mLinearLayoutManager != null) {
mLinearLayoutManager.scrollToPositionWithOffset(0, 0);
}
}
@Subscribe
public void onPassPrivateMessageEvent(PassPrivateMessageEvent passPrivateMessageEvent) {
privateMessage = passPrivateMessageEvent.message;
if (privateMessage != null) {
if (privateMessage.getAuthor().equals(accountName)) {
if (privateMessage.getReplies() != null) {
for (int i = privateMessage.getReplies().size() - 1; i >= 0; i--) {
if (!privateMessage.getReplies().get(i).getAuthor().equals(accountName)) {
replyTo = privateMessage.getReplies().get(i);
break;
}
}
}
if (replyTo == null) {
replyTo = privateMessage;
}
} else {
replyTo = privateMessage;
}
bindView();
}
}
public interface ProvideUserAvatarCallback {
void fetchAvatarSuccess(String userAvatarUrl);
}
}
|