投稿する時にアンケート作成できるようにした。一部タンスでのみ有効

This commit is contained in:
tateisu 2017-08-25 23:15:24 +09:00
parent 4bc1984feb
commit 4441006228
7 changed files with 391 additions and 145 deletions

View File

@ -9,8 +9,8 @@ android {
applicationId "jp.juggler.subwaytooter"
minSdkVersion 21
targetSdkVersion 26
versionCode 125
versionName "1.2.5"
versionCode 126
versionName "1.2.6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

View File

@ -395,6 +395,7 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
try{
TootStatus reply_status = TootStatus.parse( ActPost.this, account, new JSONObject( sv ) );
if( reply_status !=null ){
// CW をリプライ元に合わせる
if( ! TextUtils.isEmpty( reply_status.spoiler_text ) ){
cbContentWarning.setChecked( true );
@ -464,6 +465,8 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
log.trace( ex );
}
}
}catch( Throwable ex ){
log.trace( ex );
}
@ -487,8 +490,10 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
showVisibility();
updateTextCount();
showReplyTo();
showEnquete();
}
@Override protected void onDestroy(){
post_helper.onDestroy();
@ -536,6 +541,7 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
showVisibility();
updateTextCount();
showReplyTo();
showEnquete();
}
Button btnAccount;
@ -548,6 +554,11 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
CheckBox cbContentWarning;
MyEditText etContentWarning;
MyEditText etContent;
CheckBox cbEnquete;
View llEnquete;
final MyEditText[] list_etChoice = new MyEditText[4];
TextView tvCharCount;
Handler handler;
View formRoot;
@ -578,26 +589,34 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
Styler.fixHorizontalMargin( findViewById( R.id.llFooterBar ) );
formRoot = findViewById( R.id.viewRoot );
scrollView = (ScrollView) findViewById( R.id.scrollView );
btnAccount = (Button) findViewById( R.id.btnAccount );
btnVisibility = (ImageButton) findViewById( R.id.btnVisibility );
scrollView = findViewById( R.id.scrollView );
btnAccount = findViewById( R.id.btnAccount );
btnVisibility = findViewById( R.id.btnVisibility );
btnAttachment = findViewById( R.id.btnAttachment );
btnPost = findViewById( R.id.btnPost );
llAttachment = findViewById( R.id.llAttachment );
ivMedia[ 0 ] = (MyNetworkImageView) findViewById( R.id.ivMedia1 );
ivMedia[ 1 ] = (MyNetworkImageView) findViewById( R.id.ivMedia2 );
ivMedia[ 2 ] = (MyNetworkImageView) findViewById( R.id.ivMedia3 );
ivMedia[ 3 ] = (MyNetworkImageView) findViewById( R.id.ivMedia4 );
cbNSFW = (CheckBox) findViewById( R.id.cbNSFW );
cbContentWarning = (CheckBox) findViewById( R.id.cbContentWarning );
etContentWarning = (MyEditText) findViewById( R.id.etContentWarning );
etContent = (MyEditText) findViewById( R.id.etContent );
tvCharCount = (TextView) findViewById( R.id.tvCharCount );
ivMedia[ 0 ] = findViewById( R.id.ivMedia1 );
ivMedia[ 1 ] = findViewById( R.id.ivMedia2 );
ivMedia[ 2 ] = findViewById( R.id.ivMedia3 );
ivMedia[ 3 ] = findViewById( R.id.ivMedia4 );
cbNSFW = findViewById( R.id.cbNSFW );
cbContentWarning = findViewById( R.id.cbContentWarning );
etContentWarning = findViewById( R.id.etContentWarning );
etContent = findViewById( R.id.etContent );
cbEnquete = findViewById( R.id.cbEnquete );
llEnquete = findViewById( R.id.llEnquete );
list_etChoice[0] = findViewById( R.id.etChoice1 );
list_etChoice[1] = findViewById( R.id.etChoice2 );
list_etChoice[2] = findViewById( R.id.etChoice3 );
list_etChoice[3] = findViewById( R.id.etChoice4 );
tvCharCount = findViewById( R.id.tvCharCount );
llReply = findViewById( R.id.llReply );
tvReplyTo = (TextView) findViewById( R.id.tvReplyTo );
tvReplyTo = findViewById( R.id.tvReplyTo );
btnRemoveReply = findViewById( R.id.btnRemoveReply );
ivReply = (MyNetworkImageView) findViewById( R.id.ivReply );
ivReply = findViewById( R.id.ivReply );
account_list = SavedAccount.loadAccountList( ActPost.this, log );
Collections.sort( account_list, new Comparator< SavedAccount >() {
@ -628,6 +647,12 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
updateContentWarning();
}
} );
cbEnquete.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ){
showEnquete();
}
} );
post_helper = new PostHelper( this, pref, app_state.handler );
post_helper.attachEditText( formRoot, etContent, false, new PostHelper.Callback2() {
@ -1402,6 +1427,15 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
private void performPost(){
post_helper.content = etContent.getText().toString().trim();
if( ! cbEnquete.isChecked() ){
post_helper.enquete_items = null;
}else{
post_helper.enquete_items = new ArrayList<>();
for( MyEditText et : list_etChoice ){
post_helper.enquete_items.add( et.getText().toString().trim() );
}
}
if( ! cbContentWarning.isChecked() ){
post_helper.spoiler_text = null;
}else{
@ -1467,14 +1501,30 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
static final String DRAFT_REPLY_TEXT = "reply_text";
static final String DRAFT_REPLY_IMAGE = "reply_image";
static final String DRAFT_REPLY_URL = "reply_url";
static final String DRAFT_IS_ENQUETE = "is_enquete";
static final String DRAFT_ENQUETE_ITEMS = "enquete_items";
private void saveDraft(){
String content = etContent.getText().toString();
String content_warning = etContentWarning.getText().toString();
if( TextUtils.isEmpty( content.trim() ) && TextUtils.isEmpty( content_warning.trim() ) ){
String content_warning = cbContentWarning.isChecked() ? etContentWarning.getText().toString() : "";
boolean isEnquete = cbEnquete.isChecked();
String[] str_choice = new String[]{
isEnquete ? list_etChoice[0].getText().toString():"",
isEnquete ? list_etChoice[1].getText().toString():"",
isEnquete ? list_etChoice[2].getText().toString():"",
isEnquete ? list_etChoice[3].getText().toString():"",
};
boolean hasContent = false;
if( !TextUtils.isEmpty( content.trim() ) ) hasContent = true;
if( !TextUtils.isEmpty( content_warning.trim() ) ) hasContent = true;
for(String s :str_choice ){
if( !TextUtils.isEmpty( s.trim() ) ) hasContent = true;
}
if(!hasContent ){
log.d( "saveDraft: dont save empty content" );
return;
}
try{
JSONArray tmp_attachment_list = new JSONArray();
if( attachment_list != null ){
@ -1496,6 +1546,13 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
json.put( DRAFT_REPLY_IMAGE, in_reply_to_image );
json.put( DRAFT_REPLY_URL, in_reply_to_url );
json.put( DRAFT_IS_ENQUETE, isEnquete );
JSONArray array = new JSONArray( );
for(String s : str_choice){
array.put(s);
}
json.put( DRAFT_ENQUETE_ITEMS, array );
PostDraft.save( System.currentTimeMillis(), json );
}catch( Throwable ex ){
@ -1642,6 +1699,20 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
cbNSFW.setChecked( nsfw_checked );
ActPost.this.visibility = visibility;
cbEnquete.setChecked( draft.optBoolean( DRAFT_IS_ENQUETE ,false) );
JSONArray array = draft.optJSONArray( DRAFT_ENQUETE_ITEMS );
if( array != null ){
int src_index = 0;
for(MyEditText et : list_etChoice){
if( src_index < array.length() ){
et.setText( array.optString( src_index ));
++src_index;
}else{
et.setText( "");
}
}
}
if( account != null ) setAccount( account );
if( tmp_attachment_list.length() > 0 ){
@ -1670,6 +1741,7 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
showVisibility();
updateTextCount();
showReplyTo();
showEnquete();
if( ! list_warning.isEmpty() ){
StringBuilder sb = new StringBuilder();
@ -1784,7 +1856,7 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
@SuppressLint("InflateParams")
View viewRoot = getLayoutInflater().inflate( R.layout.dlg_plugin_missing, null, false );
TextView tvText = (TextView) viewRoot.findViewById( R.id.tvText );
TextView tvText = viewRoot.findViewById( R.id.tvText );
LinkClickContext lcc = new LinkClickContext() {
@Override public AcctColor findAcctColor( String url ){
return null;
@ -1794,7 +1866,7 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
tvText.setText( sv );
tvText.setMovementMethod( LinkMovementMethod.getInstance() );
TextView tvTitle = (TextView) viewRoot.findViewById( R.id.tvTitle );
TextView tvTitle = viewRoot.findViewById( R.id.tvTitle );
if( TextUtils.isEmpty( title ) ){
tvTitle.setVisibility( View.GONE );
}else{
@ -1823,4 +1895,10 @@ public class ActPost extends AppCompatActivity implements View.OnClickListener,
}
}
};
private void showEnquete(){
llEnquete.setVisibility( cbEnquete.isChecked() ? View.VISIBLE : View.GONE );
}
}

View File

@ -14,6 +14,10 @@ import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -37,7 +41,6 @@ import okhttp3.RequestBody;
public class PostHelper {
private static final LogCategory log = new LogCategory( "PostHelper" );
public interface Callback{
void onPostComplete(SavedAccount target_account,TootStatus status);
}
@ -72,6 +75,7 @@ public class PostHelper {
public boolean bNSFW;
public long in_reply_to_id;
public ArrayList< PostAttachment > attachment_list;
public ArrayList< String > enquete_items;
public void post( final SavedAccount account,final boolean bConfirmTag, final boolean bConfirmAccount ,final Callback callback){
if( TextUtils.isEmpty( content ) ){
@ -122,7 +126,11 @@ public class PostHelper {
}
}
final StringBuilder sb = new StringBuilder();
RequestBody request_body;
String body_string;
if( enquete_items == null || enquete_items.isEmpty() ){
StringBuilder sb = new StringBuilder();
sb.append( "status=" );
sb.append( Uri.encode( content ) );
@ -152,6 +160,50 @@ public class PostHelper {
}
}
body_string = sb.toString();
request_body = RequestBody.create(
TootApiClient.MEDIA_TYPE_FORM_URL_ENCODED
, body_string
);
}else{
JSONObject json = new JSONObject( );
try{
json.put("status",content);
json.put("visibility",visibility);
json.put("sensitive",bNSFW);
json.put("spoiler_text",TextUtils.isEmpty( spoiler_text ) ? "" : spoiler_text );
json.put("in_reply_to_id", in_reply_to_id == -1L ? null : in_reply_to_id );
JSONArray array = new JSONArray();
if( attachment_list != null ){
for( PostAttachment pa : attachment_list ){
if( pa.attachment != null ){
array.put( pa.attachment.id );
}
}
}
json.put("media_ids",array);
json.put("isEnquete",true);
array = new JSONArray();
for( String item : enquete_items ){
array.put( item );
}
json.put("enquete_items",array);
}catch(JSONException ex){
log.trace( ex );
log.e(ex,"status encoding failed.");
}
body_string = json.toString();
request_body = RequestBody.create(
TootApiClient.MEDIA_TYPE_JSON
, body_string
);
}
final Request.Builder request_builder = new Request.Builder()
.post(request_body );
final String digest =Utils.digestSHA256( body_string + account.acct );
final ProgressDialog progress = new ProgressDialog( activity );
final AsyncTask< Void, Void, TootApiResult > task = new AsyncTask< Void, Void, TootApiResult >() {
@ -175,14 +227,7 @@ public class PostHelper {
} );
client.setAccount( target_account );
String post_content = sb.toString();
String digest = Utils.digestSHA256( post_content + target_account.acct );
Request.Builder request_builder = new Request.Builder()
.post( RequestBody.create(
TootApiClient.MEDIA_TYPE_FORM_URL_ENCODED
, post_content
) );
if( ! pref.getBoolean( Pref.KEY_DONT_DUPLICATION_CHECK, false ) ){
request_builder.header( "Idempotency-Key", digest );
@ -191,7 +236,7 @@ public class PostHelper {
TootApiResult result = client.request( "/api/v1/statuses", request_builder );
if( result != null && result.object != null ){
status = TootStatus.parse( activity, account, result.object );
if( status != null ){
Spannable s = status.decoded_content;
MyClickableSpan[] span_list = s.getSpans( 0, s.length(), MyClickableSpan.class );
if( span_list != null ){
@ -215,6 +260,7 @@ public class PostHelper {
}
}
}
}
return result;

View File

@ -78,9 +78,9 @@
android:layout_height="40dp"
android:layout_marginStart="4dp"
android:background="@drawable/btn_bg_transparent"
android:contentDescription="@string/delete"
android:gravity="center_vertical"
android:src="?attr/btn_close"
android:contentDescription="@string/delete"
/>
</LinearLayout>
</LinearLayout>
@ -166,7 +166,6 @@
/>
</LinearLayout>
<CheckBox
android:id="@+id/cbContentWarning"
android:layout_width="match_parent"
@ -215,6 +214,115 @@
</FrameLayout>
<CheckBox
android:id="@+id/cbEnquete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/make_enquete"
/>
<LinearLayout
android:id="@+id/llEnquete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/choice1"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPostFormBackground"
>
<jp.juggler.subwaytooter.view.MyEditText
android:id="@+id/etChoice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:inputType="text"
/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/choice2"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPostFormBackground"
>
<jp.juggler.subwaytooter.view.MyEditText
android:id="@+id/etChoice2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:inputType="text"
/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/choice3"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPostFormBackground"
>
<jp.juggler.subwaytooter.view.MyEditText
android:id="@+id/etChoice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:inputType="text"
/>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="@string/choice4"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPostFormBackground"
>
<jp.juggler.subwaytooter.view.MyEditText
android:id="@+id/etChoice4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:inputType="text"
/>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
@ -275,7 +383,6 @@
android:layout_weight="1"
/>
<TextView
android:id="@+id/tvCharCount"
android:layout_width="wrap_content"

View File

@ -448,6 +448,11 @@
<string name="enquete_was_end">Enquete was end.</string>
<string name="enquete_voted">Voted!</string>
<string name="enquete_vote_failed">Vote failed. %1$s</string>
<string name="choice1">choice 1</string>
<string name="choice2">choice 2</string>
<string name="choice3">choice 3(optional)</string>
<string name="choice4">choice 4(optional)</string>
<string name="make_enquete">Make enquete</string>
<!--<string name="abc_action_bar_home_description">Revenir à l\'accueil</string>-->
<!--<string name="abc_action_bar_home_description_format">%1$s, %2$s</string>-->

View File

@ -736,5 +736,10 @@
<string name="enquete_was_end">投票は終わってる</string>
<string name="enquete_voted">投票しました</string>
<string name="enquete_vote_failed">投票に失敗。%1$s</string>
<string name="choice1">項目1</string>
<string name="choice2">項目2</string>
<string name="choice3">項目3(オプション)</string>
<string name="choice4">項目4(オプション)</string>
<string name="make_enquete">アンケートを作成する(一部タンスのみ有効)</string>
</resources>

View File

@ -444,5 +444,10 @@
<string name="enquete_was_end">Enquete was end.</string>
<string name="enquete_voted">Voted!</string>
<string name="enquete_vote_failed">Vote failed. %1$s</string>
<string name="choice1">choice 1</string>
<string name="choice2">choice 2</string>
<string name="choice3">choice 3(optional)</string>
<string name="choice4">choice 4(optional)</string>
<string name="make_enquete">Make enquete</string>
</resources>