zhibo/android-app/app/src/main/java/com/example/livestreaming/ProfileActivity.java

170 lines
7.2 KiB
Java
Raw Normal View History

2025-12-17 15:38:00 +08:00
package com.example.livestreaming;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
2025-12-18 14:20:41 +08:00
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;
2025-12-17 15:38:00 +08:00
import androidx.appcompat.app.AppCompatActivity;
2025-12-18 14:20:41 +08:00
import androidx.appcompat.app.AlertDialog;
2025-12-17 15:38:00 +08:00
2025-12-18 14:20:41 +08:00
import com.bumptech.glide.Glide;
2025-12-17 15:38:00 +08:00
import com.example.livestreaming.databinding.ActivityProfileBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class ProfileActivity extends AppCompatActivity {
private ActivityProfileBinding binding;
2025-12-18 14:20:41 +08:00
private static final String PREFS_NAME = "profile_prefs";
private static final String KEY_NAME = "profile_name";
private static final String KEY_BIO = "profile_bio";
private static final String KEY_LEVEL = "profile_level";
private static final String KEY_FANS_BADGE = "profile_fans_badge";
private static final String KEY_BADGE = "profile_badge";
private static final String KEY_AVATAR_RES = "profile_avatar_res";
private static final String KEY_AVATAR_URI = "profile_avatar_uri";
private static final String KEY_BIRTHDAY = "profile_birthday";
private static final String KEY_GENDER = "profile_gender";
private static final String KEY_LOCATION = "profile_location";
2025-12-17 15:38:00 +08:00
public static void start(Context context) {
Intent intent = new Intent(context, ProfileActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityProfileBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
2025-12-18 14:20:41 +08:00
loadProfileFromPrefs();
setupEditableAreas();
setupNavigationClicks();
2025-12-17 15:38:00 +08:00
BottomNavigationView bottomNavigation = binding.bottomNavInclude.bottomNavigation;
bottomNavigation.setSelectedItemId(R.id.nav_profile);
bottomNavigation.setOnItemSelectedListener(item -> {
int id = item.getItemId();
if (id == R.id.nav_home) {
startActivity(new Intent(this, MainActivity.class));
finish();
return true;
}
if (id == R.id.nav_friends) {
startActivity(new Intent(this, FishPondActivity.class));
finish();
return true;
}
if (id == R.id.nav_wish_tree) {
WishTreeActivity.start(this);
finish();
return true;
}
if (id == R.id.nav_messages) {
MessagesActivity.start(this);
finish();
return true;
}
return true;
});
}
2025-12-18 14:20:41 +08:00
private void loadProfileFromPrefs() {
String n = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getString(KEY_NAME, null);
if (!TextUtils.isEmpty(n)) binding.name.setText(n);
String bio = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getString(KEY_BIO, null);
if (!TextUtils.isEmpty(bio)) {
binding.bioText.setText(bio);
binding.bioText.setTextColor(0xFF111111);
}
String avatarUri = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getString(KEY_AVATAR_URI, null);
if (!TextUtils.isEmpty(avatarUri)) {
Glide.with(this).load(avatarUri).into(binding.avatar);
} else {
int avatarRes = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).getInt(KEY_AVATAR_RES, 0);
if (avatarRes != 0) {
binding.avatar.setImageResource(avatarRes);
}
}
// 等级/称号/徽章:保持固定显示(例如“月亮/星耀/至尊”),不从本地缓存覆盖。
}
private void setupEditableAreas() {
binding.name.setOnClickListener(v -> showEditDialog("编辑昵称", binding.name.getText() != null ? binding.name.getText().toString() : "", text -> {
binding.name.setText(text);
getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(KEY_NAME, text).apply();
}));
binding.bioText.setOnClickListener(v -> showEditDialog("编辑签名", binding.bioText.getText() != null ? binding.bioText.getText().toString() : "", text -> {
if (TextUtils.isEmpty(text)) {
binding.bioText.setText("填写个人签名更容易获得关注,点击此处添加");
binding.bioText.setTextColor(0xFF999999);
getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().remove(KEY_BIO).apply();
} else {
binding.bioText.setText(text);
binding.bioText.setTextColor(0xFF111111);
getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(KEY_BIO, text).apply();
}
}));
}
private void setupNavigationClicks() {
binding.topActionSearch.setOnClickListener(v -> TabPlaceholderActivity.start(this, "搜索"));
binding.topActionClock.setOnClickListener(v -> TabPlaceholderActivity.start(this, "定位/发现"));
binding.topActionMore.setOnClickListener(v -> TabPlaceholderActivity.start(this, "更多"));
binding.following.setOnClickListener(v -> FollowingListActivity.start(this));
binding.followers.setOnClickListener(v -> FansListActivity.start(this));
binding.likes.setOnClickListener(v -> LikesListActivity.start(this));
binding.recordBtn.setOnClickListener(v -> Toast.makeText(this, "录制声音(待实现)", Toast.LENGTH_SHORT).show());
binding.action1.setOnClickListener(v -> TabPlaceholderActivity.start(this, "公园勋章"));
binding.action2.setOnClickListener(v -> WatchHistoryActivity.start(this));
binding.action3.setOnClickListener(v -> startActivity(new Intent(this, MyFriendsActivity.class)));
binding.editProfile.setOnClickListener(v -> EditProfileActivity.start(this));
binding.shareHome.setOnClickListener(v -> TabPlaceholderActivity.start(this, "分享主页"));
binding.addFriendBtn.setOnClickListener(v -> TabPlaceholderActivity.start(this, "加好友"));
}
private interface OnTextSaved {
void onSaved(String text);
}
private void showEditDialog(String title, String initialValue, OnTextSaved onSaved) {
EditText editText = new EditText(this);
editText.setText(initialValue != null ? initialValue : "");
editText.setSelection(editText.getText() != null ? editText.getText().length() : 0);
int pad = (int) (16 * getResources().getDisplayMetrics().density);
editText.setPadding(pad, pad, pad, pad);
new AlertDialog.Builder(this)
.setTitle(title)
.setView(editText)
.setNegativeButton("取消", null)
.setPositiveButton("保存", (d, w) -> {
String t = editText.getText() != null ? editText.getText().toString().trim() : "";
if (onSaved != null) onSaved.onSaved(t);
})
.show();
}
2025-12-17 15:38:00 +08:00
@Override
protected void onResume() {
super.onResume();
if (binding != null) {
2025-12-18 14:20:41 +08:00
loadProfileFromPrefs();
2025-12-17 15:38:00 +08:00
binding.bottomNavInclude.bottomNavigation.setSelectedItemId(R.id.nav_profile);
}
}
}