2025-12-17 15:38:00 +08:00
|
|
|
package com.example.livestreaming;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
2025-12-18 17:33:36 +08:00
|
|
|
import android.content.ClipData;
|
|
|
|
|
import android.content.ClipboardManager;
|
2025-12-17 15:38:00 +08:00
|
|
|
import android.os.Bundle;
|
2025-12-18 14:20:41 +08:00
|
|
|
import android.text.TextUtils;
|
2025-12-18 17:33:36 +08:00
|
|
|
import android.view.View;
|
2025-12-18 14:20:41 +08:00
|
|
|
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-18 17:33:36 +08:00
|
|
|
setupProfileTabs();
|
2025-12-18 14:20:41 +08:00
|
|
|
|
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() {
|
2025-12-18 17:33:36 +08:00
|
|
|
binding.topActionSearch.setOnClickListener(v -> TabPlaceholderActivity.start(this, "定位/发现"));
|
|
|
|
|
binding.topActionClock.setOnClickListener(v -> WatchHistoryActivity.start(this));
|
2025-12-18 14:20:41 +08:00
|
|
|
binding.topActionMore.setOnClickListener(v -> TabPlaceholderActivity.start(this, "更多"));
|
|
|
|
|
|
2025-12-18 17:33:36 +08:00
|
|
|
binding.copyIdBtn.setOnClickListener(v -> {
|
|
|
|
|
String idText = binding.idLine.getText() != null ? binding.idLine.getText().toString() : "";
|
|
|
|
|
if (TextUtils.isEmpty(idText)) return;
|
|
|
|
|
String digits = idText.replaceAll("\\D+", "");
|
|
|
|
|
if (TextUtils.isEmpty(digits)) return;
|
|
|
|
|
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
|
|
|
|
if (cm != null) {
|
|
|
|
|
cm.setPrimaryClip(ClipData.newPlainText("id", digits));
|
|
|
|
|
Toast.makeText(this, "已复制:" + digits, Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-18 14:20:41 +08:00
|
|
|
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, "加好友"));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 17:33:36 +08:00
|
|
|
private void setupProfileTabs() {
|
|
|
|
|
showTab(0);
|
|
|
|
|
|
|
|
|
|
binding.profileTabs.addOnTabSelectedListener(new com.google.android.material.tabs.TabLayout.OnTabSelectedListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onTabSelected(com.google.android.material.tabs.TabLayout.Tab tab) {
|
|
|
|
|
if (tab == null) return;
|
|
|
|
|
showTab(tab.getPosition());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onTabUnselected(com.google.android.material.tabs.TabLayout.Tab tab) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onTabReselected(com.google.android.material.tabs.TabLayout.Tab tab) {
|
|
|
|
|
if (tab == null) return;
|
|
|
|
|
showTab(tab.getPosition());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
binding.worksPublishBtn.setOnClickListener(v -> Toast.makeText(this, "发布功能待接入", Toast.LENGTH_SHORT).show());
|
|
|
|
|
binding.likedGoBrowseBtn.setOnClickListener(v -> startActivity(new Intent(this, MainActivity.class)));
|
|
|
|
|
binding.favGoBrowseBtn.setOnClickListener(v -> startActivity(new Intent(this, MainActivity.class)));
|
|
|
|
|
binding.profileEditFromTab.setOnClickListener(v -> EditProfileActivity.start(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showTab(int index) {
|
|
|
|
|
binding.tabWorks.setVisibility(index == 0 ? View.VISIBLE : View.GONE);
|
|
|
|
|
binding.tabLiked.setVisibility(index == 1 ? View.VISIBLE : View.GONE);
|
|
|
|
|
binding.tabFavorites.setVisibility(index == 2 ? View.VISIBLE : View.GONE);
|
|
|
|
|
binding.tabProfile.setVisibility(index == 3 ? View.VISIBLE : View.GONE);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:20:41 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|