diff --git a/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java index 84ddf44..779f6f8 100644 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/UserViewModel.java @@ -45,15 +45,15 @@ private final Retrofit retrofit; private static UserResource userResource; - private static final MutableLiveData user = new MutableLiveData<>(); - private final MutableLiveData token = new MutableLiveData<>(); - private final MutableLiveData icon = new MutableLiveData<>(); - private final MutableLiveDataloading = new MutableLiveData<>(false); - private static final MutableLiveData error = new MutableLiveData<>(); + private static final MutableLiveData user = new MutableLiveData<>(); + private final MutableLiveData token = new MutableLiveData<>(); + private final MutableLiveData icon = new MutableLiveData<>(); + private final MutableLiveData loading = new MutableLiveData<>(false); + private static final MutableLiveData error = new MutableLiveData<>(); //コンストラクタ - public UserViewModel(){ + public UserViewModel() { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/tampopo/") .addConverterFactory(ScalarsConverterFactory.create()) @@ -62,11 +62,25 @@ this.userResource = retrofit.create(UserResource.class); } - public LiveData getUser() { return user; } - public LiveData getToken() { return token; } - public LiveData getIcon() { return icon; } - public LiveDataisLoading() { return loading;} - public static LiveData getError() { return error; } + public LiveData getUser() { + return user; + } + + public LiveData getToken() { + return token; + } + + public LiveData getIcon() { + return icon; + } + + public LiveData isLoading() { + return loading; + } + + public static LiveData getError() { + return error; + } //新規登録 enqueueで非同期処理、Callbackで成功失敗の処理、LiveDataに反映 //サーバーから返ってきた型と合わせないとFailureに流れる @@ -74,7 +88,8 @@ public void createUser(String id, String password) { loading.setValue(true); userResource.createUser(id, password).enqueue(new Callback() { - @Override public void onResponse(Call c, Response res) { + @Override + public void onResponse(Call c, Response res) { loading.setValue(false); if (res.isSuccessful()) { User u = res.body(); @@ -83,7 +98,9 @@ error.setValue("登録失敗: " + res.code()); } } - @Override public void onFailure(Call c, Throwable t) { + + @Override + public void onFailure(Call c, Throwable t) { loading.setValue(false); error.setValue("エラー: " + t.getMessage()); } @@ -94,7 +111,8 @@ public void login(String id, String password) { loading.setValue(true); userResource.login(id, password).enqueue(new Callback() { - @Override public void onResponse(Call c, Response res) { + @Override + public void onResponse(Call c, Response res) { loading.setValue(false); if (res.isSuccessful()) { userResource.getUser(id); @@ -104,12 +122,15 @@ error.setValue("ログイン失敗: " + res.code()); } } - @Override public void onFailure(Call c, Throwable t) { + + @Override + public void onFailure(Call c, Throwable t) { loading.setValue(false); error.setValue("エラー: " + t.getMessage()); } }); } + //ニックネーム public String getNickname(String id) { Call call = userResource.getName(id); @@ -129,8 +150,8 @@ // } - //アイコン - public String getIcon(String id) { + //フレンドのアイコンをとってくる + public String getFriendIcon(String id) { Call call = userResource.getIcon(id); try { Response response = call.execute(); @@ -138,7 +159,6 @@ if (response.isSuccessful()) { System.out.println(response.code()); String iconUrl = response.body(); - icon.postValue(iconUrl); return iconUrl; } else { System.out.println(response.code()); @@ -163,7 +183,14 @@ // }); } - //アイコン(非同期) + //アイコン(非同期)idはログインユーザーのuser-id + + /** + * 自身のアイコンを取ってくる + * + * @param id ログインユーザーのuser-id + * @deprecated + */ public void getIconAsync(String id) { Call call = userResource.getIcon(id); call.enqueue(new Callback() { @@ -182,6 +209,31 @@ }); } + /** + * 自身のアイコンを取ってくる + */ + public void getMyIcon(String id) { +// User currentUser = user.getValue(); +// if (currentUser == null) { +// return; +// } + Call call = userResource.getIcon(id); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + String iconUrl = response.body(); + icon.postValue(iconUrl); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("UserViewModel", "Error"); + } + }); + } + public void updateIcon(String id, String newIcon, String token) { Call call = userResource.updateIcon(id, newIcon, token); @@ -196,7 +248,9 @@ System.out.println(response.code()); } } - @Override public void onFailure(Call call, Throwable t) { + + @Override + public void onFailure(Call call, Throwable t) { System.out.println("エラー: " + t.getMessage()); } }); @@ -229,11 +283,12 @@ } }); } + //パスワードの変更(市井) - public void updatePassword(String userId, String newPassword ,String tokenValue) { + public void updatePassword(String userId, String newPassword, String tokenValue) { loading.setValue(true); - userResource.updatePassword(userId, newPassword ,tokenValue).enqueue(new Callback() { + userResource.updatePassword(userId, newPassword, tokenValue).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { loading.setValue(false); @@ -288,8 +343,6 @@ } - - //viewModelのところでを呼び出すがフレンド系は西村さんの方で管理する } diff --git a/app/src/main/java/com/example/tampopo_client/views/MainActivity.java b/app/src/main/java/com/example/tampopo_client/views/MainActivity.java index cc87426..cd5d3b3 100644 --- a/app/src/main/java/com/example/tampopo_client/views/MainActivity.java +++ b/app/src/main/java/com/example/tampopo_client/views/MainActivity.java @@ -32,6 +32,7 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; +import com.bumptech.glide.Glide; import com.example.tampopo_client.R; import com.example.tampopo_client.Tampopo; import com.example.tampopo_client.models.Activity; @@ -40,6 +41,7 @@ import com.example.tampopo_client.viewmodels.ChatViewModel; import com.example.tampopo_client.viewmodels.ChatViewModelFactory; import com.example.tampopo_client.viewmodels.UserViewModel; +import com.google.android.material.imageview.ShapeableImageView; import java.util.ArrayList; import java.util.HashMap; @@ -111,6 +113,18 @@ //tampopoを宣言する tampopo = (Tampopo) getApplication(); + String uid = tampopo.getUserId(); + String token = tampopo.getToken(); + ShapeableImageView myIcon = findViewById(R.id.myicon); + + userViewModel.getMyIcon(uid); + userViewModel.getIcon().observe(this, new Observer() { + @Override + public void onChanged(String iconUrl) { + Glide.with(MainActivity.this).load(iconUrl).into(myIcon); + } + }); + //activityViewModelを宣言する ActivityViewModelFactory factory = new ActivityViewModelFactory(tampopo.getUserId(), tampopo.getToken()); // Factoryを使って、引数をコンストラクタにわたしつつViewModelを作成 activityViewModel = new ViewModelProvider(this, factory).get(ActivityViewModel.class); @@ -395,7 +409,6 @@ LinearLayout messageList = findViewById(R.id.messageList); ConstraintLayout layout = findViewById(R.id.main); - i = 0; for (String friendId : friends) { FriendIconView userView = userViews.get(friendId); //userViews.put(friendId, null); @@ -418,17 +431,18 @@ //FriendIconView container = new FriendIconView(this); - new Thread(new Runnable() { - @Override - public void run() { - FriendIconView container = new FriendIconView(MainActivity.this, friendId, userViewModel.getNickname(friendId), chatViewModel,userViewModel.getIcon(friendId)); + if(userView == null) { + new Thread(new Runnable() { + @Override + public void run() { + FriendIconView container = new FriendIconView(MainActivity.this, friendId, userViewModel.getNickname(friendId), chatViewModel, userViewModel.getFriendIcon(friendId)); - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - @Override - public void run() { - container.setPadding(16, 16, 16, 16); - container.setId(View.generateViewId()); + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + @Override + public void run() { + container.setPadding(16, 16, 16, 16); + container.setId(View.generateViewId()); // // ユーザのアイコン(固定) // ShapeableImageView iconView = new ShapeableImageView(MainActivity.this); @@ -441,74 +455,75 @@ // .setAllCornerSizes(50) // 丸く // .build() // ); - ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams( - ConstraintLayout.LayoutParams.WRAP_CONTENT, - ConstraintLayout.LayoutParams.WRAP_CONTENT - ); - container.setLayoutParams(params); - layout.addView(container); + ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ); + container.setLayoutParams(params); + layout.addView(container); // // // ユーザIDに応じてアイコンリソースを決定(仮にハードコード or マッピング) // iconView.setImageResource(getUserIconResource(friendId)); // ←ここがポイント - // Mapに登録、画面に追加 - userViews.put(friendId, container); + // Mapに登録、画面に追加 + userViews.put(friendId, container); // userView = container; // messageList.addView(container); - ConstraintSet set = new ConstraintSet(); - set.clone(layout); - int marginTopInPx = (int) TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - marginTopInDp[i], - getResources().getDisplayMetrics() - ); - int marginStartInPx = (int) TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - marginStartInDp[i], - getResources().getDisplayMetrics() - ); + ConstraintSet set = new ConstraintSet(); + set.clone(layout); + int marginTopInPx = (int) TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + marginTopInDp[i], + getResources().getDisplayMetrics() + ); + int marginStartInPx = (int) TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + marginStartInDp[i], + getResources().getDisplayMetrics() + ); - set.connect(container.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, marginTopInPx); - set.connect(container.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, marginStartInPx); + set.connect(container.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, marginTopInPx); + set.connect(container.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, marginStartInPx); - set.applyTo(layout); + set.applyTo(layout); - if (i < 6) { - i++; - } - - // TODO: CHANGE - activitiesLiveData.observe(MainActivity.this, activities -> { - if (activities == null || activities.isEmpty()) return; - - Activity latest = activities.get(activities.size() - 1); - - // UIスレッドで安全に更新 - FriendIconView friendView = userViews.get(friendId); - if (friendView != null) { - friendView.setComment(latest.getText()); - Log.d("ActivityUpdate", friendId + " のコメントを更新: " + latest.getText()); + if (i < 6) { + i++; } - }); - List sortedFriendUserIds = activityViewModel.getSortedFriendUserIds(); - int size = sortedFriendUserIds.size(); - List latestSix = sortedFriendUserIds.subList(Math.max(size - 6, 0), size); + // TODO: CHANGE + activitiesLiveData.observe(MainActivity.this, activities -> { + if (activities == null || activities.isEmpty()) return; - synchronized (recentUpdatedFriends) { - if (latestSix.contains(friendId)) { - recentUpdatedFriends.remove(friendId); - recentUpdatedFriends.add(0, friendId); - if (recentUpdatedFriends.size() > 6) { - recentUpdatedFriends.remove(recentUpdatedFriends.size() - 1); + Activity latest = activities.get(activities.size() - 1); + + // UIスレッドで安全に更新 + FriendIconView friendView = userViews.get(friendId); + if (friendView != null) { + friendView.setComment(latest.getText()); + Log.d("ActivityUpdate", friendId + " のコメントを更新: " + latest.getText()); + } + }); + + List sortedFriendUserIds = activityViewModel.getSortedFriendUserIds(); + int size = sortedFriendUserIds.size(); + List latestSix = sortedFriendUserIds.subList(Math.max(size - 6, 0), size); + + synchronized (recentUpdatedFriends) { + if (latestSix.contains(friendId)) { + recentUpdatedFriends.remove(friendId); + recentUpdatedFriends.add(0, friendId); + if (recentUpdatedFriends.size() > 6) { + recentUpdatedFriends.remove(recentUpdatedFriends.size() - 1); + } } } } - } - }); - } - }).start(); + }); + } + }).start(); + } } } }