Skip to content

Instantly share code, notes, and snippets.

@yanzm
Created April 24, 2015 17:36
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yanzm/d0a759300cfa1356c0b5 to your computer and use it in GitHub Desktop.
Save yanzm/d0a759300cfa1356c0b5 to your computer and use it in GitHub Desktop.
package net.yanzm.profileapplication;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity2 extends Activity implements NetworkFragment.NetworkFragmentListener {
private static final int REQUEST_CODE_PICK_IMAGE = 1;
private ProfileView profileView;
private Button sendButton;
private View progressView;
private View profileContainer;
private NetworkFragment networkFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
assert getActionBar() != null;
getActionBar().setDisplayHomeAsUpEnabled(true);
// ビューの設定
progressView = findViewById(R.id.progress);
profileContainer = findViewById(R.id.profile_container);
profileView = (ProfileView) findViewById(R.id.profile);
profileView.setOnImagePickListener(new ProfileView.OnImagePickListener() {
@Override
public void onRequestImagePick() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
});
sendButton = (Button) findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// バリデーション
if (profileView.validate()) {
send();
}
}
});
final FragmentManager fragmentManager = getFragmentManager();
networkFragment = (NetworkFragment) fragmentManager.findFragmentByTag("NetworkFragment");
if (networkFragment == null) {
networkFragment = new NetworkFragment();
networkFragment.setRetainInstance(true);
fragmentManager.beginTransaction()
.add(networkFragment, "NetworkFragment")
.commit();
}
// 画面回転対応
if (savedInstanceState != null) {
switch (networkFragment.getStatus()) {
case NetworkFragment.STATUS_LOADING:
progressView.setVisibility(View.VISIBLE);
profileContainer.setVisibility(View.GONE);
break;
case NetworkFragment.STATUS_SENDING:
progressView.setVisibility(View.VISIBLE);
break;
default:
progressView.setVisibility(View.GONE);
}
return;
}
// プログレスを表示
progressView.setVisibility(View.VISIBLE);
// (同期)編集フォームを非表示or無効化
profileContainer.setVisibility(View.GONE);
// 1. サーバーからプロフィールデータを取得
networkFragment.loadProfileData();
}
@Override
public void onDataLoaded(NetworkUtils.ProfileData profileData) {
// 2. プロフィールデータを表示
profileView.setName(profileData.name);
profileView.setBio(profileData.bio);
profileView.setExperience(profileData.experience);
profileView.setEventStatus(profileData.eventStatus);
profileView.setFavoriteDesserts(profileData.favoriteDessert);
// 編集フォームを表示or有効化
profileContainer.setVisibility(View.VISIBLE);
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
ObjectAnimator.ofFloat(profileContainer, "alpha", 0f, 1f),
ObjectAnimator.ofFloat(progressView, "alpha", 1f, 0f)
);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animatorSet.removeListener(this);
// プログレスを非表示
progressView.setVisibility(View.GONE);
}
});
animatorSet.start();
}
private void send() {
final Uri imageUri = profileView.getThumbnailImageUri();
final NetworkUtils.ProfileData profileData = new NetworkUtils.ProfileData();
profileData.name = profileView.getName();
profileData.bio = profileView.getBio();
profileData.experience = profileView.getExperience();
profileData.eventStatus = profileView.getEventStatus();
profileData.favoriteDessert = profileView.getFavoriteDesserts();
// 送信ボタン無効化
sendButton.setEnabled(false);
// プログレスを表示
progressView.setVisibility(View.VISIBLE);
progressView.animate().alpha(1f);
// 5. データの更新をサーバーに送信
networkFragment.sendProfileData(imageUri, profileData);
}
@Override
public void onDataSent(boolean isSuccess) {
if (isFinishing()) {
return;
}
if (isSuccess) {
Toast.makeText(MainActivity2.this, "更新しました", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(MainActivity2.this, "更新できませんでした", Toast.LENGTH_SHORT).show();
// 送信ボタン有効化
sendButton.setEnabled(true);
final ObjectAnimator animator = ObjectAnimator.ofFloat(progressView, "alpha", 1f, 0f);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animator.removeListener(this);
// プログレスを非表示
progressView.setVisibility(View.GONE);
}
});
animator.start();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
if (resultCode != RESULT_OK) {
return;
}
profileView.setThumbnailFromUri(data.getData());
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment