Allow preview for split feature

This commit is contained in:
tom79 2019-10-06 16:35:08 +02:00
parent a59298fb33
commit b1335f6f5c
6 changed files with 202 additions and 32 deletions

View File

@ -34,7 +34,6 @@ import androidx.core.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -164,7 +163,6 @@ public class InstanceHealthActivity extends BaseActivity {
HashMap<String, String> parameters = new HashMap<>();
parameters.put("name", instance.trim());
final String response = new HttpsConnection(InstanceHealthActivity.this, instance).get("https://instances.social/api/1.0/instances/show", 30, parameters, Helper.THEKINRAR_SECRET_TOKEN);
Log.v(Helper.TAG,"response: " + response);
if (response != null)
instanceSocial = API.parseInstanceSocialResponse(getApplicationContext(), new JSONObject(response));
runOnUiThread(new Runnable() {

View File

@ -42,6 +42,7 @@ import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
@ -173,6 +174,7 @@ import app.fedilab.android.drawers.AccountsSearchAdapter;
import app.fedilab.android.drawers.CustomEmojiAdapter;
import app.fedilab.android.drawers.DraftsListAdapter;
import app.fedilab.android.drawers.EmojisSearchAdapter;
import app.fedilab.android.drawers.SliderAdapter;
import app.fedilab.android.drawers.SuggestionsAdapter;
import app.fedilab.android.drawers.TagsSearchAdapter;
import app.fedilab.android.helper.FileNameCleaner;
@ -2217,9 +2219,59 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
}
createAndSendToot(tootContent, content_type, timestamp);
} else {
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size);
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size, true);
tootContent = splitToot.get(0);
stepSpliToot = 1;
AlertDialog.Builder builderInner = new AlertDialog.Builder(TootActivity.this, style);
builderInner.setTitle(R.string.message_preview);
View preview = getLayoutInflater().inflate(R.layout.popup_message_preview, new LinearLayout(getApplicationContext()), false);
builderInner.setView(preview);
//Text for report
final TextView textView = preview.findViewById(R.id.preview);
textView.setText("");
final SwitchCompat report_mention = preview.findViewById(R.id.report_mention);
int finalSplit_toot_size = split_toot_size;
report_mention.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), finalSplit_toot_size, isChecked);
textView.setText("");
int inc = 0;
for(String prev: splitToot){
if( inc < splitToot.size()-1) {
textView.setText(textView.getText() + prev + "\n----------\n");
}
}
}
});
int inc = 0;
for(String prev: splitToot){
if( inc < splitToot.size()-1) {
textView.setText(textView.getText() + prev + "\n----------\n");
}
}
builderInner.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toot_it.setEnabled(true);
dialog.dismiss();
}
});
builderInner.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createAndSendToot(tootContent, content_type, timestamp);
dialog.dismiss();
}
});
AlertDialog alertDialog = builderInner.create();
alertDialog.show();
}
@ -2628,10 +2680,6 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate,
}
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS + userId + instance, false);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE + userId + instance, Helper.SPLIT_TOOT_SIZE);
int cwSize = toot_cw_content.getText().toString().trim().length();
int size = toot_content.getText().toString().trim().length() + cwSize;
if (split_toot && splitToot != null && stepSpliToot < splitToot.size()) {
String tootContent = splitToot.get(stepSpliToot);

View File

@ -35,6 +35,7 @@ import android.os.Environment;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.TooltipCompat;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
@ -69,6 +70,7 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
@ -518,13 +520,11 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
}
}
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false);
final String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE + userId + instance, Helper.SPLIT_TOOT_SIZE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS + userId + instance, false);
int cwSize = toot_cw_content.getText().toString().trim().length();
int size = toot_content.getText().toString().trim().length() + cwSize;
if (split_toot && splitToot != null && stepSpliToot < splitToot.size()) {
String tootContent = splitToot.get(stepSpliToot);
@ -4102,21 +4102,84 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
return;
}
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false);
final String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE + userId + instance, Helper.SPLIT_TOOT_SIZE);
boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS + userId + instance, false);
String tootContent;
if (toot_cw_content.getText() != null && toot_cw_content.getText().toString().trim().length() > 0)
split_toot_size -= toot_cw_content.getText().toString().trim().length();
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || !split_toot || (TootActivity.countLength(social, toot_content, toot_cw_content) < split_toot_size)) {
tootContent = toot_content.getText().toString().trim();
createAndSendToot(tootContent, content_type, userId, instance);
} else {
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size);
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size, true);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
tootContent = splitToot.get(0);
stepSpliToot = 1;
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK) {
style = R.style.DialogBlack;
} else {
style = R.style.Dialog;
}
AlertDialog.Builder builderInner = new AlertDialog.Builder(context, style);
builderInner.setTitle(R.string.message_preview);
View preview = ((Activity) context).getLayoutInflater().inflate(R.layout.popup_message_preview, new LinearLayout(context), false);
builderInner.setView(preview);
//Text for report
final TextView textView = preview.findViewById(R.id.preview);
textView.setText("");
final SwitchCompat report_mention = preview.findViewById(R.id.report_mention);
int finalSplit_toot_size = split_toot_size;
report_mention.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
splitToot = Helper.splitToots(toot_content.getText().toString().trim(), finalSplit_toot_size, isChecked);
textView.setText("");
int inc = 0;
for(String prev: splitToot){
if( inc < splitToot.size()-1) {
textView.setText(textView.getText() + prev + "\n----------\n");
}
}
}
});
int inc = 0;
for(String prev: splitToot){
if( inc < splitToot.size()-1) {
textView.setText(textView.getText() + prev + "\n----------\n");
}
}
builderInner.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builderInner.setPositiveButton(R.string.validate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createAndSendToot(tootContent, content_type, userId, instance);
dialog.dismiss();
}
});
AlertDialog alertDialog = builderInner.create();
alertDialog.show();
}
}
private void createAndSendToot(String tootContent, String content_type, String userId, String instance){
Status toot = new Status();
if (content_type != null)
toot.setContentType(content_type);
@ -4132,6 +4195,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
}
private void bookmark(Status status) {
if (type != RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS) {
status.setBookmarked(!status.isBookmarked());

View File

@ -3791,30 +3791,33 @@ public class Helper {
* @param maxChars int the max chars per toot (minus 10 to write the page: 1/x, 2/x etc.)
* @return ArrayList<String> split toot
*/
public static ArrayList<String> splitToots(String content, int maxChars) {
public static ArrayList<String> splitToots(String content, int maxChars, boolean reportMentions) {
String[] splitContent = content.split("\\s");
ArrayList<String> mentions = new ArrayList<>();
Matcher matcher = mentionLongPattern.matcher(content);
while (matcher.find()) {
String mentionLong = matcher.group(1);
if (!mentions.contains(mentionLong)) {
mentions.add(mentionLong);
}
}
matcher = mentionPattern.matcher(content);
while (matcher.find()) {
String mention = matcher.group(1);
if (!mentions.contains(mention)) {
mentions.add(mention);
}
}
int mentionLength = 0;
StringBuilder mentionString = new StringBuilder();
for (String mention : mentions) {
mentionString.append(mention).append(" ");
if( reportMentions) {
Matcher matcher = mentionLongPattern.matcher(content);
while (matcher.find()) {
String mentionLong = matcher.group(1);
if (!mentions.contains(mentionLong)) {
mentions.add(mentionLong);
}
}
matcher = mentionPattern.matcher(content);
while (matcher.find()) {
String mention = matcher.group(1);
if (!mentions.contains(mention)) {
mentions.add(mention);
}
}
for (String mention : mentions) {
mentionString.append(mention).append(" ");
}
mentionLength = mentionString.length() + 1;
}
int mentionLength = mentionString.length() + 1;
int maxCharsPerMessage = (maxChars - 10) - mentionLength;
int totalCurrent = 0;
ArrayList<String> reply = new ArrayList<>();

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2019 Thomas Schneider
This file is a part of Fedilab
This program is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with Fedilab; if not,
see <http://www.gnu.org/licenses>.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="app.fedilab.android.activities.TootActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ScrollView
android:layout_margin="@dimen/fab_margin"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<TextView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="@dimen/settings_option_margin"
android:layout_marginBottom="@dimen/settings_option_margin"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="@string/report_mention"
android:textSize="16sp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/report_mention"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

View File

@ -1233,4 +1233,6 @@
<string name="upload_media">Upload media</string>
<string name="pixelfed_compose_placeholder">Add an optional caption</string>
<string name="long_api_error">The app received a very long error message from the API %1$s</string>
<string name="message_preview">Message preview</string>
<string name="report_mention">Add mentions in each message</string>
</resources>