From c7ebdc50c2e915f1b5de99a333860d634714bad7 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 15 Aug 2018 11:24:57 +0200 Subject: [PATCH] Auto-split long toots --- .../mastodon/activities/TootActivity.java | 37 ++++++++++++++++++- .../mastodon/fragments/SettingsFragment.java | 13 +++++++ .../gouv/etalab/mastodon/helper/Helper.java | 33 +++++++++++++++++ .../res/layout-sw600dp/fragment_settings.xml | 6 +++ app/src/main/res/layout/fragment_settings.xml | 8 ++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 97 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index 0ac76d5cb..b5fd61609 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -50,6 +50,7 @@ import android.text.Html; import android.text.InputFilter; import android.text.InputType; import android.text.TextWatcher; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -192,6 +193,8 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount private String idRedirect; private String userId, instance; private Account account; + private ArrayList splitToot; + private int stepSpliToot; @Override protected void onCreate(Bundle savedInstanceState) { @@ -484,6 +487,17 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount toot_it.setEnabled(true); return; } + boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false); + int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE, Helper.SPLIT_TOOT_SIZE); + String tootContent; + if( !split_toot || (toot_content.getText().toString().trim().length() < split_toot_size)){ + tootContent = toot_content.getText().toString().trim(); + }else{ + + splitToot = Helper.splitToots(toot_content.getText().toString().trim(), split_toot_size); + tootContent = splitToot.get(0); + stepSpliToot = 1; + } Status toot = new Status(); toot.setSensitive(isSensitive); toot.setMedia_attachments(attachments); @@ -492,7 +506,7 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount toot.setVisibility(visibility); if( tootReply != null) toot.setIn_reply_to_id(tootReply.getId()); - toot.setContent(toot_content.getText().toString().trim()); + toot.setContent(tootContent); new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -1549,6 +1563,27 @@ public class TootActivity extends BaseActivity implements OnRetrieveSearcAccount } return; } + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false); + int split_toot_size = sharedpreferences.getInt(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE, Helper.SPLIT_TOOT_SIZE); + + + if( split_toot && (toot_content.getText().toString().trim().length() >= split_toot_size) && stepSpliToot < splitToot.size()){ + String tootContent = splitToot.get(stepSpliToot); + stepSpliToot += 1; + Status toot = new Status(); + toot.setSensitive(isSensitive); + toot.setMedia_attachments(attachments); + if( toot_cw_content.getText().toString().trim().length() > 0) + toot.setSpoiler_text(toot_cw_content.getText().toString().trim()); + toot.setVisibility(visibility); + if( apiResponse.getStatuses() != null) + toot.setIn_reply_to_id(apiResponse.getStatuses().get(0).getId()); + toot.setContent(tootContent); + new PostStatusAsyncTask(getApplicationContext(), accountReply, toot, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + return; + + } if(restored != -1){ SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); new StatusStoredDAO(getApplicationContext(), db).remove(restored); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java index 0d7c269f6..0d7d0daf7 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsFragment.java @@ -671,6 +671,19 @@ public class SettingsFragment extends Fragment { } }); + + boolean split_toot = sharedpreferences.getBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, false); + final CheckBox set_split_toot = rootView.findViewById(R.id.set_automatically_split_toot); + set_split_toot.setChecked(split_toot); + set_split_toot.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_AUTOMATICALLY_SPLIT_TOOTS, set_split_toot.isChecked()); + editor.apply(); + } + }); + //Translators final Spinner translation_layout_spinner = rootView.findViewById(R.id.translation_layout_spinner); ArrayAdapter adapterTrans = ArrayAdapter.createFromResource(getContext(), diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 799e4fed2..4792879a2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -297,6 +297,8 @@ public class Helper { public static final String SET_DISPLAY_GLOBAL = "set_display_global"; public static final String SET_ALLOW_CROSS_ACTIONS = "set_allow_cross_actions"; public static final String SET_DISPLAY_BOOST_COUNT = "set_display_boost_count"; + public static final String SET_AUTOMATICALLY_SPLIT_TOOTS = "set_automatically_split_toots"; + public static final String SET_AUTOMATICALLY_SPLIT_TOOTS_SIZE = "set_automatically_split_toots_size"; //End points public static final String EP_AUTHORIZE = "/oauth/authorize"; @@ -311,6 +313,7 @@ public class Helper { //Refresh job public static final int MINUTES_BETWEEN_NOTIFICATIONS_REFRESH = 15; public static final int MINUTES_BETWEEN_HOME_TIMELINE = 30; + public static final int SPLIT_TOOT_SIZE = 500; //Translate wait time public static final String LAST_TRANSLATION_TIME = "last_translation_time"; @@ -2202,4 +2205,34 @@ public class Helper { pagerAdapter.addTabPage(title); } + + /** + * Allows to split the toot by dot "." for sentences - adds number at the end automatically + * @param content String initial content + * @param maxChars int the max chars per toot (minus 10 to write the page: 1/x, 2/x etc.) + * @return ArrayList split toot + */ + public static ArrayList splitToots(String content, int maxChars){ + String[] splitContent = content.split("\\."); + ArrayList splitToot = new ArrayList<>(); + StringBuilder tempContent = new StringBuilder(splitContent[0]); + for(int i= 0 ; i < splitContent.length ; i++){ + if( i < (splitContent.length-1) && (tempContent.length() + splitContent[i+1].length()) < (maxChars-10)) { + tempContent.append(".").append(splitContent[i + 1]); + }else { + splitToot.add(tempContent.toString()); + if( i < (splitContent.length-1) ) + tempContent = new StringBuilder(splitContent[i+1]); + } + } + int i=1; + ArrayList reply = new ArrayList<>(); + for(String newContent : splitToot){ + reply.add((i-1), newContent + " - " + i + "/" + splitToot.size()); + i++; + } + return reply; + } + + } diff --git a/app/src/main/res/layout-sw600dp/fragment_settings.xml b/app/src/main/res/layout-sw600dp/fragment_settings.xml index 79bcfcbd1..e10c4b8c1 100644 --- a/app/src/main/res/layout-sw600dp/fragment_settings.xml +++ b/app/src/main/res/layout-sw600dp/fragment_settings.xml @@ -178,6 +178,12 @@ android:text="@string/set_share_details" android:layout_height="wrap_content" /> + + + + + + Display profile pictures? Allow interactions between accounts? Fit preview images + Automatically split toots over 500 chars in replies You have reached the 160 characters allowed! You have reached the 30 characters allowed!