18
18
import androidx .core .content .FileProvider ;
19
19
import androidx .recyclerview .widget .LinearLayoutManager ;
20
20
21
+ import com .giphy .sdk .core .models .Media ;
22
+ import com .giphy .sdk .ui .GPHContentType ;
23
+ import com .giphy .sdk .ui .Giphy ;
24
+ import com .giphy .sdk .ui .views .GiphyDialogFragment ;
21
25
import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
22
26
import com .google .android .material .snackbar .Snackbar ;
23
27
36
40
import javax .inject .Named ;
37
41
38
42
import ml .docilealligator .infinityforreddit .Infinity ;
43
+ import ml .docilealligator .infinityforreddit .thing .GiphyGif ;
39
44
import ml .docilealligator .infinityforreddit .thing .MediaMetadata ;
40
45
import ml .docilealligator .infinityforreddit .R ;
41
46
import ml .docilealligator .infinityforreddit .thing .UploadedImage ;
55
60
import retrofit2 .Response ;
56
61
import retrofit2 .Retrofit ;
57
62
58
- public class EditCommentActivity extends BaseActivity implements UploadImageEnabledActivity {
63
+ public class EditCommentActivity extends BaseActivity implements UploadImageEnabledActivity ,
64
+ GiphyDialogFragment .GifSelectionListener {
59
65
60
66
public static final String EXTRA_CONTENT = "EC" ;
61
67
public static final String EXTRA_FULLNAME = "EF" ;
@@ -69,6 +75,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab
69
75
private static final int MARKDOWN_PREVIEW_REQUEST_CODE = 300 ;
70
76
71
77
private static final String UPLOADED_IMAGES_STATE = "UIS" ;
78
+ private static final String GIPHY_GIF_STATE = "GGS" ;
72
79
73
80
@ Inject
74
81
@ Named ("oauth" )
@@ -92,6 +99,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab
92
99
private boolean isSubmitting = false ;
93
100
private Uri capturedImageUri ;
94
101
private ArrayList <UploadedImage > uploadedImages = new ArrayList <>();
102
+ private GiphyGif giphyGif ;
95
103
private ActivityEditCommentBinding binding ;
96
104
97
105
@ Override
@@ -141,10 +149,11 @@ protected void onCreate(Bundle savedInstanceState) {
141
149
142
150
if (savedInstanceState != null ) {
143
151
uploadedImages = savedInstanceState .getParcelableArrayList (UPLOADED_IMAGES_STATE );
152
+ giphyGif = savedInstanceState .getParcelable (GIPHY_GIF_STATE );
144
153
}
145
154
146
155
MarkdownBottomBarRecyclerViewAdapter adapter = new MarkdownBottomBarRecyclerViewAdapter (
147
- mCustomThemeWrapper , true , new MarkdownBottomBarRecyclerViewAdapter .ItemClickListener () {
156
+ mCustomThemeWrapper , true , true , new MarkdownBottomBarRecyclerViewAdapter .ItemClickListener () {
148
157
@ Override
149
158
public void onClick (int item ) {
150
159
MarkdownBottomBarRecyclerViewAdapter .bindEditTextWithItemClickListener (
@@ -161,6 +170,11 @@ public void onUploadImage() {
161
170
fragment .setArguments (arguments );
162
171
fragment .show (getSupportFragmentManager (), fragment .getTag ());
163
172
}
173
+
174
+ @ Override
175
+ public void onSelectGiphyGif () {
176
+ GiphyDialogFragment .Companion .newInstance ().show (getSupportFragmentManager (), "giphy_dialog" );
177
+ }
164
178
});
165
179
166
180
binding .markdownBottomBarRecyclerViewEditCommentActivity .setLayoutManager (new LinearLayoutManagerBugFixed (this ,
@@ -169,6 +183,8 @@ public void onUploadImage() {
169
183
170
184
binding .commentEditTextEditCommentActivity .requestFocus ();
171
185
Utils .showKeyboard (this , new Handler (), binding .commentEditTextEditCommentActivity );
186
+
187
+ Giphy .INSTANCE .configure (this , APIUtils .GIPHY_GIF_API_KEY );
172
188
}
173
189
174
190
@ Override
@@ -237,9 +253,9 @@ private void editComment() {
237
253
238
254
Map <String , String > params = new HashMap <>();
239
255
params .put (APIUtils .THING_ID_KEY , mFullName );
240
- if (!uploadedImages .isEmpty ()) {
256
+ if (!uploadedImages .isEmpty () || giphyGif != null ) {
241
257
try {
242
- params .put (APIUtils .RICHTEXT_JSON_KEY , new RichTextJSONConverter ().constructRichTextJSON (this , content , uploadedImages ));
258
+ params .put (APIUtils .RICHTEXT_JSON_KEY , new RichTextJSONConverter ().constructRichTextJSON (this , content , uploadedImages , giphyGif ));
243
259
params .put (APIUtils .TEXT_KEY , "" );
244
260
} catch (JSONException e ) {
245
261
isSubmitting = false ;
@@ -314,6 +330,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
314
330
protected void onSaveInstanceState (@ NonNull Bundle outState ) {
315
331
super .onSaveInstanceState (outState );
316
332
outState .putParcelableArrayList (UPLOADED_IMAGES_STATE , uploadedImages );
333
+ outState .putParcelable (GIPHY_GIF_STATE , giphyGif );
317
334
}
318
335
319
336
@ Override
@@ -379,4 +396,32 @@ public void insertImageUrl(UploadedImage uploadedImage) {
379
396
0 , "![]()\n " .length () + uploadedImage .imageUrlOrKey .length ());
380
397
}
381
398
}
399
+
400
+ @ Override
401
+ public void didSearchTerm (@ NonNull String s ) {
402
+
403
+ }
404
+
405
+ @ Override
406
+ public void onGifSelected (@ NonNull Media media , @ Nullable String s , @ NonNull GPHContentType gphContentType ) {
407
+ this .giphyGif = new GiphyGif (media .getId (), true );
408
+
409
+ int start = Math .max (binding .commentEditTextEditCommentActivity .getSelectionStart (), 0 );
410
+ int end = Math .max (binding .commentEditTextEditCommentActivity .getSelectionEnd (), 0 );
411
+ int realStart = Math .min (start , end );
412
+ if (realStart > 0 && binding .commentEditTextEditCommentActivity .getText ().toString ().charAt (realStart - 1 ) != '\n' ) {
413
+ binding .commentEditTextEditCommentActivity .getText ().replace (realStart , Math .max (start , end ),
414
+ "\n \n " ,
415
+ 0 , "\n ![gif]()\n " .length () + giphyGif .id .length ());
416
+ } else {
417
+ binding .commentEditTextEditCommentActivity .getText ().replace (realStart , Math .max (start , end ),
418
+ "\n " ,
419
+ 0 , "![gif]()\n " .length () + giphyGif .id .length ());
420
+ }
421
+ }
422
+
423
+ @ Override
424
+ public void onDismissed (@ NonNull GPHContentType gphContentType ) {
425
+
426
+ }
382
427
}
0 commit comments