Migrate Profile Package to Kotlin #6118 #5928

This commit is contained in:
Sujal-Gupta-SG 2025-01-11 18:57:24 +05:30
parent e60bd20ab2
commit 7101fdee85
3 changed files with 191 additions and 231 deletions

View file

@ -1737,10 +1737,11 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
return return
} }
ProfileActivity.startYourself( ProfileActivity.startYourself(
activity, requireActivity(), // Ensure this is a non-null Activity context
media!!.user, media?.user ?: "", // Provide a fallback value if media?.user is null
sessionManager.userName != media!!.user sessionManager.userName != media?.user // This can remain as is, null check will apply
) )
} }
/** /**

View file

@ -1,265 +1,224 @@
package fr.free.nrw.commons.profile; package fr.free.nrw.commons.profile
import android.content.Context; import android.content.Context
import android.content.Intent; import android.content.Intent
import android.graphics.Bitmap; import android.graphics.Bitmap
import android.net.Uri; import android.net.Uri
import android.os.Bundle; import android.os.Bundle
import android.view.LayoutInflater; import android.view.*
import android.view.Menu; import android.widget.ImageView
import android.view.MenuInflater; import android.widget.TextView
import android.view.MenuItem; import androidx.core.content.FileProvider
import android.view.View; import androidx.fragment.app.Fragment
import android.widget.ImageView; import fr.free.nrw.commons.R
import android.widget.TextView; import fr.free.nrw.commons.Utils
import androidx.annotation.NonNull; import fr.free.nrw.commons.ViewPagerAdapter
import androidx.core.content.FileProvider; import fr.free.nrw.commons.auth.SessionManager
import androidx.fragment.app.Fragment; import fr.free.nrw.commons.contributions.ContributionsFragment
import androidx.fragment.app.FragmentManager; import fr.free.nrw.commons.databinding.ActivityProfileBinding
import fr.free.nrw.commons.R; import fr.free.nrw.commons.profile.achievements.AchievementsFragment
import fr.free.nrw.commons.Utils; import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment
import fr.free.nrw.commons.ViewPagerAdapter; import fr.free.nrw.commons.theme.BaseActivity
import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.utils.DialogUtil
import fr.free.nrw.commons.contributions.ContributionsFragment; import java.io.File
import fr.free.nrw.commons.databinding.ActivityProfileBinding; import java.io.FileOutputStream
import fr.free.nrw.commons.profile.achievements.AchievementsFragment; import java.util.*
import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment; import javax.inject.Inject
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.DialogUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
/** /**
* This activity will set two tabs, achievements and * This activity will set two tabs, achievements and
* each tab will have their own fragments * each tab will have their own fragments
*/ */
public class ProfileActivity extends BaseActivity { class ProfileActivity : BaseActivity() {
private FragmentManager supportFragmentManager; private lateinit var binding: ActivityProfileBinding
public ActivityProfileBinding binding;
@Inject @Inject
SessionManager sessionManager; lateinit var sessionManager: SessionManager
private ViewPagerAdapter viewPagerAdapter; private lateinit var viewPagerAdapter: ViewPagerAdapter
private AchievementsFragment achievementsFragment; private lateinit var achievementsFragment: AchievementsFragment
private LeaderboardFragment leaderboardFragment; private lateinit var leaderboardFragment: LeaderboardFragment
private lateinit var userName: String
private var shouldShowContributions: Boolean = false
private var contributionsFragment: ContributionsFragment? = null
public static final String KEY_USERNAME ="username"; fun setScroll(canScroll: Boolean) {
public static final String KEY_SHOULD_SHOW_CONTRIBUTIONS ="shouldShowContributions"; binding.viewPager.setCanScroll(canScroll)
String userName;
private boolean shouldShowContributions;
ContributionsFragment contributionsFragment;
public void setScroll(boolean canScroll){
binding.viewPager.setCanScroll(canScroll);
} }
@Override
protected void onRestoreInstanceState(final Bundle savedInstanceState) { override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState)
if (savedInstanceState != null) { savedInstanceState.let {
userName = savedInstanceState.getString(KEY_USERNAME); userName = it.getString(KEY_USERNAME, "")
shouldShowContributions = savedInstanceState.getBoolean(KEY_SHOULD_SHOW_CONTRIBUTIONS); shouldShowContributions = it.getBoolean(KEY_SHOULD_SHOW_CONTRIBUTIONS)
} }
} }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Override binding = ActivityProfileBinding.inflate(layoutInflater)
protected void onCreate(Bundle savedInstanceState) { setContentView(binding.root)
super.onCreate(savedInstanceState); setSupportActionBar(binding.toolbarBinding.toolbar)
binding = ActivityProfileBinding.inflate(getLayoutInflater()); binding.toolbarBinding.toolbar.setNavigationOnClickListener {
setContentView(binding.getRoot()); onSupportNavigateUp()
setSupportActionBar(binding.toolbarBinding.toolbar);
binding.toolbarBinding.toolbar.setNavigationOnClickListener(view -> {
onSupportNavigateUp();
});
userName = getIntent().getStringExtra(KEY_USERNAME);
setTitle(userName);
shouldShowContributions = getIntent().getBooleanExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, false);
supportFragmentManager = getSupportFragmentManager();
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
binding.viewPager.setAdapter(viewPagerAdapter);
binding.tabLayout.setupWithViewPager(binding.viewPager);
setTabs();
}
/**
* Navigate up event
* @return boolean
*/
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
/**
* Creates a way to change current activity to AchievementActivity
*
* @param context
*/
public static void startYourself(final Context context, final String userName,
final boolean shouldShowContributions) {
Intent intent = new Intent(context, ProfileActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(KEY_USERNAME, userName);
intent.putExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, shouldShowContributions);
context.startActivity(intent);
}
/**
* Set the tabs for the fragments
*/
private void setTabs() {
List<Fragment> fragmentList = new ArrayList<>();
List<String> titleList = new ArrayList<>();
achievementsFragment = new AchievementsFragment();
Bundle achievementsBundle = new Bundle();
achievementsBundle.putString(KEY_USERNAME, userName);
achievementsFragment.setArguments(achievementsBundle);
fragmentList.add(achievementsFragment);
titleList.add(getResources().getString(R.string.achievements_tab_title).toUpperCase());
leaderboardFragment = new LeaderboardFragment();
Bundle leaderBoardBundle = new Bundle();
leaderBoardBundle.putString(KEY_USERNAME, userName);
leaderboardFragment.setArguments(leaderBoardBundle);
fragmentList.add(leaderboardFragment);
titleList.add(getResources().getString(R.string.leaderboard_tab_title).toUpperCase(Locale.ROOT));
contributionsFragment = new ContributionsFragment();
Bundle contributionsListBundle = new Bundle();
contributionsListBundle.putString(KEY_USERNAME, userName);
contributionsFragment.setArguments(contributionsListBundle);
fragmentList.add(contributionsFragment);
titleList.add(getString(R.string.contributions_fragment).toUpperCase(Locale.ROOT));
viewPagerAdapter.setTabData(fragmentList, titleList);
viewPagerAdapter.notifyDataSetChanged();
}
@Override
public void onDestroy() {
super.onDestroy();
getCompositeDisposable().clear();
}
/**
* To inflate menu
* @param menu Menu
* @return boolean
*/
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
final MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_about, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* To receive the id of selected item and handle further logic for that selected item
* @param item MenuItem
* @return boolean
*/
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
// take screenshot in form of bitmap and show it in Alert Dialog
if (item.getItemId() == R.id.share_app_icon) {
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
final Bitmap screenShot = Utils.getScreenShot(rootView);
showAlert(screenShot);
return true;
} }
return super.onOptionsItemSelected(item);
userName = intent.getStringExtra(KEY_USERNAME) ?: ""
title = userName
shouldShowContributions = intent.getBooleanExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, false)
viewPagerAdapter = ViewPagerAdapter(supportFragmentManager)
binding.viewPager.adapter = viewPagerAdapter
binding.tabLayout.setupWithViewPager(binding.viewPager)
setTabs()
} }
/** override fun onSupportNavigateUp(): Boolean {
* It displays the alertDialog with Image of screenshot onBackPressed()
* @param screenshot screenshot of the present screen return true
*/ }
public void showAlert(final Bitmap screenshot) {
final LayoutInflater factory = LayoutInflater.from(this); private fun setTabs() {
final View view = factory.inflate(R.layout.image_alert_layout, null); val fragmentList = mutableListOf<Fragment>()
final ImageView screenShotImage = view.findViewById(R.id.alert_image); val titleList = mutableListOf<String>()
screenShotImage.setImageBitmap(screenshot);
final TextView shareMessage = view.findViewById(R.id.alert_text); // Add Achievements tab
shareMessage.setText(R.string.achievements_share_message); achievementsFragment = AchievementsFragment().apply {
DialogUtil.showAlertDialog(this, arguments = Bundle().apply {
putString(KEY_USERNAME, userName)
}
}
fragmentList.add(achievementsFragment)
titleList.add(resources.getString(R.string.achievements_tab_title).uppercase())
// Add Leaderboard tab
leaderboardFragment = LeaderboardFragment().apply {
arguments = Bundle().apply {
putString(KEY_USERNAME, userName)
}
}
fragmentList.add(leaderboardFragment)
titleList.add(resources.getString(R.string.leaderboard_tab_title).uppercase(Locale.ROOT))
// Add Contributions tab
contributionsFragment = ContributionsFragment().apply {
arguments = Bundle().apply {
putString(KEY_USERNAME, userName)
}
}
contributionsFragment?.let {
fragmentList.add(it)
titleList.add(getString(R.string.contributions_fragment).uppercase(Locale.ROOT))
}
viewPagerAdapter.setTabData(fragmentList, titleList)
viewPagerAdapter.notifyDataSetChanged()
}
override fun onDestroy() {
super.onDestroy()
compositeDisposable.clear()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_about, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.share_app_icon -> {
val rootView = window.decorView.findViewById<View>(android.R.id.content)
val screenShot = Utils.getScreenShot(rootView)
showAlert(screenShot)
true
}
else -> super.onOptionsItemSelected(item)
}
}
fun showAlert(screenshot: Bitmap) {
val view = layoutInflater.inflate(R.layout.image_alert_layout, null)
val screenShotImage = view.findViewById<ImageView>(R.id.alert_image)
val shareMessage = view.findViewById<TextView>(R.id.alert_text)
screenShotImage.setImageBitmap(screenshot)
shareMessage.setText(R.string.achievements_share_message)
DialogUtil.showAlertDialog(
this,
null, null,
null, null,
getString(R.string.about_translate_proceed), getString(R.string.about_translate_proceed),
getString(R.string.cancel), getString(R.string.cancel),
() -> shareScreen(screenshot), { shareScreen(screenshot) },
() -> {}, {},
view view
); )
} }
/** private fun shareScreen(bitmap: Bitmap) {
* To take bitmap and store it temporary storage and share it
* @param bitmap bitmap of screenshot
*/
void shareScreen(final Bitmap bitmap) {
try { try {
final File file = new File(getExternalCacheDir(), "screen.png"); val file = File(externalCacheDir, "screen.png")
final FileOutputStream fileOutputStream = new FileOutputStream(file); FileOutputStream(file).use { out ->
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
fileOutputStream.flush(); out.flush()
fileOutputStream.close(); }
file.setReadable(true, false); file.setReadable(true, false)
final Uri fileUri = FileProvider val fileUri = FileProvider.getUriForFile(
.getUriForFile(getApplicationContext(), applicationContext,
getPackageName() + ".provider", file); "$packageName.provider",
grantUriPermission(getPackageName(), fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); file
final Intent intent = new Intent(android.content.Intent.ACTION_SEND); )
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, fileUri); grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.setType("image/png");
startActivity(Intent.createChooser(intent, getString(R.string.share_image_via))); val intent = Intent(Intent.ACTION_SEND).apply {
} catch (final IOException e) { flags = Intent.FLAG_ACTIVITY_NEW_TASK
e.printStackTrace(); putExtra(Intent.EXTRA_STREAM, fileUri)
type = "image/png"
}
startActivity(Intent.createChooser(intent, getString(R.string.share_image_via)))
} catch (e: Exception) {
e.printStackTrace()
} }
} }
@Override override fun onSaveInstanceState(outState: Bundle) {
protected void onSaveInstanceState(@NonNull final Bundle outState) { super.onSaveInstanceState(outState)
outState.putString(KEY_USERNAME, userName); outState.putString(KEY_USERNAME, userName)
outState.putBoolean(KEY_SHOULD_SHOW_CONTRIBUTIONS, shouldShowContributions); outState.putBoolean(KEY_SHOULD_SHOW_CONTRIBUTIONS, shouldShowContributions)
super.onSaveInstanceState(outState);
} }
@Override override fun onBackPressed() {
public void onBackPressed() { if (contributionsFragment?.mediaDetailPagerFragment?.isVisible == true) {
// Checking if MediaDetailPagerFragment is visible, If visible then show ContributionListFragment else close the ProfileActivity contributionsFragment?.backButtonClicked()
if(contributionsFragment != null && contributionsFragment.getMediaDetailPagerFragment() != null && contributionsFragment.getMediaDetailPagerFragment().isVisible()) { binding.tabLayout.visibility = View.VISIBLE
contributionsFragment.backButtonClicked(); } else {
binding.tabLayout.setVisibility(View.VISIBLE); super.onBackPressed()
}else {
super.onBackPressed();
} }
} }
/** fun setTabLayoutVisibility(isVisible: Boolean) {
* To set the visibility of tab layout binding.tabLayout.visibility = if (isVisible) View.VISIBLE else View.GONE
* @param isVisible boolean
*/
public void setTabLayoutVisibility(boolean isVisible) {
binding.tabLayout.setVisibility(isVisible ? View.VISIBLE : View.GONE);
} }
}
companion object {
const val KEY_USERNAME = "username"
const val KEY_SHOULD_SHOW_CONTRIBUTIONS = "shouldShowContributions"
@JvmStatic
fun startYourself(context: Context, userName: String, shouldShowContributions: Boolean) {
val intent = Intent(context, ProfileActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)
putExtra(KEY_USERNAME, userName)
putExtra(KEY_SHOULD_SHOW_CONTRIBUTIONS, shouldShowContributions)
}
context.startActivity(intent)
}
}
}

View file

@ -58,7 +58,7 @@ class LeaderboardListAdapter : PagedListAdapter<LeaderboardList, ListViewHolder>
if (view.context is ProfileActivity) { if (view.context is ProfileActivity) {
((view.context) as Activity).finish() ((view.context) as Activity).finish()
} }
ProfileActivity.startYourself(view.context, item.username, true) ProfileActivity.startYourself(view.context, item.username?:"", true)
} }
} }
} }