package com.example.nemophila; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.example.nemophila.entities.Post; import com.example.nemophila.viewmodels.AccountViewModel; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Base64; import java.util.Collection; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import static android.os.AsyncTask.execute; public class MyPageActivity extends AppCompatActivity { private final ExecutorService executor = Executors.newSingleThreadExecutor(); private final Handler handler = new Handler(); private String iconUrlString; //MyPageActivity画面の作成 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mypage); //初期化 Nemophila nemophila = (Nemophila) this.getApplication(); String uid = nemophila.getUid(); String name = nemophila.getName(); Bitmap icon = nemophila.getIcon(); String token = nemophila.getToken(); TextView myName = (TextView) findViewById(R.id.name); myName.setText(name); TextView id = (TextView) findViewById(R.id.id); id.setText(uid); //アイコンを表示する ImageView iconView = findViewById(R.id.icon); iconView.setImageBitmap(icon); // iconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/icon"+uid+".jpg";; // setIcon(icon); // 「閉じる」ボタンを押したときにメイン画面へ遷移する Button closeButton = (Button) findViewById(R.id.close); closeButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, MapsActivity.class); startActivity(intent); } }); //「フレンドリスト」ボタンを押したときにフレンドリスト画面へ遷移する Button friendButton = (Button) findViewById(R.id.friend); friendButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, FriendActivity.class); startActivity(intent); } }); // 「フレンド許可する」ボタンを押したときにフレンド許可画面へ遷移する Button requestedButton = (Button) findViewById(R.id.requested); requestedButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, RequestedActivity.class); startActivity(intent); } }); // 「マイページ編集」ボタンを押したときにマイページ編集画面へ遷移する Button myPageEditButton = (Button) findViewById(R.id.myPageEdit); myPageEditButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, MyPageEditorActivity.class); startActivity(intent); } }); //getNameLiveDataとの通信により今のユーザーネームを表示する AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class); accountViewModel.getNameLiveData().observe(this, new Observer <String>() { @Override public void onChanged(String name) { TextView myName = (TextView) findViewById(R.id.name); myName.setText(name); nemophila.setName(name); } }); RecyclerView rv = findViewById(R.id.postsList); LinearLayoutManager llm = new LinearLayoutManager(this); List<MyPageActDataModel> myPageActDataset = new ArrayList<>(); MyPageActAdapter adapter = new MyPageActAdapter(myPageActDataset) { // 「(投稿)削除」ボタンを押した後、「Yes」を押すと対象の投稿を削除する @Override void onDeleteButtonClick(View view, int position, MyPageActDataModel dataModel) { new AlertDialog.Builder(MyPageActivity.this) .setTitle("警告") .setMessage("この投稿を削除しますか?\n削除すると元に戻せなくなります!") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MyPageActivity.this, "投稿を削除しました。", Toast.LENGTH_SHORT).show(); System.out.println("投稿を削除しました。"); accountViewModel.deleteAccountPost(uid, dataModel.getPid(), token); } }) .setNegativeButton("No", null) .create() .show(); } }; rv.setHasFixedSize(true); rv.setLayoutManager(llm); rv.setAdapter(adapter); // getAccountPostsLiveDataとの通信により今までにユーザーがしている投稿を表示する accountViewModel.getAccountPosts(uid); accountViewModel.getAccountPostsLiveData().observe(this, new Observer <Collection<Post>>() { @Override public void onChanged(Collection<Post> Posts) { //MyPageActDatasetを一旦クリアし、LiveDataから受け取ったデータを一つずつセット myPageActDataset.clear(); for (Post post: Posts){ MyPageActDataModel data = new MyPageActDataModel(); data.setDate(post.getDate()); data.setShopName(post.getShopName()); data.setGenre(post.getGenre()); //rateはintで受け取った評価に対応した星の数のStringに変換してセット int intRate = post.getRate(); String strRate = ""; for (int i = 0; i < 5; i++){ if(intRate > i ){ strRate += "★"; }else{ strRate += "☆"; } } data.setRate(strRate); data.setComment(post.getComment()); //画像がなければViewの表示をgoneに、あればvisibleにして画像をセット executor.execute(() -> { String[] imageUrls = new String[3]; Bitmap[] bitmaps = new Bitmap[3]; for(int i=1; i<=3; i++) { imageUrls[i-1] = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/post_image"+ uid +"-" + post.getPid() +"-" + i +".jpg"; try { URL image1Url = new URL(imageUrls[i - 1]); HttpURLConnection con = (HttpURLConnection) image1Url.openConnection(); con.connect(); InputStream is = con.getInputStream(); bitmaps[i - 1] = BitmapFactory.decodeStream(is); is.close(); con.disconnect(); } catch (Exception e) { e.printStackTrace(); bitmaps [i-1] = null; } } handler.post(() -> { for(int i=1; i<=3; i++) { if (i == 1) { data.setImage1(bitmaps[i - 1]); } else if (i == 2) { data.setImage2(bitmaps[i - 1]); } else if (i == 3) { data.setImage3(bitmaps[i - 1]); } } adapter.notifyDataSetChanged(); }); }); data.setPid(post.getPid()); myPageActDataset.add(data); } //RecyclerViewをonChangedが呼ばれるたび表示(アダプターを更新してRecyclerViewにセット) adapter.setList(myPageActDataset); rv.setAdapter(adapter); } }); } // Adapter abstract public class MyPageActAdapter extends RecyclerView.Adapter<MyPageActViewHolder> { private List<MyPageActDataModel> list; public void setList(List<MyPageActDataModel> list) { this.list = list; } public MyPageActAdapter(List<MyPageActDataModel> list) { this.list = list; } @Override public MyPageActViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_my_page_activity_posts, parent,false); MyPageActViewHolder vh = new MyPageActViewHolder(inflate); vh.deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position = vh.getBindingAdapterPosition(); onDeleteButtonClick(view, position, list.get(position)); } }); return vh; } @Override public void onBindViewHolder(@NonNull MyPageActViewHolder holder, int position) { holder.shopNameView.setText(list.get(position).getShopName()); holder.dateView.setText(list.get(position).getDate()); holder.rateView.setText(list.get(position).getRate()); holder.genreView.setText(list.get(position).getGenre()); holder.commentView.setText(list.get(position).getComment()); holder.imageView1.setImageBitmap(list.get(position).getImage1()); holder.imageView2.setImageBitmap(list.get(position).getImage2()); holder.imageView3.setImageBitmap(list.get(position).getImage3()); } @Override public int getItemCount() { return list.size(); } abstract void onDeleteButtonClick(View view, int position, MyPageActDataModel dataModel); } // ViewHolder public class MyPageActViewHolder extends RecyclerView.ViewHolder { public TextView shopNameView; public TextView dateView; public TextView rateView; public TextView genreView; public TextView commentView; public ImageView imageView1; public ImageView imageView2; public ImageView imageView3; public ImageButton deleteButton; public MyPageActViewHolder(@NonNull View view) { super(view); shopNameView = (TextView) view.findViewById(R.id.nameOfShop); dateView = (TextView) view.findViewById(R.id.date); rateView = (TextView) view.findViewById(R.id.rate); genreView = (TextView) view.findViewById(R.id.genre); commentView = (TextView) view.findViewById(R.id.comment); imageView1 = (ImageView) itemView.findViewById(R.id.image1); imageView2 = (ImageView) itemView.findViewById(R.id.image2); imageView3 = (ImageView) itemView.findViewById(R.id.image3); deleteButton = view.findViewById(R.id.deleteButton); } } // DataModel public static class MyPageActDataModel { private String shopName; private String date; private String rate; private String genre; private String comment; private String pid; private Bitmap image1; private Bitmap image2; private Bitmap image3; public String getShopName() { return shopName; } public void setShopName(String shopName) { this.shopName = shopName; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public Bitmap getImage1() { return image1; } public void setImage1(Bitmap image1) { this.image1 = image1; } public Bitmap getImage2() { return image2; } public void setImage2(Bitmap image2) { this.image2 = image2; } public Bitmap getImage3() { return image3; } public void setImage3(Bitmap image3) { this.image3 = image3; } } }