1
0
mirror of https://github.com/tateisu/SubwayTooter synced 2024-12-25 00:21:26 +01:00

リファクタ。フォロー後のトーストが複数回出る問題の対策。

This commit is contained in:
tateisu 2017-04-26 10:20:20 +09:00
parent 568d10e289
commit b9a4f6f41a
2 changed files with 124 additions and 106 deletions

View File

@ -126,7 +126,7 @@ public class ActMain extends AppCompatActivity
llEmpty.setVisibility( View.VISIBLE );
}
Uri uri = ActOAuthCallback.last_uri.getAndSet(null);
Uri uri = ActOAuthCallback.last_uri.getAndSet( null );
if( uri != null ){
handleIntentUri( uri );
}
@ -180,14 +180,14 @@ public class ActMain extends AppCompatActivity
@Override
public void onBackPressed(){
DrawerLayout drawer = (DrawerLayout) findViewById( R.id.drawer_layout );
if( drawer.isDrawerOpen( GravityCompat.START ) ){
drawer.closeDrawer( GravityCompat.START );
return;
}
switch( pref.getInt(Pref.KEY_BACK_BUTTON_ACTION,0)){
switch( pref.getInt( Pref.KEY_BACK_BUTTON_ACTION, 0 ) ){
default:
case ActAppSetting.BACK_ASK_ALWAYS:
ActionsDialog dialog = new ActionsDialog();
@ -206,17 +206,17 @@ public class ActMain extends AppCompatActivity
ActMain.this.finish();
}
} );
dialog.show( this,null );
dialog.show( this, null );
break;
case ActAppSetting.BACK_CLOSE_COLUMN:
performColumnClose( false, pager_adapter.getColumn( pager.getCurrentItem() ) );
break;
case ActAppSetting.BACK_EXIT_APP:
ActMain.this.finish();
break;
case ActAppSetting.BACK_OPEN_COLUMN_LIST:
performColumnList();
break;
@ -288,7 +288,6 @@ public class ActMain extends AppCompatActivity
}else if( id == R.id.nav_app_exit ){
finish();
// Handle the camera action
// }else if( id == R.id.nav_gallery ){
//
@ -913,7 +912,7 @@ public class ActMain extends AppCompatActivity
}
public void performMention( SavedAccount account, TootAccount who ){
ActPost.open( this, account.db_id, "@"+account.getFullAcct( who ) + " " );
ActPost.open( this, account.db_id, "@" + account.getFullAcct( who ) + " " );
}
/////////////////////////////////////////////////////////////////////////
@ -1255,60 +1254,56 @@ public class ActMain extends AppCompatActivity
void onRelationChanged();
}
private void callFollow( final SavedAccount access_info, final TootAccount who, final boolean bFollow, final RelationChangedCallback callback ){
private void callFollow( final SavedAccount access_info, final TootAccount who
, final boolean bFollow, final RelationChangedCallback callback ){
new AsyncTask< Void, Void, TootApiResult >() {
@Override
protected TootApiResult doInBackground( Void... params ){
@Override protected TootApiResult doInBackground( Void... params ){
TootApiClient client = new TootApiClient( ActMain.this, new TootApiClient.Callback() {
@Override
public boolean isApiCancelled(){
@Override public boolean isApiCancelled(){
return isCancelled();
}
@Override
public void publishApiProgress( String s ){
@Override public void publishApiProgress( String s ){
}
} );
client.setAccount( access_info );
TootApiResult result;
if( bFollow & who.acct.contains( "@" ) ){
// リモートフォローする
Request.Builder request_builder = new Request.Builder().post(
RequestBody.create(
TootApiClient.MEDIA_TYPE_FORM_URL_ENCODED
, "uri=" + Uri.encode( who.acct )
) );
result = client.request( "/api/v1/follows", request_builder );
if( result != null ){
if( result.object != null ){
remote_who = TootAccount.parse( log, access_info, result.object );
Utils.showToast( ActMain.this, false, R.string.follow_succeeded );
}else if( who.locked && result.response.code() == 422 ){
Utils.showToast( ActMain.this, false, R.string.cant_follow_locked_user );
}else{
Utils.showToast( ActMain.this, false, result.error );
}
}
}else{
// ローカルでフォロー/アンフォローする
Request.Builder request_builder = new Request.Builder().post(
RequestBody.create(
TootApiClient.MEDIA_TYPE_FORM_URL_ENCODED
, "" // 空データ
) );
result = client.request( "/api/v1/accounts/" + who.id + ( bFollow ? "/follow" : "/unfollow" )
result = client.request( "/api/v1/accounts/" + who.id
+ ( bFollow ? "/follow" : "/unfollow" )
, request_builder );
if( result != null ){
if( result.object != null ){
relation = TootRelationShip.parse( log, result.object );
Utils.showToast( ActMain.this, false, bFollow ? R.string.follow_succeeded : R.string.unfollow_succeeded );
}else if( bFollow && who.locked && result.response.code() == 422 ){
Utils.showToast( ActMain.this, false, R.string.cant_follow_locked_user );
}else{
Utils.showToast( ActMain.this, false, result.error );
}
}
}
@ -1335,34 +1330,42 @@ public class ActMain extends AppCompatActivity
// callback.onRelationChanged( App1.relationship_map.get( access_info, remote_who.id ) );
// }
if( relation != null ){
//noinspection StatementWithEmptyBody
if( result == null ){
// cancelled.
}else if( relation != null || remote_who != null ){
// ローカル操作成功もしくはリモートフォロー成功
if( callback != null ) callback.onRelationChanged();
}else if( remote_who != null ){
if( callback != null )
callback.onRelationChanged();
}else if( bFollow && who.locked && result.response.code() == 422 ){
Utils.showToast( ActMain.this, false, R.string.cant_follow_locked_user );
}else{
Utils.showToast( ActMain.this, false, result.error );
}
}
}.execute();
}
// acct で指定したユーザをリモートフォローする
void callRemoteFollow( final SavedAccount access_info, final String acct, final boolean locked, final RelationChangedCallback callback ){
void callRemoteFollow( final SavedAccount access_info
, final String acct, final boolean locked, final RelationChangedCallback callback
){
new AsyncTask< Void, Void, TootApiResult >() {
@Override
protected TootApiResult doInBackground( Void... params ){
@Override protected TootApiResult doInBackground( Void... params ){
TootApiClient client = new TootApiClient( ActMain.this, new TootApiClient.Callback() {
@Override
public boolean isApiCancelled(){
@Override public boolean isApiCancelled(){
return isCancelled();
}
@Override
public void publishApiProgress( String s ){
@Override public void publishApiProgress( String s ){
}
} );
client.setAccount( access_info );
Request.Builder request_builder = new Request.Builder().post(
@ -1370,16 +1373,12 @@ public class ActMain extends AppCompatActivity
TootApiClient.MEDIA_TYPE_FORM_URL_ENCODED
, "uri=" + Uri.encode( acct )
) );
TootApiResult result = client.request( "/api/v1/follows", request_builder );
if( result != null ){
if( result.object != null ){
who = TootAccount.parse( log, access_info, result.object );
Utils.showToast( ActMain.this, false, R.string.follow_succeeded );
}else if( locked && result.response.code() == 422 ){
Utils.showToast( ActMain.this, false, R.string.cant_follow_locked_user );
}else{
Utils.showToast( ActMain.this, false, result.error );
}
}
@ -1395,7 +1394,11 @@ public class ActMain extends AppCompatActivity
@Override
protected void onPostExecute( TootApiResult result ){
if( who != null ){
//noinspection StatementWithEmptyBody
if( result == null ){
// cancelled.
}else if( who != null ){
// App1.relationship_map.addFollowing( access_info, who.id );
// if( callback != null )
// callback.onRelationChanged( App1.relationship_map.get( access_info, who.id ) );
@ -1403,6 +1406,10 @@ public class ActMain extends AppCompatActivity
if( callback != null )
callback.onRelationChanged();
}else if( locked && result.response.code() == 422 ){
Utils.showToast( ActMain.this, false, R.string.cant_follow_locked_user );
}else{
Utils.showToast( ActMain.this, false, result.error );
}
}
}.execute();
@ -1559,6 +1566,7 @@ public class ActMain extends AppCompatActivity
}
}.execute();
}
private void deleteStatus( final SavedAccount access_info, final long status_id ){
new AsyncTask< Void, Void, TootApiResult >() {
@ -1579,7 +1587,7 @@ public class ActMain extends AppCompatActivity
Request.Builder request_builder = new Request.Builder().delete();
TootApiResult result = client.request( "/api/v1/statuses/" + status_id , request_builder );
TootApiResult result = client.request( "/api/v1/statuses/" + status_id, request_builder );
return result;
}
@ -1594,7 +1602,7 @@ public class ActMain extends AppCompatActivity
boolean bOK = false;
if( result != null ){
if( result.object != null ){
Utils.showToast( ActMain.this, false,R.string.delete_succeeded );
Utils.showToast( ActMain.this, false, R.string.delete_succeeded );
for( Column column : pager_adapter.column_list ){
column.removeStatus( access_info, status_id );
}
@ -1606,7 +1614,6 @@ public class ActMain extends AppCompatActivity
}
}.execute();
}
interface ReportCompleteCallback {
void onReportComplete( TootApiResult result );
@ -1694,6 +1701,14 @@ public class ActMain extends AppCompatActivity
Utils.showToast( ActMain.this, false, R.string.follow_succeeded );
}
};
final RelationChangedCallback unfollow_comolete_callback = new RelationChangedCallback() {
// @Override public void onRelationChanged( TootRelationShip relationship ){
// Utils.showToast( ActMain.this,false,R.string.follow_succeeded );
// }
@Override public void onRelationChanged(){
Utils.showToast( ActMain.this, false, R.string.unfollow_succeeded );
}
};
// ステータスのmoreメニュー
public void openStatusMoreMenu( final SavedAccount access_info, final TootStatus status ){
@ -1749,7 +1764,7 @@ public class ActMain extends AppCompatActivity
dialog.addAction( getString( R.string.unfollow ), new Runnable() {
@Override public void run(){
callFollow( access_info, status.account, false, null );
callFollow( access_info, status.account, false, unfollow_comolete_callback );
}
} );
dialog.addAction( getString( R.string.mute ), new Runnable() {
@ -1777,7 +1792,7 @@ public class ActMain extends AppCompatActivity
openReportForm( access_info, status.account, status );
}
} );
if( access_info.isMe( status.account) ){
if( access_info.isMe( status.account ) ){
dialog.addAction( getString( R.string.delete ), new Runnable() {
@Override public void run(){
deleteStatus( access_info, status.id );
@ -1788,7 +1803,6 @@ public class ActMain extends AppCompatActivity
}
public void openAccountMoreMenu( final SavedAccount access_info, final TootAccount who ){
ActionsDialog dialog = new ActionsDialog();
@ -1810,7 +1824,7 @@ public class ActMain extends AppCompatActivity
dialog.addAction( getString( R.string.unfollow ), new Runnable() {
@Override
public void run(){
callFollow( access_info, who, false, null );
callFollow( access_info, who, false, unfollow_comolete_callback );
}
} );
dialog.addAction( getString( R.string.mute ), new Runnable() {

View File

@ -4,6 +4,8 @@ import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
@ -124,7 +126,12 @@ public class SavedAccount extends TootAccount implements LinkClickContext {
return dst;
}
public static long insert( String host, String acct, JSONObject account, JSONObject token ){
public static long insert(
@NonNull String host,
@NonNull String acct,
@NonNull JSONObject account,
@NonNull JSONObject token
){
try{
ContentValues cv = new ContentValues();
cv.put( COL_HOST, host );
@ -133,63 +140,60 @@ public class SavedAccount extends TootAccount implements LinkClickContext {
cv.put( COL_TOKEN, token.toString() );
return App1.getDB().insert( table, null, cv );
}catch( Throwable ex ){
log.e( ex, "saveAccount failed." );
ex.printStackTrace();
throw new RuntimeException( "SavedAccount.insert failed.", ex );
}
return INVALID_ID;
}
public void delete(){
try{
App1.getDB().delete( table, COL_ID + "=?", new String[]{ Long.toString( db_id ) } );
}catch( Throwable ex ){
log.e( ex, "saveAccount failed." );
ex.printStackTrace();
throw new RuntimeException( "SavedAccount.delete failed.", ex );
}
}
public void updateTokenInfo( JSONObject token_info ){
if( db_id != INVALID_ID ){
this.token_info = token_info;
ContentValues cv = new ContentValues();
cv.put( COL_TOKEN, token_info.toString() );
App1.getDB().update( table, cv, COL_ID + "=?", new String[]{ Long.toString( db_id ) } );
}
public void updateTokenInfo( @NonNull JSONObject token_info ){
if( db_id == INVALID_ID )
throw new RuntimeException( "SavedAccount.updateTokenInfo missing db_id" );
this.token_info = token_info;
ContentValues cv = new ContentValues();
cv.put( COL_TOKEN, token_info.toString() );
App1.getDB().update( table, cv, COL_ID + "=?", new String[]{ Long.toString( db_id ) } );
}
public void saveSetting(){
if( db_id != INVALID_ID ){
ContentValues cv = new ContentValues();
cv.put( COL_VISIBILITY, visibility );
cv.put( COL_CONFIRM_BOOST, confirm_boost ? 1 : 0 );
cv.put( COL_DONT_HIDE_NSFW, dont_hide_nsfw ? 1 : 0 );
cv.put( COL_NOTIFICATION_MENTION, notification_mention ? 1 : 0 );
cv.put( COL_NOTIFICATION_BOOST, notification_boost ? 1 : 0 );
cv.put( COL_NOTIFICATION_FAVOURITE, notification_favourite ? 1 : 0 );
cv.put( COL_NOTIFICATION_FOLLOW, notification_follow ? 1 : 0 );
App1.getDB().update( table, cv, COL_ID + "=?", new String[]{ Long.toString( db_id ) } );
}
if( db_id == INVALID_ID )
throw new RuntimeException( "SavedAccount.saveSetting missing db_id" );
ContentValues cv = new ContentValues();
cv.put( COL_VISIBILITY, visibility );
cv.put( COL_CONFIRM_BOOST, confirm_boost ? 1 : 0 );
cv.put( COL_DONT_HIDE_NSFW, dont_hide_nsfw ? 1 : 0 );
cv.put( COL_NOTIFICATION_MENTION, notification_mention ? 1 : 0 );
cv.put( COL_NOTIFICATION_BOOST, notification_boost ? 1 : 0 );
cv.put( COL_NOTIFICATION_FAVOURITE, notification_favourite ? 1 : 0 );
cv.put( COL_NOTIFICATION_FOLLOW, notification_follow ? 1 : 0 );
App1.getDB().update( table, cv, COL_ID + "=?", new String[]{ Long.toString( db_id ) } );
}
// onResumeの時に設定を読み直す
public void reloadSetting(){
if( db_id != INVALID_ID ){
SavedAccount b = loadAccount( log, db_id );
if( b != null ){
this.visibility = b.visibility;
this.confirm_boost = b.confirm_boost;
this.dont_hide_nsfw = b.dont_hide_nsfw;
this.token_info = b.token_info;
this.notification_mention = b.notification_follow;
this.notification_boost = b.notification_boost;
this.notification_favourite = b.notification_favourite;
this.notification_follow = b.notification_follow;
}
}
if( db_id == INVALID_ID )
throw new RuntimeException( "SavedAccount.reloadSetting missing db_id" );
SavedAccount b = loadAccount( log, db_id );
if( b == null ) return; // DBから削除されてる
this.visibility = b.visibility;
this.confirm_boost = b.confirm_boost;
this.dont_hide_nsfw = b.dont_hide_nsfw;
this.token_info = b.token_info;
this.notification_mention = b.notification_follow;
this.notification_boost = b.notification_boost;
this.notification_favourite = b.notification_favourite;
this.notification_follow = b.notification_follow;
}
public static SavedAccount loadAccount( LogCategory log, long db_id ){
public static @Nullable SavedAccount loadAccount( @NonNull LogCategory log, long db_id ){
try{
Cursor cursor = App1.getDB().query( table, null, COL_ID + "=?", new String[]{ Long.toString( db_id ) }, null, null, null );
try{
@ -200,36 +204,36 @@ public class SavedAccount extends TootAccount implements LinkClickContext {
cursor.close();
}
}catch( Throwable ex ){
log.e( ex, "loadToken failed." );
ex.printStackTrace();
log.e( ex, "loadAccount failed." );
}
return null;
}
public static ArrayList< SavedAccount > loadAccountList( LogCategory log ){
public static @NonNull ArrayList< SavedAccount > loadAccountList( @NonNull LogCategory log ){
ArrayList< SavedAccount > result = new ArrayList<>();
try{
Cursor cursor = App1.getDB().query( table, null, null, null, null, null, null );
try{
while( cursor.moveToNext() ){
result.add( parse( cursor ) );
}
return result;
}finally{
cursor.close();
}
}catch( Throwable ex ){
ex.printStackTrace();
log.e( ex, "loadAccountList failed." );
throw new RuntimeException( "SavedAccount.loadAccountList failed.", ex );
}
return null;
return result;
}
public String getFullAcct( TootAccount who ){
return getFullAcct( who == null ? null : who.acct );
public String getFullAcct( @NonNull TootAccount who ){
return getFullAcct( who.acct );
}
public String getFullAcct( String acct ){
if( acct == null ) return "?";
public String getFullAcct( @NonNull String acct ){
if( - 1 != acct.indexOf( '@' ) ){
return acct;
}else{
@ -237,7 +241,7 @@ public class SavedAccount extends TootAccount implements LinkClickContext {
}
}
public boolean isMe( TootAccount who ){
public boolean isMe( @NonNull TootAccount who ){
int pos = this.acct.indexOf( '@' );
String this_user = this.acct.substring( 0, pos );
//
@ -247,7 +251,7 @@ public class SavedAccount extends TootAccount implements LinkClickContext {
return pos == - 1 || this.host.equalsIgnoreCase( who.acct.substring( pos + 1 ) );
}
public boolean isMe( String who_acct ){
public boolean isMe( @NonNull String who_acct ){
int pos = this.acct.indexOf( '@' );
String this_user = this.acct.substring( 0, pos );
//