-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Refactor: Migrate About screen UI from XML to Jetpack Compose #6803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 68 additions & 157 deletions
225
app/src/main/java/fr/free/nrw/commons/AboutActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,207 +1,118 @@ | ||
| package fr.free.nrw.commons | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.content.ActivityNotFoundException | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.Intent.ACTION_VIEW | ||
| import android.net.Uri | ||
| import android.os.Bundle | ||
| import android.view.Menu | ||
| import android.view.MenuItem | ||
| import android.view.View | ||
| import android.widget.ArrayAdapter | ||
| import android.widget.LinearLayout | ||
| import android.widget.Spinner | ||
| import fr.free.nrw.commons.CommonsApplication.Companion.instance | ||
| import fr.free.nrw.commons.databinding.ActivityAboutBinding | ||
| import androidx.activity.compose.setContent | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Surface | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.core.net.toUri | ||
| import fr.free.nrw.commons.theme.BaseActivity | ||
| import fr.free.nrw.commons.theme.CommonsAppTheme | ||
| import fr.free.nrw.commons.utils.ConfigUtils.getVersionNameWithSha | ||
| import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog | ||
| import java.util.Collections | ||
| import androidx.core.net.toUri | ||
| import fr.free.nrw.commons.utils.applyEdgeToEdgeTopInsets | ||
| import fr.free.nrw.commons.utils.handleWebUrl | ||
| import fr.free.nrw.commons.utils.setUnderlinedText | ||
|
|
||
| /** | ||
| * Represents about screen of this app | ||
| */ | ||
| class AboutActivity : BaseActivity() { | ||
| /* | ||
| This View Binding class is auto-generated for each xml file. The format is usually the name | ||
| of the file with PascalCasing (The underscore characters will be ignored). | ||
| More information is available at https://developer.android.com/topic/libraries/view-binding | ||
| */ | ||
| private var binding: ActivityAboutBinding? = null | ||
|
|
||
| /** | ||
| * This method helps in the creation About screen | ||
| * | ||
| * @param savedInstanceState Data bundle | ||
| */ | ||
| @SuppressLint("StringFormatInvalid") //TODO: | ||
| public override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| /* | ||
| Instead of just setting the view with the xml file. We need to use View Binding class. | ||
| */ | ||
| binding = ActivityAboutBinding.inflate(layoutInflater) | ||
| val view: View = binding!!.root | ||
| applyEdgeToEdgeTopInsets(binding!!.toolbarLayout) | ||
| setContentView(view) | ||
|
|
||
| setSupportActionBar(binding!!.toolbarBinding.toolbar) | ||
| supportActionBar!!.setDisplayHomeAsUpEnabled(true) | ||
| val aboutText = getString(R.string.about_license) | ||
| /* | ||
| We can then access all the views by just using the id names like this. | ||
| camelCasing is used with underscore characters being ignored. | ||
| */ | ||
| binding!!.aboutLicense.setHtmlText(aboutText) | ||
|
|
||
| @SuppressLint("StringFormatMatches") // TODO: | ||
| val improveText = | ||
| String.format(getString(R.string.about_improve), Urls.NEW_ISSUE_URL) | ||
| binding!!.aboutImprove.setHtmlText(improveText) | ||
| binding!!.aboutVersion.text = applicationContext.getVersionNameWithSha() | ||
|
|
||
| binding!!.aboutFaq.setUnderlinedText(R.string.about_faq) | ||
| binding!!.aboutRateUs.setUnderlinedText(R.string.about_rate_us) | ||
| binding!!.aboutUserGuide.setUnderlinedText(R.string.user_guide) | ||
| binding!!.aboutPrivacyPolicy.setUnderlinedText(R.string.about_privacy_policy) | ||
| binding!!.aboutTranslate.setUnderlinedText(R.string.about_translate) | ||
| binding!!.aboutCredits.setUnderlinedText(R.string.about_credits) | ||
|
|
||
| /* | ||
| To set listeners, we can create a separate method and use lambda syntax. | ||
| */ | ||
| binding!!.facebookLaunchIcon.setOnClickListener(::launchFacebook) | ||
| binding!!.githubLaunchIcon.setOnClickListener(::launchGithub) | ||
| binding!!.websiteLaunchIcon.setOnClickListener(::launchWebsite) | ||
| binding!!.aboutRateUs.setOnClickListener(::launchRatings) | ||
| binding!!.aboutCredits.setOnClickListener(::launchCredits) | ||
| binding!!.aboutPrivacyPolicy.setOnClickListener(::launchPrivacyPolicy) | ||
| binding!!.aboutUserGuide.setOnClickListener(::launchUserGuide) | ||
| binding!!.aboutFaq.setOnClickListener(::launchFrequentlyAskedQuesions) | ||
| binding!!.aboutTranslate.setOnClickListener(::launchTranslate) | ||
| enableEdgeToEdge() | ||
| val versionName = applicationContext.getVersionNameWithSha() | ||
|
|
||
| setContent { | ||
| CommonsAppTheme{ | ||
| Surface( | ||
| modifier = Modifier.fillMaxSize(), | ||
| color = MaterialTheme.colorScheme.background | ||
| ) { | ||
| AboutScreen( | ||
| versionName = versionName, | ||
| onNavigateUp = { onBackPressedDispatcher.onBackPressed() }, | ||
| onShareClicked = { shareApp() }, | ||
| onFacebookClicked = { launchFacebook(this) }, | ||
| onGithubClicked = { launchGithub(this) }, | ||
| onWebsiteClicked = { launchWebsite(this) }, | ||
| onRateUsClicked = { launchRatings(this) }, | ||
| onUserGuideClicked = { launchUserGuide(this) }, | ||
| onPrivacyPolicyClicked = { launchPrivacyPolicy(this) }, | ||
| onTranslateProceed = { langCode -> launchTranslate(this, langCode) }, | ||
| onCreditsClicked = { launchCredits(this) }, | ||
| onFaqClicked = { launchFrequentlyAskedQuesions(this) } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onSupportNavigateUp(): Boolean { | ||
| onBackPressed() | ||
| return true | ||
| private fun shareApp() { | ||
| val shareText = getString(R.string.share_text).format(Urls.PLAY_STORE_URL_PREFIX + packageName) | ||
| val sendIntent = Intent().apply { | ||
| action = Intent.ACTION_SEND | ||
| putExtra(Intent.EXTRA_TEXT, shareText) | ||
| type = "text/plain" | ||
| } | ||
| startActivity(Intent.createChooser(sendIntent, getString(R.string.share_via))) | ||
| } | ||
|
|
||
| fun launchFacebook(view: View?) { | ||
| val intent: Intent | ||
| try { | ||
| intent = Intent(ACTION_VIEW, Urls.FACEBOOK_APP_URL.toUri()) | ||
| private fun launchFacebook(context: Context) { | ||
| try{ | ||
| val intent = Intent(ACTION_VIEW, Urls.FACEBOOK_APP_URL.toUri()) | ||
| intent.setPackage(Urls.FACEBOOK_PACKAGE_NAME) | ||
| startActivity(intent) | ||
| context.startActivity(intent) | ||
| } catch (e: Exception) { | ||
| handleWebUrl(this, Urls.FACEBOOK_WEB_URL.toUri()) | ||
| handleWebUrl(context, Urls.FACEBOOK_WEB_URL.toUri()) | ||
| } | ||
| } | ||
|
|
||
| fun launchGithub(view: View?) { | ||
| val intent: Intent | ||
| try { | ||
| intent = Intent(ACTION_VIEW, Urls.GITHUB_REPO_URL.toUri()) | ||
| private fun launchGithub(context: Context) { | ||
| try{ | ||
| val intent = Intent(ACTION_VIEW, Urls.GITHUB_REPO_URL.toUri()) | ||
| intent.setPackage(Urls.GITHUB_PACKAGE_NAME) | ||
| startActivity(intent) | ||
| context.startActivity(intent) | ||
| } catch (e: Exception) { | ||
| handleWebUrl(this, Urls.GITHUB_REPO_URL.toUri()) | ||
| handleWebUrl(context, Urls.GITHUB_REPO_URL.toUri()) | ||
| } | ||
| } | ||
|
|
||
| fun launchWebsite(view: View?) { | ||
| handleWebUrl(this, Urls.WEBSITE_URL.toUri()) | ||
| private fun launchWebsite(context: Context) { | ||
| handleWebUrl(context, Urls.WEBSITE_URL.toUri()) | ||
| } | ||
|
|
||
| fun launchRatings(view: View?) { | ||
| private fun launchRatings(context: Context) { | ||
| try { | ||
| startActivity( | ||
| context.startActivity( | ||
| Intent( | ||
| ACTION_VIEW, | ||
| (Urls.PLAY_STORE_PREFIX + packageName).toUri() | ||
| ) | ||
| ) | ||
| } catch (_: ActivityNotFoundException) { | ||
| handleWebUrl(this, (Urls.PLAY_STORE_URL_PREFIX + packageName).toUri()) | ||
| handleWebUrl(context, (Urls.PLAY_STORE_URL_PREFIX + packageName).toUri()) | ||
| } | ||
| } | ||
|
|
||
| fun launchCredits(view: View?) { | ||
| handleWebUrl(this, Urls.CREDITS_URL.toUri()) | ||
| private fun launchCredits(context: Context) { | ||
| handleWebUrl(context, Urls.CREDITS_URL.toUri()) | ||
| } | ||
|
|
||
| fun launchUserGuide(view: View?) { | ||
| handleWebUrl(this, Urls.USER_GUIDE_URL.toUri()) | ||
| private fun launchUserGuide(context: Context) { | ||
| handleWebUrl(context, Urls.USER_GUIDE_URL.toUri()) | ||
| } | ||
|
|
||
| fun launchPrivacyPolicy(view: View?) { | ||
| handleWebUrl(this, BuildConfig.PRIVACY_POLICY_URL.toUri()) | ||
| } | ||
|
|
||
| fun launchFrequentlyAskedQuesions(view: View?) { | ||
| handleWebUrl(this, Urls.FAQ_URL.toUri()) | ||
| } | ||
|
|
||
| override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
| val inflater = menuInflater | ||
| inflater.inflate(R.menu.menu_about, menu) | ||
| return super.onCreateOptionsMenu(menu) | ||
| } | ||
|
|
||
| override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
| when (item.itemId) { | ||
| R.id.share_app_icon -> { | ||
| val shareText = String.format( | ||
| getString(R.string.share_text), | ||
| Urls.PLAY_STORE_URL_PREFIX + this.packageName | ||
| ) | ||
| val sendIntent = Intent() | ||
| sendIntent.setAction(Intent.ACTION_SEND) | ||
| sendIntent.putExtra(Intent.EXTRA_TEXT, shareText) | ||
| sendIntent.setType("text/plain") | ||
| startActivity(Intent.createChooser(sendIntent, getString(R.string.share_via))) | ||
| return true | ||
| } | ||
| private fun launchPrivacyPolicy(context: Context) { | ||
| handleWebUrl(context, BuildConfig.PRIVACY_POLICY_URL.toUri()) } | ||
|
|
||
| else -> return super.onOptionsItemSelected(item) | ||
| } | ||
| private fun launchFrequentlyAskedQuesions(context: Context) { | ||
| handleWebUrl(context, Urls.FAQ_URL.toUri()) | ||
|
Roniscend marked this conversation as resolved.
|
||
| } | ||
|
|
||
| fun launchTranslate(view: View?) { | ||
| val sortedLocalizedNamesRef = instance.languageLookUpTable!!.getCanonicalNames() | ||
| Collections.sort(sortedLocalizedNamesRef) | ||
| val languageAdapter = ArrayAdapter( | ||
| this@AboutActivity, | ||
| android.R.layout.simple_spinner_dropdown_item, sortedLocalizedNamesRef | ||
| ) | ||
| val spinner = Spinner(this@AboutActivity) | ||
| spinner.layoutParams = | ||
| LinearLayout.LayoutParams( | ||
| LinearLayout.LayoutParams.WRAP_CONTENT, | ||
| LinearLayout.LayoutParams.WRAP_CONTENT | ||
| ) | ||
| spinner.adapter = languageAdapter | ||
| spinner.gravity = 17 | ||
| spinner.setPadding(50, 0, 0, 0) | ||
|
|
||
| val positiveButtonRunnable = Runnable { | ||
| val langCode = instance.languageLookUpTable!!.getCodes()[spinner.selectedItemPosition] | ||
| handleWebUrl(this@AboutActivity, (Urls.TRANSLATE_WIKI_URL + langCode).toUri()) | ||
| } | ||
| showAlertDialog( | ||
| this, | ||
| getString(R.string.about_translate_title), | ||
| getString(R.string.about_translate_message), | ||
| getString(R.string.about_translate_proceed), | ||
| getString(R.string.about_translate_cancel), | ||
| positiveButtonRunnable, | ||
| {}, | ||
| spinner | ||
| ) | ||
| private fun launchTranslate(context: Context, langCode: String) { | ||
| handleWebUrl(context, (Urls.TRANSLATE_WIKI_URL + langCode).toUri()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.