Skip to content

Commit

Permalink
Merge branch 'main' into recent-search
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-raoul authored Dec 10, 2024
2 parents 62aad4c + 7331197 commit 59fe558
Show file tree
Hide file tree
Showing 223 changed files with 8,062 additions and 7,998 deletions.
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ dependencies {

implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'com.github.deano2390:MaterialShowcaseView:1.2.0'
implementation "com.google.android.material:material:1.9.0"
implementation "com.google.android.material:material:1.12.0"
implementation 'com.karumi:dexter:5.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

// Jetpack Compose
def composeBom = platform('androidx.compose:compose-bom:2024.08.00')
def composeBom = platform('androidx.compose:compose-bom:2024.11.00')

implementation "androidx.activity:activity-compose:1.9.1"
implementation "androidx.activity:activity-compose:1.9.3"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.8.4"
implementation (composeBom)
implementation "androidx.compose.runtime:runtime"
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.ui:ui-viewbinding"
implementation "androidx.compose.ui:ui-graphics"
implementation "androidx.compose.ui:ui-tooling"
implementation "androidx.compose.foundation:foundation"
Expand Down Expand Up @@ -138,7 +139,7 @@ dependencies {
implementation "androidx.browser:browser:1.3.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.exifinterface:exifinterface:1.3.2"
implementation 'androidx.exifinterface:exifinterface:1.3.7'
implementation "androidx.core:core-ktx:$CORE_KTX_VERSION"
implementation 'com.simplecityapps:recyclerview-fastscroll:2.0.1'

Expand Down Expand Up @@ -313,6 +314,7 @@ android {
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes\""
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.org/wiki/Special:PasswordReset\""
buildConfigField "String", "PRIVACY_POLICY_URL", "\"https://github.com/commons-app/commons-app-documentation/blob/master/android/Privacy-policy.md\""
buildConfigField "String", "FILE_USAGES_BASE_URL", "\"https://commons.wikimedia.org/w/api.php?action=query&format=json&formatversion=2\""
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons\""
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.contributions.contentprovider\""
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.modifications.contentprovider\""
Expand Down Expand Up @@ -348,6 +350,7 @@ android {
buildConfigField "String", "SIGNUP_SUCCESS_REDIRECTION_URL", "\"https://commons.m.wikimedia.beta.wmflabs.org/w/index.php?title=Main_Page&welcome=yes\""
buildConfigField "String", "FORGOT_PASSWORD_URL", "\"https://commons.wikimedia.beta.wmflabs.org/wiki/Special:PasswordReset\""
buildConfigField "String", "PRIVACY_POLICY_URL", "\"https://github.com/commons-app/commons-app-documentation/blob/master/android/Privacy-policy.md\""
buildConfigField "String", "FILE_USAGES_BASE_URL", "\"https://commons.wikimedia.org/w/api.php?action=query&format=json&formatversion=2\""
buildConfigField "String", "ACCOUNT_TYPE", "\"fr.free.nrw.commons.beta\""
buildConfigField "String", "CONTRIBUTION_AUTHORITY", "\"fr.free.nrw.commons.beta.contributions.contentprovider\""
buildConfigField "String", "MODIFICATION_AUTHORITY", "\"fr.free.nrw.commons.beta.modifications.contentprovider\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AboutActivityTest {
fun testLaunchTranslate() {
Espresso.onView(ViewMatchers.withId(R.id.about_translate)).perform(ViewActions.click())
Espresso.onView(ViewMatchers.withId(android.R.id.button1)).perform(ViewActions.click())
val langCode = CommonsApplication.instance.languageLookUpTable!!.codes[0]
val langCode = CommonsApplication.instance.languageLookUpTable!!.getCodes()[0]
Intents.intended(
CoreMatchers.allOf(
IntentMatchers.hasAction(Intent.ACTION_VIEW),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class LoginClient(
.subscribe({ response: MwQueryResponse? ->
loginResult.userId = response?.query()?.userInfo()?.id() ?: 0
loginResult.groups =
response?.query()?.getUserResponse(userName)?.groups ?: emptySet()
response?.query()?.getUserResponse(userName)?.getGroups() ?: emptySet()
cb.success(loginResult)
}, { caught: Throwable ->
Timber.e(caught, "Login succeeded but getting group information failed. ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class CampaignsPresenter @Inject constructor(
return
}

okHttpJsonApiClient.campaigns
okHttpJsonApiClient.getCampaigns()
.observeOn(mainThreadScheduler)
.subscribeOn(ioScheduler)
.doOnSubscribe { disposable = it }
.subscribe({ campaignResponseDTO ->
val campaigns = campaignResponseDTO.campaigns?.toMutableList()
val campaigns = campaignResponseDTO?.campaigns?.toMutableList()
if (campaigns.isNullOrEmpty()) {
Timber.e("The campaigns list is empty")
view!!.showCampaigns(null)
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level
import timber.log.Timber
import java.io.File
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton
Expand Down Expand Up @@ -170,14 +169,13 @@ class NetworkingModule {
@Named(NAMED_WIKI_DATA_WIKI_SITE)
fun provideWikidataWikiSite(): WikiSite = WikiSite(BuildConfig.WIKIDATA_URL)


/**
* Gson objects are very heavy. The app should ideally be using just one instance of it instead of creating new instances everywhere.
* @return returns a singleton Gson instance
*/
@Provides
@Singleton
fun provideGson(): Gson = GsonUtil.getDefaultGson()
fun provideGson(): Gson = GsonUtil.defaultGson

@Provides
@Singleton
Expand Down Expand Up @@ -294,9 +292,8 @@ class NetworkingModule {
@Provides
@Singleton
@Named(NAMED_LANGUAGE_WIKI_PEDIA_WIKI_SITE)
fun provideLanguageWikipediaSite(): WikiSite {
return WikiSite.forLanguageCode(Locale.getDefault().language)
}
fun provideLanguageWikipediaSite(): WikiSite =
WikiSite.forDefaultLocaleLanguageCode()

companion object {
private const val WIKIDATA_SPARQL_QUERY_URL = "https://query.wikidata.org/sparql"
Expand Down
123 changes: 65 additions & 58 deletions app/src/main/java/fr/free/nrw/commons/edit/EditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import android.animation.ValueAnimator
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.ExifInterface
import android.os.Bundle
import android.util.Log
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.rotationMatrix
import androidx.core.graphics.scaleMatrix
import androidx.core.net.toUri
import androidx.exifinterface.media.ExifInterface
import androidx.lifecycle.ViewModelProvider
import fr.free.nrw.commons.databinding.ActivityEditBinding
import timber.log.Timber
import java.io.File
import kotlin.math.ceil

/**
* An activity class for editing and rotating images using LLJTran with EXIF attribute preservation.
Expand All @@ -42,11 +42,12 @@ class EditActivity : AppCompatActivity() {
supportActionBar?.title = ""
val intent = intent
imageUri = intent.getStringExtra("image") ?: ""
vm = ViewModelProvider(this).get(EditViewModel::class.java)
vm = ViewModelProvider(this)[EditViewModel::class.java]
val sourceExif = imageUri.toUri().path?.let { ExifInterface(it) }

val exifTags =
arrayOf(
ExifInterface.TAG_APERTURE,
ExifInterface.TAG_F_NUMBER,
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_EXPOSURE_TIME,
ExifInterface.TAG_FLASH,
Expand All @@ -62,13 +63,13 @@ class EditActivity : AppCompatActivity() {
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_IMAGE_LENGTH,
ExifInterface.TAG_IMAGE_WIDTH,
ExifInterface.TAG_ISO,
ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY,
ExifInterface.TAG_MAKE,
ExifInterface.TAG_MODEL,
ExifInterface.TAG_ORIENTATION,
ExifInterface.TAG_WHITE_BALANCE,
ExifInterface.WHITEBALANCE_AUTO,
ExifInterface.WHITEBALANCE_MANUAL,
ExifInterface.WHITE_BALANCE_AUTO,
ExifInterface.WHITE_BALANCE_MANUAL,
)
for (tag in exifTags) {
val attribute = sourceExif?.getAttribute(tag.toString())
Expand All @@ -88,38 +89,36 @@ class EditActivity : AppCompatActivity() {
private fun init() {
binding.iv.adjustViewBounds = true
binding.iv.scaleType = ImageView.ScaleType.MATRIX
binding.iv.post(
Runnable {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imageUri, options)
binding.iv.post {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imageUri, options)

val bitmapWidth = options.outWidth
val bitmapHeight = options.outHeight
val bitmapWidth = options.outWidth
val bitmapHeight = options.outHeight

// Check if the bitmap dimensions exceed a certain threshold
val maxBitmapSize = 2000 // Set your maximum size here
if (bitmapWidth > maxBitmapSize || bitmapHeight > maxBitmapSize) {
val scaleFactor = calculateScaleFactor(bitmapWidth, bitmapHeight, maxBitmapSize)
options.inSampleSize = scaleFactor
options.inJustDecodeBounds = false
val scaledBitmap = BitmapFactory.decodeFile(imageUri, options)
binding.iv.setImageBitmap(scaledBitmap)
// Update the ImageView with the scaled bitmap
val scale = binding.iv.measuredWidth.toFloat() / scaledBitmap.width.toFloat()
binding.iv.layoutParams.height = (scale * scaledBitmap.height).toInt()
binding.iv.imageMatrix = scaleMatrix(scale, scale)
} else {
options.inJustDecodeBounds = false
val bitmap = BitmapFactory.decodeFile(imageUri, options)
binding.iv.setImageBitmap(bitmap)
// Check if the bitmap dimensions exceed a certain threshold
val maxBitmapSize = 2000 // Set your maximum size here
if (bitmapWidth > maxBitmapSize || bitmapHeight > maxBitmapSize) {
val scaleFactor = calculateScaleFactor(bitmapWidth, bitmapHeight, maxBitmapSize)
options.inSampleSize = scaleFactor
options.inJustDecodeBounds = false
val scaledBitmap = BitmapFactory.decodeFile(imageUri, options)
binding.iv.setImageBitmap(scaledBitmap)
// Update the ImageView with the scaled bitmap
val scale = binding.iv.measuredWidth.toFloat() / scaledBitmap.width.toFloat()
binding.iv.layoutParams.height = (scale * scaledBitmap.height).toInt()
binding.iv.imageMatrix = scaleMatrix(scale, scale)
} else {
options.inJustDecodeBounds = false
val bitmap = BitmapFactory.decodeFile(imageUri, options)
binding.iv.setImageBitmap(bitmap)

val scale = binding.iv.measuredWidth.toFloat() / bitmapWidth.toFloat()
binding.iv.layoutParams.height = (scale * bitmapHeight).toInt()
binding.iv.imageMatrix = scaleMatrix(scale, scale)
}
},
)
val scale = binding.iv.measuredWidth.toFloat() / bitmapWidth.toFloat()
binding.iv.layoutParams.height = (scale * bitmapHeight).toInt()
binding.iv.imageMatrix = scaleMatrix(scale, scale)
}
}
binding.rotateBtn.setOnClickListener {
animateImageHeight()
}
Expand All @@ -143,15 +142,15 @@ class EditActivity : AppCompatActivity() {
val drawableWidth: Float =
binding.iv
.getDrawable()
.getIntrinsicWidth()
.intrinsicWidth
.toFloat()
val drawableHeight: Float =
binding.iv
.getDrawable()
.getIntrinsicHeight()
.intrinsicHeight
.toFloat()
val viewWidth: Float = binding.iv.getMeasuredWidth().toFloat()
val viewHeight: Float = binding.iv.getMeasuredHeight().toFloat()
val viewWidth: Float = binding.iv.measuredWidth.toFloat()
val viewHeight: Float = binding.iv.measuredHeight.toFloat()
val rotation = imageRotation % 360
val newRotation = rotation + 90

Expand All @@ -162,16 +161,23 @@ class EditActivity : AppCompatActivity() {
Timber.d("Rotation $rotation")
Timber.d("new Rotation $newRotation")

if (rotation == 0 || rotation == 180) {
imageScale = viewWidth / drawableWidth
newImageScale = viewWidth / drawableHeight
newViewHeight = (drawableWidth * newImageScale).toInt()
} else if (rotation == 90 || rotation == 270) {
imageScale = viewWidth / drawableHeight
newImageScale = viewWidth / drawableWidth
newViewHeight = (drawableHeight * newImageScale).toInt()
} else {
throw UnsupportedOperationException("rotation can 0, 90, 180 or 270. \${rotation} is unsupported")
when (rotation) {
0, 180 -> {
imageScale = viewWidth / drawableWidth
newImageScale = viewWidth / drawableHeight
newViewHeight = (drawableWidth * newImageScale).toInt()
}
90, 270 -> {
imageScale = viewWidth / drawableHeight
newImageScale = viewWidth / drawableWidth
newViewHeight = (drawableHeight * newImageScale).toInt()
}
else -> {
throw
UnsupportedOperationException(
"rotation can 0, 90, 180 or 270. \${rotation} is unsupported"
)
}
}

val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(1000L)
Expand Down Expand Up @@ -204,7 +210,7 @@ class EditActivity : AppCompatActivity() {
(complementaryAnimVal * viewHeight + animVal * newViewHeight).toInt()
val animatedScale = complementaryAnimVal * imageScale + animVal * newImageScale
val animatedRotation = complementaryAnimVal * rotation + animVal * newRotation
binding.iv.getLayoutParams().height = animatedHeight
binding.iv.layoutParams.height = animatedHeight
val matrix: Matrix =
rotationMatrix(
animatedRotation,
Expand All @@ -218,8 +224,8 @@ class EditActivity : AppCompatActivity() {
drawableHeight / 2,
)
matrix.postTranslate(
-(drawableWidth - binding.iv.getMeasuredWidth()) / 2,
-(drawableHeight - binding.iv.getMeasuredHeight()) / 2,
-(drawableWidth - binding.iv.measuredWidth) / 2,
-(drawableHeight - binding.iv.measuredHeight) / 2,
)
binding.iv.setImageMatrix(matrix)
binding.iv.requestLayout()
Expand Down Expand Up @@ -267,9 +273,9 @@ class EditActivity : AppCompatActivity() {
*/
private fun copyExifData(editedImageExif: ExifInterface?) {
for (attr in sourceExifAttributeList) {
Log.d("Tag is ${attr.first}", "Value is ${attr.second}")
Timber.d("Value is ${attr.second}")
editedImageExif!!.setAttribute(attr.first, attr.second)
Log.d("Tag is ${attr.first}", "Value is ${attr.second}")
Timber.d("Value is ${attr.second}")
}

editedImageExif?.saveAttributes()
Expand Down Expand Up @@ -298,9 +304,10 @@ class EditActivity : AppCompatActivity() {
var scaleFactor = 1

if (originalWidth > maxSize || originalHeight > maxSize) {
// Calculate the largest power of 2 that is less than or equal to the desired width and height
val widthRatio = Math.ceil((originalWidth.toDouble() / maxSize.toDouble())).toInt()
val heightRatio = Math.ceil((originalHeight.toDouble() / maxSize.toDouble())).toInt()
// Calculate the largest power of 2 that is less than or equal to the desired
// width and height
val widthRatio = ceil((originalWidth.toDouble() / maxSize.toDouble())).toInt()
val heightRatio = ceil((originalHeight.toDouble() / maxSize.toDouble())).toInt()

scaleFactor = if (widthRatio > heightRatio) widthRatio else heightRatio
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import fr.free.nrw.commons.wikidata.model.Entities
import fr.free.nrw.commons.wikidata.model.gallery.ExtMetadata
import fr.free.nrw.commons.wikidata.model.gallery.ImageInfo
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage
import org.apache.commons.lang3.StringUtils
import java.text.ParseException
import java.util.Date
import javax.inject.Inject
Expand All @@ -24,24 +23,24 @@ class MediaConverter
entity: Entities.Entity,
imageInfo: ImageInfo,
): Media {
val metadata = imageInfo.metadata
val metadata = imageInfo.getMetadata()
requireNotNull(metadata) { "No metadata" }
// Stores mapping of title attribute to hidden attribute of each category
val myMap = mutableMapOf<String, Boolean>()
page.categories()?.forEach { myMap[it.title()] = (it.hidden()) }

return Media(
page.pageId().toString(),
imageInfo.thumbUrl.takeIf { it.isNotBlank() } ?: imageInfo.originalUrl,
imageInfo.originalUrl,
imageInfo.getThumbUrl().takeIf { it.isNotBlank() } ?: imageInfo.getOriginalUrl(),
imageInfo.getOriginalUrl(),
page.title(),
metadata.imageDescription(),
safeParseDate(metadata.dateTime()),
metadata.licenseShortName(),
metadata.prefixedLicenseUrl,
getAuthor(metadata),
getAuthor(metadata),
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories),
MediaDataExtractorUtil.extractCategoriesFromList(metadata.categories()),
metadata.latLng,
entity.labels().mapValues { it.value.value() },
entity.descriptions().mapValues { it.value.value() },
Expand Down Expand Up @@ -104,9 +103,5 @@ private val ExtMetadata.prefixedLicenseUrl: String
}

private val ExtMetadata.latLng: LatLng?
get() =
if (!StringUtils.isBlank(gpsLatitude) && !StringUtils.isBlank(gpsLongitude)) {
LatLng(gpsLatitude.toDouble(), gpsLongitude.toDouble(), 0.0f)
} else {
null
}
get() = LatLng.latLongOrNull(gpsLatitude(), gpsLongitude())

Loading

0 comments on commit 59fe558

Please sign in to comment.