mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Merge branch 'main' into added-button
This commit is contained in:
commit
d505266985
278 changed files with 9071 additions and 6727 deletions
2
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
2
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
|
|
@ -70,7 +70,7 @@ body:
|
||||||
required: false
|
required: false
|
||||||
- type: textarea
|
- type: textarea
|
||||||
attributes:
|
attributes:
|
||||||
label: Screen-shots
|
label: Screenshots
|
||||||
description: Add screenshots related to the issue (if available). Can be created by pressing the Volume Down and Power Button at the same time on Android 4.0 and higher.
|
description: Add screenshots related to the issue (if available). Can be created by pressing the Volume Down and Power Button at the same time on Android 4.0 and higher.
|
||||||
validations:
|
validations:
|
||||||
required: false
|
required: false
|
||||||
|
|
|
||||||
26
CHANGELOG.md
26
CHANGELOG.md
|
|
@ -1,5 +1,31 @@
|
||||||
# Wikimedia Commons for Android
|
# Wikimedia Commons for Android
|
||||||
|
|
||||||
|
## v6.0.2
|
||||||
|
|
||||||
|
### What's changed
|
||||||
|
* Addressed a bug that prevented the keyboard from appearing in various text fields, such as on the upload wizard
|
||||||
|
* Links in the "File usages" list are now clickable and will take you to the correct page.
|
||||||
|
* Titles for file usages are now clearer and easier to understand
|
||||||
|
* Bug fixes and stability improvements
|
||||||
|
|
||||||
|
## v6.0.1
|
||||||
|
|
||||||
|
### What's changed
|
||||||
|
* The app now supports Android 15 with an improved user interface
|
||||||
|
* Enhanced Nearby with robust and more reliable labels
|
||||||
|
* Bug fixes and stability improvements
|
||||||
|
|
||||||
|
## v5.6.1
|
||||||
|
|
||||||
|
### What's changed
|
||||||
|
* The app no longer uploads images to Wikidata if one exists already for a given item
|
||||||
|
* File usage displays correctly now
|
||||||
|
* No more infinite circular progress bar on nominating an image for deletion
|
||||||
|
* Enhanced location updates while using GPS
|
||||||
|
* Author/uploader names are now available in Media Details for Commons licensing compliance
|
||||||
|
* Improved usage of popups in Nearby
|
||||||
|
* Bug fixes and stability improvements
|
||||||
|
|
||||||
## v5.5.0
|
## v5.5.0
|
||||||
|
|
||||||
### What's changed
|
### What's changed
|
||||||
|
|
|
||||||
|
|
@ -18,14 +18,14 @@ if (isRunningOnTravisAndIsNotPRBuild) {
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "fr.free.nrw.commons"
|
namespace = "fr.free.nrw.commons"
|
||||||
compileSdk = 34
|
compileSdk = 35
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId = "fr.free.nrw.commons"
|
applicationId = "fr.free.nrw.commons"
|
||||||
minSdk = 21
|
minSdk = 21
|
||||||
targetSdk = 34
|
targetSdk = 35
|
||||||
versionCode = 1053
|
versionCode = 1058
|
||||||
versionName = "5.5.0"
|
versionName = "6.0.2"
|
||||||
|
|
||||||
setProperty("archivesBaseName", "app-commons-v$versionName-" + getBranchName())
|
setProperty("archivesBaseName", "app-commons-v$versionName-" + getBranchName())
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|
@ -226,6 +226,7 @@ dependencies {
|
||||||
implementation(libs.rxbinding)
|
implementation(libs.rxbinding)
|
||||||
implementation(libs.rxbinding.appcompat)
|
implementation(libs.rxbinding.appcompat)
|
||||||
implementation(libs.facebook.fresco)
|
implementation(libs.facebook.fresco)
|
||||||
|
implementation(libs.facebook.fresco.middleware)
|
||||||
implementation(libs.apache.commons.lang3)
|
implementation(libs.apache.commons.lang3)
|
||||||
|
|
||||||
// UI
|
// UI
|
||||||
|
|
|
||||||
|
|
@ -57,8 +57,7 @@
|
||||||
tools:replace="android:appComponentFactory">
|
tools:replace="android:appComponentFactory">
|
||||||
<activity
|
<activity
|
||||||
android:name=".activity.SingleWebViewActivity"
|
android:name=".activity.SingleWebViewActivity"
|
||||||
android:exported="false"
|
android:exported="false" />
|
||||||
android:label="@string/title_activity_single_web_view" />
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".nearby.WikidataFeedback"
|
android:name=".nearby.WikidataFeedback"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
|
@ -101,7 +100,6 @@
|
||||||
android:name=".upload.UploadActivity"
|
android:name=".upload.UploadActivity"
|
||||||
android:configChanges="orientation|screenSize|keyboard"
|
android:configChanges="orientation|screenSize|keyboard"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
android:hardwareAccelerated="false"
|
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
android:windowSoftInputMode="adjustResize">
|
android:windowSoftInputMode="adjustResize">
|
||||||
<intent-filter android:label="@string/intent_share_upload_label">
|
<intent-filter android:label="@string/intent_share_upload_label">
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import fr.free.nrw.commons.utils.ConfigUtils.getVersionNameWithSha
|
||||||
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
|
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import androidx.core.net.toUri
|
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.handleWebUrl
|
||||||
import fr.free.nrw.commons.utils.setUnderlinedText
|
import fr.free.nrw.commons.utils.setUnderlinedText
|
||||||
|
|
||||||
|
|
@ -47,6 +48,7 @@ class AboutActivity : BaseActivity() {
|
||||||
*/
|
*/
|
||||||
binding = ActivityAboutBinding.inflate(layoutInflater)
|
binding = ActivityAboutBinding.inflate(layoutInflater)
|
||||||
val view: View = binding!!.root
|
val view: View = binding!!.root
|
||||||
|
applyEdgeToEdgeTopInsets(binding!!.toolbarLayout)
|
||||||
setContentView(view)
|
setContentView(view)
|
||||||
|
|
||||||
setSupportActionBar(binding!!.toolbarBinding.toolbar)
|
setSupportActionBar(binding!!.toolbarBinding.toolbar)
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,8 @@ import com.facebook.drawee.backends.pipeline.Fresco
|
||||||
import com.facebook.imagepipeline.core.ImagePipelineConfig
|
import com.facebook.imagepipeline.core.ImagePipelineConfig
|
||||||
import fr.free.nrw.commons.auth.LoginActivity
|
import fr.free.nrw.commons.auth.LoginActivity
|
||||||
import fr.free.nrw.commons.auth.SessionManager
|
import fr.free.nrw.commons.auth.SessionManager
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao
|
|
||||||
import fr.free.nrw.commons.category.CategoryDao
|
import fr.free.nrw.commons.category.CategoryDao
|
||||||
import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler
|
import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler
|
||||||
import fr.free.nrw.commons.concurrency.ThreadPoolService
|
import fr.free.nrw.commons.concurrency.ThreadPoolService
|
||||||
|
|
@ -257,8 +256,8 @@ class CommonsApplication : MultiDexApplication() {
|
||||||
} catch (e: SQLiteException) {
|
} catch (e: SQLiteException) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
}
|
}
|
||||||
BookmarkPicturesDao.Table.onDelete(db)
|
BookmarksTable.onDelete(db)
|
||||||
BookmarkItemsDao.Table.onDelete(db)
|
BookmarkItemsTable.onDelete(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ object OkHttpConnectionFactory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class CommonHeaderRequestInterceptor : Interceptor {
|
class CommonHeaderRequestInterceptor : Interceptor {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val request = chain.request().newBuilder()
|
val request = chain.request().newBuilder()
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import fr.free.nrw.commons.databinding.ActivityWelcomeBinding
|
||||||
import fr.free.nrw.commons.databinding.PopupForCopyrightBinding
|
import fr.free.nrw.commons.databinding.PopupForCopyrightBinding
|
||||||
import fr.free.nrw.commons.quiz.QuizActivity
|
import fr.free.nrw.commons.quiz.QuizActivity
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
|
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
|
||||||
|
|
||||||
class WelcomeActivity : BaseActivity() {
|
class WelcomeActivity : BaseActivity() {
|
||||||
|
|
@ -23,6 +24,7 @@ class WelcomeActivity : BaseActivity() {
|
||||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityWelcomeBinding.inflate(layoutInflater)
|
binding = ActivityWelcomeBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.welcomePager.rootView)
|
||||||
setContentView(binding!!.root)
|
setContentView(binding!!.root)
|
||||||
|
|
||||||
isQuiz = intent?.extras?.getBoolean("isQuiz", false) ?: false
|
isQuiz = intent?.extras?.getBoolean("isQuiz", false) ?: false
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.app.NavUtils
|
import androidx.core.app.NavUtils
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.CommonsApplication
|
import fr.free.nrw.commons.CommonsApplication
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
|
|
@ -32,11 +33,13 @@ import fr.free.nrw.commons.contributions.MainActivity
|
||||||
import fr.free.nrw.commons.databinding.ActivityLoginBinding
|
import fr.free.nrw.commons.databinding.ActivityLoginBinding
|
||||||
import fr.free.nrw.commons.di.ApplicationlessInjection
|
import fr.free.nrw.commons.di.ApplicationlessInjection
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.utils.AbstractTextWatcher
|
import fr.free.nrw.commons.utils.AbstractTextWatcher
|
||||||
import fr.free.nrw.commons.utils.ActivityUtils.startActivityWithFlags
|
import fr.free.nrw.commons.utils.ActivityUtils.startActivityWithFlags
|
||||||
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
|
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
|
||||||
import fr.free.nrw.commons.utils.SystemThemeUtils
|
import fr.free.nrw.commons.utils.SystemThemeUtils
|
||||||
import fr.free.nrw.commons.utils.ViewUtil.hideKeyboard
|
import fr.free.nrw.commons.utils.ViewUtil.hideKeyboard
|
||||||
|
import fr.free.nrw.commons.utils.handleKeyboardInsets
|
||||||
import fr.free.nrw.commons.utils.handleWebUrl
|
import fr.free.nrw.commons.utils.handleWebUrl
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
@ -79,7 +82,14 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
delegate.installViewFactory()
|
delegate.installViewFactory()
|
||||||
delegate.onCreate(savedInstanceState)
|
delegate.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
WindowCompat.getInsetsController(window, window.decorView)
|
||||||
|
.isAppearanceLightStatusBars = !isDarkTheme
|
||||||
|
|
||||||
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||||
|
|
||||||
binding = ActivityLoginBinding.inflate(layoutInflater)
|
binding = ActivityLoginBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.root)
|
||||||
|
binding?.aboutPrivacyPolicy?.handleKeyboardInsets()
|
||||||
with(binding!!) {
|
with(binding!!) {
|
||||||
setContentView(root)
|
setContentView(root)
|
||||||
|
|
||||||
|
|
@ -92,7 +102,19 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
aboutPrivacyPolicy.setOnClickListener { onPrivacyPolicyClicked() }
|
aboutPrivacyPolicy.setOnClickListener { onPrivacyPolicyClicked() }
|
||||||
signUpButton.setOnClickListener { signUp() }
|
signUpButton.setOnClickListener { signUp() }
|
||||||
loginButton.setOnClickListener { performLogin() }
|
loginButton.setOnClickListener { performLogin() }
|
||||||
loginPassword.setOnEditorActionListener(::onEditorAction)
|
loginPassword.setOnEditorActionListener { textView, actionId, keyEvent ->
|
||||||
|
if (binding!!.loginButton.isEnabled && isTriggerAction(actionId, keyEvent)) {
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_NEXT && lastLoginResult != null) {
|
||||||
|
askUserForTwoFactorAuthWithKeyboard()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
performLogin()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loginPassword.onFocusChangeListener =
|
loginPassword.onFocusChangeListener =
|
||||||
View.OnFocusChangeListener(::onPasswordFocusChanged)
|
View.OnFocusChangeListener(::onPasswordFocusChanged)
|
||||||
|
|
@ -113,6 +135,39 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
fun askUserForTwoFactorAuthWithKeyboard() {
|
||||||
|
if (binding == null) {
|
||||||
|
Timber.w("Binding is null, reinitializing in askUserForTwoFactorAuthWithKeyboard")
|
||||||
|
binding = ActivityLoginBinding.inflate(layoutInflater)
|
||||||
|
setContentView(binding!!.root)
|
||||||
|
}
|
||||||
|
progressDialog!!.dismiss()
|
||||||
|
if (binding != null) {
|
||||||
|
with(binding!!) {
|
||||||
|
twoFactorContainer.visibility = View.VISIBLE
|
||||||
|
twoFactorContainer.hint = getString(if (lastLoginResult is LoginResult.EmailAuthResult) R.string.email_auth_code else R.string._2fa_code)
|
||||||
|
loginTwoFactor.visibility = View.VISIBLE
|
||||||
|
loginTwoFactor.requestFocus()
|
||||||
|
|
||||||
|
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
|
imm.showSoftInput(loginTwoFactor, InputMethodManager.SHOW_IMPLICIT)
|
||||||
|
|
||||||
|
loginTwoFactor.setOnEditorActionListener { _, actionId, event ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_DONE ||
|
||||||
|
(event != null && event.keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN)) {
|
||||||
|
performLogin()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timber.e("Binding is null in askUserForTwoFactorAuthWithKeyboard after reinitialization attempt")
|
||||||
|
}
|
||||||
|
showMessageAndCancelDialog(getString(if (lastLoginResult is LoginResult.EmailAuthResult) R.string.login_failed_email_auth_needed else R.string.login_failed_2fa_needed))
|
||||||
|
}
|
||||||
override fun onPostCreate(savedInstanceState: Bundle?) {
|
override fun onPostCreate(savedInstanceState: Bundle?) {
|
||||||
super.onPostCreate(savedInstanceState)
|
super.onPostCreate(savedInstanceState)
|
||||||
delegate.onPostCreate(savedInstanceState)
|
delegate.onPostCreate(savedInstanceState)
|
||||||
|
|
@ -236,7 +291,7 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
} else false
|
} else false
|
||||||
|
|
||||||
private fun isTriggerAction(actionId: Int, keyEvent: KeyEvent?) =
|
private fun isTriggerAction(actionId: Int, keyEvent: KeyEvent?) =
|
||||||
actionId == EditorInfo.IME_ACTION_DONE || keyEvent?.keyCode == KeyEvent.KEYCODE_ENTER
|
actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE || keyEvent?.keyCode == KeyEvent.KEYCODE_ENTER
|
||||||
|
|
||||||
private fun skipLogin() {
|
private fun skipLogin() {
|
||||||
AlertDialog.Builder(this)
|
AlertDialog.Builder(this)
|
||||||
|
|
@ -286,14 +341,14 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
Timber.d("Requesting 2FA prompt")
|
Timber.d("Requesting 2FA prompt")
|
||||||
progressDialog!!.dismiss()
|
progressDialog!!.dismiss()
|
||||||
lastLoginResult = loginResult
|
lastLoginResult = loginResult
|
||||||
askUserForTwoFactorAuth()
|
askUserForTwoFactorAuthWithKeyboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun emailAuthPrompt(loginResult: LoginResult, caught: Throwable, token: String?) {
|
override fun emailAuthPrompt(loginResult: LoginResult, caught: Throwable, token: String?) = runOnUiThread {
|
||||||
Timber.d("Requesting email auth prompt")
|
Timber.d("Requesting email auth prompt")
|
||||||
progressDialog!!.dismiss()
|
progressDialog!!.dismiss()
|
||||||
lastLoginResult = loginResult
|
lastLoginResult = loginResult
|
||||||
askUserForTwoFactorAuth()
|
askUserForTwoFactorAuthWithKeyboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun passwordResetPrompt(token: String?) = runOnUiThread {
|
override fun passwordResetPrompt(token: String?) = runOnUiThread {
|
||||||
|
|
@ -348,12 +403,31 @@ class LoginActivity : AccountAuthenticatorActivity() {
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
fun askUserForTwoFactorAuth() {
|
fun askUserForTwoFactorAuth() {
|
||||||
|
if (binding == null) {
|
||||||
|
Timber.w("Binding is null, reinitializing in askUserForTwoFactorAuth")
|
||||||
|
binding = ActivityLoginBinding.inflate(layoutInflater)
|
||||||
|
setContentView(binding!!.root)
|
||||||
|
}
|
||||||
progressDialog!!.dismiss()
|
progressDialog!!.dismiss()
|
||||||
with(binding!!) {
|
if (binding != null) {
|
||||||
twoFactorContainer.visibility = View.VISIBLE
|
with(binding!!) {
|
||||||
twoFactorContainer.hint = getString(if (lastLoginResult is LoginResult.EmailAuthResult) R.string.email_auth_code else R.string._2fa_code)
|
twoFactorContainer.visibility = View.VISIBLE
|
||||||
loginTwoFactor.visibility = View.VISIBLE
|
twoFactorContainer.hint = getString(if (lastLoginResult is LoginResult.EmailAuthResult) R.string.email_auth_code else R.string._2fa_code)
|
||||||
loginTwoFactor.requestFocus()
|
loginTwoFactor.visibility = View.VISIBLE
|
||||||
|
loginTwoFactor.requestFocus()
|
||||||
|
|
||||||
|
loginTwoFactor.setOnEditorActionListener { _, actionId, event ->
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_DONE ||
|
||||||
|
(event != null && event.keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN)) {
|
||||||
|
performLogin()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timber.e("Binding is null in askUserForTwoFactorAuth after reinitialization attempt")
|
||||||
}
|
}
|
||||||
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
|
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
|
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import android.widget.Toast
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
class SignupActivity : BaseActivity() {
|
class SignupActivity : BaseActivity() {
|
||||||
|
|
@ -21,6 +22,7 @@ class SignupActivity : BaseActivity() {
|
||||||
Timber.d("Signup Activity started")
|
Timber.d("Signup Activity started")
|
||||||
|
|
||||||
webView = WebView(this)
|
webView = WebView(this)
|
||||||
|
applyEdgeToEdgeAllInsets(webView!!)
|
||||||
with(webView!!) {
|
with(webView!!) {
|
||||||
setContentView(this)
|
setContentView(this)
|
||||||
webViewClient = MyWebViewClient()
|
webViewClient = MyWebViewClient()
|
||||||
|
|
|
||||||
|
|
@ -1,105 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentBookmarksBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
|
||||||
import fr.free.nrw.commons.theme.BaseActivity;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import fr.free.nrw.commons.contributions.ContributionController;
|
|
||||||
import javax.inject.Named;
|
|
||||||
|
|
||||||
public class BookmarkFragment extends CommonsDaggerSupportFragment {
|
|
||||||
|
|
||||||
private FragmentManager supportFragmentManager;
|
|
||||||
private BookmarksPagerAdapter adapter;
|
|
||||||
FragmentBookmarksBinding binding;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
ContributionController controller;
|
|
||||||
/**
|
|
||||||
* To check if the user is loggedIn or not.
|
|
||||||
*/
|
|
||||||
@Inject
|
|
||||||
@Named("default_preferences")
|
|
||||||
public
|
|
||||||
JsonKvStore applicationKvStore;
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public static BookmarkFragment newInstance() {
|
|
||||||
BookmarkFragment fragment = new BookmarkFragment();
|
|
||||||
fragment.setRetainInstance(true);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setScroll(boolean canScroll) {
|
|
||||||
if (binding!=null) {
|
|
||||||
binding.viewPagerBookmarks.setCanScroll(canScroll);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
||||||
@Nullable final ViewGroup container,
|
|
||||||
@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreateView(inflater, container, savedInstanceState);
|
|
||||||
binding = FragmentBookmarksBinding.inflate(inflater, container, false);
|
|
||||||
|
|
||||||
// Activity can call methods in the fragment by acquiring a
|
|
||||||
// reference to the Fragment from FragmentManager, using findFragmentById()
|
|
||||||
supportFragmentManager = getChildFragmentManager();
|
|
||||||
|
|
||||||
adapter = new BookmarksPagerAdapter(supportFragmentManager, getContext(),
|
|
||||||
applicationKvStore.getBoolean("login_skipped"));
|
|
||||||
binding.viewPagerBookmarks.setAdapter(adapter);
|
|
||||||
binding.tabLayout.setupWithViewPager(binding.viewPagerBookmarks);
|
|
||||||
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
|
||||||
|
|
||||||
setupTabLayout();
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method sets up the tab layout. If the adapter has only one element it sets the
|
|
||||||
* visibility of tabLayout to gone.
|
|
||||||
*/
|
|
||||||
public void setupTabLayout() {
|
|
||||||
binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
if (adapter.getCount() == 1) {
|
|
||||||
binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void onBackPressed() {
|
|
||||||
if (((BookmarkListRootFragment) (adapter.getItem(binding.tabLayout.getSelectedTabPosition())))
|
|
||||||
.backPressed()) {
|
|
||||||
// The event is handled internally by the adapter , no further action required.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Event is not handled by the adapter ( performed back action ) change action bar.
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,98 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import fr.free.nrw.commons.contributions.ContributionController
|
||||||
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentBookmarksBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
|
||||||
|
class BookmarkFragment : CommonsDaggerSupportFragment() {
|
||||||
|
private var adapter: BookmarksPagerAdapter? = null
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
var binding: FragmentBookmarksBinding? = null
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var controller: ContributionController? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To check if the user is loggedIn or not.
|
||||||
|
*/
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
@Named("default_preferences")
|
||||||
|
var applicationKvStore: JsonKvStore? = null
|
||||||
|
|
||||||
|
fun setScroll(canScroll: Boolean) {
|
||||||
|
binding?.let {
|
||||||
|
it.viewPagerBookmarks.canScroll = canScroll
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
super.onCreateView(inflater, container, savedInstanceState)
|
||||||
|
binding = FragmentBookmarksBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
|
// Activity can call methods in the fragment by acquiring a
|
||||||
|
// reference to the Fragment from FragmentManager, using findFragmentById()
|
||||||
|
val supportFragmentManager = childFragmentManager
|
||||||
|
|
||||||
|
adapter = BookmarksPagerAdapter(
|
||||||
|
supportFragmentManager, requireContext(),
|
||||||
|
applicationKvStore!!.getBoolean("login_skipped")
|
||||||
|
)
|
||||||
|
binding!!.viewPagerBookmarks.adapter = adapter
|
||||||
|
binding!!.tabLayout.setupWithViewPager(binding!!.viewPagerBookmarks)
|
||||||
|
|
||||||
|
(requireActivity() as MainActivity).showTabs()
|
||||||
|
(requireActivity() as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
|
||||||
|
setupTabLayout()
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets up the tab layout. If the adapter has only one element it sets the
|
||||||
|
* visibility of tabLayout to gone.
|
||||||
|
*/
|
||||||
|
fun setupTabLayout() {
|
||||||
|
binding!!.tabLayout.visibility = View.VISIBLE
|
||||||
|
if (adapter!!.count == 1) {
|
||||||
|
binding!!.tabLayout.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun onBackPressed() {
|
||||||
|
if (((adapter!!.getItem(binding!!.tabLayout.selectedTabPosition)) as BookmarkListRootFragment).backPressed()) {
|
||||||
|
// The event is handled internally by the adapter , no further action required.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event is not handled by the adapter ( performed back action ) change action bar.
|
||||||
|
(requireActivity() as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun newInstance(): BookmarkFragment = BookmarkFragment().apply {
|
||||||
|
retainInstance = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,267 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.AdapterView;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.bookmarks.category.BookmarkCategoriesFragment;
|
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsFragment;
|
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsFragment;
|
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesFragment;
|
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.category.GridViewAdapter;
|
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider;
|
|
||||||
import fr.free.nrw.commons.navtab.NavTab;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class BookmarkListRootFragment extends CommonsDaggerSupportFragment implements
|
|
||||||
FragmentManager.OnBackStackChangedListener,
|
|
||||||
MediaDetailProvider,
|
|
||||||
AdapterView.OnItemClickListener, CategoryImagesCallback {
|
|
||||||
|
|
||||||
private MediaDetailPagerFragment mediaDetails;
|
|
||||||
//private BookmarkPicturesFragment bookmarkPicturesFragment;
|
|
||||||
private BookmarkLocationsFragment bookmarkLocationsFragment;
|
|
||||||
public Fragment listFragment;
|
|
||||||
private BookmarksPagerAdapter bookmarksPagerAdapter;
|
|
||||||
|
|
||||||
FragmentFeaturedRootBinding binding;
|
|
||||||
|
|
||||||
public BookmarkListRootFragment() {
|
|
||||||
//empty constructor necessary otherwise crashes on recreate
|
|
||||||
}
|
|
||||||
|
|
||||||
public BookmarkListRootFragment(Bundle bundle, BookmarksPagerAdapter bookmarksPagerAdapter) {
|
|
||||||
String title = bundle.getString("categoryName");
|
|
||||||
int order = bundle.getInt("order");
|
|
||||||
final int orderItem = bundle.getInt("orderItem");
|
|
||||||
|
|
||||||
switch (order){
|
|
||||||
case 0: listFragment = new BookmarkPicturesFragment();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1: listFragment = new BookmarkLocationsFragment();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3: listFragment = new BookmarkCategoriesFragment();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(orderItem == 2) {
|
|
||||||
listFragment = new BookmarkItemsFragment();
|
|
||||||
}
|
|
||||||
|
|
||||||
Bundle featuredArguments = new Bundle();
|
|
||||||
featuredArguments.putString("categoryName", title);
|
|
||||||
listFragment.setArguments(featuredArguments);
|
|
||||||
this.bookmarksPagerAdapter = bookmarksPagerAdapter;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
||||||
@Nullable final ViewGroup container,
|
|
||||||
@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false);
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
if (savedInstanceState == null) {
|
|
||||||
setFragment(listFragment, mediaDetails);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFragment(Fragment fragment, Fragment otherFragment) {
|
|
||||||
if (fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (fragment.isAdded() && otherFragment == null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.add(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded()) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeFragment(Fragment fragment) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.remove(fragment)
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAttach(final Context context) {
|
|
||||||
super.onAttach(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMediaClicked(int position) {
|
|
||||||
Timber.d("on media clicked");
|
|
||||||
/*container.setVisibility(View.VISIBLE);
|
|
||||||
((BookmarkFragment)getParentFragment()).tabLayout.setVisibility(View.GONE);
|
|
||||||
mediaDetails = new MediaDetailPagerFragment(false, true, position);
|
|
||||||
setFragment(mediaDetails, bookmarkPicturesFragment);*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
|
||||||
*
|
|
||||||
* @param i It is the index of which media object is to be returned which is same as current
|
|
||||||
* index of viewPager.
|
|
||||||
* @return Media Object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Media getMediaAtPosition(int i) {
|
|
||||||
if (bookmarksPagerAdapter.getMediaAdapter() == null) {
|
|
||||||
// not yet ready to return data
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
return (Media) bookmarksPagerAdapter.getMediaAdapter().getItem(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
|
||||||
* same number of media items as that of media elements in adapter.
|
|
||||||
*
|
|
||||||
* @return Total Media count in the adapter
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTotalMediaCount() {
|
|
||||||
if (bookmarksPagerAdapter.getMediaAdapter() == null) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return bookmarksPagerAdapter.getMediaAdapter().getCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getContributionStateAt(int position) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload media detail fragment once media is nominated
|
|
||||||
*
|
|
||||||
* @param index item position that has been nominated
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void refreshNominatedMedia(int index) {
|
|
||||||
if (mediaDetails != null && !listFragment.isVisible()) {
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
mediaDetails = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
((BookmarkFragment) getParentFragment()).setScroll(false);
|
|
||||||
setFragment(mediaDetails, listFragment);
|
|
||||||
mediaDetails.showImage(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on success of API call for featured images or mobile uploads. The
|
|
||||||
* viewpager will notified that number of items have changed.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void viewPagerNotifyDataSetChanged() {
|
|
||||||
if (mediaDetails != null) {
|
|
||||||
mediaDetails.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean backPressed() {
|
|
||||||
//check mediaDetailPage fragment is not null then we check mediaDetail.is Visible or not to avoid NullPointerException
|
|
||||||
if (mediaDetails != null) {
|
|
||||||
if (mediaDetails.isVisible()) {
|
|
||||||
// todo add get list fragment
|
|
||||||
((BookmarkFragment) getParentFragment()).setupTabLayout();
|
|
||||||
ArrayList<Integer> removed = mediaDetails.getRemovedItems();
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
((BookmarkFragment) getParentFragment()).setScroll(true);
|
|
||||||
setFragment(listFragment, mediaDetails);
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
if (listFragment instanceof BookmarkPicturesFragment) {
|
|
||||||
GridViewAdapter adapter = ((GridViewAdapter) ((BookmarkPicturesFragment) listFragment)
|
|
||||||
.getAdapter());
|
|
||||||
Iterator i = removed.iterator();
|
|
||||||
while (i.hasNext()) {
|
|
||||||
adapter.remove(adapter.getItem((int) i.next()));
|
|
||||||
}
|
|
||||||
mediaDetails.clearRemoved();
|
|
||||||
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
moveToContributionsFragment();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
moveToContributionsFragment();
|
|
||||||
}
|
|
||||||
// notify mediaDetails did not handled the backPressed further actions required.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void moveToContributionsFragment() {
|
|
||||||
((MainActivity) getActivity()).setSelectedItemId(NavTab.CONTRIBUTIONS.code());
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
||||||
Timber.d("on media clicked");
|
|
||||||
binding.exploreContainer.setVisibility(View.VISIBLE);
|
|
||||||
((BookmarkFragment) getParentFragment()).binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
mediaDetails = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
((BookmarkFragment) getParentFragment()).setScroll(false);
|
|
||||||
setFragment(mediaDetails, listFragment);
|
|
||||||
mediaDetails.showImage(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onBackStackChanged() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,226 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.AdapterView
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.FragmentManager
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.bookmarks.category.BookmarkCategoriesFragment
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsFragment
|
||||||
|
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsFragment
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesFragment
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback
|
||||||
|
import fr.free.nrw.commons.category.GridViewAdapter
|
||||||
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment.Companion.newInstance
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
|
import fr.free.nrw.commons.navtab.NavTab
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
class BookmarkListRootFragment : CommonsDaggerSupportFragment,
|
||||||
|
FragmentManager.OnBackStackChangedListener, MediaDetailProvider, OnItemClickListener,
|
||||||
|
CategoryImagesCallback {
|
||||||
|
private var mediaDetails: MediaDetailPagerFragment? = null
|
||||||
|
private val bookmarkLocationsFragment: BookmarkLocationsFragment? = null
|
||||||
|
var listFragment: Fragment? = null
|
||||||
|
private var bookmarksPagerAdapter: BookmarksPagerAdapter? = null
|
||||||
|
|
||||||
|
var binding: FragmentFeaturedRootBinding? = null
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
|
||||||
|
constructor(bundle: Bundle, bookmarksPagerAdapter: BookmarksPagerAdapter) {
|
||||||
|
val title = bundle.getString("categoryName")
|
||||||
|
val order = bundle.getInt("order")
|
||||||
|
val orderItem = bundle.getInt("orderItem")
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
0 -> listFragment = BookmarkPicturesFragment()
|
||||||
|
1 -> listFragment = BookmarkLocationsFragment()
|
||||||
|
3 -> listFragment = BookmarkCategoriesFragment()
|
||||||
|
}
|
||||||
|
if (orderItem == 2) {
|
||||||
|
listFragment = BookmarkItemsFragment()
|
||||||
|
}
|
||||||
|
|
||||||
|
val featuredArguments = Bundle()
|
||||||
|
featuredArguments.putString("categoryName", title)
|
||||||
|
listFragment!!.setArguments(featuredArguments)
|
||||||
|
this.bookmarksPagerAdapter = bookmarksPagerAdapter
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View? {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false)
|
||||||
|
return binding!!.getRoot()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
if (savedInstanceState == null) {
|
||||||
|
setFragment(listFragment!!, mediaDetails)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFragment(fragment: Fragment, otherFragment: Fragment?) {
|
||||||
|
if (fragment.isAdded() && otherFragment != null) {
|
||||||
|
getChildFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
getChildFragmentManager().executePendingTransactions()
|
||||||
|
} else if (fragment.isAdded() && otherFragment == null) {
|
||||||
|
getChildFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
getChildFragmentManager().executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded() && otherFragment != null) {
|
||||||
|
getChildFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.add(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
getChildFragmentManager().executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded()) {
|
||||||
|
getChildFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
getChildFragmentManager().executePendingTransactions()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeFragment(fragment: Fragment) {
|
||||||
|
getChildFragmentManager()
|
||||||
|
.beginTransaction()
|
||||||
|
.remove(fragment)
|
||||||
|
.commit()
|
||||||
|
getChildFragmentManager().executePendingTransactions()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMediaClicked(position: Int) {
|
||||||
|
Timber.d("on media clicked")
|
||||||
|
/*container.setVisibility(View.VISIBLE);
|
||||||
|
((BookmarkFragment)getParentFragment()).tabLayout.setVisibility(View.GONE);
|
||||||
|
mediaDetails = new MediaDetailPagerFragment(false, true, position);
|
||||||
|
setFragment(mediaDetails, bookmarkPicturesFragment);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
||||||
|
*
|
||||||
|
* @param i It is the index of which media object is to be returned which is same as current
|
||||||
|
* index of viewPager.
|
||||||
|
* @return Media Object
|
||||||
|
*/
|
||||||
|
override fun getMediaAtPosition(i: Int): Media? =
|
||||||
|
bookmarksPagerAdapter!!.mediaAdapter?.getItem(i) as Media?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
||||||
|
* same number of media items as that of media elements in adapter.
|
||||||
|
*
|
||||||
|
* @return Total Media count in the adapter
|
||||||
|
*/
|
||||||
|
override fun getTotalMediaCount(): Int =
|
||||||
|
bookmarksPagerAdapter!!.mediaAdapter?.count ?: 0
|
||||||
|
|
||||||
|
override fun getContributionStateAt(position: Int): Int? {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload media detail fragment once media is nominated
|
||||||
|
*
|
||||||
|
* @param index item position that has been nominated
|
||||||
|
*/
|
||||||
|
override fun refreshNominatedMedia(index: Int) {
|
||||||
|
if (mediaDetails != null && !listFragment!!.isVisible()) {
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
mediaDetails = newInstance(false, true)
|
||||||
|
(parentFragment as BookmarkFragment).setScroll(false)
|
||||||
|
setFragment(mediaDetails!!, listFragment)
|
||||||
|
mediaDetails!!.showImage(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on success of API call for featured images or mobile uploads. The
|
||||||
|
* viewpager will notified that number of items have changed.
|
||||||
|
*/
|
||||||
|
override fun viewPagerNotifyDataSetChanged() {
|
||||||
|
if (mediaDetails != null) {
|
||||||
|
mediaDetails!!.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun backPressed(): Boolean {
|
||||||
|
//check mediaDetailPage fragment is not null then we check mediaDetail.is Visible or not to avoid NullPointerException
|
||||||
|
if (mediaDetails != null) {
|
||||||
|
if (mediaDetails!!.isVisible()) {
|
||||||
|
// todo add get list fragment
|
||||||
|
(parentFragment as BookmarkFragment).setupTabLayout()
|
||||||
|
val removed: ArrayList<Int> = mediaDetails!!.removedItems
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
(parentFragment as BookmarkFragment).setScroll(true)
|
||||||
|
setFragment(listFragment!!, mediaDetails)
|
||||||
|
(requireActivity() as MainActivity).showTabs()
|
||||||
|
if (listFragment is BookmarkPicturesFragment) {
|
||||||
|
val adapter = ((listFragment as BookmarkPicturesFragment)
|
||||||
|
.getAdapter() as GridViewAdapter?)
|
||||||
|
val i: MutableIterator<*> = removed.iterator()
|
||||||
|
while (i.hasNext()) {
|
||||||
|
adapter!!.remove(adapter.getItem(i.next() as Int))
|
||||||
|
}
|
||||||
|
mediaDetails!!.clearRemoved()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
moveToContributionsFragment()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
moveToContributionsFragment()
|
||||||
|
}
|
||||||
|
// notify mediaDetails did not handled the backPressed further actions required.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun moveToContributionsFragment() {
|
||||||
|
(requireActivity() as MainActivity).setSelectedItemId(NavTab.CONTRIBUTIONS.code())
|
||||||
|
(requireActivity() as MainActivity).showTabs()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||||
|
Timber.d("on media clicked")
|
||||||
|
binding!!.exploreContainer.visibility = View.VISIBLE
|
||||||
|
(parentFragment as BookmarkFragment).binding!!.tabLayout.setVisibility(View.GONE)
|
||||||
|
mediaDetails = newInstance(false, true)
|
||||||
|
(parentFragment as BookmarkFragment).setScroll(false)
|
||||||
|
setFragment(mediaDetails!!, listFragment)
|
||||||
|
mediaDetails!!.showImage(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBackStackChanged() = Unit
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,94 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.widget.ListAdapter;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import androidx.fragment.app.FragmentPagerAdapter;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesFragment;
|
|
||||||
|
|
||||||
public class BookmarksPagerAdapter extends FragmentPagerAdapter {
|
|
||||||
|
|
||||||
private ArrayList<BookmarkPages> pages;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default Constructor
|
|
||||||
* @param fm
|
|
||||||
* @param context
|
|
||||||
* @param onlyPictures is true if the fragment requires only BookmarkPictureFragment
|
|
||||||
* (i.e. when no user is logged in).
|
|
||||||
*/
|
|
||||||
BookmarksPagerAdapter(FragmentManager fm, Context context,boolean onlyPictures) {
|
|
||||||
super(fm);
|
|
||||||
pages = new ArrayList<>();
|
|
||||||
Bundle picturesBundle = new Bundle();
|
|
||||||
picturesBundle.putString("categoryName", context.getString(R.string.title_page_bookmarks_pictures));
|
|
||||||
picturesBundle.putInt("order", 0);
|
|
||||||
pages.add(new BookmarkPages(
|
|
||||||
new BookmarkListRootFragment(picturesBundle, this),
|
|
||||||
context.getString(R.string.title_page_bookmarks_pictures)));
|
|
||||||
if (!onlyPictures) {
|
|
||||||
// if onlyPictures is false we also add the location fragment.
|
|
||||||
Bundle locationBundle = new Bundle();
|
|
||||||
locationBundle.putString("categoryName",
|
|
||||||
context.getString(R.string.title_page_bookmarks_locations));
|
|
||||||
locationBundle.putInt("order", 1);
|
|
||||||
pages.add(new BookmarkPages(
|
|
||||||
new BookmarkListRootFragment(locationBundle, this),
|
|
||||||
context.getString(R.string.title_page_bookmarks_locations)));
|
|
||||||
|
|
||||||
locationBundle.putInt("orderItem", 2);
|
|
||||||
pages.add(new BookmarkPages(
|
|
||||||
new BookmarkListRootFragment(locationBundle, this),
|
|
||||||
context.getString(R.string.title_page_bookmarks_items)));
|
|
||||||
}
|
|
||||||
final Bundle categoriesBundle = new Bundle();
|
|
||||||
categoriesBundle.putString("categoryName",
|
|
||||||
context.getString(R.string.title_page_bookmarks_categories));
|
|
||||||
categoriesBundle.putInt("order", 3);
|
|
||||||
pages.add(new BookmarkPages(
|
|
||||||
new BookmarkListRootFragment(categoriesBundle, this),
|
|
||||||
context.getString(R.string.title_page_bookmarks_categories)));
|
|
||||||
notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Fragment getItem(int position) {
|
|
||||||
return pages.get(position).getPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getCount() {
|
|
||||||
return pages.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public CharSequence getPageTitle(int position) {
|
|
||||||
return pages.get(position).getTitle();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the Adapter used to display the picture gridview
|
|
||||||
* @return adapter
|
|
||||||
*/
|
|
||||||
public ListAdapter getMediaAdapter() {
|
|
||||||
BookmarkPicturesFragment fragment = (BookmarkPicturesFragment)(((BookmarkListRootFragment)pages.get(0).getPage()).listFragment);
|
|
||||||
return fragment.getAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the pictures list for the bookmark fragment
|
|
||||||
*/
|
|
||||||
public void requestPictureListUpdate() {
|
|
||||||
BookmarkPicturesFragment fragment = (BookmarkPicturesFragment)(((BookmarkListRootFragment)pages.get(0).getPage()).listFragment);
|
|
||||||
fragment.onResume();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,82 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.widget.ListAdapter
|
||||||
|
import androidx.core.os.bundleOf
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.FragmentManager
|
||||||
|
import androidx.fragment.app.FragmentPagerAdapter
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesFragment
|
||||||
|
|
||||||
|
class BookmarksPagerAdapter internal constructor(
|
||||||
|
fm: FragmentManager, context: Context, onlyPictures: Boolean
|
||||||
|
) : FragmentPagerAdapter(fm) {
|
||||||
|
private val pages = mutableListOf<BookmarkPages>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
* @param fm
|
||||||
|
* @param context
|
||||||
|
* @param onlyPictures is true if the fragment requires only BookmarkPictureFragment
|
||||||
|
* (i.e. when no user is logged in).
|
||||||
|
*/
|
||||||
|
init {
|
||||||
|
pages.add(
|
||||||
|
BookmarkPages(
|
||||||
|
BookmarkListRootFragment(
|
||||||
|
bundleOf(
|
||||||
|
"categoryName" to context.getString(R.string.title_page_bookmarks_pictures),
|
||||||
|
"order" to 0
|
||||||
|
), this
|
||||||
|
), context.getString(R.string.title_page_bookmarks_pictures)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (!onlyPictures) {
|
||||||
|
// if onlyPictures is false we also add the location fragment.
|
||||||
|
val locationBundle = bundleOf(
|
||||||
|
"categoryName" to context.getString(R.string.title_page_bookmarks_locations),
|
||||||
|
"order" to 1
|
||||||
|
)
|
||||||
|
|
||||||
|
pages.add(
|
||||||
|
BookmarkPages(
|
||||||
|
BookmarkListRootFragment(locationBundle, this),
|
||||||
|
context.getString(R.string.title_page_bookmarks_locations)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
locationBundle.putInt("orderItem", 2)
|
||||||
|
pages.add(
|
||||||
|
BookmarkPages(
|
||||||
|
BookmarkListRootFragment(locationBundle, this),
|
||||||
|
context.getString(R.string.title_page_bookmarks_items)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
pages.add(
|
||||||
|
BookmarkPages(
|
||||||
|
BookmarkListRootFragment(
|
||||||
|
bundleOf(
|
||||||
|
"categoryName" to context.getString(R.string.title_page_bookmarks_categories),
|
||||||
|
"order" to 3
|
||||||
|
), this),
|
||||||
|
context.getString(R.string.title_page_bookmarks_categories)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItem(position: Int): Fragment = pages[position].page!!
|
||||||
|
|
||||||
|
override fun getCount(): Int = pages.size
|
||||||
|
|
||||||
|
override fun getPageTitle(position: Int): CharSequence? = pages[position].title
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the Adapter used to display the picture gridview
|
||||||
|
* @return adapter
|
||||||
|
*/
|
||||||
|
val mediaAdapter: ListAdapter?
|
||||||
|
get() = (((pages[0].page as BookmarkListRootFragment).listFragment) as BookmarkPicturesFragment).getAdapter()
|
||||||
|
}
|
||||||
|
|
@ -1,129 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.items;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.Table.COLUMN_ID;
|
|
||||||
import static fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao.Table.TABLE_NAME;
|
|
||||||
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.database.sqlite.SQLiteQueryBuilder;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles private storage for bookmarked items
|
|
||||||
*/
|
|
||||||
public class BookmarkItemsContentProvider extends CommonsDaggerContentProvider {
|
|
||||||
|
|
||||||
private static final String BASE_PATH = "bookmarksItems";
|
|
||||||
public static final Uri BASE_URI =
|
|
||||||
Uri.parse("content://" + BuildConfig.BOOKMARK_ITEMS_AUTHORITY + "/" + BASE_PATH);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append bookmark items ID to the base uri
|
|
||||||
*/
|
|
||||||
public static Uri uriForName(final String id) {
|
|
||||||
return Uri.parse(BASE_URI + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
DBOpenHelper dbOpenHelper;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType(@NonNull final Uri uri) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queries the SQLite database for the bookmark items
|
|
||||||
* @param uri : contains the uri for bookmark items
|
|
||||||
* @param projection : contains the all fields of the table
|
|
||||||
* @param selection : handles Where
|
|
||||||
* @param selectionArgs : the condition of Where clause
|
|
||||||
* @param sortOrder : ascending or descending
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Cursor query(@NonNull final Uri uri, final String[] projection, final String selection,
|
|
||||||
final String[] selectionArgs, final String sortOrder) {
|
|
||||||
final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
|
|
||||||
queryBuilder.setTables(TABLE_NAME);
|
|
||||||
final SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
final Cursor cursor = queryBuilder.query(db, projection, selection,
|
|
||||||
selectionArgs, null, null, sortOrder);
|
|
||||||
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
|
||||||
return cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the update query of local SQLite Database
|
|
||||||
* @param uri : contains the uri for bookmark items
|
|
||||||
* @param contentValues : new values to be entered to db
|
|
||||||
* @param selection : handles Where
|
|
||||||
* @param selectionArgs : the condition of Where clause
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int update(@NonNull final Uri uri, final ContentValues contentValues,
|
|
||||||
final String selection, final String[] selectionArgs) {
|
|
||||||
final SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
final int rowsUpdated;
|
|
||||||
if (TextUtils.isEmpty(selection)) {
|
|
||||||
final int id = Integer.parseInt(uri.getLastPathSegment());
|
|
||||||
rowsUpdated = sqlDB.update(TABLE_NAME,
|
|
||||||
contentValues,
|
|
||||||
COLUMN_ID + " = ?",
|
|
||||||
new String[]{String.valueOf(id)});
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Parameter `selection` should be empty when updating an ID");
|
|
||||||
}
|
|
||||||
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rowsUpdated;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the insertion of new bookmark items record to local SQLite Database
|
|
||||||
* @param uri
|
|
||||||
* @param contentValues
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Uri insert(@NonNull final Uri uri, final ContentValues contentValues) {
|
|
||||||
final SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
final long id = sqlDB.insert(TABLE_NAME, null, contentValues);
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return Uri.parse(BASE_URI + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the deletion of new bookmark items record to local SQLite Database
|
|
||||||
* @param uri
|
|
||||||
* @param s
|
|
||||||
* @param strings
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int delete(@NonNull final Uri uri, final String s, final String[] strings) {
|
|
||||||
final int rows;
|
|
||||||
final SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
Timber.d("Deleting bookmark name %s", uri.getLastPathSegment());
|
|
||||||
rows = db.delete(
|
|
||||||
TABLE_NAME,
|
|
||||||
"item_id = ?",
|
|
||||||
new String[]{uri.getLastPathSegment()}
|
|
||||||
);
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,101 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.items
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
|
import android.net.Uri
|
||||||
|
import fr.free.nrw.commons.BuildConfig
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.TABLE_NAME
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_ID
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles private storage for bookmarked items
|
||||||
|
*/
|
||||||
|
class BookmarkItemsContentProvider : CommonsDaggerContentProvider() {
|
||||||
|
override fun getType(uri: Uri): String? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the SQLite database for the bookmark items
|
||||||
|
* @param uri : contains the uri for bookmark items
|
||||||
|
* @param projection : contains the all fields of the table
|
||||||
|
* @param selection : handles Where
|
||||||
|
* @param selectionArgs : the condition of Where clause
|
||||||
|
* @param sortOrder : ascending or descending
|
||||||
|
*/
|
||||||
|
override fun query(
|
||||||
|
uri: Uri, projection: Array<String>?, selection: String?,
|
||||||
|
selectionArgs: Array<String>?, sortOrder: String?
|
||||||
|
): Cursor {
|
||||||
|
val queryBuilder = SQLiteQueryBuilder().apply {
|
||||||
|
tables = TABLE_NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
return queryBuilder.query(
|
||||||
|
requireDb(), projection, selection,
|
||||||
|
selectionArgs, null, null, sortOrder
|
||||||
|
).apply {
|
||||||
|
setNotificationUri(context?.contentResolver, uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the update query of local SQLite Database
|
||||||
|
* @param uri : contains the uri for bookmark items
|
||||||
|
* @param contentValues : new values to be entered to db
|
||||||
|
* @param selection : handles Where
|
||||||
|
* @param selectionArgs : the condition of Where clause
|
||||||
|
*/
|
||||||
|
override fun update(
|
||||||
|
uri: Uri, contentValues: ContentValues?,
|
||||||
|
selection: String?, selectionArgs: Array<String>?
|
||||||
|
): Int {
|
||||||
|
val rowsUpdated: Int
|
||||||
|
if (selection.isNullOrEmpty()) {
|
||||||
|
val id = uri.lastPathSegment!!.toInt()
|
||||||
|
rowsUpdated = requireDb().update(
|
||||||
|
TABLE_NAME,
|
||||||
|
contentValues,
|
||||||
|
"$COLUMN_ID = ?",
|
||||||
|
arrayOf(id.toString())
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"Parameter `selection` should be empty when updating an ID"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rowsUpdated
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the insertion of new bookmark items record to local SQLite Database
|
||||||
|
*/
|
||||||
|
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
|
||||||
|
val id = requireDb().insert(TABLE_NAME, null, contentValues)
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return "$BASE_URI/$id".toUri()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the deletion of new bookmark items record to local SQLite Database
|
||||||
|
*/
|
||||||
|
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
|
||||||
|
val rows: Int = requireDb().delete(
|
||||||
|
TABLE_NAME,
|
||||||
|
"$COLUMN_ID = ?",
|
||||||
|
arrayOf(uri.lastPathSegment)
|
||||||
|
)
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val BASE_PATH = "bookmarksItems"
|
||||||
|
val BASE_URI: Uri = "content://${BuildConfig.BOOKMARK_ITEMS_AUTHORITY}/$BASE_PATH".toUri()
|
||||||
|
fun uriForName(id: String) = "$BASE_URI/$id".toUri()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.items;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles loading bookmarked items from Database
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class BookmarkItemsController {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
BookmarkItemsDao bookmarkItemsDao;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public BookmarkItemsController() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load from DB the bookmarked items
|
|
||||||
* @return a list of DepictedItem objects.
|
|
||||||
*/
|
|
||||||
public List<DepictedItem> loadFavoritesItems() {
|
|
||||||
return bookmarkItemsDao.getAllBookmarksItems();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.items
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles loading bookmarked items from Database
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
class BookmarkItemsController @Inject constructor() {
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var bookmarkItemsDao: BookmarkItemsDao? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load from DB the bookmarked items
|
||||||
|
* @return a list of DepictedItem objects.
|
||||||
|
*/
|
||||||
|
fun loadFavoritesItems(): List<DepictedItem> {
|
||||||
|
return bookmarkItemsDao?.getAllBookmarksItems() ?: emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,329 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.items;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.ContentProviderClient;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
import fr.free.nrw.commons.category.CategoryItem;
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import javax.inject.Provider;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles database operations for bookmarked items
|
|
||||||
*/
|
|
||||||
@Singleton
|
|
||||||
public class BookmarkItemsDao {
|
|
||||||
|
|
||||||
private final Provider<ContentProviderClient> clientProvider;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public BookmarkItemsDao(
|
|
||||||
@Named("bookmarksItem") final Provider<ContentProviderClient> clientProvider) {
|
|
||||||
this.clientProvider = clientProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find all persisted items bookmarks on database
|
|
||||||
* @return list of bookmarks
|
|
||||||
*/
|
|
||||||
public List<DepictedItem> getAllBookmarksItems() {
|
|
||||||
final List<DepictedItem> items = new ArrayList<>();
|
|
||||||
final ContentProviderClient db = clientProvider.get();
|
|
||||||
try (final Cursor cursor = db.query(
|
|
||||||
BookmarkItemsContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
null,
|
|
||||||
new String[]{},
|
|
||||||
null)) {
|
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
|
||||||
items.add(fromCursor(cursor));
|
|
||||||
}
|
|
||||||
} catch (final RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Look for a bookmark in database and in order to insert or delete it
|
|
||||||
* @param depictedItem : Bookmark object
|
|
||||||
* @return boolean : is bookmark now favorite ?
|
|
||||||
*/
|
|
||||||
public boolean updateBookmarkItem(final DepictedItem depictedItem) {
|
|
||||||
final boolean bookmarkExists = findBookmarkItem(depictedItem.getId());
|
|
||||||
if (bookmarkExists) {
|
|
||||||
deleteBookmarkItem(depictedItem);
|
|
||||||
} else {
|
|
||||||
addBookmarkItem(depictedItem);
|
|
||||||
}
|
|
||||||
return !bookmarkExists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a Bookmark to database
|
|
||||||
* @param depictedItem : Bookmark to add
|
|
||||||
*/
|
|
||||||
private void addBookmarkItem(final DepictedItem depictedItem) {
|
|
||||||
final ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
db.insert(BookmarkItemsContentProvider.BASE_URI, toContentValues(depictedItem));
|
|
||||||
} catch (final RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a bookmark from database
|
|
||||||
* @param depictedItem : Bookmark to delete
|
|
||||||
*/
|
|
||||||
private void deleteBookmarkItem(final DepictedItem depictedItem) {
|
|
||||||
final ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
db.delete(BookmarkItemsContentProvider.uriForName(depictedItem.getId()), null, null);
|
|
||||||
} catch (final RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a bookmark from database based on its name
|
|
||||||
* @param depictedItemID : Bookmark to find
|
|
||||||
* @return boolean : is bookmark in database ?
|
|
||||||
*/
|
|
||||||
public boolean findBookmarkItem(final String depictedItemID) {
|
|
||||||
if (depictedItemID == null) { //Avoiding NPE's
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final ContentProviderClient db = clientProvider.get();
|
|
||||||
try (final Cursor cursor = db.query(
|
|
||||||
BookmarkItemsContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
Table.COLUMN_ID + "=?",
|
|
||||||
new String[]{depictedItemID},
|
|
||||||
null
|
|
||||||
)) {
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch (final RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recives real data from cursor
|
|
||||||
* @param cursor : Object for storing database data
|
|
||||||
* @return DepictedItem
|
|
||||||
*/
|
|
||||||
@SuppressLint("Range")
|
|
||||||
DepictedItem fromCursor(final Cursor cursor) {
|
|
||||||
final String fileName = cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME));
|
|
||||||
final String description
|
|
||||||
= cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION));
|
|
||||||
final String imageUrl = cursor.getString(cursor.getColumnIndex(Table.COLUMN_IMAGE));
|
|
||||||
final String instanceListString
|
|
||||||
= cursor.getString(cursor.getColumnIndex(Table.COLUMN_INSTANCE_LIST));
|
|
||||||
final List<String> instanceList = StringToArray(instanceListString);
|
|
||||||
final String categoryNameListString = cursor.getString(cursor
|
|
||||||
.getColumnIndex(Table.COLUMN_CATEGORIES_NAME_LIST));
|
|
||||||
final List<String> categoryNameList = StringToArray(categoryNameListString);
|
|
||||||
final String categoryDescriptionListString = cursor.getString(cursor
|
|
||||||
.getColumnIndex(Table.COLUMN_CATEGORIES_DESCRIPTION_LIST));
|
|
||||||
final List<String> categoryDescriptionList = StringToArray(categoryDescriptionListString);
|
|
||||||
final String categoryThumbnailListString = cursor.getString(cursor
|
|
||||||
.getColumnIndex(Table.COLUMN_CATEGORIES_THUMBNAIL_LIST));
|
|
||||||
final List<String> categoryThumbnailList = StringToArray(categoryThumbnailListString);
|
|
||||||
final List<CategoryItem> categoryList = convertToCategoryItems(categoryNameList,
|
|
||||||
categoryDescriptionList, categoryThumbnailList);
|
|
||||||
final boolean isSelected
|
|
||||||
= Boolean.parseBoolean(cursor.getString(cursor
|
|
||||||
.getColumnIndex(Table.COLUMN_IS_SELECTED)));
|
|
||||||
final String id = cursor.getString(cursor.getColumnIndex(Table.COLUMN_ID));
|
|
||||||
|
|
||||||
return new DepictedItem(
|
|
||||||
fileName,
|
|
||||||
description,
|
|
||||||
imageUrl,
|
|
||||||
instanceList,
|
|
||||||
categoryList,
|
|
||||||
isSelected,
|
|
||||||
id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<CategoryItem> convertToCategoryItems(List<String> categoryNameList,
|
|
||||||
List<String> categoryDescriptionList, List<String> categoryThumbnailList) {
|
|
||||||
List<CategoryItem> categoryItems = new ArrayList<>();
|
|
||||||
for(int i=0; i<categoryNameList.size(); i++){
|
|
||||||
categoryItems.add(new CategoryItem(categoryNameList.get(i),
|
|
||||||
categoryDescriptionList.get(i),
|
|
||||||
categoryThumbnailList.get(i), false));
|
|
||||||
}
|
|
||||||
return categoryItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts string to List
|
|
||||||
* @param listString comma separated single string from of list items
|
|
||||||
* @return List of string
|
|
||||||
*/
|
|
||||||
private List<String> StringToArray(final String listString) {
|
|
||||||
final String[] elements = listString.split(",");
|
|
||||||
return Arrays.asList(elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts string to List
|
|
||||||
* @param list list of items
|
|
||||||
* @return string comma separated single string of items
|
|
||||||
*/
|
|
||||||
private String ArrayToString(final List<String> list) {
|
|
||||||
if (list != null) {
|
|
||||||
return StringUtils.join(list, ',');
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes data from DepictedItem and create a content value object
|
|
||||||
* @param depictedItem depicted item
|
|
||||||
* @return ContentValues
|
|
||||||
*/
|
|
||||||
private ContentValues toContentValues(final DepictedItem depictedItem) {
|
|
||||||
|
|
||||||
final List<String> namesOfCommonsCategories = new ArrayList<>();
|
|
||||||
for (final CategoryItem category :
|
|
||||||
depictedItem.getCommonsCategories()) {
|
|
||||||
namesOfCommonsCategories.add(category.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<String> descriptionsOfCommonsCategories = new ArrayList<>();
|
|
||||||
for (final CategoryItem category :
|
|
||||||
depictedItem.getCommonsCategories()) {
|
|
||||||
descriptionsOfCommonsCategories.add(category.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<String> thumbnailsOfCommonsCategories = new ArrayList<>();
|
|
||||||
for (final CategoryItem category :
|
|
||||||
depictedItem.getCommonsCategories()) {
|
|
||||||
thumbnailsOfCommonsCategories.add(category.getThumbnail());
|
|
||||||
}
|
|
||||||
|
|
||||||
final ContentValues cv = new ContentValues();
|
|
||||||
cv.put(Table.COLUMN_NAME, depictedItem.getName());
|
|
||||||
cv.put(Table.COLUMN_DESCRIPTION, depictedItem.getDescription());
|
|
||||||
cv.put(Table.COLUMN_IMAGE, depictedItem.getImageUrl());
|
|
||||||
cv.put(Table.COLUMN_INSTANCE_LIST, ArrayToString(depictedItem.getInstanceOfs()));
|
|
||||||
cv.put(Table.COLUMN_CATEGORIES_NAME_LIST, ArrayToString(namesOfCommonsCategories));
|
|
||||||
cv.put(Table.COLUMN_CATEGORIES_DESCRIPTION_LIST,
|
|
||||||
ArrayToString(descriptionsOfCommonsCategories));
|
|
||||||
cv.put(Table.COLUMN_CATEGORIES_THUMBNAIL_LIST,
|
|
||||||
ArrayToString(thumbnailsOfCommonsCategories));
|
|
||||||
cv.put(Table.COLUMN_IS_SELECTED, depictedItem.isSelected());
|
|
||||||
cv.put(Table.COLUMN_ID, depictedItem.getId());
|
|
||||||
return cv;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Table of bookmarksItems data
|
|
||||||
*/
|
|
||||||
public static final class Table {
|
|
||||||
public static final String TABLE_NAME = "bookmarksItems";
|
|
||||||
public static final String COLUMN_NAME = "item_name";
|
|
||||||
public static final String COLUMN_DESCRIPTION = "item_description";
|
|
||||||
public static final String COLUMN_IMAGE = "item_image_url";
|
|
||||||
public static final String COLUMN_INSTANCE_LIST = "item_instance_of";
|
|
||||||
public static final String COLUMN_CATEGORIES_NAME_LIST = "item_name_categories";
|
|
||||||
public static final String COLUMN_CATEGORIES_DESCRIPTION_LIST = "item_description_categories";
|
|
||||||
public static final String COLUMN_CATEGORIES_THUMBNAIL_LIST = "item_thumbnail_categories";
|
|
||||||
public static final String COLUMN_IS_SELECTED = "item_is_selected";
|
|
||||||
public static final String COLUMN_ID = "item_id";
|
|
||||||
|
|
||||||
public static final String[] ALL_FIELDS = {
|
|
||||||
COLUMN_NAME,
|
|
||||||
COLUMN_DESCRIPTION,
|
|
||||||
COLUMN_IMAGE,
|
|
||||||
COLUMN_INSTANCE_LIST,
|
|
||||||
COLUMN_CATEGORIES_NAME_LIST,
|
|
||||||
COLUMN_CATEGORIES_DESCRIPTION_LIST,
|
|
||||||
COLUMN_CATEGORIES_THUMBNAIL_LIST,
|
|
||||||
COLUMN_IS_SELECTED,
|
|
||||||
COLUMN_ID
|
|
||||||
};
|
|
||||||
|
|
||||||
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
|
|
||||||
static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
|
|
||||||
+ COLUMN_NAME + " STRING,"
|
|
||||||
+ COLUMN_DESCRIPTION + " STRING,"
|
|
||||||
+ COLUMN_IMAGE + " STRING,"
|
|
||||||
+ COLUMN_INSTANCE_LIST + " STRING,"
|
|
||||||
+ COLUMN_CATEGORIES_NAME_LIST + " STRING,"
|
|
||||||
+ COLUMN_CATEGORIES_DESCRIPTION_LIST + " STRING,"
|
|
||||||
+ COLUMN_CATEGORIES_THUMBNAIL_LIST + " STRING,"
|
|
||||||
+ COLUMN_IS_SELECTED + " STRING,"
|
|
||||||
+ COLUMN_ID + " STRING PRIMARY KEY"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates table
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
*/
|
|
||||||
public static void onCreate(final SQLiteDatabase db) {
|
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes database
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
*/
|
|
||||||
public static void onDelete(final SQLiteDatabase db) {
|
|
||||||
db.execSQL(DROP_TABLE_STATEMENT);
|
|
||||||
onCreate(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates database
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
* @param from starting
|
|
||||||
* @param to end
|
|
||||||
*/
|
|
||||||
public static void onUpdate(final SQLiteDatabase db, int from, final int to) {
|
|
||||||
if (from == to) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from < 18) {
|
|
||||||
// doesn't exist yet
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (from == 18) {
|
|
||||||
// table added in version 19
|
|
||||||
onCreate(db);
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,203 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.items
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.ContentProviderClient
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.os.RemoteException
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsContentProvider.Companion.BASE_URI
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsContentProvider.Companion.uriForName
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_CATEGORIES_DESCRIPTION_LIST
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_CATEGORIES_NAME_LIST
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_CATEGORIES_THUMBNAIL_LIST
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_DESCRIPTION
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_ID
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_IMAGE
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_INSTANCE_LIST
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_IS_SELECTED
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable.COLUMN_NAME
|
||||||
|
import fr.free.nrw.commons.category.CategoryItem
|
||||||
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
|
import fr.free.nrw.commons.utils.arrayToString
|
||||||
|
import fr.free.nrw.commons.utils.getString
|
||||||
|
import fr.free.nrw.commons.utils.getStringArray
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
import javax.inject.Provider
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles database operations for bookmarked items
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
class BookmarkItemsDao @Inject constructor(
|
||||||
|
@param:Named("bookmarksItem") private val clientProvider: Provider<ContentProviderClient>
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Find all persisted items bookmarks on database
|
||||||
|
* @return list of bookmarks
|
||||||
|
*/
|
||||||
|
fun getAllBookmarksItems(): List<DepictedItem> {
|
||||||
|
val items: MutableList<DepictedItem> = mutableListOf()
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.query(
|
||||||
|
BASE_URI,
|
||||||
|
BookmarkItemsTable.ALL_FIELDS,
|
||||||
|
null,
|
||||||
|
arrayOf(),
|
||||||
|
null
|
||||||
|
).use { cursor ->
|
||||||
|
while (cursor != null && cursor.moveToNext()) {
|
||||||
|
items.add(fromCursor(cursor))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look for a bookmark in database and in order to insert or delete it
|
||||||
|
* @param depictedItem : Bookmark object
|
||||||
|
* @return boolean : is bookmark now favorite ?
|
||||||
|
*/
|
||||||
|
fun updateBookmarkItem(depictedItem: DepictedItem): Boolean {
|
||||||
|
val bookmarkExists = findBookmarkItem(depictedItem.id)
|
||||||
|
if (bookmarkExists) {
|
||||||
|
deleteBookmarkItem(depictedItem)
|
||||||
|
} else {
|
||||||
|
addBookmarkItem(depictedItem)
|
||||||
|
}
|
||||||
|
return !bookmarkExists
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a Bookmark to database
|
||||||
|
* @param depictedItem : Bookmark to add
|
||||||
|
*/
|
||||||
|
private fun addBookmarkItem(depictedItem: DepictedItem) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.insert(BASE_URI, toContentValues(depictedItem))
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a bookmark from database
|
||||||
|
* @param depictedItem : Bookmark to delete
|
||||||
|
*/
|
||||||
|
private fun deleteBookmarkItem(depictedItem: DepictedItem) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.delete(uriForName(depictedItem.id), null, null)
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a bookmark from database based on its name
|
||||||
|
* @param depictedItemID : Bookmark to find
|
||||||
|
* @return boolean : is bookmark in database ?
|
||||||
|
*/
|
||||||
|
fun findBookmarkItem(depictedItemID: String?): Boolean {
|
||||||
|
if (depictedItemID == null) { //Avoiding NPE's
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.query(
|
||||||
|
BASE_URI,
|
||||||
|
BookmarkItemsTable.ALL_FIELDS,
|
||||||
|
COLUMN_ID + "=?",
|
||||||
|
arrayOf(depictedItemID),
|
||||||
|
null
|
||||||
|
).use { cursor ->
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recives real data from cursor
|
||||||
|
* @param cursor : Object for storing database data
|
||||||
|
* @return DepictedItem
|
||||||
|
*/
|
||||||
|
@SuppressLint("Range")
|
||||||
|
fun fromCursor(cursor: Cursor) = with(cursor) {
|
||||||
|
var name = getString(COLUMN_NAME)
|
||||||
|
if (name == null) {
|
||||||
|
name = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var id = getString(COLUMN_ID)
|
||||||
|
if (id == null) {
|
||||||
|
id = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
DepictedItem(
|
||||||
|
name,
|
||||||
|
getString(COLUMN_DESCRIPTION),
|
||||||
|
getString(COLUMN_IMAGE),
|
||||||
|
getStringArray(COLUMN_INSTANCE_LIST),
|
||||||
|
convertToCategoryItems(
|
||||||
|
getStringArray(COLUMN_CATEGORIES_NAME_LIST),
|
||||||
|
getStringArray(COLUMN_CATEGORIES_DESCRIPTION_LIST),
|
||||||
|
getStringArray(COLUMN_CATEGORIES_THUMBNAIL_LIST)
|
||||||
|
),
|
||||||
|
getString(COLUMN_IS_SELECTED).toBoolean(),
|
||||||
|
id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun convertToCategoryItems(
|
||||||
|
categoryNameList: List<String>,
|
||||||
|
categoryDescriptionList: List<String>,
|
||||||
|
categoryThumbnailList: List<String>
|
||||||
|
): List<CategoryItem> = categoryNameList.mapIndexed { index, name ->
|
||||||
|
CategoryItem(
|
||||||
|
name = name,
|
||||||
|
description = categoryDescriptionList.getOrNull(index),
|
||||||
|
thumbnail = categoryThumbnailList.getOrNull(index),
|
||||||
|
isSelected = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes data from DepictedItem and create a content value object
|
||||||
|
* @param depictedItem depicted item
|
||||||
|
* @return ContentValues
|
||||||
|
*/
|
||||||
|
private fun toContentValues(depictedItem: DepictedItem): ContentValues {
|
||||||
|
return contentValuesOf(
|
||||||
|
COLUMN_NAME to depictedItem.name,
|
||||||
|
COLUMN_DESCRIPTION to depictedItem.description,
|
||||||
|
COLUMN_IMAGE to depictedItem.imageUrl,
|
||||||
|
COLUMN_INSTANCE_LIST to arrayToString(depictedItem.instanceOfs),
|
||||||
|
COLUMN_CATEGORIES_NAME_LIST to arrayToString(depictedItem.commonsCategories.map { it.name }),
|
||||||
|
COLUMN_CATEGORIES_DESCRIPTION_LIST to arrayToString(depictedItem.commonsCategories.map { it.description }),
|
||||||
|
COLUMN_CATEGORIES_THUMBNAIL_LIST to arrayToString(depictedItem.commonsCategories.map { it.thumbnail }),
|
||||||
|
COLUMN_IS_SELECTED to depictedItem.isSelected,
|
||||||
|
COLUMN_ID to depictedItem.id,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.items;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ProgressBar;
|
|
||||||
import android.widget.RelativeLayout;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
|
||||||
import dagger.android.support.DaggerFragment;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentBookmarksItemsBinding;
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tab fragment to show list of bookmarked Wikidata Items
|
|
||||||
*/
|
|
||||||
public class BookmarkItemsFragment extends DaggerFragment {
|
|
||||||
|
|
||||||
private FragmentBookmarksItemsBinding binding;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
BookmarkItemsController controller;
|
|
||||||
|
|
||||||
public static BookmarkItemsFragment newInstance() {
|
|
||||||
return new BookmarkItemsFragment();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(
|
|
||||||
@NonNull final LayoutInflater inflater,
|
|
||||||
final ViewGroup container,
|
|
||||||
final Bundle savedInstanceState
|
|
||||||
) {
|
|
||||||
binding = FragmentBookmarksItemsBinding.inflate(inflater, container, false);
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(final @NotNull View view, @Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
initList(requireContext());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
initList(requireContext());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get list of DepictedItem and sets to the adapter
|
|
||||||
* @param context context
|
|
||||||
*/
|
|
||||||
private void initList(final Context context) {
|
|
||||||
final List<DepictedItem> depictItems = controller.loadFavoritesItems();
|
|
||||||
final BookmarkItemsAdapter adapter = new BookmarkItemsAdapter(depictItems, context);
|
|
||||||
binding.listView.setAdapter(adapter);
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(View.GONE);
|
|
||||||
if (depictItems.isEmpty()) {
|
|
||||||
binding.statusMessage.setText(R.string.bookmark_empty);
|
|
||||||
binding.statusMessage.setVisibility(View.VISIBLE);
|
|
||||||
} else {
|
|
||||||
binding.statusMessage.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.items
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import dagger.android.support.DaggerFragment
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentBookmarksItemsBinding
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tab fragment to show list of bookmarked Wikidata Items
|
||||||
|
*/
|
||||||
|
class BookmarkItemsFragment : DaggerFragment() {
|
||||||
|
private var binding: FragmentBookmarksItemsBinding? = null
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var controller: BookmarkItemsController? = null
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
binding = FragmentBookmarksItemsBinding.inflate(inflater, container, false)
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
initList(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
initList(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get list of DepictedItem and sets to the adapter
|
||||||
|
* @param context context
|
||||||
|
*/
|
||||||
|
private fun initList(context: Context) {
|
||||||
|
val depictItems = controller!!.loadFavoritesItems()
|
||||||
|
binding!!.listView.adapter = BookmarkItemsAdapter(depictItems, context)
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
if (depictItems.isEmpty()) {
|
||||||
|
binding!!.statusMessage.setText(R.string.bookmark_empty)
|
||||||
|
binding!!.statusMessage.visibility = View.VISIBLE
|
||||||
|
} else {
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.items
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Table of bookmarksItems data
|
||||||
|
*/
|
||||||
|
object BookmarkItemsTable {
|
||||||
|
const val TABLE_NAME = "bookmarksItems"
|
||||||
|
const val COLUMN_NAME = "item_name"
|
||||||
|
const val COLUMN_DESCRIPTION = "item_description"
|
||||||
|
const val COLUMN_IMAGE = "item_image_url"
|
||||||
|
const val COLUMN_INSTANCE_LIST = "item_instance_of"
|
||||||
|
const val COLUMN_CATEGORIES_NAME_LIST = "item_name_categories"
|
||||||
|
const val COLUMN_CATEGORIES_DESCRIPTION_LIST = "item_description_categories"
|
||||||
|
const val COLUMN_CATEGORIES_THUMBNAIL_LIST = "item_thumbnail_categories"
|
||||||
|
const val COLUMN_IS_SELECTED = "item_is_selected"
|
||||||
|
const val COLUMN_ID = "item_id"
|
||||||
|
|
||||||
|
val ALL_FIELDS = arrayOf(
|
||||||
|
COLUMN_NAME,
|
||||||
|
COLUMN_DESCRIPTION,
|
||||||
|
COLUMN_IMAGE,
|
||||||
|
COLUMN_INSTANCE_LIST,
|
||||||
|
COLUMN_CATEGORIES_NAME_LIST,
|
||||||
|
COLUMN_CATEGORIES_DESCRIPTION_LIST,
|
||||||
|
COLUMN_CATEGORIES_THUMBNAIL_LIST,
|
||||||
|
COLUMN_IS_SELECTED,
|
||||||
|
COLUMN_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
const val DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||||
|
|
||||||
|
val CREATE_TABLE_STATEMENT =
|
||||||
|
"""CREATE TABLE $TABLE_NAME (
|
||||||
|
$COLUMN_NAME STRING,
|
||||||
|
$COLUMN_DESCRIPTION STRING,
|
||||||
|
$COLUMN_IMAGE STRING,
|
||||||
|
$COLUMN_INSTANCE_LIST STRING,
|
||||||
|
$COLUMN_CATEGORIES_NAME_LIST STRING,
|
||||||
|
$COLUMN_CATEGORIES_DESCRIPTION_LIST STRING,
|
||||||
|
$COLUMN_CATEGORIES_THUMBNAIL_LIST STRING,
|
||||||
|
$COLUMN_IS_SELECTED STRING,
|
||||||
|
$COLUMN_ID STRING PRIMARY KEY
|
||||||
|
);""".trimIndent()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates table
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
*/
|
||||||
|
fun onCreate(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(CREATE_TABLE_STATEMENT)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes database
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
*/
|
||||||
|
fun onDelete(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(DROP_TABLE_STATEMENT)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates database
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
* @param from starting
|
||||||
|
* @param to end
|
||||||
|
*/
|
||||||
|
fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
|
if (from == to) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from < 18) {
|
||||||
|
// doesn't exist yet
|
||||||
|
onUpdate(db, from + 1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from == 18) {
|
||||||
|
// table added in version 19
|
||||||
|
onCreate(db)
|
||||||
|
onUpdate(db, from + 1, to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,120 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.pictures;
|
|
||||||
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.database.sqlite.SQLiteQueryBuilder;
|
|
||||||
// We can get uri using java.Net.Uri, but andoid implimentation is faster (but it's forgiving with handling exceptions though)
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.COLUMN_MEDIA_NAME;
|
|
||||||
import static fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao.Table.TABLE_NAME;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles private storage for Bookmark pictures
|
|
||||||
*/
|
|
||||||
public class BookmarkPicturesContentProvider extends CommonsDaggerContentProvider {
|
|
||||||
|
|
||||||
private static final String BASE_PATH = "bookmarks";
|
|
||||||
public static final Uri BASE_URI = Uri.parse("content://" + BuildConfig.BOOKMARK_AUTHORITY + "/" + BASE_PATH);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append bookmark pictures name to the base uri
|
|
||||||
*/
|
|
||||||
public static Uri uriForName(String name) {
|
|
||||||
return Uri.parse(BASE_URI.toString() + "/" + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
DBOpenHelper dbOpenHelper;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType(@NonNull Uri uri) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queries the SQLite database for the bookmark pictures
|
|
||||||
* @param uri : contains the uri for bookmark pictures
|
|
||||||
* @param projection
|
|
||||||
* @param selection : handles Where
|
|
||||||
* @param selectionArgs : the condition of Where clause
|
|
||||||
* @param sortOrder : ascending or descending
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
|
|
||||||
String[] selectionArgs, String sortOrder) {
|
|
||||||
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
|
|
||||||
queryBuilder.setTables(TABLE_NAME);
|
|
||||||
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
|
|
||||||
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
|
||||||
|
|
||||||
return cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the update query of local SQLite Database
|
|
||||||
* @param uri : contains the uri for bookmark pictures
|
|
||||||
* @param contentValues : new values to be entered to db
|
|
||||||
* @param selection : handles Where
|
|
||||||
* @param selectionArgs : the condition of Where clause
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int update(@NonNull Uri uri, ContentValues contentValues, String selection,
|
|
||||||
String[] selectionArgs) {
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
int rowsUpdated;
|
|
||||||
if (TextUtils.isEmpty(selection)) {
|
|
||||||
int id = Integer.valueOf(uri.getLastPathSegment());
|
|
||||||
rowsUpdated = sqlDB.update(TABLE_NAME,
|
|
||||||
contentValues,
|
|
||||||
COLUMN_MEDIA_NAME + " = ?",
|
|
||||||
new String[]{String.valueOf(id)});
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Parameter `selection` should be empty when updating an ID");
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rowsUpdated;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the insertion of new bookmark pictures record to local SQLite Database
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
long id = sqlDB.insert(BookmarkPicturesDao.Table.TABLE_NAME, null, contentValues);
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return Uri.parse(BASE_URI + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int delete(@NonNull Uri uri, String s, String[] strings) {
|
|
||||||
int rows;
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
Timber.d("Deleting bookmark name %s", uri.getLastPathSegment());
|
|
||||||
rows = db.delete(TABLE_NAME,
|
|
||||||
"media_name = ?",
|
|
||||||
new String[]{uri.getLastPathSegment()}
|
|
||||||
);
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,100 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
|
import android.net.Uri
|
||||||
|
import fr.free.nrw.commons.BuildConfig
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.TABLE_NAME
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles private storage for Bookmark pictures
|
||||||
|
*/
|
||||||
|
class BookmarkPicturesContentProvider : CommonsDaggerContentProvider() {
|
||||||
|
override fun getType(uri: Uri): String? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the SQLite database for the bookmark pictures
|
||||||
|
* @param uri : contains the uri for bookmark pictures
|
||||||
|
* @param projection
|
||||||
|
* @param selection : handles Where
|
||||||
|
* @param selectionArgs : the condition of Where clause
|
||||||
|
* @param sortOrder : ascending or descending
|
||||||
|
*/
|
||||||
|
override fun query(
|
||||||
|
uri: Uri, projection: Array<String>?, selection: String?,
|
||||||
|
selectionArgs: Array<String>?, sortOrder: String?
|
||||||
|
): Cursor {
|
||||||
|
val queryBuilder = SQLiteQueryBuilder().apply {
|
||||||
|
tables = TABLE_NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
val cursor = queryBuilder.query(
|
||||||
|
requireDb(), projection, selection,
|
||||||
|
selectionArgs, null, null, sortOrder
|
||||||
|
)
|
||||||
|
cursor.setNotificationUri(context?.contentResolver, uri)
|
||||||
|
|
||||||
|
return cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the update query of local SQLite Database
|
||||||
|
* @param uri : contains the uri for bookmark pictures
|
||||||
|
* @param contentValues : new values to be entered to db
|
||||||
|
* @param selection : handles Where
|
||||||
|
* @param selectionArgs : the condition of Where clause
|
||||||
|
*/
|
||||||
|
override fun update(
|
||||||
|
uri: Uri, contentValues: ContentValues?, selection: String?,
|
||||||
|
selectionArgs: Array<String>?
|
||||||
|
): Int {
|
||||||
|
val rowsUpdated: Int
|
||||||
|
if (selection.isNullOrEmpty()) {
|
||||||
|
val id = uri.lastPathSegment!!.toInt()
|
||||||
|
rowsUpdated = requireDb().update(
|
||||||
|
TABLE_NAME,
|
||||||
|
contentValues,
|
||||||
|
"$COLUMN_MEDIA_NAME = ?",
|
||||||
|
arrayOf(id.toString())
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"Parameter `selection` should be empty when updating an ID"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rowsUpdated
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the insertion of new bookmark pictures record to local SQLite Database
|
||||||
|
*/
|
||||||
|
override fun insert(uri: Uri, contentValues: ContentValues?): Uri {
|
||||||
|
val id = requireDb().insert(TABLE_NAME, null, contentValues)
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return "$BASE_URI/$id".toUri()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
|
||||||
|
val rows: Int = requireDb().delete(
|
||||||
|
TABLE_NAME,
|
||||||
|
"media_name = ?",
|
||||||
|
arrayOf(uri.lastPathSegment)
|
||||||
|
)
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val BASE_PATH = "bookmarks"
|
||||||
|
@JvmField
|
||||||
|
val BASE_URI: Uri = "content://${BuildConfig.BOOKMARK_AUTHORITY}/$BASE_PATH".toUri()
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun uriForName(name: String): Uri = "$BASE_URI/$name".toUri()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,63 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.pictures;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.bookmarks.models.Bookmark;
|
|
||||||
import fr.free.nrw.commons.media.MediaClient;
|
|
||||||
import io.reactivex.Observable;
|
|
||||||
import io.reactivex.ObservableSource;
|
|
||||||
import io.reactivex.Single;
|
|
||||||
import io.reactivex.functions.Function;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class BookmarkPicturesController {
|
|
||||||
|
|
||||||
private final MediaClient mediaClient;
|
|
||||||
private final BookmarkPicturesDao bookmarkDao;
|
|
||||||
|
|
||||||
private List<Bookmark> currentBookmarks;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public BookmarkPicturesController(MediaClient mediaClient, BookmarkPicturesDao bookmarkDao) {
|
|
||||||
this.mediaClient = mediaClient;
|
|
||||||
this.bookmarkDao = bookmarkDao;
|
|
||||||
currentBookmarks = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the Media objects from the raw data stored in DB and the API.
|
|
||||||
* @return a list of bookmarked Media object
|
|
||||||
*/
|
|
||||||
Single<List<Media>> loadBookmarkedPictures() {
|
|
||||||
List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks();
|
|
||||||
currentBookmarks = bookmarks;
|
|
||||||
return Observable.fromIterable(bookmarks)
|
|
||||||
.flatMap((Function<Bookmark, ObservableSource<Media>>) this::getMediaFromBookmark)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Observable<Media> getMediaFromBookmark(Bookmark bookmark) {
|
|
||||||
return mediaClient.getMedia(bookmark.getMediaName())
|
|
||||||
.toObservable()
|
|
||||||
.onErrorResumeNext(Observable.empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the Media objects from the raw data stored in DB and the API.
|
|
||||||
* @return a list of bookmarked Media object
|
|
||||||
*/
|
|
||||||
boolean needRefreshBookmarkedPictures() {
|
|
||||||
List<Bookmark> bookmarks = bookmarkDao.getAllBookmarks();
|
|
||||||
return bookmarks.size() != currentBookmarks.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cancels the requests to the API and the DB
|
|
||||||
*/
|
|
||||||
void stop() {
|
|
||||||
//noop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
||||||
|
import fr.free.nrw.commons.media.MediaClient
|
||||||
|
import io.reactivex.Observable
|
||||||
|
import io.reactivex.Single
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class BookmarkPicturesController @Inject constructor(
|
||||||
|
private val mediaClient: MediaClient,
|
||||||
|
private val bookmarkDao: BookmarkPicturesDao
|
||||||
|
) {
|
||||||
|
private var currentBookmarks: List<Bookmark> = listOf()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the Media objects from the raw data stored in DB and the API.
|
||||||
|
* @return a list of bookmarked Media object
|
||||||
|
*/
|
||||||
|
fun loadBookmarkedPictures(): Single<List<Media>> {
|
||||||
|
val bookmarks = bookmarkDao.getAllBookmarks()
|
||||||
|
currentBookmarks = bookmarks
|
||||||
|
return Observable.fromIterable(bookmarks).flatMap {
|
||||||
|
mediaClient.getMedia(it.mediaName)
|
||||||
|
.toObservable()
|
||||||
|
.onErrorResumeNext(Observable.empty())
|
||||||
|
}.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun needRefreshBookmarkedPictures(): Boolean {
|
||||||
|
val bookmarks = bookmarkDao.getAllBookmarks()
|
||||||
|
return bookmarks.size != currentBookmarks.size
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stop() = Unit
|
||||||
|
}
|
||||||
|
|
@ -1,227 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.pictures;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.ContentProviderClient;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import javax.inject.Provider;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.bookmarks.models.Bookmark;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.BASE_URI;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class BookmarkPicturesDao {
|
|
||||||
|
|
||||||
private final Provider<ContentProviderClient> clientProvider;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public BookmarkPicturesDao(@Named("bookmarks") Provider<ContentProviderClient> clientProvider) {
|
|
||||||
this.clientProvider = clientProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find all persisted pictures bookmarks on database
|
|
||||||
*
|
|
||||||
* @return list of bookmarks
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
public List<Bookmark> getAllBookmarks() {
|
|
||||||
List<Bookmark> items = new ArrayList<>();
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
BookmarkPicturesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
null,
|
|
||||||
new String[]{},
|
|
||||||
null);
|
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
|
||||||
items.add(fromCursor(cursor));
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Look for a bookmark in database and in order to insert or delete it
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark object
|
|
||||||
* @return boolean : is bookmark now fav ?
|
|
||||||
*/
|
|
||||||
public boolean updateBookmark(Bookmark bookmark) {
|
|
||||||
boolean bookmarkExists = findBookmark(bookmark);
|
|
||||||
if (bookmarkExists) {
|
|
||||||
deleteBookmark(bookmark);
|
|
||||||
} else {
|
|
||||||
addBookmark(bookmark);
|
|
||||||
}
|
|
||||||
return !bookmarkExists;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a Bookmark to database
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to add
|
|
||||||
*/
|
|
||||||
private void addBookmark(Bookmark bookmark) {
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
db.insert(BASE_URI, toContentValues(bookmark));
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a bookmark from database
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to delete
|
|
||||||
*/
|
|
||||||
private void deleteBookmark(Bookmark bookmark) {
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
if (bookmark.getContentUri() == null) {
|
|
||||||
throw new RuntimeException("tried to delete item with no content URI");
|
|
||||||
} else {
|
|
||||||
db.delete(bookmark.getContentUri(), null, null);
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find a bookmark from database based on its name
|
|
||||||
*
|
|
||||||
* @param bookmark : Bookmark to find
|
|
||||||
* @return boolean : is bookmark in database ?
|
|
||||||
*/
|
|
||||||
public boolean findBookmark(Bookmark bookmark) {
|
|
||||||
if (bookmark == null) {//Avoiding NPE's
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
BookmarkPicturesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
Table.COLUMN_MEDIA_NAME + "=?",
|
|
||||||
new String[]{bookmark.getMediaName()},
|
|
||||||
null);
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
// This feels lazy, but to hell with checked exceptions. :)
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("Range")
|
|
||||||
@NonNull
|
|
||||||
Bookmark fromCursor(Cursor cursor) {
|
|
||||||
String fileName = cursor.getString(cursor.getColumnIndex(Table.COLUMN_MEDIA_NAME));
|
|
||||||
return new Bookmark(
|
|
||||||
fileName,
|
|
||||||
cursor.getString(cursor.getColumnIndex(Table.COLUMN_CREATOR)),
|
|
||||||
BookmarkPicturesContentProvider.uriForName(fileName)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ContentValues toContentValues(Bookmark bookmark) {
|
|
||||||
ContentValues cv = new ContentValues();
|
|
||||||
cv.put(BookmarkPicturesDao.Table.COLUMN_MEDIA_NAME, bookmark.getMediaName());
|
|
||||||
cv.put(BookmarkPicturesDao.Table.COLUMN_CREATOR, bookmark.getMediaCreator());
|
|
||||||
return cv;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static class Table {
|
|
||||||
public static final String TABLE_NAME = "bookmarks";
|
|
||||||
|
|
||||||
public static final String COLUMN_MEDIA_NAME = "media_name";
|
|
||||||
public static final String COLUMN_CREATOR = "media_creator";
|
|
||||||
|
|
||||||
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
|
||||||
public static final String[] ALL_FIELDS = {
|
|
||||||
COLUMN_MEDIA_NAME,
|
|
||||||
COLUMN_CREATOR
|
|
||||||
};
|
|
||||||
|
|
||||||
public static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
|
|
||||||
|
|
||||||
public static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
|
|
||||||
+ COLUMN_MEDIA_NAME + " STRING PRIMARY KEY,"
|
|
||||||
+ COLUMN_CREATOR + " STRING"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
public static void onCreate(SQLiteDatabase db) {
|
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onDelete(SQLiteDatabase db) {
|
|
||||||
db.execSQL(DROP_TABLE_STATEMENT);
|
|
||||||
onCreate(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void onUpdate(SQLiteDatabase db, int from, int to) {
|
|
||||||
if (from == to) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from < 7) {
|
|
||||||
// doesn't exist yet
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (from == 7) {
|
|
||||||
// table added in version 8
|
|
||||||
onCreate(db);
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (from == 8) {
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,144 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.content.ContentProviderClient
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.os.RemoteException
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import fr.free.nrw.commons.bookmarks.models.Bookmark
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.BASE_URI
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesContentProvider.Companion.uriForName
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.ALL_FIELDS
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_CREATOR
|
||||||
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable.COLUMN_MEDIA_NAME
|
||||||
|
import fr.free.nrw.commons.utils.getString
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
import javax.inject.Provider
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class BookmarkPicturesDao @Inject constructor(
|
||||||
|
@param:Named("bookmarks") private val clientProvider: Provider<ContentProviderClient>
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* Find all persisted pictures bookmarks on database
|
||||||
|
*
|
||||||
|
* @return list of bookmarks
|
||||||
|
*/
|
||||||
|
fun getAllBookmarks(): List<Bookmark> {
|
||||||
|
val items: MutableList<Bookmark> = mutableListOf()
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI, ALL_FIELDS, null, arrayOf(), null
|
||||||
|
)
|
||||||
|
while (cursor != null && cursor.moveToNext()) {
|
||||||
|
items.add(fromCursor(cursor))
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look for a bookmark in database and in order to insert or delete it
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark object
|
||||||
|
* @return boolean : is bookmark now fav ?
|
||||||
|
*/
|
||||||
|
fun updateBookmark(bookmark: Bookmark): Boolean {
|
||||||
|
val bookmarkExists = findBookmark(bookmark)
|
||||||
|
if (bookmarkExists) {
|
||||||
|
deleteBookmark(bookmark)
|
||||||
|
} else {
|
||||||
|
addBookmark(bookmark)
|
||||||
|
}
|
||||||
|
return !bookmarkExists
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a Bookmark to database
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to add
|
||||||
|
*/
|
||||||
|
private fun addBookmark(bookmark: Bookmark) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
db.insert(BASE_URI, toContentValues(bookmark))
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a bookmark from database
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to delete
|
||||||
|
*/
|
||||||
|
private fun deleteBookmark(bookmark: Bookmark) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
if (bookmark.contentUri == null) {
|
||||||
|
throw RuntimeException("tried to delete item with no content URI")
|
||||||
|
} else {
|
||||||
|
db.delete(bookmark.contentUri!!, null, null)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a bookmark from database based on its name
|
||||||
|
*
|
||||||
|
* @param bookmark : Bookmark to find
|
||||||
|
* @return boolean : is bookmark in database ?
|
||||||
|
*/
|
||||||
|
fun findBookmark(bookmark: Bookmark?): Boolean {
|
||||||
|
if (bookmark == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI, ALL_FIELDS, "$COLUMN_MEDIA_NAME=?", arrayOf(bookmark.mediaName), null
|
||||||
|
)
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fromCursor(cursor: Cursor): Bookmark {
|
||||||
|
var fileName = cursor.getString(COLUMN_MEDIA_NAME)
|
||||||
|
if (fileName == null) {
|
||||||
|
fileName = ""
|
||||||
|
}
|
||||||
|
return Bookmark(
|
||||||
|
fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toContentValues(bookmark: Bookmark): ContentValues = contentValuesOf(
|
||||||
|
COLUMN_MEDIA_NAME to bookmark.mediaName,
|
||||||
|
COLUMN_CREATOR to bookmark.mediaCreator
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -1,218 +0,0 @@
|
||||||
package fr.free.nrw.commons.bookmarks.pictures;
|
|
||||||
|
|
||||||
import static android.view.View.GONE;
|
|
||||||
import static android.view.View.VISIBLE;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.AdapterView;
|
|
||||||
import android.widget.ListAdapter;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import dagger.android.support.DaggerFragment;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.bookmarks.BookmarkListRootFragment;
|
|
||||||
import fr.free.nrw.commons.category.GridViewAdapter;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentBookmarksPicturesBinding;
|
|
||||||
import fr.free.nrw.commons.utils.NetworkUtils;
|
|
||||||
import fr.free.nrw.commons.utils.ViewUtil;
|
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
||||||
import io.reactivex.disposables.CompositeDisposable;
|
|
||||||
import io.reactivex.schedulers.Schedulers;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class BookmarkPicturesFragment extends DaggerFragment {
|
|
||||||
|
|
||||||
private GridViewAdapter gridAdapter;
|
|
||||||
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
|
||||||
|
|
||||||
private FragmentBookmarksPicturesBinding binding;
|
|
||||||
@Inject
|
|
||||||
BookmarkPicturesController controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an instance of the fragment with the right bundle parameters
|
|
||||||
* @return an instance of the fragment
|
|
||||||
*/
|
|
||||||
public static BookmarkPicturesFragment newInstance() {
|
|
||||||
return new BookmarkPicturesFragment();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(
|
|
||||||
@NonNull LayoutInflater inflater,
|
|
||||||
ViewGroup container,
|
|
||||||
Bundle savedInstanceState
|
|
||||||
) {
|
|
||||||
binding = FragmentBookmarksPicturesBinding.inflate(inflater, container, false);
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
binding.bookmarkedPicturesList.setOnItemClickListener((AdapterView.OnItemClickListener) getParentFragment());
|
|
||||||
initList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
super.onStop();
|
|
||||||
controller.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
compositeDisposable.clear();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
if (controller.needRefreshBookmarkedPictures()) {
|
|
||||||
binding.bookmarkedPicturesList.setVisibility(GONE);
|
|
||||||
if (gridAdapter != null) {
|
|
||||||
gridAdapter.clear();
|
|
||||||
((BookmarkListRootFragment)getParentFragment()).viewPagerNotifyDataSetChanged();
|
|
||||||
}
|
|
||||||
initList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks for internet connection and then initializes
|
|
||||||
* the recycler view with bookmarked pictures
|
|
||||||
*/
|
|
||||||
@SuppressLint("CheckResult")
|
|
||||||
private void initList() {
|
|
||||||
if (!NetworkUtils.isInternetConnectionEstablished(getContext())) {
|
|
||||||
handleNoInternet();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(VISIBLE);
|
|
||||||
binding.statusMessage.setVisibility(GONE);
|
|
||||||
|
|
||||||
compositeDisposable.add(controller.loadBookmarkedPictures()
|
|
||||||
.subscribeOn(Schedulers.io())
|
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.subscribe(this::handleSuccess, this::handleError));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the UI updates for no internet scenario
|
|
||||||
*/
|
|
||||||
private void handleNoInternet() {
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(GONE);
|
|
||||||
if (gridAdapter == null || gridAdapter.isEmpty()) {
|
|
||||||
binding.statusMessage.setVisibility(VISIBLE);
|
|
||||||
binding.statusMessage.setText(getString(R.string.no_internet));
|
|
||||||
} else {
|
|
||||||
ViewUtil.showShortSnackbar(binding.parentLayout, R.string.no_internet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs and handles API error scenario
|
|
||||||
* @param throwable
|
|
||||||
*/
|
|
||||||
private void handleError(Throwable throwable) {
|
|
||||||
Timber.e(throwable, "Error occurred while loading images inside a category");
|
|
||||||
try{
|
|
||||||
ViewUtil.showShortSnackbar(binding.getRoot(), R.string.error_loading_images);
|
|
||||||
initErrorView();
|
|
||||||
}catch (Exception e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the UI updates for a error scenario
|
|
||||||
*/
|
|
||||||
private void initErrorView() {
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(GONE);
|
|
||||||
if (gridAdapter == null || gridAdapter.isEmpty()) {
|
|
||||||
binding.statusMessage.setVisibility(VISIBLE);
|
|
||||||
binding.statusMessage.setText(getString(R.string.no_images_found));
|
|
||||||
} else {
|
|
||||||
binding.statusMessage.setVisibility(GONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the UI updates when there is no bookmarks
|
|
||||||
*/
|
|
||||||
private void initEmptyBookmarkListView() {
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(GONE);
|
|
||||||
if (gridAdapter == null || gridAdapter.isEmpty()) {
|
|
||||||
binding.statusMessage.setVisibility(VISIBLE);
|
|
||||||
binding.statusMessage.setText(getString(R.string.bookmark_empty));
|
|
||||||
} else {
|
|
||||||
binding.statusMessage.setVisibility(GONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles the success scenario
|
|
||||||
* On first load, it initializes the grid view. On subsequent loads, it adds items to the adapter
|
|
||||||
* @param collection List of new Media to be displayed
|
|
||||||
*/
|
|
||||||
private void handleSuccess(List<Media> collection) {
|
|
||||||
if (collection == null) {
|
|
||||||
initErrorView();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (collection.isEmpty()) {
|
|
||||||
initEmptyBookmarkListView();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gridAdapter == null) {
|
|
||||||
setAdapter(collection);
|
|
||||||
} else {
|
|
||||||
if (gridAdapter.containsAll(collection)) {
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(GONE);
|
|
||||||
binding.statusMessage.setVisibility(GONE);
|
|
||||||
binding.bookmarkedPicturesList.setVisibility(VISIBLE);
|
|
||||||
binding.bookmarkedPicturesList.setAdapter(gridAdapter);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
gridAdapter.addItems(collection);
|
|
||||||
((BookmarkListRootFragment) getParentFragment()).viewPagerNotifyDataSetChanged();
|
|
||||||
}
|
|
||||||
binding.loadingImagesProgressBar.setVisibility(GONE);
|
|
||||||
binding.statusMessage.setVisibility(GONE);
|
|
||||||
binding.bookmarkedPicturesList.setVisibility(VISIBLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the adapter with a list of Media objects
|
|
||||||
* @param mediaList List of new Media to be displayed
|
|
||||||
*/
|
|
||||||
private void setAdapter(List<Media> mediaList) {
|
|
||||||
gridAdapter = new GridViewAdapter(
|
|
||||||
this.getContext(),
|
|
||||||
R.layout.layout_category_images,
|
|
||||||
mediaList
|
|
||||||
);
|
|
||||||
binding.bookmarkedPicturesList.setAdapter(gridAdapter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* It return an instance of gridView adapter which helps in extracting media details
|
|
||||||
* used by the gridView
|
|
||||||
* @return GridView Adapter
|
|
||||||
*/
|
|
||||||
public ListAdapter getAdapter() {
|
|
||||||
return binding.bookmarkedPicturesList.getAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,201 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import android.widget.ListAdapter
|
||||||
|
import dagger.android.support.DaggerFragment
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.bookmarks.BookmarkListRootFragment
|
||||||
|
import fr.free.nrw.commons.category.GridViewAdapter
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentBookmarksPicturesBinding
|
||||||
|
import fr.free.nrw.commons.utils.NetworkUtils.isInternetConnectionEstablished
|
||||||
|
import fr.free.nrw.commons.utils.ViewUtil.showShortSnackbar
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
|
import io.reactivex.functions.Consumer
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class BookmarkPicturesFragment : DaggerFragment() {
|
||||||
|
private var gridAdapter: GridViewAdapter? = null
|
||||||
|
private val compositeDisposable = CompositeDisposable()
|
||||||
|
|
||||||
|
private var binding: FragmentBookmarksPicturesBinding? = null
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var controller: BookmarkPicturesController? = null
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
binding = FragmentBookmarksPicturesBinding.inflate(inflater, container, false)
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
binding!!.bookmarkedPicturesList.onItemClickListener =
|
||||||
|
parentFragment as OnItemClickListener?
|
||||||
|
initList()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStop() {
|
||||||
|
super.onStop()
|
||||||
|
controller!!.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
compositeDisposable.clear()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
if (controller!!.needRefreshBookmarkedPictures()) {
|
||||||
|
binding!!.bookmarkedPicturesList.visibility = View.GONE
|
||||||
|
gridAdapter?.let {
|
||||||
|
it.clear()
|
||||||
|
(parentFragment as BookmarkListRootFragment).viewPagerNotifyDataSetChanged()
|
||||||
|
}
|
||||||
|
initList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for internet connection and then initializes
|
||||||
|
* the recycler view with bookmarked pictures
|
||||||
|
*/
|
||||||
|
@SuppressLint("CheckResult")
|
||||||
|
private fun initList() {
|
||||||
|
if (!isInternetConnectionEstablished(context)) {
|
||||||
|
handleNoInternet()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.VISIBLE
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
|
||||||
|
compositeDisposable.add(
|
||||||
|
controller!!.loadBookmarkedPictures()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(::handleSuccess, ::handleError)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the UI updates for no internet scenario
|
||||||
|
*/
|
||||||
|
private fun handleNoInternet() {
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
if (gridAdapter == null || gridAdapter!!.isEmpty) {
|
||||||
|
binding!!.statusMessage.visibility = View.VISIBLE
|
||||||
|
binding!!.statusMessage.text = getString(R.string.no_internet)
|
||||||
|
} else {
|
||||||
|
showShortSnackbar(binding!!.parentLayout, R.string.no_internet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs and handles API error scenario
|
||||||
|
* @param throwable
|
||||||
|
*/
|
||||||
|
private fun handleError(throwable: Throwable) {
|
||||||
|
Timber.e(throwable, "Error occurred while loading images inside a category")
|
||||||
|
try {
|
||||||
|
showShortSnackbar(binding!!.root, R.string.error_loading_images)
|
||||||
|
initErrorView()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the UI updates for a error scenario
|
||||||
|
*/
|
||||||
|
private fun initErrorView() {
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
if (gridAdapter == null || gridAdapter!!.isEmpty) {
|
||||||
|
binding!!.statusMessage.visibility = View.VISIBLE
|
||||||
|
binding!!.statusMessage.text = getString(R.string.no_images_found)
|
||||||
|
} else {
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the UI updates when there is no bookmarks
|
||||||
|
*/
|
||||||
|
private fun initEmptyBookmarkListView() {
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
if (gridAdapter == null || gridAdapter!!.isEmpty) {
|
||||||
|
binding!!.statusMessage.visibility = View.VISIBLE
|
||||||
|
binding!!.statusMessage.text = getString(R.string.bookmark_empty)
|
||||||
|
} else {
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the success scenario
|
||||||
|
* On first load, it initializes the grid view. On subsequent loads, it adds items to the adapter
|
||||||
|
* @param collection List of new Media to be displayed
|
||||||
|
*/
|
||||||
|
private fun handleSuccess(collection: List<Media>?) {
|
||||||
|
if (collection == null) {
|
||||||
|
initErrorView()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (collection.isEmpty()) {
|
||||||
|
initEmptyBookmarkListView()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gridAdapter == null) {
|
||||||
|
setAdapter(collection)
|
||||||
|
} else {
|
||||||
|
if (gridAdapter!!.containsAll(collection)) {
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
binding!!.bookmarkedPicturesList.visibility = View.VISIBLE
|
||||||
|
binding!!.bookmarkedPicturesList.adapter = gridAdapter
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gridAdapter!!.addItems(collection)
|
||||||
|
(parentFragment as BookmarkListRootFragment).viewPagerNotifyDataSetChanged()
|
||||||
|
}
|
||||||
|
binding!!.loadingImagesProgressBar.visibility = View.GONE
|
||||||
|
binding!!.statusMessage.visibility = View.GONE
|
||||||
|
binding!!.bookmarkedPicturesList.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the adapter with a list of Media objects
|
||||||
|
* @param mediaList List of new Media to be displayed
|
||||||
|
*/
|
||||||
|
private fun setAdapter(mediaList: List<Media>) {
|
||||||
|
gridAdapter = GridViewAdapter(
|
||||||
|
requireContext(),
|
||||||
|
R.layout.layout_category_images,
|
||||||
|
mediaList.toMutableList()
|
||||||
|
)
|
||||||
|
binding?.let { it.bookmarkedPicturesList.adapter = gridAdapter }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It return an instance of gridView adapter which helps in extracting media details
|
||||||
|
* used by the gridView
|
||||||
|
* @return GridView Adapter
|
||||||
|
*/
|
||||||
|
fun getAdapter(): ListAdapter? = binding?.bookmarkedPicturesList?.adapter
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package fr.free.nrw.commons.bookmarks.pictures
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
|
||||||
|
object BookmarksTable {
|
||||||
|
const val TABLE_NAME: String = "bookmarks"
|
||||||
|
const val COLUMN_MEDIA_NAME: String = "media_name"
|
||||||
|
const val COLUMN_CREATOR: String = "media_creator"
|
||||||
|
|
||||||
|
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
||||||
|
val ALL_FIELDS = arrayOf(
|
||||||
|
COLUMN_MEDIA_NAME,
|
||||||
|
COLUMN_CREATOR
|
||||||
|
)
|
||||||
|
|
||||||
|
const val DROP_TABLE_STATEMENT: String = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||||
|
|
||||||
|
const val CREATE_TABLE_STATEMENT: String = ("CREATE TABLE $TABLE_NAME (" +
|
||||||
|
"$COLUMN_MEDIA_NAME STRING PRIMARY KEY, " +
|
||||||
|
"$COLUMN_CREATOR STRING" +
|
||||||
|
");")
|
||||||
|
|
||||||
|
fun onCreate(db: SQLiteDatabase) =
|
||||||
|
db.execSQL(CREATE_TABLE_STATEMENT)
|
||||||
|
|
||||||
|
fun onDelete(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(DROP_TABLE_STATEMENT)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
|
if (from == to) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from < 7) {
|
||||||
|
// doesn't exist yet
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from == 7) {
|
||||||
|
// table added in version 8
|
||||||
|
onCreate(db)
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from == 8) {
|
||||||
|
onUpdate(db, from+1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,8 +7,8 @@ import com.google.gson.annotations.SerializedName
|
||||||
*/
|
*/
|
||||||
class CampaignConfig {
|
class CampaignConfig {
|
||||||
@SerializedName("showOnlyLiveCampaigns")
|
@SerializedName("showOnlyLiveCampaigns")
|
||||||
private val showOnlyLiveCampaigns = false
|
var showOnlyLiveCampaigns = false
|
||||||
|
|
||||||
@SerializedName("sortBy")
|
@SerializedName("sortBy")
|
||||||
private val sortBy: String? = null
|
var sortBy: String? = null
|
||||||
}
|
}
|
||||||
|
|
@ -8,8 +8,8 @@ import fr.free.nrw.commons.campaigns.models.Campaign
|
||||||
*/
|
*/
|
||||||
class CampaignResponseDTO {
|
class CampaignResponseDTO {
|
||||||
@SerializedName("config")
|
@SerializedName("config")
|
||||||
val campaignConfig: CampaignConfig? = null
|
var campaignConfig: CampaignConfig? = null
|
||||||
|
|
||||||
@SerializedName("campaigns")
|
@SerializedName("campaigns")
|
||||||
val campaigns: List<Campaign>? = null
|
var campaigns: List<Campaign>? = null
|
||||||
}
|
}
|
||||||
|
|
@ -9,12 +9,9 @@ import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteQueryBuilder
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import androidx.annotation.NonNull
|
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
import timber.log.Timber
|
import androidx.core.net.toUri
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
class CategoryContentProvider : CommonsDaggerContentProvider() {
|
class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
|
|
||||||
|
|
@ -23,9 +20,6 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
addURI(BuildConfig.CATEGORY_AUTHORITY, "${BASE_PATH}/#", CATEGORIES_ID)
|
addURI(BuildConfig.CATEGORY_AUTHORITY, "${BASE_PATH}/#", CATEGORIES_ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var dbOpenHelper: DBOpenHelper
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
|
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
|
||||||
selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
|
selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
|
||||||
|
|
@ -34,7 +28,7 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
}
|
}
|
||||||
|
|
||||||
val uriType = uriMatcher.match(uri)
|
val uriType = uriMatcher.match(uri)
|
||||||
val db = dbOpenHelper.readableDatabase
|
val db = requireDb()
|
||||||
|
|
||||||
val cursor: Cursor? = when (uriType) {
|
val cursor: Cursor? = when (uriType) {
|
||||||
CATEGORIES -> queryBuilder.query(
|
CATEGORIES -> queryBuilder.query(
|
||||||
|
|
@ -58,45 +52,37 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
else -> throw IllegalArgumentException("Unknown URI $uri")
|
else -> throw IllegalArgumentException("Unknown URI $uri")
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor?.setNotificationUri(context?.contentResolver, uri)
|
cursor?.setNotificationUri(requireContext().contentResolver, uri)
|
||||||
return cursor
|
return cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getType(uri: Uri): String? {
|
override fun getType(uri: Uri): String? = null
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
|
override fun insert(uri: Uri, contentValues: ContentValues?): Uri {
|
||||||
val uriType = uriMatcher.match(uri)
|
val uriType = uriMatcher.match(uri)
|
||||||
val sqlDB = dbOpenHelper.writableDatabase
|
|
||||||
val id: Long
|
val id: Long
|
||||||
when (uriType) {
|
when (uriType) {
|
||||||
CATEGORIES -> {
|
CATEGORIES -> {
|
||||||
id = sqlDB.insert(TABLE_NAME, null, contentValues)
|
id = requireDb().insert(TABLE_NAME, null, contentValues)
|
||||||
}
|
}
|
||||||
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
||||||
}
|
}
|
||||||
context?.contentResolver?.notifyChange(uri, null)
|
requireContext().contentResolver?.notifyChange(uri, null)
|
||||||
return Uri.parse("${Companion.BASE_URI}/$id")
|
return "${BASE_URI}/$id".toUri()
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
|
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0
|
||||||
// Not implemented
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
override fun bulkInsert(uri: Uri, values: Array<ContentValues>): Int {
|
override fun bulkInsert(uri: Uri, values: Array<ContentValues>): Int {
|
||||||
Timber.d("Hello, bulk insert! (CategoryContentProvider)")
|
|
||||||
val uriType = uriMatcher.match(uri)
|
val uriType = uriMatcher.match(uri)
|
||||||
val sqlDB = dbOpenHelper.writableDatabase
|
val sqlDB = requireDb()
|
||||||
sqlDB.beginTransaction()
|
sqlDB.beginTransaction()
|
||||||
when (uriType) {
|
when (uriType) {
|
||||||
CATEGORIES -> {
|
CATEGORIES -> {
|
||||||
for (value in values) {
|
for (value in values) {
|
||||||
Timber.d("Inserting! %s", value)
|
|
||||||
sqlDB.insert(TABLE_NAME, null, value)
|
sqlDB.insert(TABLE_NAME, null, value)
|
||||||
}
|
}
|
||||||
sqlDB.setTransactionSuccessful()
|
sqlDB.setTransactionSuccessful()
|
||||||
|
|
@ -104,7 +90,7 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
||||||
}
|
}
|
||||||
sqlDB.endTransaction()
|
sqlDB.endTransaction()
|
||||||
context?.contentResolver?.notifyChange(uri, null)
|
requireContext().contentResolver?.notifyChange(uri, null)
|
||||||
return values.size
|
return values.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,17 +98,18 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
override fun update(uri: Uri, contentValues: ContentValues?, selection: String?,
|
override fun update(uri: Uri, contentValues: ContentValues?, selection: String?,
|
||||||
selectionArgs: Array<String>?): Int {
|
selectionArgs: Array<String>?): Int {
|
||||||
val uriType = uriMatcher.match(uri)
|
val uriType = uriMatcher.match(uri)
|
||||||
val sqlDB = dbOpenHelper.writableDatabase
|
|
||||||
val rowsUpdated: Int
|
val rowsUpdated: Int
|
||||||
when (uriType) {
|
when (uriType) {
|
||||||
CATEGORIES_ID -> {
|
CATEGORIES_ID -> {
|
||||||
if (TextUtils.isEmpty(selection)) {
|
if (TextUtils.isEmpty(selection)) {
|
||||||
val id = uri.lastPathSegment?.toInt()
|
val id = uri.lastPathSegment?.toInt()
|
||||||
?: throw IllegalArgumentException("Invalid ID")
|
?: throw IllegalArgumentException("Invalid ID")
|
||||||
rowsUpdated = sqlDB.update(TABLE_NAME,
|
rowsUpdated = requireDb().update(
|
||||||
|
TABLE_NAME,
|
||||||
contentValues,
|
contentValues,
|
||||||
"$COLUMN_ID = ?",
|
"$COLUMN_ID = ?",
|
||||||
arrayOf(id.toString()))
|
arrayOf(id.toString())
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
throw IllegalArgumentException(
|
throw IllegalArgumentException(
|
||||||
"Parameter `selection` should be empty when updating an ID")
|
"Parameter `selection` should be empty when updating an ID")
|
||||||
|
|
@ -130,7 +117,7 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
}
|
}
|
||||||
else -> throw IllegalArgumentException("Unknown URI: $uri with type $uriType")
|
else -> throw IllegalArgumentException("Unknown URI: $uri with type $uriType")
|
||||||
}
|
}
|
||||||
context?.contentResolver?.notifyChange(uri, null)
|
requireContext().contentResolver?.notifyChange(uri, null)
|
||||||
return rowsUpdated
|
return rowsUpdated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -165,13 +152,9 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
"$COLUMN_TIMES_USED INTEGER" +
|
"$COLUMN_TIMES_USED INTEGER" +
|
||||||
");"
|
");"
|
||||||
|
|
||||||
fun uriForId(id: Int): Uri {
|
fun uriForId(id: Int): Uri = Uri.parse("${BASE_URI}/$id")
|
||||||
return Uri.parse("${BASE_URI}/$id")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onCreate(db: SQLiteDatabase) {
|
fun onCreate(db: SQLiteDatabase) = db.execSQL(CREATE_TABLE_STATEMENT)
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onDelete(db: SQLiteDatabase) {
|
fun onDelete(db: SQLiteDatabase) {
|
||||||
db.execSQL(DROP_TABLE_STATEMENT)
|
db.execSQL(DROP_TABLE_STATEMENT)
|
||||||
|
|
@ -200,6 +183,6 @@ class CategoryContentProvider : CommonsDaggerContentProvider() {
|
||||||
private const val CATEGORIES = 1
|
private const val CATEGORIES = 1
|
||||||
private const val CATEGORIES_ID = 2
|
private const val CATEGORIES_ID = 2
|
||||||
private const val BASE_PATH = "categories"
|
private const val BASE_PATH = "categories"
|
||||||
val BASE_URI: Uri = Uri.parse("content://${BuildConfig.CATEGORY_AUTHORITY}/${Companion.BASE_PATH}")
|
val BASE_URI: Uri = "content://${BuildConfig.CATEGORY_AUTHORITY}/${BASE_PATH}".toUri()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import fr.free.nrw.commons.explore.categories.sub.SubCategoriesFragment
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.utils.handleWebUrl
|
import fr.free.nrw.commons.utils.handleWebUrl
|
||||||
import fr.free.nrw.commons.wikidata.model.WikiSite
|
import fr.free.nrw.commons.wikidata.model.WikiSite
|
||||||
import fr.free.nrw.commons.wikidata.model.page.PageTitle
|
import fr.free.nrw.commons.wikidata.model.page.PageTitle
|
||||||
|
|
@ -57,6 +58,7 @@ class CategoryDetailsActivity : BaseActivity(),
|
||||||
|
|
||||||
binding = ActivityCategoryDetailsBinding.inflate(layoutInflater)
|
binding = ActivityCategoryDetailsBinding.inflate(layoutInflater)
|
||||||
val view = binding.root
|
val view = binding.root
|
||||||
|
applyEdgeToEdgeAllInsets(view)
|
||||||
setContentView(view)
|
setContentView(view)
|
||||||
supportFragmentManager = getSupportFragmentManager()
|
supportFragmentManager = getSupportFragmentManager()
|
||||||
viewPagerAdapter = ViewPagerAdapter(this, supportFragmentManager)
|
viewPagerAdapter = ViewPagerAdapter(this, supportFragmentManager)
|
||||||
|
|
|
||||||
|
|
@ -180,8 +180,8 @@ class ContributionController @Inject constructor(@param:Named("default_preferenc
|
||||||
showAlertDialog(
|
showAlertDialog(
|
||||||
activity, activity.getString(R.string.location_permission_title),
|
activity, activity.getString(R.string.location_permission_title),
|
||||||
activity.getString(R.string.in_app_camera_location_permission_rationale),
|
activity.getString(R.string.in_app_camera_location_permission_rationale),
|
||||||
activity.getString(android.R.string.ok),
|
activity.getString(R.string.ok),
|
||||||
activity.getString(android.R.string.cancel),
|
activity.getString(R.string.cancel),
|
||||||
{
|
{
|
||||||
createDialogsAndHandleLocationPermissions(
|
createDialogsAndHandleLocationPermissions(
|
||||||
activity,
|
activity,
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ import fr.free.nrw.commons.quiz.QuizChecker
|
||||||
import fr.free.nrw.commons.settings.SettingsFragment
|
import fr.free.nrw.commons.settings.SettingsFragment
|
||||||
import fr.free.nrw.commons.startWelcome
|
import fr.free.nrw.commons.startWelcome
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.upload.UploadProgressActivity
|
import fr.free.nrw.commons.upload.UploadProgressActivity
|
||||||
import fr.free.nrw.commons.upload.worker.WorkRequestHelper.Companion.makeOneTimeWorkRequest
|
import fr.free.nrw.commons.upload.worker.WorkRequestHelper.Companion.makeOneTimeWorkRequest
|
||||||
import fr.free.nrw.commons.utils.ViewUtilWrapper
|
import fr.free.nrw.commons.utils.ViewUtilWrapper
|
||||||
|
|
@ -112,6 +113,7 @@ class MainActivity : BaseActivity(), FragmentManager.OnBackStackChangedListener
|
||||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = MainBinding.inflate(layoutInflater)
|
binding = MainBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.root)
|
||||||
setContentView(binding!!.root)
|
setContentView(binding!!.root)
|
||||||
setSupportActionBar(binding!!.toolbarBinding.toolbar)
|
setSupportActionBar(binding!!.toolbarBinding.toolbar)
|
||||||
tabLayout = binding!!.fragmentMainNavTabLayout
|
tabLayout = binding!!.fragmentMainNavTabLayout
|
||||||
|
|
@ -151,21 +153,7 @@ after opening the app.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setUpPager()
|
setUpPager()
|
||||||
/**
|
|
||||||
* Ask the user for media location access just after login
|
|
||||||
* so that location in the EXIF metadata of the images shared by the user
|
|
||||||
* is retained on devices running Android 10 or above
|
|
||||||
*/
|
|
||||||
// if (VERSION.SDK_INT >= VERSION_CODES.Q) {
|
|
||||||
// ActivityCompat.requestPermissions(this,
|
|
||||||
// new String[]{Manifest.permission.ACCESS_MEDIA_LOCATION}, 0);
|
|
||||||
// PermissionUtils.checkPermissionsAndPerformAction(
|
|
||||||
// this,
|
|
||||||
// () -> {},
|
|
||||||
// R.string.media_location_permission_denied,
|
|
||||||
// R.string.add_location_manually,
|
|
||||||
// permission.ACCESS_MEDIA_LOCATION);
|
|
||||||
// }
|
|
||||||
checkAndResumeStuckUploads()
|
checkAndResumeStuckUploads()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -336,7 +324,7 @@ after opening the app.
|
||||||
)
|
)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.blockingGet()
|
.blockingGet()
|
||||||
Timber.d("Resuming " + stuckUploads.size + " uploads...")
|
Timber.d("Resuming %d uploads...", stuckUploads.size)
|
||||||
if (!stuckUploads.isEmpty()) {
|
if (!stuckUploads.isEmpty()) {
|
||||||
for (contribution in stuckUploads) {
|
for (contribution in stuckUploads) {
|
||||||
contribution.state = Contribution.STATE_QUEUED
|
contribution.state = Contribution.STATE_QUEUED
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,10 @@ class SetWallpaperWorker(context: Context, params: WorkerParameters) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>?) {
|
override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage?>?>) {
|
||||||
Timber.d("Error getting bitmap from image url %s", imageUrl.toString())
|
Timber.d("Error getting bitmap from image url %s", imageUrl.toString())
|
||||||
showNotification(context, "Setting Wallpaper Failed", "Failed to download image.")
|
showNotification(context, "Setting Wallpaper Failed", "Failed to download image.")
|
||||||
dataSource?.close()
|
dataSource.close()
|
||||||
}
|
}
|
||||||
}, CallerThreadExecutor.getInstance())
|
}, CallerThreadExecutor.getInstance())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,4 +39,11 @@ data class Folder(
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = bucketId.hashCode()
|
||||||
|
result = 31 * result + name.hashCode()
|
||||||
|
result = 31 * result + images.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package fr.free.nrw.commons.customselector.model
|
package fr.free.nrw.commons.customselector.model
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
import android.os.Parcel
|
import android.os.Parcel
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
|
|
||||||
|
|
@ -48,7 +49,12 @@ data class Image(
|
||||||
this(
|
this(
|
||||||
parcel.readLong(),
|
parcel.readLong(),
|
||||||
parcel.readString()!!,
|
parcel.readString()!!,
|
||||||
parcel.readParcelable(Uri::class.java.classLoader)!!,
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
parcel.readParcelable(Uri::class.java.classLoader, Uri::class.java)!!
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
parcel.readParcelable(Uri::class.java.classLoader)!!
|
||||||
|
},
|
||||||
parcel.readString()!!,
|
parcel.readString()!!,
|
||||||
parcel.readLong(),
|
parcel.readLong(),
|
||||||
parcel.readString()!!,
|
parcel.readString()!!,
|
||||||
|
|
@ -121,4 +127,16 @@ data class Image(
|
||||||
|
|
||||||
override fun newArray(size: Int): Array<Image?> = arrayOfNulls(size)
|
override fun newArray(size: Int): Array<Image?> = arrayOfNulls(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
var result = id.hashCode()
|
||||||
|
result = 31 * result + bucketId.hashCode()
|
||||||
|
result = 31 * result + name.hashCode()
|
||||||
|
result = 31 * result + uri.hashCode()
|
||||||
|
result = 31 * result + path.hashCode()
|
||||||
|
result = 31 * result + bucketName.hashCode()
|
||||||
|
result = 31 * result + sha1.hashCode()
|
||||||
|
result = 31 * result + date.hashCode()
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -168,8 +168,7 @@ class ImageAdapter(
|
||||||
|
|
||||||
// Getting selected index when switch is off
|
// Getting selected index when switch is off
|
||||||
} else if (actionableImagesMap.size > position) {
|
} else if (actionableImagesMap.size > position) {
|
||||||
ImageHelper
|
ImageHelper.getIndex(selectedImages, ArrayList(actionableImagesMap.values)[position])
|
||||||
.getIndex(selectedImages, ArrayList(actionableImagesMap.values)[position])
|
|
||||||
|
|
||||||
// For any other case return -1
|
// For any other case return -1
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -348,8 +347,14 @@ class ImageAdapter(
|
||||||
numberOfSelectedImagesMarkedAsNotForUpload--
|
numberOfSelectedImagesMarkedAsNotForUpload--
|
||||||
}
|
}
|
||||||
notifyItemChanged(position, ImageUnselected())
|
notifyItemChanged(position, ImageUnselected())
|
||||||
|
// Notify listener of deselection to update UI
|
||||||
|
imageSelectListener.onSelectedImagesChanged(selectedImages, numberOfSelectedImagesMarkedAsNotForUpload)
|
||||||
} else {
|
} else {
|
||||||
val image = images[position]
|
// Prevent adding the same image multiple times
|
||||||
|
val image = if (showAlreadyActionedImages) images[position] else ArrayList(actionableImagesMap.values)[position]
|
||||||
|
if (selectedImages.contains(image)) {
|
||||||
|
return // Image already selected, ignore additional clicks
|
||||||
|
}
|
||||||
scope.launch(ioDispatcher) {
|
scope.launch(ioDispatcher) {
|
||||||
val imageSHA1 = imageLoader.getSHA1(image, defaultDispatcher)
|
val imageSHA1 = imageLoader.getSHA1(image, defaultDispatcher)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
|
|
@ -373,7 +378,6 @@ class ImageAdapter(
|
||||||
}
|
}
|
||||||
selectedImages.add(image)
|
selectedImages.add(image)
|
||||||
notifyItemChanged(position, ImageSelectedOrUpdated())
|
notifyItemChanged(position, ImageSelectedOrUpdated())
|
||||||
|
|
||||||
imageSelectListener.onSelectedImagesChanged(selectedImages, numberOfSelectedImagesMarkedAsNotForUpload)
|
imageSelectListener.onSelectedImagesChanged(selectedImages, numberOfSelectedImagesMarkedAsNotForUpload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.constraintlayout.widget.ConstraintLayout
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.view.ViewGroupCompat
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.customselector.database.NotForUploadStatus
|
import fr.free.nrw.commons.customselector.database.NotForUploadStatus
|
||||||
|
|
@ -56,6 +57,8 @@ import fr.free.nrw.commons.media.ZoomableActivity
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
||||||
import fr.free.nrw.commons.utils.CustomSelectorUtils
|
import fr.free.nrw.commons.utils.CustomSelectorUtils
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomPaddingInsets
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeTopInsets
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
@ -198,6 +201,9 @@ class CustomSelectorActivity :
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
ViewGroupCompat.installCompatInsetsDispatch(binding.root)
|
||||||
|
applyEdgeToEdgeTopInsets(toolbarBinding.toolbarLayout)
|
||||||
|
bottomSheetBinding.bottomLayout.applyEdgeToEdgeBottomPaddingInsets()
|
||||||
val view = binding.root
|
val view = binding.root
|
||||||
setContentView(view)
|
setContentView(view)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import fr.free.nrw.commons.databinding.FragmentCustomSelectorBinding
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
import fr.free.nrw.commons.media.MediaClient
|
import fr.free.nrw.commons.media.MediaClient
|
||||||
import fr.free.nrw.commons.upload.FileProcessor
|
import fr.free.nrw.commons.upload.FileProcessor
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomPaddingInsets
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,6 +100,7 @@ class FolderFragment : CommonsDaggerSupportFragment() {
|
||||||
selectorRV = binding?.selectorRv
|
selectorRV = binding?.selectorRv
|
||||||
loader = binding?.loader
|
loader = binding?.loader
|
||||||
with(binding?.selectorRv) {
|
with(binding?.selectorRv) {
|
||||||
|
this?.applyEdgeToEdgeBottomPaddingInsets()
|
||||||
this?.layoutManager = gridLayoutManager
|
this?.layoutManager = gridLayoutManager
|
||||||
this?.setHasFixedSize(true)
|
this?.setHasFixedSize(true)
|
||||||
this?.adapter = folderAdapter
|
this?.adapter = folderAdapter
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.ProgressBar
|
import android.widget.ProgressBar
|
||||||
import android.widget.Switch
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.constraintlayout.widget.ConstraintLayout
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
|
|
@ -20,6 +19,7 @@ import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.lifecycle.repeatOnLifecycle
|
import androidx.lifecycle.repeatOnLifecycle
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.google.android.material.switchmaterial.SwitchMaterial
|
||||||
import fr.free.nrw.commons.contributions.Contribution
|
import fr.free.nrw.commons.contributions.Contribution
|
||||||
import fr.free.nrw.commons.contributions.ContributionDao
|
import fr.free.nrw.commons.contributions.ContributionDao
|
||||||
import fr.free.nrw.commons.customselector.database.NotForUploadStatusDao
|
import fr.free.nrw.commons.customselector.database.NotForUploadStatusDao
|
||||||
|
|
@ -41,11 +41,13 @@ import fr.free.nrw.commons.media.MediaClient
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
import fr.free.nrw.commons.upload.FileProcessor
|
import fr.free.nrw.commons.upload.FileProcessor
|
||||||
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomPaddingInsets
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import timber.log.Timber
|
||||||
import java.util.TreeMap
|
import java.util.TreeMap
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
|
@ -80,7 +82,7 @@ class ImageFragment :
|
||||||
*/
|
*/
|
||||||
private var selectorRV: RecyclerView? = null
|
private var selectorRV: RecyclerView? = null
|
||||||
private var loader: ProgressBar? = null
|
private var loader: ProgressBar? = null
|
||||||
private var switch: Switch? = null
|
private var switch: SwitchMaterial? = null
|
||||||
lateinit var filteredImages: ArrayList<Image>
|
lateinit var filteredImages: ArrayList<Image>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -210,13 +212,18 @@ class ImageFragment :
|
||||||
savedInstanceState: Bundle?,
|
savedInstanceState: Bundle?,
|
||||||
): View? {
|
): View? {
|
||||||
_binding = FragmentCustomSelectorBinding.inflate(inflater, container, false)
|
_binding = FragmentCustomSelectorBinding.inflate(inflater, container, false)
|
||||||
imageAdapter =
|
|
||||||
ImageAdapter(requireActivity(), activity as ImageSelectListener, imageLoader!!)
|
// ensures imageAdapter is initialized
|
||||||
|
if (!::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter = ImageAdapter(requireActivity(), activity as ImageSelectListener, imageLoader!!)
|
||||||
|
Timber.d("Initialized imageAdapter in onCreateView")
|
||||||
|
}
|
||||||
// Set single selection mode if needed
|
// Set single selection mode if needed
|
||||||
val singleSelection = (activity as? CustomSelectorActivity)?.intent?.getBooleanExtra(CustomSelectorActivity.EXTRA_SINGLE_SELECTION, false) == true
|
val singleSelection = (activity as? CustomSelectorActivity)?.intent?.getBooleanExtra(CustomSelectorActivity.EXTRA_SINGLE_SELECTION, false) == true
|
||||||
imageAdapter.setSingleSelection(singleSelection)
|
imageAdapter.setSingleSelection(singleSelection)
|
||||||
gridLayoutManager = GridLayoutManager(context, getSpanCount())
|
gridLayoutManager = GridLayoutManager(context, getSpanCount())
|
||||||
with(binding?.selectorRv) {
|
with(binding?.selectorRv) {
|
||||||
|
this?.applyEdgeToEdgeBottomPaddingInsets()
|
||||||
this?.layoutManager = gridLayoutManager
|
this?.layoutManager = gridLayoutManager
|
||||||
this?.setHasFixedSize(true)
|
this?.setHasFixedSize(true)
|
||||||
this?.adapter = imageAdapter
|
this?.adapter = imageAdapter
|
||||||
|
|
@ -368,7 +375,12 @@ class ImageFragment :
|
||||||
* notifyDataSetChanged, rebuild the holder views to account for deleted images.
|
* notifyDataSetChanged, rebuild the holder views to account for deleted images.
|
||||||
*/
|
*/
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
imageAdapter.notifyDataSetChanged()
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.notifyDataSetChanged()
|
||||||
|
Timber.d("Notified imageAdapter in onResume")
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in onResume")
|
||||||
|
}
|
||||||
super.onResume()
|
super.onResume()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -378,14 +390,19 @@ class ImageFragment :
|
||||||
* Save the Image Fragment state.
|
* Save the Image Fragment state.
|
||||||
*/
|
*/
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
imageAdapter.cleanUp()
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.cleanUp()
|
||||||
|
Timber.d("Cleaned up imageAdapter in onDestroy")
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in onDestroy, skipping cleanup")
|
||||||
|
}
|
||||||
|
|
||||||
val position =
|
val position =
|
||||||
(selectorRV?.layoutManager as GridLayoutManager)
|
(selectorRV?.layoutManager as? GridLayoutManager)
|
||||||
.findFirstVisibleItemPosition()
|
?.findFirstVisibleItemPosition() ?: -1
|
||||||
|
|
||||||
// Check for empty RecyclerView.
|
// check for valid position and non-empty image list
|
||||||
if (position != -1 && filteredImages.size > 0) {
|
if (position != -1 && filteredImages.isNotEmpty() && ::imageAdapter.isInitialized) {
|
||||||
context?.let { context ->
|
context?.let { context ->
|
||||||
context
|
context
|
||||||
.getSharedPreferences(
|
.getSharedPreferences(
|
||||||
|
|
@ -394,34 +411,57 @@ class ImageFragment :
|
||||||
)?.let { prefs ->
|
)?.let { prefs ->
|
||||||
prefs.edit()?.let { editor ->
|
prefs.edit()?.let { editor ->
|
||||||
editor.putLong("ItemId", imageAdapter.getImageIdAt(position))?.apply()
|
editor.putLong("ItemId", imageAdapter.getImageIdAt(position))?.apply()
|
||||||
|
Timber.d("Saved last visible item ID: %d", imageAdapter.getImageIdAt(position))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Timber.d("Skipped saving item ID: position=%d, filteredImages.size=%d, imageAdapter initialized=%b",
|
||||||
|
position, filteredImages.size, ::imageAdapter.isInitialized)
|
||||||
}
|
}
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
_binding = null
|
_binding = null
|
||||||
|
selectorRV = null
|
||||||
|
loader = null
|
||||||
|
switch = null
|
||||||
|
progressLayout = null
|
||||||
super.onDestroyView()
|
super.onDestroyView()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun refresh() {
|
override fun refresh() {
|
||||||
imageAdapter.refresh(filteredImages, allImages, getUploadingContributions())
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.refresh(filteredImages, allImages, getUploadingContributions())
|
||||||
|
Timber.d("Refreshed imageAdapter")
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in refresh")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the image from the actionable image map
|
* Removes the image from the actionable image map
|
||||||
*/
|
*/
|
||||||
fun removeImage(image: Image) {
|
fun removeImage(image: Image) {
|
||||||
imageAdapter.removeImageFromActionableImageMap(image)
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.removeImageFromActionableImageMap(image)
|
||||||
|
Timber.d("Removed image from actionable image map")
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in removeImage")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the selected images
|
* Clears the selected images
|
||||||
*/
|
*/
|
||||||
fun clearSelectedImages() {
|
fun clearSelectedImages() {
|
||||||
imageAdapter.clearSelectedImages()
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.clearSelectedImages()
|
||||||
|
Timber.d("Cleared selected images")
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in clearSelectedImages")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -432,6 +472,15 @@ class ImageFragment :
|
||||||
selectedImages: ArrayList<Image>,
|
selectedImages: ArrayList<Image>,
|
||||||
shouldRefresh: Boolean,
|
shouldRefresh: Boolean,
|
||||||
) {
|
) {
|
||||||
|
if (::imageAdapter.isInitialized) {
|
||||||
|
imageAdapter.setSelectedImages(selectedImages)
|
||||||
|
if (shouldRefresh) {
|
||||||
|
imageAdapter.refresh(filteredImages, allImages, getUploadingContributions())
|
||||||
|
}
|
||||||
|
Timber.d("Passed %d selected images to imageAdapter, shouldRefresh=%b", selectedImages.size, shouldRefresh)
|
||||||
|
} else {
|
||||||
|
Timber.w("imageAdapter not initialized in passSelectedImages")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -441,6 +490,7 @@ class ImageFragment :
|
||||||
if (!progressDialog.isShowing) {
|
if (!progressDialog.isShowing) {
|
||||||
progressDialogLayout.progressDialogText.text = text
|
progressDialogLayout.progressDialogText.text = text
|
||||||
progressDialog.show()
|
progressDialog.show()
|
||||||
|
Timber.d("Showing mark/unmark progress dialog: %s", text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -450,6 +500,7 @@ class ImageFragment :
|
||||||
fun dismissMarkUnmarkProgressDialog() {
|
fun dismissMarkUnmarkProgressDialog() {
|
||||||
if (progressDialog.isShowing) {
|
if (progressDialog.isShowing) {
|
||||||
progressDialog.dismiss()
|
progressDialog.dismiss()
|
||||||
|
Timber.d("Dismissed mark/unmark progress dialog")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,10 @@ import android.content.Context
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteException
|
import android.database.sqlite.SQLiteException
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsTable
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
import fr.free.nrw.commons.bookmarks.pictures.BookmarksTable
|
||||||
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao
|
|
||||||
import fr.free.nrw.commons.category.CategoryDao
|
import fr.free.nrw.commons.category.CategoryDao
|
||||||
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable
|
||||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -30,17 +29,17 @@ class DBOpenHelper(
|
||||||
*/
|
*/
|
||||||
override fun onCreate(db: SQLiteDatabase) {
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
CategoryDao.Table.onCreate(db)
|
CategoryDao.Table.onCreate(db)
|
||||||
BookmarkPicturesDao.Table.onCreate(db)
|
BookmarksTable.onCreate(db)
|
||||||
BookmarkItemsDao.Table.onCreate(db)
|
BookmarkItemsTable.onCreate(db)
|
||||||
RecentSearchesDao.Table.onCreate(db)
|
RecentSearchesTable.onCreate(db)
|
||||||
RecentLanguagesDao.Table.onCreate(db)
|
RecentLanguagesDao.Table.onCreate(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUpgrade(db: SQLiteDatabase, from: Int, to: Int) {
|
override fun onUpgrade(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
CategoryDao.Table.onUpdate(db, from, to)
|
CategoryDao.Table.onUpdate(db, from, to)
|
||||||
BookmarkPicturesDao.Table.onUpdate(db, from, to)
|
BookmarksTable.onUpdate(db, from, to)
|
||||||
BookmarkItemsDao.Table.onUpdate(db, from, to)
|
BookmarkItemsTable.onUpdate(db, from, to)
|
||||||
RecentSearchesDao.Table.onUpdate(db, from, to)
|
RecentSearchesTable.onUpdate(db, from, to)
|
||||||
RecentLanguagesDao.Table.onUpdate(db, from, to)
|
RecentLanguagesDao.Table.onUpdate(db, from, to)
|
||||||
deleteTable(db, CONTRIBUTIONS_TABLE)
|
deleteTable(db, CONTRIBUTIONS_TABLE)
|
||||||
deleteTable(db, BOOKMARKS_LOCATIONS)
|
deleteTable(db, BOOKMARKS_LOCATIONS)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import android.speech.RecognizerIntent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.activity.result.ActivityResult
|
import androidx.activity.result.ActivityResult
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import fr.free.nrw.commons.CommonsApplication
|
import fr.free.nrw.commons.CommonsApplication
|
||||||
|
|
@ -20,9 +21,11 @@ import fr.free.nrw.commons.description.EditDescriptionConstants.WIKITEXT
|
||||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
|
||||||
import fr.free.nrw.commons.settings.Prefs
|
import fr.free.nrw.commons.settings.Prefs
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomInsets
|
||||||
import fr.free.nrw.commons.upload.UploadMediaDetail
|
import fr.free.nrw.commons.upload.UploadMediaDetail
|
||||||
import fr.free.nrw.commons.upload.UploadMediaDetailAdapter
|
import fr.free.nrw.commons.upload.UploadMediaDetailAdapter
|
||||||
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
|
import fr.free.nrw.commons.utils.DialogUtil.showAlertDialog
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeTopPaddingInsets
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.functions.Consumer
|
import io.reactivex.functions.Consumer
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
|
@ -87,6 +90,10 @@ class DescriptionEditActivity :
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
binding = ActivityDescriptionEditBinding.inflate(layoutInflater)
|
binding = ActivityDescriptionEditBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeBottomInsets(binding.btnEditSubmit)
|
||||||
|
WindowCompat.getInsetsController(window, window.decorView)
|
||||||
|
.isAppearanceLightStatusBars = false
|
||||||
|
binding.toolbar.applyEdgeToEdgeTopPaddingInsets()
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
val bundle = intent.extras
|
val bundle = intent.extras
|
||||||
|
|
@ -143,7 +150,7 @@ class DescriptionEditActivity :
|
||||||
this,
|
this,
|
||||||
getString(titleStringID),
|
getString(titleStringID),
|
||||||
getString(messageStringId),
|
getString(messageStringId),
|
||||||
getString(android.R.string.ok),
|
getString(R.string.ok),
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,25 @@
|
||||||
package fr.free.nrw.commons.di
|
package fr.free.nrw.commons.di
|
||||||
|
|
||||||
import android.content.ContentProvider
|
import android.content.ContentProvider
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import fr.free.nrw.commons.data.DBOpenHelper
|
||||||
import fr.free.nrw.commons.di.ApplicationlessInjection.Companion.getInstance
|
import fr.free.nrw.commons.di.ApplicationlessInjection.Companion.getInstance
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
abstract class CommonsDaggerContentProvider : ContentProvider() {
|
abstract class CommonsDaggerContentProvider : ContentProvider() {
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var dbOpenHelper: DBOpenHelper? = null
|
||||||
|
|
||||||
override fun onCreate(): Boolean {
|
override fun onCreate(): Boolean {
|
||||||
inject()
|
inject()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun requireDbOpenHelper(): DBOpenHelper = dbOpenHelper!!
|
||||||
|
|
||||||
|
fun requireDb(): SQLiteDatabase = requireDbOpenHelper().writableDatabase!!
|
||||||
|
|
||||||
private fun inject() {
|
private fun inject() {
|
||||||
val injection = getInstance(context!!)
|
val injection = getInstance(context!!)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import dagger.Provides
|
||||||
import fr.free.nrw.commons.BetaConstants
|
import fr.free.nrw.commons.BetaConstants
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.OkHttpConnectionFactory
|
import fr.free.nrw.commons.OkHttpConnectionFactory
|
||||||
|
import fr.free.nrw.commons.CommonHeaderRequestInterceptor
|
||||||
import fr.free.nrw.commons.actions.PageEditClient
|
import fr.free.nrw.commons.actions.PageEditClient
|
||||||
import fr.free.nrw.commons.actions.PageEditInterface
|
import fr.free.nrw.commons.actions.PageEditInterface
|
||||||
import fr.free.nrw.commons.actions.ThanksInterface
|
import fr.free.nrw.commons.actions.ThanksInterface
|
||||||
|
|
@ -60,6 +61,7 @@ class NetworkingModule {
|
||||||
.connectTimeout(120, TimeUnit.SECONDS)
|
.connectTimeout(120, TimeUnit.SECONDS)
|
||||||
.writeTimeout(120, TimeUnit.SECONDS)
|
.writeTimeout(120, TimeUnit.SECONDS)
|
||||||
.addInterceptor(httpLoggingInterceptor)
|
.addInterceptor(httpLoggingInterceptor)
|
||||||
|
.addInterceptor(CommonHeaderRequestInterceptor())
|
||||||
.readTimeout(120, TimeUnit.SECONDS)
|
.readTimeout(120, TimeUnit.SECONDS)
|
||||||
.cache(Cache(File(context.cacheDir, "okHttpCache"), OK_HTTP_CACHE_SIZE))
|
.cache(Cache(File(context.cacheDir, "okHttpCache"), OK_HTTP_CACHE_SIZE))
|
||||||
.build()
|
.build()
|
||||||
|
|
|
||||||
|
|
@ -1,260 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore;
|
|
||||||
|
|
||||||
import static androidx.viewpager.widget.ViewPager.SCROLL_STATE_IDLE;
|
|
||||||
import static fr.free.nrw.commons.ViewPagerAdapter.pairOf;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuInflater;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.fragment.app.FragmentPagerAdapter;
|
|
||||||
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.ViewPagerAdapter;
|
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentExploreBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
|
||||||
import fr.free.nrw.commons.theme.BaseActivity;
|
|
||||||
import fr.free.nrw.commons.utils.ActivityUtils;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import kotlin.Pair;
|
|
||||||
|
|
||||||
public class ExploreFragment extends CommonsDaggerSupportFragment {
|
|
||||||
|
|
||||||
private static final String FEATURED_IMAGES_CATEGORY = "Featured_pictures_on_Wikimedia_Commons";
|
|
||||||
private static final String MOBILE_UPLOADS_CATEGORY = "Uploaded_with_Mobile/Android";
|
|
||||||
private static final String EXPLORE_MAP = "Map";
|
|
||||||
private static final String MEDIA_DETAILS_FRAGMENT_TAG = "MediaDetailsFragment";
|
|
||||||
|
|
||||||
|
|
||||||
public FragmentExploreBinding binding;
|
|
||||||
ViewPagerAdapter viewPagerAdapter;
|
|
||||||
private ExploreListRootFragment featuredRootFragment;
|
|
||||||
private ExploreListRootFragment mobileRootFragment;
|
|
||||||
private ExploreMapRootFragment mapRootFragment;
|
|
||||||
@Inject
|
|
||||||
@Named("default_preferences")
|
|
||||||
public JsonKvStore applicationKvStore;
|
|
||||||
|
|
||||||
// Nearby map state (for if we came from Nearby fragment)
|
|
||||||
private double prevZoom;
|
|
||||||
private double prevLatitude;
|
|
||||||
private double prevLongitude;
|
|
||||||
|
|
||||||
public void setScroll(boolean canScroll) {
|
|
||||||
if (binding != null) {
|
|
||||||
binding.viewPager.setCanScroll(canScroll);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public static ExploreFragment newInstance() {
|
|
||||||
ExploreFragment fragment = new ExploreFragment();
|
|
||||||
fragment.setRetainInstance(true);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
|
||||||
@Nullable Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
loadNearbyMapData();
|
|
||||||
binding = FragmentExploreBinding.inflate(inflater, container, false);
|
|
||||||
|
|
||||||
viewPagerAdapter = new ViewPagerAdapter(requireContext(), getChildFragmentManager(),
|
|
||||||
FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
|
||||||
|
|
||||||
binding.viewPager.setAdapter(viewPagerAdapter);
|
|
||||||
binding.viewPager.setId(R.id.viewPager);
|
|
||||||
binding.tabLayout.setupWithViewPager(binding.viewPager);
|
|
||||||
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
|
|
||||||
@Override
|
|
||||||
public void onPageScrolled(int position, float positionOffset,
|
|
||||||
int positionOffsetPixels) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPageSelected(int position) {
|
|
||||||
if (position == 2) {
|
|
||||||
binding.viewPager.setCanScroll(false);
|
|
||||||
} else {
|
|
||||||
binding.viewPager.setCanScroll(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPageScrollStateChanged(int state) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setTabs();
|
|
||||||
setHasOptionsMenu(true);
|
|
||||||
|
|
||||||
// if we came from 'Show in Explore' in Nearby, jump to Map tab
|
|
||||||
if (isCameFromNearbyMap()) {
|
|
||||||
binding.viewPager.setCurrentItem(2);
|
|
||||||
}
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the titles in the tabLayout and fragments in the viewPager
|
|
||||||
*/
|
|
||||||
public void setTabs() {
|
|
||||||
Bundle featuredArguments = new Bundle();
|
|
||||||
featuredArguments.putString("categoryName", FEATURED_IMAGES_CATEGORY);
|
|
||||||
|
|
||||||
Bundle mobileArguments = new Bundle();
|
|
||||||
mobileArguments.putString("categoryName", MOBILE_UPLOADS_CATEGORY);
|
|
||||||
|
|
||||||
Bundle mapArguments = new Bundle();
|
|
||||||
mapArguments.putString("categoryName", EXPLORE_MAP);
|
|
||||||
|
|
||||||
// if we came from 'Show in Explore' in Nearby, pass on zoom and center to Explore map root
|
|
||||||
if (isCameFromNearbyMap()) {
|
|
||||||
mapArguments.putDouble("prev_zoom", prevZoom);
|
|
||||||
mapArguments.putDouble("prev_latitude", prevLatitude);
|
|
||||||
mapArguments.putDouble("prev_longitude", prevLongitude);
|
|
||||||
}
|
|
||||||
|
|
||||||
featuredRootFragment = new ExploreListRootFragment(featuredArguments);
|
|
||||||
mobileRootFragment = new ExploreListRootFragment(mobileArguments);
|
|
||||||
mapRootFragment = new ExploreMapRootFragment(mapArguments);
|
|
||||||
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
|
|
||||||
|
|
||||||
viewPagerAdapter.setTabs(
|
|
||||||
pairOf(R.string.explore_tab_title_featured, featuredRootFragment),
|
|
||||||
pairOf(R.string.explore_tab_title_mobile, mobileRootFragment),
|
|
||||||
pairOf(R.string.explore_tab_title_map, mapRootFragment)
|
|
||||||
);
|
|
||||||
viewPagerAdapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch Nearby map camera data from fragment arguments if any.
|
|
||||||
*/
|
|
||||||
public void loadNearbyMapData() {
|
|
||||||
// get fragment arguments
|
|
||||||
if (getArguments() != null) {
|
|
||||||
prevZoom = getArguments().getDouble("prev_zoom");
|
|
||||||
prevLatitude = getArguments().getDouble("prev_latitude");
|
|
||||||
prevLongitude = getArguments().getDouble("prev_longitude");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if fragment arguments contain data from Nearby map. if present, then the user
|
|
||||||
* navigated from Nearby using 'Show in Explore'.
|
|
||||||
*
|
|
||||||
* @return true if user navigated from Nearby map
|
|
||||||
**/
|
|
||||||
public boolean isCameFromNearbyMap() {
|
|
||||||
return prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean onBackPressed() {
|
|
||||||
if (binding.tabLayout.getSelectedTabPosition() == 0) {
|
|
||||||
if (featuredRootFragment.backPressed()) {
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar()
|
|
||||||
.setDisplayHomeAsUpEnabled(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else if (binding.tabLayout.getSelectedTabPosition() == 1) { //Mobile root fragment
|
|
||||||
if (mobileRootFragment.backPressed()) {
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar()
|
|
||||||
.setDisplayHomeAsUpEnabled(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} else { //explore map fragment
|
|
||||||
if (mapRootFragment.backPressed()) {
|
|
||||||
((BaseActivity) getActivity()).getSupportActionBar()
|
|
||||||
.setDisplayHomeAsUpEnabled(false);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method inflates the menu in the toolbar
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
||||||
// if logged in 'Show in Nearby' menu item is visible
|
|
||||||
if (applicationKvStore.getBoolean("login_skipped") == false) {
|
|
||||||
inflater.inflate(R.menu.explore_fragment_menu, menu);
|
|
||||||
|
|
||||||
MenuItem others = menu.findItem(R.id.list_item_show_in_nearby);
|
|
||||||
|
|
||||||
if (binding.viewPager.getCurrentItem() == 2) {
|
|
||||||
others.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if on Map tab, show all menu options, else only show search
|
|
||||||
binding.viewPager.addOnPageChangeListener(new OnPageChangeListener() {
|
|
||||||
@Override
|
|
||||||
public void onPageScrolled(int position, float positionOffset,
|
|
||||||
int positionOffsetPixels) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPageSelected(int position) {
|
|
||||||
others.setVisible((position == 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPageScrollStateChanged(int state) {
|
|
||||||
if (state == SCROLL_STATE_IDLE && binding.viewPager.getCurrentItem() == 2) {
|
|
||||||
onPageSelected(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
inflater.inflate(R.menu.menu_search, menu);
|
|
||||||
}
|
|
||||||
super.onCreateOptionsMenu(menu, inflater);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method handles the logic on ItemSelect in toolbar menu Currently only 1 choice is
|
|
||||||
* available to open search page of the app
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
|
|
||||||
// Handle item selection
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case R.id.action_search:
|
|
||||||
ActivityUtils.startActivityWithFlags(getActivity(), SearchActivity.class);
|
|
||||||
return true;
|
|
||||||
case R.id.list_item_show_in_nearby:
|
|
||||||
mapRootFragment.loadNearbyMapFromExplore();
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
227
app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt
Normal file
227
app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.kt
Normal file
|
|
@ -0,0 +1,227 @@
|
||||||
|
package fr.free.nrw.commons.explore
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuInflater
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.FragmentPagerAdapter
|
||||||
|
import androidx.viewpager.widget.ViewPager
|
||||||
|
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.ViewPagerAdapter
|
||||||
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentExploreBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.ActivityUtils.startActivityWithFlags
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
|
||||||
|
class ExploreFragment : CommonsDaggerSupportFragment() {
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
@Named("default_preferences")
|
||||||
|
var applicationKvStore: JsonKvStore? = null
|
||||||
|
|
||||||
|
private var featuredRootFragment: ExploreListRootFragment? = null
|
||||||
|
private var mobileRootFragment: ExploreListRootFragment? = null
|
||||||
|
private var mapRootFragment: ExploreMapRootFragment? = null
|
||||||
|
private var prevZoom = 0.0
|
||||||
|
private var prevLatitude = 0.0
|
||||||
|
private var prevLongitude = 0.0
|
||||||
|
private var viewPagerAdapter: ViewPagerAdapter? = null
|
||||||
|
var binding: FragmentExploreBinding? = null
|
||||||
|
|
||||||
|
fun setScroll(canScroll: Boolean) {
|
||||||
|
if (binding != null) {
|
||||||
|
binding!!.viewPager.canScroll = canScroll
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
loadNearbyMapData()
|
||||||
|
binding = FragmentExploreBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
|
viewPagerAdapter = ViewPagerAdapter(
|
||||||
|
requireContext(), childFragmentManager,
|
||||||
|
FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
|
||||||
|
)
|
||||||
|
|
||||||
|
binding!!.viewPager.adapter = viewPagerAdapter
|
||||||
|
binding!!.viewPager.id = R.id.viewPager
|
||||||
|
binding!!.tabLayout.setupWithViewPager(binding!!.viewPager)
|
||||||
|
binding!!.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
|
||||||
|
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
|
||||||
|
override fun onPageScrollStateChanged(state: Int) = Unit
|
||||||
|
override fun onPageSelected(position: Int) {
|
||||||
|
binding!!.viewPager.canScroll = position != 2
|
||||||
|
if (position == 2) {
|
||||||
|
mapRootFragment?.requestLocationIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
setTabs()
|
||||||
|
setHasOptionsMenu(true)
|
||||||
|
|
||||||
|
// if we came from 'Show in Explore' in Nearby, jump to Map tab
|
||||||
|
if (isCameFromNearbyMap) {
|
||||||
|
binding!!.viewPager.currentItem = 2
|
||||||
|
}
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the titles in the tabLayout and fragments in the viewPager
|
||||||
|
*/
|
||||||
|
fun setTabs() {
|
||||||
|
val featuredArguments = Bundle()
|
||||||
|
featuredArguments.putString("categoryName", FEATURED_IMAGES_CATEGORY)
|
||||||
|
|
||||||
|
val mobileArguments = Bundle()
|
||||||
|
mobileArguments.putString("categoryName", MOBILE_UPLOADS_CATEGORY)
|
||||||
|
|
||||||
|
val mapArguments = Bundle()
|
||||||
|
mapArguments.putString("categoryName", EXPLORE_MAP)
|
||||||
|
|
||||||
|
// if we came from 'Show in Explore' in Nearby, pass on zoom and center to Explore map root
|
||||||
|
if (isCameFromNearbyMap) {
|
||||||
|
mapArguments.putDouble("prev_zoom", prevZoom)
|
||||||
|
mapArguments.putDouble("prev_latitude", prevLatitude)
|
||||||
|
mapArguments.putDouble("prev_longitude", prevLongitude)
|
||||||
|
}
|
||||||
|
|
||||||
|
featuredRootFragment = ExploreListRootFragment(featuredArguments)
|
||||||
|
mobileRootFragment = ExploreListRootFragment(mobileArguments)
|
||||||
|
mapRootFragment = ExploreMapRootFragment(mapArguments)
|
||||||
|
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
(activity as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
|
||||||
|
viewPagerAdapter!!.setTabs(
|
||||||
|
R.string.explore_tab_title_featured to featuredRootFragment!!,
|
||||||
|
R.string.explore_tab_title_mobile to mobileRootFragment!!,
|
||||||
|
R.string.explore_tab_title_map to mapRootFragment!!
|
||||||
|
)
|
||||||
|
viewPagerAdapter!!.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch Nearby map camera data from fragment arguments if any.
|
||||||
|
*/
|
||||||
|
private fun loadNearbyMapData() {
|
||||||
|
// get fragment arguments
|
||||||
|
if (arguments != null) {
|
||||||
|
with (requireArguments()) {
|
||||||
|
prevZoom = getDouble("prev_zoom")
|
||||||
|
prevLatitude = getDouble("prev_latitude")
|
||||||
|
prevLongitude = getDouble("prev_longitude")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if fragment arguments contain data from Nearby map. if present, then the user
|
||||||
|
* navigated from Nearby using 'Show in Explore'.
|
||||||
|
*
|
||||||
|
* @return true if user navigated from Nearby map
|
||||||
|
*/
|
||||||
|
private val isCameFromNearbyMap: Boolean
|
||||||
|
get() = prevZoom != 0.0 || prevLatitude != 0.0 || prevLongitude != 0.0
|
||||||
|
|
||||||
|
fun onBackPressed(): Boolean {
|
||||||
|
if (binding!!.tabLayout.selectedTabPosition == 0) {
|
||||||
|
if (featuredRootFragment!!.backPressed()) {
|
||||||
|
(activity as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else if (binding!!.tabLayout.selectedTabPosition == 1) { //Mobile root fragment
|
||||||
|
if (mobileRootFragment!!.backPressed()) {
|
||||||
|
(activity as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else { //explore map fragment
|
||||||
|
if (mapRootFragment!!.backPressed()) {
|
||||||
|
(activity as BaseActivity).supportActionBar!!.setDisplayHomeAsUpEnabled(false)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method inflates the menu in the toolbar
|
||||||
|
*/
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
|
// if logged in 'Show in Nearby' menu item is visible
|
||||||
|
if (applicationKvStore!!.getBoolean("login_skipped") == false) {
|
||||||
|
inflater.inflate(R.menu.explore_fragment_menu, menu)
|
||||||
|
|
||||||
|
val others = menu.findItem(R.id.list_item_show_in_nearby)
|
||||||
|
|
||||||
|
if (binding!!.viewPager.currentItem == 2) {
|
||||||
|
others.setVisible(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if on Map tab, show all menu options, else only show search
|
||||||
|
binding!!.viewPager.addOnPageChangeListener(object : OnPageChangeListener {
|
||||||
|
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
|
||||||
|
override fun onPageScrollStateChanged(state: Int) = Unit
|
||||||
|
override fun onPageSelected(position: Int) {
|
||||||
|
binding!!.viewPager.canScroll = position != 2
|
||||||
|
others.setVisible(position == 2)
|
||||||
|
if (position == 2) {
|
||||||
|
mapRootFragment?.requestLocationIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
inflater.inflate(R.menu.menu_search, menu)
|
||||||
|
}
|
||||||
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method handles the logic on ItemSelect in toolbar menu Currently only 1 choice is
|
||||||
|
* available to open search page of the app
|
||||||
|
*/
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
// Handle item selection
|
||||||
|
when (item.itemId) {
|
||||||
|
R.id.action_search -> {
|
||||||
|
startActivityWithFlags(requireActivity(), SearchActivity::class.java)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.list_item_show_in_nearby -> {
|
||||||
|
mapRootFragment!!.loadNearbyMapFromExplore()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> return super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val FEATURED_IMAGES_CATEGORY = "Featured_pictures_on_Wikimedia_Commons"
|
||||||
|
private const val MOBILE_UPLOADS_CATEGORY = "Uploaded_with_Mobile/Android"
|
||||||
|
private const val EXPLORE_MAP = "Map"
|
||||||
|
|
||||||
|
fun newInstance(): ExploreFragment = ExploreFragment().apply {
|
||||||
|
retainInstance = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,215 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.explore.categories.media.CategoriesMediaFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider;
|
|
||||||
import fr.free.nrw.commons.navtab.NavTab;
|
|
||||||
|
|
||||||
public class ExploreListRootFragment extends CommonsDaggerSupportFragment implements
|
|
||||||
MediaDetailProvider, CategoryImagesCallback {
|
|
||||||
|
|
||||||
private MediaDetailPagerFragment mediaDetails;
|
|
||||||
private CategoriesMediaFragment listFragment;
|
|
||||||
|
|
||||||
private FragmentFeaturedRootBinding binding;
|
|
||||||
|
|
||||||
public ExploreListRootFragment() {
|
|
||||||
//empty constructor necessary otherwise crashes on recreate
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExploreListRootFragment(Bundle bundle) {
|
|
||||||
String title = bundle.getString("categoryName");
|
|
||||||
listFragment = new CategoriesMediaFragment();
|
|
||||||
Bundle featuredArguments = new Bundle();
|
|
||||||
featuredArguments.putString("categoryName", title);
|
|
||||||
listFragment.setArguments(featuredArguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
||||||
@Nullable final ViewGroup container,
|
|
||||||
@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false);
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
if (savedInstanceState == null) {
|
|
||||||
setFragment(listFragment, mediaDetails);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFragment(Fragment fragment, Fragment otherFragment) {
|
|
||||||
if (fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (fragment.isAdded() && otherFragment == null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.add(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded()) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeFragment(Fragment fragment) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.remove(fragment)
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAttach(final Context context) {
|
|
||||||
super.onAttach(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMediaClicked(int position) {
|
|
||||||
if (binding!=null) {
|
|
||||||
binding.exploreContainer.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
if (((ExploreFragment) getParentFragment()).binding!=null) {
|
|
||||||
((ExploreFragment) getParentFragment()).binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
mediaDetails = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
((ExploreFragment) getParentFragment()).setScroll(false);
|
|
||||||
setFragment(mediaDetails, listFragment);
|
|
||||||
mediaDetails.showImage(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
|
||||||
*
|
|
||||||
* @param i It is the index of which media object is to be returned which is same as current
|
|
||||||
* index of viewPager.
|
|
||||||
* @return Media Object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Media getMediaAtPosition(int i) {
|
|
||||||
if (listFragment != null) {
|
|
||||||
return listFragment.getMediaAtPosition(i);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
|
||||||
* same number of media items as that of media elements in adapter.
|
|
||||||
*
|
|
||||||
* @return Total Media count in the adapter
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTotalMediaCount() {
|
|
||||||
if (listFragment != null) {
|
|
||||||
return listFragment.getTotalMediaCount();
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getContributionStateAt(int position) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload media detail fragment once media is nominated
|
|
||||||
*
|
|
||||||
* @param index item position that has been nominated
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void refreshNominatedMedia(int index) {
|
|
||||||
if (mediaDetails != null && !listFragment.isVisible()) {
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
onMediaClicked(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on success of API call for featured images or mobile uploads. The
|
|
||||||
* viewpager will notified that number of items have changed.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void viewPagerNotifyDataSetChanged() {
|
|
||||||
if (mediaDetails != null) {
|
|
||||||
mediaDetails.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs back pressed action on the fragment. Return true if the event was handled by the
|
|
||||||
* mediaDetails otherwise returns false.
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public boolean backPressed() {
|
|
||||||
if (null != mediaDetails && mediaDetails.isVisible()) {
|
|
||||||
if (((ExploreFragment) getParentFragment()).binding != null) {
|
|
||||||
((ExploreFragment) getParentFragment()).binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
((ExploreFragment) getParentFragment()).setScroll(true);
|
|
||||||
setFragment(listFragment, mediaDetails);
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
if (((MainActivity) getActivity()) != null) {
|
|
||||||
((MainActivity) getActivity()).setSelectedItemId(NavTab.CONTRIBUTIONS.code());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (((MainActivity) getActivity()) != null) {
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,182 @@
|
||||||
|
package fr.free.nrw.commons.explore
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.core.os.bundleOf
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback
|
||||||
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.explore.categories.media.CategoriesMediaFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
|
import fr.free.nrw.commons.navtab.NavTab
|
||||||
|
|
||||||
|
class ExploreListRootFragment : CommonsDaggerSupportFragment, MediaDetailProvider,
|
||||||
|
CategoryImagesCallback {
|
||||||
|
private var mediaDetails: MediaDetailPagerFragment? = null
|
||||||
|
private var listFragment: CategoriesMediaFragment? = null
|
||||||
|
private var binding: FragmentFeaturedRootBinding? = null
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
|
||||||
|
constructor(bundle: Bundle) {
|
||||||
|
listFragment = CategoriesMediaFragment().apply {
|
||||||
|
arguments = bundleOf(
|
||||||
|
"categoryName" to bundle.getString("categoryName")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false)
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
if (savedInstanceState == null) {
|
||||||
|
setFragment(listFragment!!, mediaDetails)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFragment(fragment: Fragment, otherFragment: Fragment?) {
|
||||||
|
if (fragment.isAdded && otherFragment != null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (fragment.isAdded && otherFragment == null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded && otherFragment != null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.add(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeFragment(fragment: Fragment) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.remove(fragment)
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMediaClicked(position: Int) {
|
||||||
|
if (binding != null) {
|
||||||
|
binding!!.exploreContainer.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
if ((parentFragment as ExploreFragment).binding != null) {
|
||||||
|
(parentFragment as ExploreFragment).binding!!.tabLayout.visibility =
|
||||||
|
View.GONE
|
||||||
|
}
|
||||||
|
mediaDetails = MediaDetailPagerFragment.newInstance(false, true)
|
||||||
|
(parentFragment as ExploreFragment).setScroll(false)
|
||||||
|
setFragment(mediaDetails!!, listFragment)
|
||||||
|
mediaDetails!!.showImage(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
||||||
|
*
|
||||||
|
* @param i It is the index of which media object is to be returned which is same as current
|
||||||
|
* index of viewPager.
|
||||||
|
* @return Media Object
|
||||||
|
*/
|
||||||
|
override fun getMediaAtPosition(i: Int): Media? = listFragment?.getMediaAtPosition(i)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
||||||
|
* same number of media items as that of media elements in adapter.
|
||||||
|
*
|
||||||
|
* @return Total Media count in the adapter
|
||||||
|
*/
|
||||||
|
override fun getTotalMediaCount(): Int = listFragment?.getTotalMediaCount() ?: 0
|
||||||
|
|
||||||
|
override fun getContributionStateAt(position: Int): Int? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload media detail fragment once media is nominated
|
||||||
|
*
|
||||||
|
* @param index item position that has been nominated
|
||||||
|
*/
|
||||||
|
override fun refreshNominatedMedia(index: Int) {
|
||||||
|
if (mediaDetails != null && !listFragment!!.isVisible) {
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
onMediaClicked(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on success of API call for featured images or mobile uploads. The
|
||||||
|
* viewpager will notified that number of items have changed.
|
||||||
|
*/
|
||||||
|
override fun viewPagerNotifyDataSetChanged() {
|
||||||
|
mediaDetails?.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs back pressed action on the fragment. Return true if the event was handled by the
|
||||||
|
* mediaDetails otherwise returns false.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
fun backPressed(): Boolean {
|
||||||
|
if (null != mediaDetails && mediaDetails!!.isVisible) {
|
||||||
|
if ((parentFragment as ExploreFragment).binding != null) {
|
||||||
|
(parentFragment as ExploreFragment).binding!!.tabLayout.visibility =
|
||||||
|
View.VISIBLE
|
||||||
|
}
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
(parentFragment as ExploreFragment).setScroll(true)
|
||||||
|
setFragment(listFragment!!, mediaDetails)
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
if ((activity as MainActivity?) != null) {
|
||||||
|
(activity as MainActivity).setSelectedItemId(NavTab.CONTRIBUTIONS.code())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((activity as MainActivity?) != null) {
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,239 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.contributions.MainActivity;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.explore.map.ExploreMapFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider;
|
|
||||||
import fr.free.nrw.commons.navtab.NavTab;
|
|
||||||
|
|
||||||
public class ExploreMapRootFragment extends CommonsDaggerSupportFragment implements
|
|
||||||
MediaDetailProvider, CategoryImagesCallback {
|
|
||||||
|
|
||||||
private MediaDetailPagerFragment mediaDetails;
|
|
||||||
private ExploreMapFragment mapFragment;
|
|
||||||
|
|
||||||
private FragmentFeaturedRootBinding binding;
|
|
||||||
|
|
||||||
public ExploreMapRootFragment() {
|
|
||||||
//empty constructor necessary otherwise crashes on recreate
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
public static ExploreMapRootFragment newInstance() {
|
|
||||||
ExploreMapRootFragment fragment = new ExploreMapRootFragment();
|
|
||||||
fragment.setRetainInstance(true);
|
|
||||||
return fragment;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExploreMapRootFragment(Bundle bundle) {
|
|
||||||
// get fragment arguments
|
|
||||||
String title = bundle.getString("categoryName");
|
|
||||||
double zoom = bundle.getDouble("prev_zoom");
|
|
||||||
double latitude = bundle.getDouble("prev_latitude");
|
|
||||||
double longitude = bundle.getDouble("prev_longitude");
|
|
||||||
|
|
||||||
mapFragment = new ExploreMapFragment();
|
|
||||||
Bundle featuredArguments = new Bundle();
|
|
||||||
featuredArguments.putString("categoryName", title);
|
|
||||||
|
|
||||||
// if we came from 'Show in Explore' in Nearby, pass on zoom and center
|
|
||||||
if (zoom != 0.0 || latitude != 0.0 || longitude != 0.0) {
|
|
||||||
featuredArguments.putDouble("prev_zoom", zoom);
|
|
||||||
featuredArguments.putDouble("prev_latitude", latitude);
|
|
||||||
featuredArguments.putDouble("prev_longitude", longitude);
|
|
||||||
}
|
|
||||||
mapFragment.setArguments(featuredArguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public View onCreateView(@NonNull final LayoutInflater inflater,
|
|
||||||
@Nullable final ViewGroup container,
|
|
||||||
@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false);
|
|
||||||
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
if (savedInstanceState == null) {
|
|
||||||
setFragment(mapFragment, mediaDetails);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFragment(Fragment fragment, Fragment otherFragment) {
|
|
||||||
if (fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (fragment.isAdded() && otherFragment == null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.show(fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded() && otherFragment != null) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(otherFragment)
|
|
||||||
.add(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
} else if (!fragment.isAdded()) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.explore_container, fragment)
|
|
||||||
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeFragment(Fragment fragment) {
|
|
||||||
getChildFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.remove(fragment)
|
|
||||||
.commit();
|
|
||||||
getChildFragmentManager().executePendingTransactions();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAttach(final Context context) {
|
|
||||||
super.onAttach(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onMediaClicked(int position) {
|
|
||||||
binding.exploreContainer.setVisibility(View.VISIBLE);
|
|
||||||
((ExploreFragment) getParentFragment()).binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
mediaDetails = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
((ExploreFragment) getParentFragment()).setScroll(false);
|
|
||||||
setFragment(mediaDetails, mapFragment);
|
|
||||||
mediaDetails.showImage(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
|
||||||
*
|
|
||||||
* @param i It is the index of which media object is to be returned which is same as current
|
|
||||||
* index of viewPager.
|
|
||||||
* @return Media Object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Media getMediaAtPosition(int i) {
|
|
||||||
if (mapFragment != null && mapFragment.mediaList != null) {
|
|
||||||
return mapFragment.mediaList.get(i);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
|
||||||
* same number of media items as that of media elements in adapter.
|
|
||||||
*
|
|
||||||
* @return Total Media count in the adapter
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTotalMediaCount() {
|
|
||||||
if (mapFragment != null && mapFragment.mediaList != null) {
|
|
||||||
return mapFragment.mediaList.size();
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getContributionStateAt(int position) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload media detail fragment once media is nominated
|
|
||||||
*
|
|
||||||
* @param index item position that has been nominated
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void refreshNominatedMedia(int index) {
|
|
||||||
if (mediaDetails != null && !mapFragment.isVisible()) {
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
onMediaClicked(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on success of API call for featured images or mobile uploads. The
|
|
||||||
* viewpager will notified that number of items have changed.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void viewPagerNotifyDataSetChanged() {
|
|
||||||
if (mediaDetails != null) {
|
|
||||||
mediaDetails.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs back pressed action on the fragment. Return true if the event was handled by the
|
|
||||||
* mediaDetails otherwise returns false.
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public boolean backPressed() {
|
|
||||||
if (null != mediaDetails && mediaDetails.isVisible()) {
|
|
||||||
((ExploreFragment) getParentFragment()).binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
removeFragment(mediaDetails);
|
|
||||||
((ExploreFragment) getParentFragment()).setScroll(true);
|
|
||||||
setFragment(mapFragment, mediaDetails);
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
|
||||||
if (mapFragment != null && mapFragment.isVisible()) {
|
|
||||||
if (mapFragment.backButtonClicked()) {
|
|
||||||
// Explore map fragment handled the event no further action required.
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
((MainActivity) getActivity()).setSelectedItemId(NavTab.CONTRIBUTIONS.code());
|
|
||||||
}
|
|
||||||
((MainActivity) getActivity()).showTabs();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadNearbyMapFromExplore() {
|
|
||||||
mapFragment.loadNearbyMapFromExplore();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,212 @@
|
||||||
|
package fr.free.nrw.commons.explore
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.core.os.bundleOf
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback
|
||||||
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentFeaturedRootBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.explore.map.ExploreMapFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
|
import fr.free.nrw.commons.navtab.NavTab
|
||||||
|
|
||||||
|
class ExploreMapRootFragment : CommonsDaggerSupportFragment, MediaDetailProvider,
|
||||||
|
CategoryImagesCallback {
|
||||||
|
private var mediaDetails: MediaDetailPagerFragment? = null
|
||||||
|
private var mapFragment: ExploreMapFragment? = null
|
||||||
|
private var binding: FragmentFeaturedRootBinding? = null
|
||||||
|
|
||||||
|
constructor()
|
||||||
|
|
||||||
|
constructor(bundle: Bundle) {
|
||||||
|
// get fragment arguments
|
||||||
|
val title = bundle.getString("categoryName")
|
||||||
|
val zoom = bundle.getDouble("prev_zoom")
|
||||||
|
val latitude = bundle.getDouble("prev_latitude")
|
||||||
|
val longitude = bundle.getDouble("prev_longitude")
|
||||||
|
|
||||||
|
mapFragment = ExploreMapFragment()
|
||||||
|
val featuredArguments = bundleOf(
|
||||||
|
"categoryName" to title
|
||||||
|
)
|
||||||
|
|
||||||
|
// if we came from 'Show in Explore' in Nearby, pass on zoom and center
|
||||||
|
if (zoom != 0.0 || latitude != 0.0 || longitude != 0.0) {
|
||||||
|
featuredArguments.putDouble("prev_zoom", zoom)
|
||||||
|
featuredArguments.putDouble("prev_latitude", latitude)
|
||||||
|
featuredArguments.putDouble("prev_longitude", longitude)
|
||||||
|
}
|
||||||
|
mapFragment!!.arguments = featuredArguments
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
binding = FragmentFeaturedRootBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
if (savedInstanceState == null) {
|
||||||
|
setFragment(mapFragment!!, mediaDetails)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFragment(fragment: Fragment, otherFragment: Fragment?) {
|
||||||
|
if (fragment.isAdded && otherFragment != null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (fragment.isAdded && otherFragment == null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.show(fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded && otherFragment != null) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(otherFragment)
|
||||||
|
.add(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
} else if (!fragment.isAdded) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.explore_container, fragment)
|
||||||
|
.addToBackStack("CONTRIBUTION_LIST_FRAGMENT_TAG")
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeFragment(fragment: Fragment) {
|
||||||
|
childFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.remove(fragment)
|
||||||
|
.commit()
|
||||||
|
childFragmentManager.executePendingTransactions()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMediaClicked(position: Int) {
|
||||||
|
binding!!.exploreContainer.visibility = View.VISIBLE
|
||||||
|
(parentFragment as ExploreFragment).binding!!.tabLayout.visibility = View.GONE
|
||||||
|
mediaDetails = MediaDetailPagerFragment.newInstance(false, true)
|
||||||
|
(parentFragment as ExploreFragment).setScroll(false)
|
||||||
|
setFragment(mediaDetails!!, mapFragment)
|
||||||
|
mediaDetails!!.showImage(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
||||||
|
*
|
||||||
|
* @param i It is the index of which media object is to be returned which is same as current
|
||||||
|
* index of viewPager.
|
||||||
|
* @return Media Object
|
||||||
|
*/
|
||||||
|
override fun getMediaAtPosition(i: Int): Media? = mapFragment?.mediaList?.get(i)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on from getCount of MediaDetailPagerFragment The viewpager will contain
|
||||||
|
* same number of media items as that of media elements in adapter.
|
||||||
|
*
|
||||||
|
* @return Total Media count in the adapter
|
||||||
|
*/
|
||||||
|
override fun getTotalMediaCount(): Int = mapFragment?.mediaList?.size ?: 0
|
||||||
|
|
||||||
|
override fun getContributionStateAt(position: Int): Int? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload media detail fragment once media is nominated
|
||||||
|
*
|
||||||
|
* @param index item position that has been nominated
|
||||||
|
*/
|
||||||
|
override fun refreshNominatedMedia(index: Int) {
|
||||||
|
if (mediaDetails != null && !mapFragment!!.isVisible) {
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
onMediaClicked(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on success of API call for featured images or mobile uploads. The
|
||||||
|
* viewpager will notified that number of items have changed.
|
||||||
|
*/
|
||||||
|
override fun viewPagerNotifyDataSetChanged() {
|
||||||
|
mediaDetails?.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs back pressed action on the fragment. Return true if the event was handled by the
|
||||||
|
* mediaDetails otherwise returns false.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
fun backPressed(): Boolean {
|
||||||
|
if (null != mediaDetails && mediaDetails!!.isVisible) {
|
||||||
|
(parentFragment as ExploreFragment).binding!!.tabLayout.visibility = View.VISIBLE
|
||||||
|
removeFragment(mediaDetails!!)
|
||||||
|
(parentFragment as ExploreFragment).setScroll(true)
|
||||||
|
setFragment(mapFragment!!, mediaDetails)
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (mapFragment != null && mapFragment!!.isVisible) {
|
||||||
|
if (mapFragment!!.backButtonClicked()) {
|
||||||
|
// Explore map fragment handled the event no further action required.
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(activity as MainActivity).setSelectedItemId(NavTab.CONTRIBUTIONS.code())
|
||||||
|
}
|
||||||
|
(activity as MainActivity).showTabs()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadNearbyMapFromExplore() = mapFragment?.loadNearbyMapFromExplore()
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun requestLocationIfNeeded() {
|
||||||
|
mapFragment?.requestLocationIfNeeded()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
|
||||||
|
super.setUserVisibleHint(isVisibleToUser)
|
||||||
|
if (isVisibleToUser) {
|
||||||
|
requestLocationIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun newInstance(): ExploreMapRootFragment = ExploreMapRootFragment().apply {
|
||||||
|
retainInstance = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,66 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.AttributeSet;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import androidx.viewpager.widget.ViewPager;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ParentViewPager A custom viewPager whose scrolling can be enabled and disabled.
|
|
||||||
*/
|
|
||||||
public class ParentViewPager extends ViewPager {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Boolean variable that stores the current state of pager scroll i.e(enabled or disabled)
|
|
||||||
*/
|
|
||||||
private boolean canScroll = true;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default constructors
|
|
||||||
*/
|
|
||||||
public ParentViewPager(Context context) {
|
|
||||||
super(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParentViewPager(Context context, AttributeSet attrs) {
|
|
||||||
super(context, attrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter method for canScroll.
|
|
||||||
*/
|
|
||||||
public void setCanScroll(boolean canScroll) {
|
|
||||||
this.canScroll = canScroll;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter method for canScroll.
|
|
||||||
*/
|
|
||||||
public boolean isCanScroll() {
|
|
||||||
return canScroll;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method that prevents scrolling if canScroll is set to false.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean onTouchEvent(MotionEvent ev) {
|
|
||||||
return canScroll && super.onTouchEvent(ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A facilitator method that allows parent to intercept touch events before its children. thus
|
|
||||||
* making it possible to prevent swiping parent on child end.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
|
||||||
return canScroll && super.onInterceptTouchEvent(ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package fr.free.nrw.commons.explore
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.view.MotionEvent
|
||||||
|
import androidx.viewpager.widget.ViewPager
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ParentViewPager A custom viewPager whose scrolling can be enabled and disabled.
|
||||||
|
*/
|
||||||
|
class ParentViewPager : ViewPager {
|
||||||
|
var canScroll: Boolean = true
|
||||||
|
|
||||||
|
constructor(context: Context) : super(context)
|
||||||
|
|
||||||
|
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
|
||||||
|
|
||||||
|
override fun onTouchEvent(ev: MotionEvent): Boolean {
|
||||||
|
return canScroll && super.onTouchEvent(ev)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
|
||||||
|
return canScroll && super.onInterceptTouchEvent(ev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,285 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.ViewPagerAdapter.pairOf;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.view.View;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
|
||||||
import com.jakewharton.rxbinding2.view.RxView;
|
|
||||||
import com.jakewharton.rxbinding2.widget.RxSearchView;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.ViewPagerAdapter;
|
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.databinding.ActivitySearchBinding;
|
|
||||||
import fr.free.nrw.commons.explore.categories.search.SearchCategoryFragment;
|
|
||||||
import fr.free.nrw.commons.explore.depictions.search.SearchDepictionsFragment;
|
|
||||||
import fr.free.nrw.commons.explore.media.SearchMediaFragment;
|
|
||||||
import fr.free.nrw.commons.explore.models.RecentSearch;
|
|
||||||
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao;
|
|
||||||
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider;
|
|
||||||
import fr.free.nrw.commons.theme.BaseActivity;
|
|
||||||
import fr.free.nrw.commons.utils.FragmentUtils;
|
|
||||||
import fr.free.nrw.commons.utils.ViewUtil;
|
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents search screen of this app
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class SearchActivity extends BaseActivity
|
|
||||||
implements MediaDetailProvider, CategoryImagesCallback {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
RecentSearchesDao recentSearchesDao;
|
|
||||||
|
|
||||||
private SearchMediaFragment searchMediaFragment;
|
|
||||||
private SearchCategoryFragment searchCategoryFragment;
|
|
||||||
private SearchDepictionsFragment searchDepictionsFragment;
|
|
||||||
private RecentSearchesFragment recentSearchesFragment;
|
|
||||||
private FragmentManager supportFragmentManager;
|
|
||||||
private MediaDetailPagerFragment mediaDetails;
|
|
||||||
ViewPagerAdapter viewPagerAdapter;
|
|
||||||
|
|
||||||
private ActivitySearchBinding binding;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
binding = ActivitySearchBinding.inflate(getLayoutInflater());
|
|
||||||
setContentView(binding.getRoot());
|
|
||||||
|
|
||||||
setTitle(getString(R.string.title_activity_search));
|
|
||||||
setSupportActionBar(binding.toolbarSearch);
|
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
||||||
binding.toolbarSearch.setNavigationOnClickListener(v->onBackPressed());
|
|
||||||
supportFragmentManager = getSupportFragmentManager();
|
|
||||||
setSearchHistoryFragment();
|
|
||||||
viewPagerAdapter = new ViewPagerAdapter(this, getSupportFragmentManager());
|
|
||||||
binding.viewPager.setAdapter(viewPagerAdapter);
|
|
||||||
binding.viewPager.setOffscreenPageLimit(2); // Because we want all the fragments to be alive
|
|
||||||
binding.tabLayout.setupWithViewPager(binding.viewPager);
|
|
||||||
setTabs();
|
|
||||||
binding.searchBox.setQueryHint(getString(R.string.search_commons));
|
|
||||||
binding.searchBox.onActionViewExpanded();
|
|
||||||
binding.searchBox.clearFocus();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method sets the search history fragment.
|
|
||||||
* Search history fragment is displayed when query is empty.
|
|
||||||
*/
|
|
||||||
private void setSearchHistoryFragment() {
|
|
||||||
recentSearchesFragment = new RecentSearchesFragment();
|
|
||||||
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
|
|
||||||
transaction.add(R.id.searchHistoryContainer, recentSearchesFragment).commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the titles in the tabLayout and fragments in the viewPager
|
|
||||||
*/
|
|
||||||
public void setTabs() {
|
|
||||||
searchMediaFragment = new SearchMediaFragment();
|
|
||||||
searchDepictionsFragment = new SearchDepictionsFragment();
|
|
||||||
searchCategoryFragment= new SearchCategoryFragment();
|
|
||||||
|
|
||||||
viewPagerAdapter.setTabs(
|
|
||||||
pairOf(R.string.search_tab_title_media, searchMediaFragment),
|
|
||||||
pairOf(R.string.search_tab_title_categories, searchCategoryFragment),
|
|
||||||
pairOf(R.string.search_tab_title_depictions, searchDepictionsFragment)
|
|
||||||
);
|
|
||||||
viewPagerAdapter.notifyDataSetChanged();
|
|
||||||
getCompositeDisposable().add(RxSearchView.queryTextChanges(binding.searchBox)
|
|
||||||
.takeUntil(RxView.detaches(binding.searchBox))
|
|
||||||
.debounce(500, TimeUnit.MILLISECONDS)
|
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.subscribe(this::handleSearch, Timber::e
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleSearch(final CharSequence query) {
|
|
||||||
if (!TextUtils.isEmpty(query)) {
|
|
||||||
saveRecentSearch(query.toString());
|
|
||||||
binding.viewPager.setVisibility(View.VISIBLE);
|
|
||||||
binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
binding.searchHistoryContainer.setVisibility(View.GONE);
|
|
||||||
|
|
||||||
if (FragmentUtils.isFragmentUIActive(searchDepictionsFragment)) {
|
|
||||||
searchDepictionsFragment.onQueryUpdated(query.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FragmentUtils.isFragmentUIActive(searchMediaFragment)) {
|
|
||||||
searchMediaFragment.onQueryUpdated(query.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FragmentUtils.isFragmentUIActive(searchCategoryFragment)) {
|
|
||||||
searchCategoryFragment.onQueryUpdated(query.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
//Open RecentSearchesFragment
|
|
||||||
recentSearchesFragment.updateRecentSearches();
|
|
||||||
binding.viewPager.setVisibility(View.GONE);
|
|
||||||
binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
setSearchHistoryFragment();
|
|
||||||
binding.searchHistoryContainer.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveRecentSearch(@NonNull final String query) {
|
|
||||||
final RecentSearch recentSearch = recentSearchesDao.find(query);
|
|
||||||
// Newly searched query...
|
|
||||||
if (recentSearch == null) {
|
|
||||||
recentSearchesDao.save(new RecentSearch(null, query, new Date()));
|
|
||||||
} else {
|
|
||||||
recentSearch.setLastSearched(new Date());
|
|
||||||
recentSearchesDao.save(recentSearch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns Media Object at position
|
|
||||||
* @param i position of Media in the imagesRecyclerView adapter.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Media getMediaAtPosition(int i) {
|
|
||||||
return searchMediaFragment.getMediaAtPosition(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns total number of images present in the imagesRecyclerView adapter.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTotalMediaCount() {
|
|
||||||
return searchMediaFragment.getTotalMediaCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getContributionStateAt(int position) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload media detail fragment once media is nominated
|
|
||||||
*
|
|
||||||
* @param index item position that has been nominated
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void refreshNominatedMedia(int index) {
|
|
||||||
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
|
||||||
onBackPressed();
|
|
||||||
onMediaClicked(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on success of API call for image Search.
|
|
||||||
* The viewpager will notified that number of items have changed.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void viewPagerNotifyDataSetChanged() {
|
|
||||||
if (mediaDetails!=null){
|
|
||||||
mediaDetails.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Open media detail pager fragment on click of image in search results
|
|
||||||
* @param index item index that should be opened
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onMediaClicked(int index) {
|
|
||||||
ViewUtil.hideKeyboard(this.findViewById(R.id.searchBox));
|
|
||||||
binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
binding.viewPager.setVisibility(View.GONE);
|
|
||||||
binding.mediaContainer.setVisibility(View.VISIBLE);
|
|
||||||
binding.searchBox.setVisibility(View.GONE);// to remove searchview when mediaDetails fragment open
|
|
||||||
if (mediaDetails == null || !mediaDetails.isVisible()) {
|
|
||||||
// set isFeaturedImage true for featured images, to include author field on media detail
|
|
||||||
mediaDetails = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
supportFragmentManager
|
|
||||||
.beginTransaction()
|
|
||||||
.hide(supportFragmentManager.getFragments().get(supportFragmentManager.getBackStackEntryCount()))
|
|
||||||
.add(R.id.mediaContainer, mediaDetails)
|
|
||||||
.addToBackStack(null)
|
|
||||||
.commit();
|
|
||||||
// Reason for using hide, add instead of replace is to maintain scroll position after
|
|
||||||
// coming back to the search activity. See https://github.com/commons-app/apps-android-commons/issues/1631
|
|
||||||
// https://stackoverflow.com/questions/11353075/how-can-i-maintain-fragment-state-when-added-to-the-back-stack/19022550#19022550
|
|
||||||
supportFragmentManager.executePendingTransactions();
|
|
||||||
}
|
|
||||||
mediaDetails.showImage(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on Screen Rotation
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void onResume() {
|
|
||||||
if (supportFragmentManager.getBackStackEntryCount()==1){
|
|
||||||
//FIXME: Temporary fix for screen rotation inside media details. If we don't call onBackPressed then fragment stack is increasing every time.
|
|
||||||
//FIXME: Similar issue like this https://github.com/commons-app/apps-android-commons/issues/894
|
|
||||||
// This is called on screen rotation when user is inside media details. Ideally it should show Media Details but since we are not saving the state now. We are throwing the user to search screen otherwise the app was crashing.
|
|
||||||
//
|
|
||||||
onBackPressed();
|
|
||||||
}
|
|
||||||
super.onResume();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on backPressed of anyFragment in the activity.
|
|
||||||
* If condition is called when mediaDetailFragment is opened.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onBackPressed() {
|
|
||||||
//Remove the backstack entry that gets added when share button is clicked
|
|
||||||
//fixing:https://github.com/commons-app/apps-android-commons/issues/2296
|
|
||||||
if (getSupportFragmentManager().getBackStackEntryCount() == 2) {
|
|
||||||
supportFragmentManager
|
|
||||||
.beginTransaction()
|
|
||||||
.remove(mediaDetails)
|
|
||||||
.commit();
|
|
||||||
supportFragmentManager.popBackStack();
|
|
||||||
supportFragmentManager.executePendingTransactions();
|
|
||||||
}
|
|
||||||
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
|
||||||
// back to search so show search toolbar and hide navigation toolbar
|
|
||||||
binding.searchBox.setVisibility(View.VISIBLE);//set the searchview
|
|
||||||
binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
binding.viewPager.setVisibility(View.VISIBLE);
|
|
||||||
binding.mediaContainer.setVisibility(View.GONE);
|
|
||||||
} else {
|
|
||||||
binding.toolbarSearch.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
super.onBackPressed();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on click of a recent search to update query in SearchView.
|
|
||||||
* @param query Recent Search Query
|
|
||||||
*/
|
|
||||||
public void updateText(String query) {
|
|
||||||
binding.searchBox.setQuery(query, true);
|
|
||||||
// Clear focus of searchView now. searchView.clearFocus(); does not seem to work Check the below link for more details.
|
|
||||||
// https://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control/15481511
|
|
||||||
binding.viewPager.requestFocus();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override protected void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
//Dispose the disposables when the activity is destroyed
|
|
||||||
getCompositeDisposable().dispose();
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
254
app/src/main/java/fr/free/nrw/commons/explore/SearchActivity.kt
Normal file
254
app/src/main/java/fr/free/nrw/commons/explore/SearchActivity.kt
Normal file
|
|
@ -0,0 +1,254 @@
|
||||||
|
package fr.free.nrw.commons.explore
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.text.TextUtils
|
||||||
|
import android.view.View
|
||||||
|
import androidx.fragment.app.FragmentManager
|
||||||
|
import com.jakewharton.rxbinding2.view.RxView
|
||||||
|
import com.jakewharton.rxbinding2.widget.RxSearchView
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.ViewPagerAdapter
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback
|
||||||
|
import fr.free.nrw.commons.databinding.ActivitySearchBinding
|
||||||
|
import fr.free.nrw.commons.explore.categories.search.SearchCategoryFragment
|
||||||
|
import fr.free.nrw.commons.explore.depictions.search.SearchDepictionsFragment
|
||||||
|
import fr.free.nrw.commons.explore.media.SearchMediaFragment
|
||||||
|
import fr.free.nrw.commons.explore.models.RecentSearch
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.FragmentUtils.isFragmentUIActive
|
||||||
|
import fr.free.nrw.commons.utils.ViewUtil.hideKeyboard
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents search screen of this app
|
||||||
|
*/
|
||||||
|
class SearchActivity : BaseActivity(), MediaDetailProvider, CategoryImagesCallback {
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var recentSearchesDao: RecentSearchesDao? = null
|
||||||
|
|
||||||
|
private var searchMediaFragment: SearchMediaFragment? = null
|
||||||
|
private var searchCategoryFragment: SearchCategoryFragment? = null
|
||||||
|
private var searchDepictionsFragment: SearchDepictionsFragment? = null
|
||||||
|
private var recentSearchesFragment: RecentSearchesFragment? = null
|
||||||
|
private var supportFragmentManager: FragmentManager? = null
|
||||||
|
private var mediaDetails: MediaDetailPagerFragment? = null
|
||||||
|
private var viewPagerAdapter: ViewPagerAdapter? = null
|
||||||
|
private var binding: ActivitySearchBinding? = null
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
binding = ActivitySearchBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.root)
|
||||||
|
setContentView(binding!!.root)
|
||||||
|
|
||||||
|
title = getString(R.string.title_activity_search)
|
||||||
|
setSupportActionBar(binding!!.toolbarSearch)
|
||||||
|
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
|
||||||
|
binding!!.toolbarSearch.setNavigationOnClickListener { onBackPressed() }
|
||||||
|
supportFragmentManager = getSupportFragmentManager()
|
||||||
|
setSearchHistoryFragment()
|
||||||
|
viewPagerAdapter = ViewPagerAdapter(this, getSupportFragmentManager())
|
||||||
|
binding!!.viewPager.adapter = viewPagerAdapter
|
||||||
|
binding!!.viewPager.offscreenPageLimit = 2 // Because we want all the fragments to be alive
|
||||||
|
binding!!.tabLayout.setupWithViewPager(binding!!.viewPager)
|
||||||
|
setTabs()
|
||||||
|
binding!!.searchBox.queryHint = getString(R.string.search_commons)
|
||||||
|
binding!!.searchBox.onActionViewExpanded()
|
||||||
|
binding!!.searchBox.clearFocus()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the search history fragment.
|
||||||
|
* Search history fragment is displayed when query is empty.
|
||||||
|
*/
|
||||||
|
private fun setSearchHistoryFragment() {
|
||||||
|
recentSearchesFragment = RecentSearchesFragment()
|
||||||
|
val transaction = supportFragmentManager!!.beginTransaction()
|
||||||
|
transaction.add(R.id.searchHistoryContainer, recentSearchesFragment!!).commit()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the titles in the tabLayout and fragments in the viewPager
|
||||||
|
*/
|
||||||
|
fun setTabs() {
|
||||||
|
searchMediaFragment = SearchMediaFragment()
|
||||||
|
searchDepictionsFragment = SearchDepictionsFragment()
|
||||||
|
searchCategoryFragment = SearchCategoryFragment()
|
||||||
|
|
||||||
|
viewPagerAdapter!!.setTabs(
|
||||||
|
R.string.search_tab_title_media to searchMediaFragment!!,
|
||||||
|
R.string.search_tab_title_categories to searchCategoryFragment!!,
|
||||||
|
R.string.search_tab_title_depictions to searchDepictionsFragment!!
|
||||||
|
)
|
||||||
|
viewPagerAdapter!!.notifyDataSetChanged()
|
||||||
|
compositeDisposable.add(
|
||||||
|
RxSearchView.queryTextChanges(binding!!.searchBox)
|
||||||
|
.takeUntil(RxView.detaches(binding!!.searchBox))
|
||||||
|
.debounce(500, TimeUnit.MILLISECONDS)
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(::handleSearch, Timber::e)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleSearch(query: CharSequence) {
|
||||||
|
if (!TextUtils.isEmpty(query)) {
|
||||||
|
saveRecentSearch(query.toString())
|
||||||
|
binding!!.viewPager.visibility = View.VISIBLE
|
||||||
|
binding!!.tabLayout.visibility = View.VISIBLE
|
||||||
|
binding!!.searchHistoryContainer.visibility = View.GONE
|
||||||
|
|
||||||
|
if (isFragmentUIActive(searchDepictionsFragment)) {
|
||||||
|
searchDepictionsFragment!!.onQueryUpdated(query.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFragmentUIActive(searchMediaFragment)) {
|
||||||
|
searchMediaFragment!!.onQueryUpdated(query.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFragmentUIActive(searchCategoryFragment)) {
|
||||||
|
searchCategoryFragment!!.onQueryUpdated(query.toString())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//Open RecentSearchesFragment
|
||||||
|
recentSearchesFragment!!.updateRecentSearches()
|
||||||
|
binding!!.viewPager.visibility = View.GONE
|
||||||
|
binding!!.tabLayout.visibility = View.GONE
|
||||||
|
setSearchHistoryFragment()
|
||||||
|
binding!!.searchHistoryContainer.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveRecentSearch(query: String) {
|
||||||
|
val recentSearch = recentSearchesDao!!.find(query)
|
||||||
|
// Newly searched query...
|
||||||
|
if (recentSearch == null) {
|
||||||
|
recentSearchesDao!!.save(RecentSearch(null, query, Date()))
|
||||||
|
} else {
|
||||||
|
recentSearch.lastSearched = Date()
|
||||||
|
recentSearchesDao!!.save(recentSearch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getMediaAtPosition(i: Int): Media? = searchMediaFragment!!.getMediaAtPosition(i)
|
||||||
|
|
||||||
|
override fun getTotalMediaCount(): Int = searchMediaFragment!!.getTotalMediaCount()
|
||||||
|
|
||||||
|
override fun getContributionStateAt(position: Int): Int? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload media detail fragment once media is nominated
|
||||||
|
*
|
||||||
|
* @param index item position that has been nominated
|
||||||
|
*/
|
||||||
|
override fun refreshNominatedMedia(index: Int) {
|
||||||
|
if (getSupportFragmentManager().backStackEntryCount == 1) {
|
||||||
|
onBackPressed()
|
||||||
|
onMediaClicked(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on success of API call for image Search.
|
||||||
|
* The viewpager will notified that number of items have changed.
|
||||||
|
*/
|
||||||
|
override fun viewPagerNotifyDataSetChanged() {
|
||||||
|
mediaDetails?.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open media detail pager fragment on click of image in search results
|
||||||
|
* @param position item index that should be opened
|
||||||
|
*/
|
||||||
|
override fun onMediaClicked(position: Int) {
|
||||||
|
hideKeyboard(findViewById(R.id.searchBox))
|
||||||
|
binding!!.tabLayout.visibility = View.GONE
|
||||||
|
binding!!.viewPager.visibility = View.GONE
|
||||||
|
binding!!.mediaContainer.visibility = View.VISIBLE
|
||||||
|
binding!!.searchBox.visibility =
|
||||||
|
View.GONE // to remove searchview when mediaDetails fragment open
|
||||||
|
if (mediaDetails == null || !mediaDetails!!.isVisible) {
|
||||||
|
// set isFeaturedImage true for featured images, to include author field on media detail
|
||||||
|
mediaDetails = MediaDetailPagerFragment.newInstance(false, true)
|
||||||
|
supportFragmentManager!!
|
||||||
|
.beginTransaction()
|
||||||
|
.hide(supportFragmentManager!!.fragments[supportFragmentManager!!.backStackEntryCount])
|
||||||
|
.add(R.id.mediaContainer, mediaDetails!!)
|
||||||
|
.addToBackStack(null)
|
||||||
|
.commit()
|
||||||
|
// Reason for using hide, add instead of replace is to maintain scroll position after
|
||||||
|
// coming back to the search activity. See https://github.com/commons-app/apps-android-commons/issues/1631
|
||||||
|
// https://stackoverflow.com/questions/11353075/how-can-i-maintain-fragment-state-when-added-to-the-back-stack/19022550#19022550
|
||||||
|
supportFragmentManager!!.executePendingTransactions()
|
||||||
|
}
|
||||||
|
mediaDetails!!.showImage(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on Screen Rotation
|
||||||
|
*/
|
||||||
|
override fun onResume() {
|
||||||
|
if (supportFragmentManager!!.backStackEntryCount == 1) {
|
||||||
|
//FIXME: Temporary fix for screen rotation inside media details. If we don't call onBackPressed then fragment stack is increasing every time.
|
||||||
|
//FIXME: Similar issue like this https://github.com/commons-app/apps-android-commons/issues/894
|
||||||
|
// This is called on screen rotation when user is inside media details. Ideally it should show Media Details but since we are not saving the state now. We are throwing the user to search screen otherwise the app was crashing.
|
||||||
|
onBackPressed()
|
||||||
|
}
|
||||||
|
super.onResume()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on backPressed of anyFragment in the activity.
|
||||||
|
* If condition is called when mediaDetailFragment is opened.
|
||||||
|
*/
|
||||||
|
override fun onBackPressed() {
|
||||||
|
//Remove the backstack entry that gets added when share button is clicked
|
||||||
|
//fixing:https://github.com/commons-app/apps-android-commons/issues/2296
|
||||||
|
if (getSupportFragmentManager().backStackEntryCount == 2) {
|
||||||
|
supportFragmentManager!!
|
||||||
|
.beginTransaction()
|
||||||
|
.remove(mediaDetails!!)
|
||||||
|
.commit()
|
||||||
|
supportFragmentManager!!.popBackStack()
|
||||||
|
supportFragmentManager!!.executePendingTransactions()
|
||||||
|
}
|
||||||
|
if (getSupportFragmentManager().backStackEntryCount == 1) {
|
||||||
|
// back to search so show search toolbar and hide navigation toolbar
|
||||||
|
binding!!.searchBox.visibility = View.VISIBLE //set the searchview
|
||||||
|
binding!!.tabLayout.visibility = View.VISIBLE
|
||||||
|
binding!!.viewPager.visibility = View.VISIBLE
|
||||||
|
binding!!.mediaContainer.visibility = View.GONE
|
||||||
|
} else {
|
||||||
|
binding!!.toolbarSearch.visibility = View.GONE
|
||||||
|
}
|
||||||
|
super.onBackPressed()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on click of a recent search to update query in SearchView.
|
||||||
|
* @param query Recent Search Query
|
||||||
|
*/
|
||||||
|
fun updateText(query: String?) {
|
||||||
|
binding!!.searchBox.setQuery(query, true)
|
||||||
|
// Clear focus of searchView now. searchView.clearFocus(); does not seem to work Check the below link for more details.
|
||||||
|
// https://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control/15481511
|
||||||
|
binding!!.viewPager.requestFocus()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
//Dispose the disposables when the activity is destroyed
|
||||||
|
compositeDisposable.dispose()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,6 @@ import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
abstract class PageableDepictionsFragment : BasePagingFragment<DepictedItem>() {
|
abstract class PageableDepictionsFragment : BasePagingFragment<DepictedItem>() {
|
||||||
override val errorTextId: Int = R.string.error_loading_depictions
|
override val errorTextId: Int = R.string.error_loading_depictions
|
||||||
override val pagedListAdapter by lazy {
|
override val pagedListAdapter by lazy {
|
||||||
DepictionAdapter { WikidataItemDetailsActivity.startYourself(context, it) }
|
DepictionAdapter { WikidataItemDetailsActivity.startYourself(requireContext(), it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,302 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.depictions;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.ViewPagerAdapter.pairOf;
|
|
||||||
import static fr.free.nrw.commons.utils.UrlUtilsKt.handleWebUrl;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuInflater;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.ViewPagerAdapter;
|
|
||||||
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao;
|
|
||||||
import fr.free.nrw.commons.category.CategoryImagesCallback;
|
|
||||||
import fr.free.nrw.commons.databinding.ActivityWikidataItemDetailsBinding;
|
|
||||||
import fr.free.nrw.commons.explore.depictions.child.ChildDepictionsFragment;
|
|
||||||
import fr.free.nrw.commons.explore.depictions.media.DepictedImagesFragment;
|
|
||||||
import fr.free.nrw.commons.explore.depictions.parent.ParentDepictionsFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
|
|
||||||
import fr.free.nrw.commons.media.MediaDetailProvider;
|
|
||||||
import fr.free.nrw.commons.theme.BaseActivity;
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictModel;
|
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
|
|
||||||
import fr.free.nrw.commons.wikidata.WikidataConstants;
|
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
||||||
import io.reactivex.disposables.CompositeDisposable;
|
|
||||||
import io.reactivex.schedulers.Schedulers;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Activity to show depiction media, parent classes and child classes of depicted items in Explore
|
|
||||||
*/
|
|
||||||
public class WikidataItemDetailsActivity extends BaseActivity implements MediaDetailProvider,
|
|
||||||
CategoryImagesCallback {
|
|
||||||
private FragmentManager supportFragmentManager;
|
|
||||||
private DepictedImagesFragment depictionImagesListFragment;
|
|
||||||
private MediaDetailPagerFragment mediaDetailPagerFragment;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Name of the depicted item
|
|
||||||
* Ex: Rabbit
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Inject BookmarkItemsDao bookmarkItemsDao;
|
|
||||||
private CompositeDisposable compositeDisposable;
|
|
||||||
@Inject
|
|
||||||
DepictModel depictModel;
|
|
||||||
private String wikidataItemName;
|
|
||||||
private ActivityWikidataItemDetailsBinding binding;
|
|
||||||
|
|
||||||
ViewPagerAdapter viewPagerAdapter;
|
|
||||||
private DepictedItem wikidataItem;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
binding = ActivityWikidataItemDetailsBinding.inflate(getLayoutInflater());
|
|
||||||
setContentView(binding.getRoot());
|
|
||||||
compositeDisposable = new CompositeDisposable();
|
|
||||||
supportFragmentManager = getSupportFragmentManager();
|
|
||||||
viewPagerAdapter = new ViewPagerAdapter(this, getSupportFragmentManager());
|
|
||||||
binding.viewPager.setAdapter(viewPagerAdapter);
|
|
||||||
binding.viewPager.setOffscreenPageLimit(2);
|
|
||||||
binding.tabLayout.setupWithViewPager(binding.viewPager);
|
|
||||||
|
|
||||||
final DepictedItem depictedItem = getIntent().getParcelableExtra(
|
|
||||||
WikidataConstants.BOOKMARKS_ITEMS);
|
|
||||||
wikidataItem = depictedItem;
|
|
||||||
setSupportActionBar(binding.toolbarBinding.toolbar);
|
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
||||||
setTabs();
|
|
||||||
setPageTitle();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the passed wikidataItemName from the intents and displays it as the page title
|
|
||||||
*/
|
|
||||||
private void setPageTitle() {
|
|
||||||
if (getIntent() != null && getIntent().getStringExtra("wikidataItemName") != null) {
|
|
||||||
setTitle(getIntent().getStringExtra("wikidataItemName"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on success of API call for featured Images.
|
|
||||||
* The viewpager will notified that number of items have changed.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void viewPagerNotifyDataSetChanged() {
|
|
||||||
if (mediaDetailPagerFragment !=null){
|
|
||||||
mediaDetailPagerFragment.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This activity contains 3 tabs and a viewpager. This method is used to set the titles of tab,
|
|
||||||
* Set the fragments according to the tab selected in the viewPager.
|
|
||||||
*/
|
|
||||||
private void setTabs() {
|
|
||||||
depictionImagesListFragment = new DepictedImagesFragment();
|
|
||||||
ChildDepictionsFragment childDepictionsFragment = new ChildDepictionsFragment();
|
|
||||||
ParentDepictionsFragment parentDepictionsFragment = new ParentDepictionsFragment();
|
|
||||||
wikidataItemName = getIntent().getStringExtra("wikidataItemName");
|
|
||||||
String entityId = getIntent().getStringExtra("entityId");
|
|
||||||
if (getIntent() != null && wikidataItemName != null) {
|
|
||||||
Bundle arguments = new Bundle();
|
|
||||||
arguments.putString("wikidataItemName", wikidataItemName);
|
|
||||||
arguments.putString("entityId", entityId);
|
|
||||||
depictionImagesListFragment.setArguments(arguments);
|
|
||||||
parentDepictionsFragment.setArguments(arguments);
|
|
||||||
childDepictionsFragment.setArguments(arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
viewPagerAdapter.setTabs(
|
|
||||||
pairOf(R.string.title_for_media, depictionImagesListFragment),
|
|
||||||
pairOf(R.string.title_for_subcategories, childDepictionsFragment),
|
|
||||||
pairOf(R.string.title_for_parent_categories, parentDepictionsFragment)
|
|
||||||
);
|
|
||||||
binding.viewPager.setOffscreenPageLimit(2);
|
|
||||||
viewPagerAdapter.notifyDataSetChanged();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Shows media detail fragment when user clicks on any image in the list
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onMediaClicked(int position) {
|
|
||||||
binding.tabLayout.setVisibility(View.GONE);
|
|
||||||
binding.viewPager.setVisibility(View.GONE);
|
|
||||||
binding.mediaContainer.setVisibility(View.VISIBLE);
|
|
||||||
if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment.isVisible()) {
|
|
||||||
// set isFeaturedImage true for featured images, to include author field on media detail
|
|
||||||
mediaDetailPagerFragment = MediaDetailPagerFragment.newInstance(false, true);
|
|
||||||
FragmentManager supportFragmentManager = getSupportFragmentManager();
|
|
||||||
supportFragmentManager
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.mediaContainer, mediaDetailPagerFragment)
|
|
||||||
.addToBackStack(null)
|
|
||||||
.commit();
|
|
||||||
supportFragmentManager.executePendingTransactions();
|
|
||||||
}
|
|
||||||
mediaDetailPagerFragment.showImage(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
|
||||||
* @param i It is the index of which media object is to be returned which is same as
|
|
||||||
* current index of viewPager.
|
|
||||||
* @return Media Object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Media getMediaAtPosition(int i) {
|
|
||||||
return depictionImagesListFragment.getMediaAtPosition(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on backPressed of anyFragment in the activity.
|
|
||||||
* If condition is called when mediaDetailFragment is opened.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onBackPressed() {
|
|
||||||
if (supportFragmentManager.getBackStackEntryCount() == 1){
|
|
||||||
binding.tabLayout.setVisibility(View.VISIBLE);
|
|
||||||
binding.viewPager.setVisibility(View.VISIBLE);
|
|
||||||
binding.mediaContainer.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
super.onBackPressed();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on from getCount of MediaDetailPagerFragment
|
|
||||||
* The viewpager will contain same number of media items as that of media elements in adapter.
|
|
||||||
* @return Total Media count in the adapter
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public int getTotalMediaCount() {
|
|
||||||
return depictionImagesListFragment.getTotalMediaCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer getContributionStateAt(int position) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reload media detail fragment once media is nominated
|
|
||||||
*
|
|
||||||
* @param index item position that has been nominated
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void refreshNominatedMedia(int index) {
|
|
||||||
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
|
|
||||||
onBackPressed();
|
|
||||||
onMediaClicked(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consumers should be simply using this method to use this activity.
|
|
||||||
*
|
|
||||||
* @param context A Context of the application package implementing this class.
|
|
||||||
* @param depictedItem Name of the depicts for displaying its details
|
|
||||||
*/
|
|
||||||
public static void startYourself(Context context, DepictedItem depictedItem) {
|
|
||||||
Intent intent = new Intent(context, WikidataItemDetailsActivity.class);
|
|
||||||
intent.putExtra("wikidataItemName", depictedItem.getName());
|
|
||||||
intent.putExtra("entityId", depictedItem.getId());
|
|
||||||
intent.putExtra(WikidataConstants.BOOKMARKS_ITEMS, depictedItem);
|
|
||||||
context.startActivity(intent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function inflates the menu
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
MenuInflater menuInflater=getMenuInflater();
|
|
||||||
menuInflater.inflate(R.menu.menu_wikidata_item,menu);
|
|
||||||
|
|
||||||
updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_item));
|
|
||||||
|
|
||||||
return super.onCreateOptionsMenu(menu);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method handles the logic on item select in toolbar menu
|
|
||||||
* Currently only 1 choice is available to open Wikidata item details page in browser
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
|
|
||||||
switch (item.getItemId()){
|
|
||||||
case R.id.browser_actions_menu_items:
|
|
||||||
String entityId=getIntent().getStringExtra("entityId");
|
|
||||||
Uri uri = Uri.parse("https://www.wikidata.org/wiki/" + entityId);
|
|
||||||
handleWebUrl(this, uri);
|
|
||||||
return true;
|
|
||||||
case R.id.menu_bookmark_current_item:
|
|
||||||
|
|
||||||
if(getIntent().getStringExtra("fragment") != null) {
|
|
||||||
compositeDisposable.add(depictModel.getDepictions(
|
|
||||||
getIntent().getStringExtra("entityId")
|
|
||||||
).subscribeOn(Schedulers.io())
|
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.subscribe(depictedItems -> {
|
|
||||||
final boolean bookmarkExists = bookmarkItemsDao.updateBookmarkItem(
|
|
||||||
depictedItems.get(0));
|
|
||||||
final Snackbar snackbar
|
|
||||||
= bookmarkExists ? Snackbar.make(findViewById(R.id.toolbar_layout),
|
|
||||||
R.string.add_bookmark, Snackbar.LENGTH_LONG)
|
|
||||||
: Snackbar.make(findViewById(R.id.toolbar_layout),
|
|
||||||
R.string.remove_bookmark,
|
|
||||||
Snackbar.LENGTH_LONG);
|
|
||||||
|
|
||||||
snackbar.show();
|
|
||||||
updateBookmarkState(item);
|
|
||||||
}));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
final boolean bookmarkExists
|
|
||||||
= bookmarkItemsDao.updateBookmarkItem(wikidataItem);
|
|
||||||
final Snackbar snackbar
|
|
||||||
= bookmarkExists ? Snackbar.make(findViewById(R.id.toolbar_layout),
|
|
||||||
R.string.add_bookmark, Snackbar.LENGTH_LONG)
|
|
||||||
: Snackbar.make(findViewById(R.id.toolbar_layout), R.string.remove_bookmark,
|
|
||||||
Snackbar.LENGTH_LONG);
|
|
||||||
|
|
||||||
snackbar.show();
|
|
||||||
updateBookmarkState(item);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
case android.R.id.home:
|
|
||||||
onBackPressed();
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateBookmarkState(final MenuItem item) {
|
|
||||||
final boolean isBookmarked;
|
|
||||||
if(getIntent().getStringExtra("fragment") != null) {
|
|
||||||
isBookmarked
|
|
||||||
= bookmarkItemsDao.findBookmarkItem(getIntent().getStringExtra("entityId"));
|
|
||||||
} else {
|
|
||||||
isBookmarked = bookmarkItemsDao.findBookmarkItem(wikidataItem.getId());
|
|
||||||
}
|
|
||||||
final int icon
|
|
||||||
= isBookmarked ? R.drawable.menu_ic_round_star_filled_24px
|
|
||||||
: R.drawable.menu_ic_round_star_border_24px;
|
|
||||||
item.setIcon(icon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,297 @@
|
||||||
|
package fr.free.nrw.commons.explore.depictions
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.Menu
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import androidx.core.os.bundleOf
|
||||||
|
import androidx.fragment.app.FragmentManager
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.ViewPagerAdapter
|
||||||
|
import fr.free.nrw.commons.bookmarks.items.BookmarkItemsDao
|
||||||
|
import fr.free.nrw.commons.category.CategoryImagesCallback
|
||||||
|
import fr.free.nrw.commons.databinding.ActivityWikidataItemDetailsBinding
|
||||||
|
import fr.free.nrw.commons.explore.depictions.child.ChildDepictionsFragment
|
||||||
|
import fr.free.nrw.commons.explore.depictions.media.DepictedImagesFragment
|
||||||
|
import fr.free.nrw.commons.explore.depictions.parent.ParentDepictionsFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailPagerFragment
|
||||||
|
import fr.free.nrw.commons.media.MediaDetailProvider
|
||||||
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.upload.structure.depictions.DepictModel
|
||||||
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
|
import fr.free.nrw.commons.utils.handleWebUrl
|
||||||
|
import fr.free.nrw.commons.wikidata.WikidataConstants
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
import io.reactivex.functions.Consumer
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Activity to show depiction media, parent classes and child classes of depicted items in Explore
|
||||||
|
*/
|
||||||
|
class WikidataItemDetailsActivity : BaseActivity(), MediaDetailProvider, CategoryImagesCallback {
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var bookmarkItemsDao: BookmarkItemsDao? = null
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var depictModel: DepictModel? = null
|
||||||
|
|
||||||
|
private var supportFragmentManager: FragmentManager? = null
|
||||||
|
private var depictionImagesListFragment: DepictedImagesFragment? = null
|
||||||
|
private var mediaDetailPagerFragment: MediaDetailPagerFragment? = null
|
||||||
|
private var binding: ActivityWikidataItemDetailsBinding? = null
|
||||||
|
|
||||||
|
var viewPagerAdapter: ViewPagerAdapter? = null
|
||||||
|
private var wikidataItem: DepictedItem? = null
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
binding = ActivityWikidataItemDetailsBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.root)
|
||||||
|
setContentView(binding!!.root)
|
||||||
|
supportFragmentManager = getSupportFragmentManager()
|
||||||
|
viewPagerAdapter = ViewPagerAdapter(this, getSupportFragmentManager())
|
||||||
|
binding!!.viewPager.adapter = viewPagerAdapter
|
||||||
|
binding!!.viewPager.offscreenPageLimit = 2
|
||||||
|
binding!!.tabLayout.setupWithViewPager(binding!!.viewPager)
|
||||||
|
|
||||||
|
wikidataItem = intent.getParcelableExtra(WikidataConstants.BOOKMARKS_ITEMS)
|
||||||
|
setSupportActionBar(binding!!.toolbarBinding.toolbar)
|
||||||
|
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
|
||||||
|
setTabs()
|
||||||
|
setPageTitle()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the passed wikidataItemName from the intents and displays it as the page title
|
||||||
|
*/
|
||||||
|
private fun setPageTitle() {
|
||||||
|
if (intent != null && intent.getStringExtra("wikidataItemName") != null) {
|
||||||
|
title = intent.getStringExtra("wikidataItemName")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on success of API call for featured Images.
|
||||||
|
* The viewpager will notified that number of items have changed.
|
||||||
|
*/
|
||||||
|
override fun viewPagerNotifyDataSetChanged() {
|
||||||
|
if (mediaDetailPagerFragment != null) {
|
||||||
|
mediaDetailPagerFragment!!.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This activity contains 3 tabs and a viewpager. This method is used to set the titles of tab,
|
||||||
|
* Set the fragments according to the tab selected in the viewPager.
|
||||||
|
*/
|
||||||
|
private fun setTabs() {
|
||||||
|
depictionImagesListFragment = DepictedImagesFragment()
|
||||||
|
val childDepictionsFragment = ChildDepictionsFragment()
|
||||||
|
val parentDepictionsFragment = ParentDepictionsFragment()
|
||||||
|
val wikidataItemName = intent.getStringExtra("wikidataItemName")
|
||||||
|
val entityId = intent.getStringExtra("entityId")
|
||||||
|
if (intent != null && wikidataItemName != null) {
|
||||||
|
val arguments = bundleOf(
|
||||||
|
"wikidataItemName" to wikidataItemName,
|
||||||
|
"entityId" to entityId
|
||||||
|
)
|
||||||
|
depictionImagesListFragment!!.arguments = arguments
|
||||||
|
parentDepictionsFragment.arguments = arguments
|
||||||
|
childDepictionsFragment.arguments = arguments
|
||||||
|
}
|
||||||
|
|
||||||
|
viewPagerAdapter!!.setTabs(
|
||||||
|
R.string.title_for_media to depictionImagesListFragment!!,
|
||||||
|
R.string.title_for_child_classes to childDepictionsFragment,
|
||||||
|
R.string.title_for_parent_classes to parentDepictionsFragment
|
||||||
|
)
|
||||||
|
binding!!.viewPager.offscreenPageLimit = 2
|
||||||
|
viewPagerAdapter!!.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows media detail fragment when user clicks on any image in the list
|
||||||
|
*/
|
||||||
|
override fun onMediaClicked(position: Int) {
|
||||||
|
binding!!.tabLayout.visibility = View.GONE
|
||||||
|
binding!!.viewPager.visibility = View.GONE
|
||||||
|
binding!!.mediaContainer.visibility = View.VISIBLE
|
||||||
|
if (mediaDetailPagerFragment == null || !mediaDetailPagerFragment!!.isVisible) {
|
||||||
|
// set isFeaturedImage true for featured images, to include author field on media detail
|
||||||
|
mediaDetailPagerFragment = MediaDetailPagerFragment.newInstance(false, true)
|
||||||
|
val supportFragmentManager = getSupportFragmentManager()
|
||||||
|
supportFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.mediaContainer, mediaDetailPagerFragment!!)
|
||||||
|
.addToBackStack(null)
|
||||||
|
.commit()
|
||||||
|
supportFragmentManager.executePendingTransactions()
|
||||||
|
}
|
||||||
|
mediaDetailPagerFragment!!.showImage(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called mediaDetailPagerFragment. It returns the Media Object at that Index
|
||||||
|
* @param i It is the index of which media object is to be returned which is same as
|
||||||
|
* current index of viewPager.
|
||||||
|
* @return Media Object
|
||||||
|
*/
|
||||||
|
override fun getMediaAtPosition(i: Int): Media? {
|
||||||
|
return depictionImagesListFragment!!.getMediaAtPosition(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on backPressed of anyFragment in the activity.
|
||||||
|
* If condition is called when mediaDetailFragment is opened.
|
||||||
|
*/
|
||||||
|
override fun onBackPressed() {
|
||||||
|
if (supportFragmentManager!!.backStackEntryCount == 1) {
|
||||||
|
binding!!.tabLayout.visibility = View.VISIBLE
|
||||||
|
binding!!.viewPager.visibility = View.VISIBLE
|
||||||
|
binding!!.mediaContainer.visibility = View.GONE
|
||||||
|
}
|
||||||
|
super.onBackPressed()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on from getCount of MediaDetailPagerFragment
|
||||||
|
* The viewpager will contain same number of media items as that of media elements in adapter.
|
||||||
|
* @return Total Media count in the adapter
|
||||||
|
*/
|
||||||
|
override fun getTotalMediaCount(): Int = depictionImagesListFragment!!.getTotalMediaCount()
|
||||||
|
|
||||||
|
override fun getContributionStateAt(position: Int): Int? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload media detail fragment once media is nominated
|
||||||
|
*
|
||||||
|
* @param index item position that has been nominated
|
||||||
|
*/
|
||||||
|
override fun refreshNominatedMedia(index: Int) {
|
||||||
|
if (getSupportFragmentManager().backStackEntryCount == 1) {
|
||||||
|
onBackPressed()
|
||||||
|
onMediaClicked(index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function inflates the menu
|
||||||
|
*/
|
||||||
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
val menuInflater = menuInflater
|
||||||
|
menuInflater.inflate(R.menu.menu_wikidata_item, menu)
|
||||||
|
|
||||||
|
updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_item))
|
||||||
|
|
||||||
|
return super.onCreateOptionsMenu(menu)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method handles the logic on item select in toolbar menu
|
||||||
|
* Currently only 1 choice is available to open Wikidata item details page in browser
|
||||||
|
*/
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
when (item.itemId) {
|
||||||
|
R.id.browser_actions_menu_items -> {
|
||||||
|
val entityId = intent.getStringExtra("entityId")
|
||||||
|
val uri = Uri.parse("https://www.wikidata.org/wiki/$entityId")
|
||||||
|
handleWebUrl(this, uri)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
R.id.menu_bookmark_current_item -> {
|
||||||
|
if (intent.getStringExtra("fragment") != null) {
|
||||||
|
compositeDisposable!!.add(
|
||||||
|
depictModel!!.getDepictions(
|
||||||
|
intent.getStringExtra("entityId")!!
|
||||||
|
).subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(Consumer<List<DepictedItem?>> { depictedItems: List<DepictedItem?> ->
|
||||||
|
val bookmarkExists = bookmarkItemsDao!!.updateBookmarkItem(
|
||||||
|
depictedItems[0]!!
|
||||||
|
)
|
||||||
|
val snackbar = if (bookmarkExists)
|
||||||
|
Snackbar.make(
|
||||||
|
findViewById(R.id.toolbar_layout),
|
||||||
|
R.string.add_bookmark, Snackbar.LENGTH_LONG
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Snackbar.make(
|
||||||
|
findViewById(R.id.toolbar_layout),
|
||||||
|
R.string.remove_bookmark,
|
||||||
|
Snackbar.LENGTH_LONG
|
||||||
|
)
|
||||||
|
|
||||||
|
snackbar.show()
|
||||||
|
updateBookmarkState(item)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val bookmarkExists = bookmarkItemsDao!!.updateBookmarkItem(wikidataItem!!)
|
||||||
|
val snackbar = if (bookmarkExists)
|
||||||
|
Snackbar.make(
|
||||||
|
findViewById(R.id.toolbar_layout),
|
||||||
|
R.string.add_bookmark, Snackbar.LENGTH_LONG
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Snackbar.make(
|
||||||
|
findViewById(R.id.toolbar_layout), R.string.remove_bookmark,
|
||||||
|
Snackbar.LENGTH_LONG
|
||||||
|
)
|
||||||
|
|
||||||
|
snackbar.show()
|
||||||
|
updateBookmarkState(item)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
android.R.id.home -> {
|
||||||
|
onBackPressed()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> return super.onOptionsItemSelected(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateBookmarkState(item: MenuItem) {
|
||||||
|
val isBookmarked: Boolean = if (intent.getStringExtra("fragment") != null) {
|
||||||
|
bookmarkItemsDao!!.findBookmarkItem(intent.getStringExtra("entityId"))
|
||||||
|
} else {
|
||||||
|
bookmarkItemsDao!!.findBookmarkItem(wikidataItem!!.id)
|
||||||
|
}
|
||||||
|
item.setIcon(if (isBookmarked) {
|
||||||
|
R.drawable.menu_ic_round_star_filled_24px
|
||||||
|
} else {
|
||||||
|
R.drawable.menu_ic_round_star_border_24px
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Consumers should be simply using this method to use this activity.
|
||||||
|
*
|
||||||
|
* @param context A Context of the application package implementing this class.
|
||||||
|
* @param depictedItem Name of the depicts for displaying its details
|
||||||
|
*/
|
||||||
|
fun startYourself(context: Context, depictedItem: DepictedItem) {
|
||||||
|
val intent = Intent(context, WikidataItemDetailsActivity::class.java).apply {
|
||||||
|
putExtra("wikidataItemName", depictedItem.name)
|
||||||
|
putExtra("entityId", depictedItem.id)
|
||||||
|
putExtra(WikidataConstants.BOOKMARKS_ITEMS, depictedItem)
|
||||||
|
}
|
||||||
|
context.startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.map;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
|
||||||
import fr.free.nrw.commons.media.MediaClient;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
public class ExploreMapCalls {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
MediaClient mediaClient;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ExploreMapCalls() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls method to query Commons for uploads around a location
|
|
||||||
*
|
|
||||||
* @param currentLatLng coordinates of search location
|
|
||||||
* @return list of places obtained
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
List<Media> callCommonsQuery(final LatLng currentLatLng) {
|
|
||||||
String coordinates = currentLatLng.getLatitude() + "|" + currentLatLng.getLongitude();
|
|
||||||
return mediaClient.getMediaListFromGeoSearch(coordinates).blockingGet();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package fr.free.nrw.commons.explore.map
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
import fr.free.nrw.commons.media.MediaClient
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class ExploreMapCalls @Inject constructor() {
|
||||||
|
@Inject
|
||||||
|
@JvmField
|
||||||
|
var mediaClient: MediaClient? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls method to query Commons for uploads around a location
|
||||||
|
*
|
||||||
|
* @param currentLatLng coordinates of search location
|
||||||
|
* @return list of places obtained
|
||||||
|
*/
|
||||||
|
fun callCommonsQuery(currentLatLng: LatLng): List<Media> {
|
||||||
|
val coordinates = currentLatLng.latitude.toString() + "|" + currentLatLng.longitude
|
||||||
|
return mediaClient!!.getMediaListFromGeoSearch(coordinates).blockingGet()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.map;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import fr.free.nrw.commons.BaseMarker;
|
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
|
||||||
import fr.free.nrw.commons.location.LocationServiceManager;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ExploreMapContract {
|
|
||||||
|
|
||||||
interface View {
|
|
||||||
boolean isNetworkConnectionEstablished();
|
|
||||||
void populatePlaces(LatLng curlatLng);
|
|
||||||
void askForLocationPermission();
|
|
||||||
void recenterMap(LatLng curLatLng);
|
|
||||||
void hideBottomDetailsSheet();
|
|
||||||
LatLng getMapCenter();
|
|
||||||
LatLng getMapFocus();
|
|
||||||
LatLng getLastMapFocus();
|
|
||||||
void addMarkersToMap(final List<BaseMarker> nearbyBaseMarkers);
|
|
||||||
void clearAllMarkers();
|
|
||||||
void addSearchThisAreaButtonAction();
|
|
||||||
void setSearchThisAreaButtonVisibility(boolean isVisible);
|
|
||||||
void setProgressBarVisibility(boolean isVisible);
|
|
||||||
boolean isDetailsBottomSheetVisible();
|
|
||||||
boolean isSearchThisAreaButtonVisible();
|
|
||||||
Context getContext();
|
|
||||||
LatLng getLastLocation();
|
|
||||||
void disableFABRecenter();
|
|
||||||
void enableFABRecenter();
|
|
||||||
void setFABRecenterAction(android.view.View.OnClickListener onClickListener);
|
|
||||||
boolean backButtonClicked();
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UserActions {
|
|
||||||
void updateMap(LocationServiceManager.LocationChangeType locationChangeType);
|
|
||||||
void lockUnlockNearby(boolean isNearbyLocked);
|
|
||||||
void attachView(View view);
|
|
||||||
void detachView();
|
|
||||||
void setActionListeners(JsonKvStore applicationKvStore);
|
|
||||||
boolean backButtonClicked();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package fr.free.nrw.commons.explore.map
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.view.View
|
||||||
|
import fr.free.nrw.commons.BaseMarker
|
||||||
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType
|
||||||
|
|
||||||
|
class ExploreMapContract {
|
||||||
|
interface View {
|
||||||
|
fun isNetworkConnectionEstablished(): Boolean
|
||||||
|
fun populatePlaces(curlatLng: LatLng?)
|
||||||
|
fun askForLocationPermission()
|
||||||
|
fun recenterMap(curLatLng: LatLng?)
|
||||||
|
fun hideBottomDetailsSheet()
|
||||||
|
fun getMapCenter(): LatLng?
|
||||||
|
fun getMapFocus(): LatLng?
|
||||||
|
fun getLastMapFocus(): LatLng?
|
||||||
|
fun addMarkersToMap(nearbyBaseMarkers: List<BaseMarker?>?)
|
||||||
|
fun clearAllMarkers()
|
||||||
|
fun addSearchThisAreaButtonAction()
|
||||||
|
fun setSearchThisAreaButtonVisibility(isVisible: Boolean)
|
||||||
|
fun setProgressBarVisibility(isVisible: Boolean)
|
||||||
|
fun isDetailsBottomSheetVisible(): Boolean
|
||||||
|
fun isSearchThisAreaButtonVisible(): Boolean
|
||||||
|
fun getContext(): Context?
|
||||||
|
fun getLastLocation(): LatLng?
|
||||||
|
fun disableFABRecenter()
|
||||||
|
fun enableFABRecenter()
|
||||||
|
fun setFABRecenterAction(onClickListener: android.view.View.OnClickListener?)
|
||||||
|
fun backButtonClicked(): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserActions {
|
||||||
|
fun updateMap(locationChangeType: LocationChangeType)
|
||||||
|
fun lockUnlockNearby(isNearbyLocked: Boolean)
|
||||||
|
fun attachView(view: View?)
|
||||||
|
fun detachView()
|
||||||
|
fun setActionListeners(applicationKvStore: JsonKvStore?)
|
||||||
|
fun backButtonClicked(): Boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,213 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.map;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
|
|
||||||
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
|
|
||||||
import com.bumptech.glide.Glide;
|
|
||||||
import com.bumptech.glide.request.RequestOptions;
|
|
||||||
import com.bumptech.glide.request.target.CustomTarget;
|
|
||||||
import com.bumptech.glide.request.transition.Transition;
|
|
||||||
import fr.free.nrw.commons.BaseMarker;
|
|
||||||
import fr.free.nrw.commons.MapController;
|
|
||||||
import fr.free.nrw.commons.Media;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
|
||||||
import fr.free.nrw.commons.nearby.Place;
|
|
||||||
import fr.free.nrw.commons.utils.ImageUtils;
|
|
||||||
import fr.free.nrw.commons.utils.LocationUtils;
|
|
||||||
import fr.free.nrw.commons.utils.PlaceUtils;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class ExploreMapController extends MapController {
|
|
||||||
|
|
||||||
private final ExploreMapCalls exploreMapCalls;
|
|
||||||
public LatLng latestSearchLocation; // Can be current and camera target on search this area button is used
|
|
||||||
public LatLng currentLocation; // current location of user
|
|
||||||
public double latestSearchRadius = 0; // Any last search radius
|
|
||||||
public double currentLocationSearchRadius = 0; // Search radius of only searches around current location
|
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public ExploreMapController(ExploreMapCalls explorePlaces) {
|
|
||||||
this.exploreMapCalls = explorePlaces;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes location as parameter and returns ExplorePlaces info that holds currentLatLng, mediaList,
|
|
||||||
* explorePlaceList and boundaryCoordinates
|
|
||||||
*
|
|
||||||
* @param currentLatLng is current geolocation
|
|
||||||
* @param searchLatLng is the location that we want to search around
|
|
||||||
* @param checkingAroundCurrentLocation is a boolean flag. True if we want to check around
|
|
||||||
* current location, false if another location
|
|
||||||
* @return explorePlacesInfo info that holds currentLatLng, mediaList, explorePlaceList and
|
|
||||||
* boundaryCoordinates
|
|
||||||
*/
|
|
||||||
public ExplorePlacesInfo loadAttractionsFromLocation(LatLng currentLatLng, LatLng searchLatLng,
|
|
||||||
boolean checkingAroundCurrentLocation) {
|
|
||||||
|
|
||||||
if (searchLatLng == null) {
|
|
||||||
Timber.d("Loading attractions explore map, but search is null");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
ExplorePlacesInfo explorePlacesInfo = new ExplorePlacesInfo();
|
|
||||||
try {
|
|
||||||
explorePlacesInfo.currentLatLng = currentLatLng;
|
|
||||||
latestSearchLocation = searchLatLng;
|
|
||||||
|
|
||||||
List<Media> mediaList = exploreMapCalls.callCommonsQuery(searchLatLng);
|
|
||||||
LatLng[] boundaryCoordinates = {mediaList.get(0).getCoordinates(), // south
|
|
||||||
mediaList.get(0).getCoordinates(), // north
|
|
||||||
mediaList.get(0).getCoordinates(), // west
|
|
||||||
mediaList.get(0).getCoordinates()};// east, init with a random location
|
|
||||||
|
|
||||||
if (searchLatLng != null) {
|
|
||||||
Timber.d("Sorting places by distance...");
|
|
||||||
final Map<Media, Double> distances = new HashMap<>();
|
|
||||||
for (Media media : mediaList) {
|
|
||||||
distances.put(media,
|
|
||||||
computeDistanceBetween(media.getCoordinates(), searchLatLng));
|
|
||||||
// Find boundaries with basic find max approach
|
|
||||||
if (media.getCoordinates().getLatitude()
|
|
||||||
< boundaryCoordinates[0].getLatitude()) {
|
|
||||||
boundaryCoordinates[0] = media.getCoordinates();
|
|
||||||
}
|
|
||||||
if (media.getCoordinates().getLatitude()
|
|
||||||
> boundaryCoordinates[1].getLatitude()) {
|
|
||||||
boundaryCoordinates[1] = media.getCoordinates();
|
|
||||||
}
|
|
||||||
if (media.getCoordinates().getLongitude()
|
|
||||||
< boundaryCoordinates[2].getLongitude()) {
|
|
||||||
boundaryCoordinates[2] = media.getCoordinates();
|
|
||||||
}
|
|
||||||
if (media.getCoordinates().getLongitude()
|
|
||||||
> boundaryCoordinates[3].getLongitude()) {
|
|
||||||
boundaryCoordinates[3] = media.getCoordinates();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
explorePlacesInfo.mediaList = mediaList;
|
|
||||||
explorePlacesInfo.explorePlaceList = PlaceUtils.mediaToExplorePlace(mediaList);
|
|
||||||
explorePlacesInfo.boundaryCoordinates = boundaryCoordinates;
|
|
||||||
|
|
||||||
// Sets latestSearchRadius to maximum distance among boundaries and search location
|
|
||||||
for (LatLng bound : boundaryCoordinates) {
|
|
||||||
double distance = LocationUtils.calculateDistance(bound.getLatitude(),
|
|
||||||
bound.getLongitude(), searchLatLng.getLatitude(), searchLatLng.getLongitude());
|
|
||||||
if (distance > latestSearchRadius) {
|
|
||||||
latestSearchRadius = distance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Our radius searched around us, will be used to understand when user search their own location, we will follow them
|
|
||||||
if (checkingAroundCurrentLocation) {
|
|
||||||
currentLocationSearchRadius = latestSearchRadius;
|
|
||||||
currentLocation = currentLatLng;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return explorePlacesInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads attractions from location for map view, we need to return places in Place data type
|
|
||||||
*
|
|
||||||
* @return baseMarkerOptions list that holds nearby places with their icons
|
|
||||||
*/
|
|
||||||
public static List<BaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
|
|
||||||
LatLng currentLatLng,
|
|
||||||
final List<Place> placeList,
|
|
||||||
Context context,
|
|
||||||
NearbyBaseMarkerThumbCallback callback,
|
|
||||||
ExplorePlacesInfo explorePlacesInfo) {
|
|
||||||
List<BaseMarker> baseMarkerList = new ArrayList<>();
|
|
||||||
|
|
||||||
if (placeList == null) {
|
|
||||||
return baseMarkerList;
|
|
||||||
}
|
|
||||||
|
|
||||||
VectorDrawableCompat vectorDrawable = null;
|
|
||||||
try {
|
|
||||||
vectorDrawable = VectorDrawableCompat.create(
|
|
||||||
context.getResources(), R.drawable.ic_custom_map_marker_dark, context.getTheme());
|
|
||||||
|
|
||||||
} catch (Resources.NotFoundException e) {
|
|
||||||
// ignore when running tests.
|
|
||||||
}
|
|
||||||
if (vectorDrawable != null) {
|
|
||||||
for (Place explorePlace : placeList) {
|
|
||||||
final BaseMarker baseMarker = new BaseMarker();
|
|
||||||
String distance = formatDistanceBetween(currentLatLng, explorePlace.location);
|
|
||||||
explorePlace.setDistance(distance);
|
|
||||||
|
|
||||||
baseMarker.setTitle(
|
|
||||||
explorePlace.name.substring(5, explorePlace.name.lastIndexOf(".")));
|
|
||||||
baseMarker.setPosition(
|
|
||||||
new fr.free.nrw.commons.location.LatLng(
|
|
||||||
explorePlace.location.getLatitude(),
|
|
||||||
explorePlace.location.getLongitude(), 0));
|
|
||||||
baseMarker.setPlace(explorePlace);
|
|
||||||
|
|
||||||
Glide.with(context)
|
|
||||||
.asBitmap()
|
|
||||||
.load(explorePlace.getThumb())
|
|
||||||
.placeholder(R.drawable.image_placeholder_96)
|
|
||||||
.apply(new RequestOptions().override(96, 96).centerCrop())
|
|
||||||
.into(new CustomTarget<Bitmap>() {
|
|
||||||
// We add icons to markers when bitmaps are ready
|
|
||||||
@Override
|
|
||||||
public void onResourceReady(@NonNull Bitmap resource,
|
|
||||||
@Nullable Transition<? super Bitmap> transition) {
|
|
||||||
baseMarker.setIcon(
|
|
||||||
ImageUtils.addRedBorder(resource, 6, context));
|
|
||||||
baseMarkerList.add(baseMarker);
|
|
||||||
if (baseMarkerList.size()
|
|
||||||
== placeList.size()) { // if true, we added all markers to list and can trigger thumbs ready callback
|
|
||||||
callback.onNearbyBaseMarkerThumbsReady(baseMarkerList,
|
|
||||||
explorePlacesInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoadCleared(@Nullable Drawable placeholder) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// We add thumbnail icon for images that couldn't be loaded
|
|
||||||
@Override
|
|
||||||
public void onLoadFailed(@Nullable final Drawable errorDrawable) {
|
|
||||||
super.onLoadFailed(errorDrawable);
|
|
||||||
baseMarker.fromResource(context, R.drawable.image_placeholder_96);
|
|
||||||
baseMarkerList.add(baseMarker);
|
|
||||||
if (baseMarkerList.size()
|
|
||||||
== placeList.size()) { // if true, we added all markers to list and can trigger thumbs ready callback
|
|
||||||
callback.onNearbyBaseMarkerThumbsReady(baseMarkerList,
|
|
||||||
explorePlacesInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return baseMarkerList;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface NearbyBaseMarkerThumbCallback {
|
|
||||||
|
|
||||||
// Callback to notify thumbnails of explore markers are added as icons and ready
|
|
||||||
void onNearbyBaseMarkerThumbsReady(List<BaseMarker> baseMarkers,
|
|
||||||
ExplorePlacesInfo explorePlacesInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,219 @@
|
||||||
|
package fr.free.nrw.commons.explore.map
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.res.Resources
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
|
import com.bumptech.glide.request.RequestOptions
|
||||||
|
import com.bumptech.glide.request.target.CustomTarget
|
||||||
|
import com.bumptech.glide.request.transition.Transition
|
||||||
|
import fr.free.nrw.commons.BaseMarker
|
||||||
|
import fr.free.nrw.commons.MapController
|
||||||
|
import fr.free.nrw.commons.Media
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
import fr.free.nrw.commons.nearby.Place
|
||||||
|
import fr.free.nrw.commons.utils.ImageUtils.addRedBorder
|
||||||
|
import fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween
|
||||||
|
import fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween
|
||||||
|
import fr.free.nrw.commons.utils.LocationUtils.calculateDistance
|
||||||
|
import fr.free.nrw.commons.utils.PlaceUtils.mediaToExplorePlace
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class ExploreMapController @Inject constructor(
|
||||||
|
private val exploreMapCalls: ExploreMapCalls
|
||||||
|
) : MapController() {
|
||||||
|
// Can be current and camera target on search this area button is used
|
||||||
|
private var latestSearchLocation: LatLng? = null
|
||||||
|
|
||||||
|
// Any last search radius
|
||||||
|
private var latestSearchRadius: Double = 0.0
|
||||||
|
|
||||||
|
// Search radius of only searches around current location
|
||||||
|
private var currentLocationSearchRadius: Double = 0.0
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
// current location of user
|
||||||
|
var currentLocation: LatLng? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes location as parameter and returns ExplorePlaces info that holds currentLatLng, mediaList,
|
||||||
|
* explorePlaceList and boundaryCoordinates
|
||||||
|
*
|
||||||
|
* @param currentLatLng is current geolocation
|
||||||
|
* @param searchLatLng is the location that we want to search around
|
||||||
|
* @param checkingAroundCurrentLocation is a boolean flag. True if we want to check around
|
||||||
|
* current location, false if another location
|
||||||
|
* @return explorePlacesInfo info that holds currentLatLng, mediaList, explorePlaceList and
|
||||||
|
* boundaryCoordinates
|
||||||
|
*/
|
||||||
|
fun loadAttractionsFromLocation(
|
||||||
|
currentLatLng: LatLng?, searchLatLng: LatLng?,
|
||||||
|
checkingAroundCurrentLocation: Boolean
|
||||||
|
): ExplorePlacesInfo? {
|
||||||
|
if (searchLatLng == null) {
|
||||||
|
Timber.d("Loading attractions explore map, but search is null")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
val explorePlacesInfo = ExplorePlacesInfo()
|
||||||
|
try {
|
||||||
|
explorePlacesInfo.currentLatLng = currentLatLng
|
||||||
|
latestSearchLocation = searchLatLng
|
||||||
|
|
||||||
|
val mediaList = exploreMapCalls.callCommonsQuery(searchLatLng)
|
||||||
|
val boundaryCoordinates = arrayOf(
|
||||||
|
mediaList[0].coordinates!!, // south
|
||||||
|
mediaList[0].coordinates!!, // north
|
||||||
|
mediaList[0].coordinates!!, // west
|
||||||
|
mediaList[0].coordinates!!
|
||||||
|
) // east, init with a random location
|
||||||
|
|
||||||
|
Timber.d("Sorting places by distance...")
|
||||||
|
val distances: MutableMap<Media, Double> = HashMap()
|
||||||
|
for (media in mediaList) {
|
||||||
|
distances[media] = computeDistanceBetween(media.coordinates!!, searchLatLng)
|
||||||
|
// Find boundaries with basic find max approach
|
||||||
|
if (media.coordinates!!.latitude
|
||||||
|
< boundaryCoordinates[0]!!.latitude
|
||||||
|
) {
|
||||||
|
boundaryCoordinates[0] = media.coordinates!!
|
||||||
|
}
|
||||||
|
if (media.coordinates!!.latitude
|
||||||
|
> boundaryCoordinates[1]!!.latitude
|
||||||
|
) {
|
||||||
|
boundaryCoordinates[1] = media.coordinates!!
|
||||||
|
}
|
||||||
|
if (media.coordinates!!.longitude
|
||||||
|
< boundaryCoordinates[2]!!.longitude
|
||||||
|
) {
|
||||||
|
boundaryCoordinates[2] = media.coordinates!!
|
||||||
|
}
|
||||||
|
if (media.coordinates!!.longitude
|
||||||
|
> boundaryCoordinates[3]!!.longitude
|
||||||
|
) {
|
||||||
|
boundaryCoordinates[3] = media.coordinates!!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
explorePlacesInfo.mediaList = mediaList
|
||||||
|
explorePlacesInfo.explorePlaceList = mediaToExplorePlace(mediaList)
|
||||||
|
explorePlacesInfo.boundaryCoordinates = boundaryCoordinates
|
||||||
|
|
||||||
|
// Sets latestSearchRadius to maximum distance among boundaries and search location
|
||||||
|
for ((latitude, longitude) in boundaryCoordinates) {
|
||||||
|
val distance = calculateDistance(
|
||||||
|
latitude,
|
||||||
|
longitude, searchLatLng.latitude, searchLatLng.longitude
|
||||||
|
)
|
||||||
|
if (distance > latestSearchRadius) {
|
||||||
|
latestSearchRadius = distance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Our radius searched around us, will be used to understand when user search their own location, we will follow them
|
||||||
|
if (checkingAroundCurrentLocation) {
|
||||||
|
currentLocationSearchRadius = latestSearchRadius
|
||||||
|
currentLocation = currentLatLng
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
}
|
||||||
|
return explorePlacesInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NearbyBaseMarkerThumbCallback {
|
||||||
|
// Callback to notify thumbnails of explore markers are added as icons and ready
|
||||||
|
fun onNearbyBaseMarkerThumbsReady(
|
||||||
|
baseMarkers: List<BaseMarker>?,
|
||||||
|
explorePlacesInfo: ExplorePlacesInfo?
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Loads attractions from location for map view, we need to return places in Place data type
|
||||||
|
*
|
||||||
|
* @return baseMarkerOptions list that holds nearby places with their icons
|
||||||
|
*/
|
||||||
|
fun loadAttractionsFromLocationToBaseMarkerOptions(
|
||||||
|
currentLatLng: LatLng?,
|
||||||
|
placeList: List<Place>?,
|
||||||
|
context: Context,
|
||||||
|
callback: NearbyBaseMarkerThumbCallback,
|
||||||
|
explorePlacesInfo: ExplorePlacesInfo?
|
||||||
|
): List<BaseMarker> {
|
||||||
|
val baseMarkerList: MutableList<BaseMarker> = ArrayList()
|
||||||
|
|
||||||
|
if (placeList == null) {
|
||||||
|
return baseMarkerList
|
||||||
|
}
|
||||||
|
|
||||||
|
var vectorDrawable: VectorDrawableCompat? = null
|
||||||
|
try {
|
||||||
|
vectorDrawable = VectorDrawableCompat.create(
|
||||||
|
context.resources, R.drawable.ic_custom_map_marker_dark, context.theme
|
||||||
|
)
|
||||||
|
} catch (e: Resources.NotFoundException) {
|
||||||
|
// ignore when running tests.
|
||||||
|
}
|
||||||
|
if (vectorDrawable != null) {
|
||||||
|
for (explorePlace in placeList) {
|
||||||
|
val baseMarker = BaseMarker()
|
||||||
|
val distance = formatDistanceBetween(currentLatLng, explorePlace.location)
|
||||||
|
explorePlace.setDistance(distance)
|
||||||
|
|
||||||
|
baseMarker.title =
|
||||||
|
explorePlace.name.substring(5, explorePlace.name.lastIndexOf("."))
|
||||||
|
baseMarker.position = LatLng(
|
||||||
|
explorePlace.location.latitude,
|
||||||
|
explorePlace.location.longitude, 0f
|
||||||
|
)
|
||||||
|
baseMarker.place = explorePlace
|
||||||
|
|
||||||
|
Glide.with(context)
|
||||||
|
.asBitmap()
|
||||||
|
.load(explorePlace.thumb)
|
||||||
|
.placeholder(R.drawable.image_placeholder_96)
|
||||||
|
.apply(RequestOptions().override(96, 96).centerCrop())
|
||||||
|
.into(object : CustomTarget<Bitmap>() {
|
||||||
|
// We add icons to markers when bitmaps are ready
|
||||||
|
override fun onResourceReady(
|
||||||
|
resource: Bitmap,
|
||||||
|
transition: Transition<in Bitmap>?
|
||||||
|
) {
|
||||||
|
baseMarker.icon = addRedBorder(resource, 6, context)
|
||||||
|
baseMarkerList.add(baseMarker)
|
||||||
|
if (baseMarkerList.size == placeList.size) {
|
||||||
|
// if true, we added all markers to list and can trigger thumbs ready callback
|
||||||
|
callback.onNearbyBaseMarkerThumbsReady(
|
||||||
|
baseMarkerList,
|
||||||
|
explorePlacesInfo
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLoadCleared(placeholder: Drawable?) = Unit
|
||||||
|
|
||||||
|
// We add thumbnail icon for images that couldn't be loaded
|
||||||
|
override fun onLoadFailed(errorDrawable: Drawable?) {
|
||||||
|
super.onLoadFailed(errorDrawable)
|
||||||
|
baseMarker.fromResource(context, R.drawable.image_placeholder_96)
|
||||||
|
baseMarkerList.add(baseMarker)
|
||||||
|
if (baseMarkerList.size == placeList.size) {
|
||||||
|
// if true, we added all markers to list and can trigger thumbs ready callback
|
||||||
|
callback.onNearbyBaseMarkerThumbsReady(
|
||||||
|
baseMarkerList,
|
||||||
|
explorePlacesInfo
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return baseMarkerList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -1,237 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.map;
|
|
||||||
|
|
||||||
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED;
|
|
||||||
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.SEARCH_CUSTOM_AREA;
|
|
||||||
|
|
||||||
|
|
||||||
import android.location.Location;
|
|
||||||
import android.view.View;
|
|
||||||
import fr.free.nrw.commons.BaseMarker;
|
|
||||||
import fr.free.nrw.commons.MapController;
|
|
||||||
import fr.free.nrw.commons.MapController.ExplorePlacesInfo;
|
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao;
|
|
||||||
import fr.free.nrw.commons.explore.map.ExploreMapController.NearbyBaseMarkerThumbCallback;
|
|
||||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
|
||||||
import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType;
|
|
||||||
import fr.free.nrw.commons.nearby.Place;
|
|
||||||
import io.reactivex.Observable;
|
|
||||||
import java.lang.reflect.Proxy;
|
|
||||||
import java.util.List;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class ExploreMapPresenter
|
|
||||||
implements ExploreMapContract.UserActions,
|
|
||||||
NearbyBaseMarkerThumbCallback {
|
|
||||||
|
|
||||||
BookmarkLocationsDao bookmarkLocationDao;
|
|
||||||
private boolean isNearbyLocked;
|
|
||||||
private LatLng currentLatLng;
|
|
||||||
private ExploreMapController exploreMapController;
|
|
||||||
|
|
||||||
private static final ExploreMapContract.View DUMMY = (ExploreMapContract.View) Proxy
|
|
||||||
.newProxyInstance(
|
|
||||||
ExploreMapContract.View.class.getClassLoader(),
|
|
||||||
new Class[]{ExploreMapContract.View.class}, (proxy, method, args) -> {
|
|
||||||
if (method.getName().equals("onMyEvent")) {
|
|
||||||
return null;
|
|
||||||
} else if (String.class == method.getReturnType()) {
|
|
||||||
return "";
|
|
||||||
} else if (Integer.class == method.getReturnType()) {
|
|
||||||
return Integer.valueOf(0);
|
|
||||||
} else if (int.class == method.getReturnType()) {
|
|
||||||
return 0;
|
|
||||||
} else if (Boolean.class == method.getReturnType()) {
|
|
||||||
return Boolean.FALSE;
|
|
||||||
} else if (boolean.class == method.getReturnType()) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
private ExploreMapContract.View exploreMapFragmentView = DUMMY;
|
|
||||||
|
|
||||||
public ExploreMapPresenter(BookmarkLocationsDao bookmarkLocationDao) {
|
|
||||||
this.bookmarkLocationDao = bookmarkLocationDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateMap(LocationChangeType locationChangeType) {
|
|
||||||
Timber.d("Presenter updates map and list" + locationChangeType.toString());
|
|
||||||
if (isNearbyLocked) {
|
|
||||||
Timber.d("Nearby is locked, so updateMapAndList returns");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!exploreMapFragmentView.isNetworkConnectionEstablished()) {
|
|
||||||
Timber.d("Network connection is not established");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Significant changed - Markers and current location will be updated together
|
|
||||||
* Slightly changed - Only current position marker will be updated
|
|
||||||
*/
|
|
||||||
if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED)) {
|
|
||||||
Timber.d("LOCATION_SIGNIFICANTLY_CHANGED");
|
|
||||||
LatLng populateLatLng = exploreMapFragmentView.getMapCenter();
|
|
||||||
|
|
||||||
//If "Show in Explore" was selected in Nearby, use the previous LatLng
|
|
||||||
if (exploreMapFragmentView instanceof ExploreMapFragment) {
|
|
||||||
ExploreMapFragment exploreMapFragment = (ExploreMapFragment)exploreMapFragmentView;
|
|
||||||
if (exploreMapFragment.recentlyCameFromNearbyMap()) {
|
|
||||||
//Ensure this LatLng will not be used again if user searches their GPS location
|
|
||||||
exploreMapFragment.setRecentlyCameFromNearbyMap(false);
|
|
||||||
|
|
||||||
populateLatLng = exploreMapFragment.getPreviousLatLng();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lockUnlockNearby(true);
|
|
||||||
exploreMapFragmentView.setProgressBarVisibility(true);
|
|
||||||
exploreMapFragmentView.populatePlaces(populateLatLng);
|
|
||||||
} else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) {
|
|
||||||
Timber.d("SEARCH_CUSTOM_AREA");
|
|
||||||
lockUnlockNearby(true);
|
|
||||||
exploreMapFragmentView.setProgressBarVisibility(true);
|
|
||||||
exploreMapFragmentView.populatePlaces(exploreMapFragmentView.getMapFocus());
|
|
||||||
} else { // Means location changed slightly, ie user is walking or driving.
|
|
||||||
Timber.d("Means location changed slightly");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nearby updates takes time, since they are network operations. During update time, we don't
|
|
||||||
* want to get any other calls from user. So locking nearby.
|
|
||||||
*
|
|
||||||
* @param isNearbyLocked true means lock, false means unlock
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void lockUnlockNearby(boolean isNearbyLocked) {
|
|
||||||
this.isNearbyLocked = isNearbyLocked;
|
|
||||||
if (isNearbyLocked) {
|
|
||||||
exploreMapFragmentView.disableFABRecenter();
|
|
||||||
} else {
|
|
||||||
exploreMapFragmentView.enableFABRecenter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void attachView(ExploreMapContract.View view) {
|
|
||||||
exploreMapFragmentView = view;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void detachView() {
|
|
||||||
exploreMapFragmentView = DUMMY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets click listener of FAB
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setActionListeners(JsonKvStore applicationKvStore) {
|
|
||||||
exploreMapFragmentView.setFABRecenterAction(v -> {
|
|
||||||
exploreMapFragmentView.recenterMap(currentLatLng);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean backButtonClicked() {
|
|
||||||
return exploreMapFragmentView.backButtonClicked();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onMapReady(ExploreMapController exploreMapController) {
|
|
||||||
this.exploreMapController = exploreMapController;
|
|
||||||
if (null != exploreMapFragmentView) {
|
|
||||||
exploreMapFragmentView.addSearchThisAreaButtonAction();
|
|
||||||
initializeMapOperations();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeMapOperations() {
|
|
||||||
lockUnlockNearby(false);
|
|
||||||
updateMap(LOCATION_SIGNIFICANTLY_CHANGED);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Observable<ExplorePlacesInfo> loadAttractionsFromLocation(LatLng currentLatLng,
|
|
||||||
LatLng searchLatLng, boolean checkingAroundCurrent) {
|
|
||||||
return Observable
|
|
||||||
.fromCallable(() -> exploreMapController
|
|
||||||
.loadAttractionsFromLocation(currentLatLng, searchLatLng, checkingAroundCurrent));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Populates places for custom location, should be used for finding nearby places around a
|
|
||||||
* location where you are not at.
|
|
||||||
*
|
|
||||||
* @param explorePlacesInfo This variable has placeToCenter list information and distances.
|
|
||||||
*/
|
|
||||||
public void updateMapMarkers(
|
|
||||||
MapController.ExplorePlacesInfo explorePlacesInfo) {
|
|
||||||
if (explorePlacesInfo.mediaList != null) {
|
|
||||||
prepareNearbyBaseMarkers(explorePlacesInfo);
|
|
||||||
} else {
|
|
||||||
lockUnlockNearby(false); // So that new location updates wont come
|
|
||||||
exploreMapFragmentView.setProgressBarVisibility(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepareNearbyBaseMarkers(MapController.ExplorePlacesInfo explorePlacesInfo) {
|
|
||||||
exploreMapController
|
|
||||||
.loadAttractionsFromLocationToBaseMarkerOptions(explorePlacesInfo.currentLatLng,
|
|
||||||
// Curlatlang will be used to calculate distances
|
|
||||||
(List<Place>) explorePlacesInfo.explorePlaceList,
|
|
||||||
exploreMapFragmentView.getContext(),
|
|
||||||
this,
|
|
||||||
explorePlacesInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNearbyBaseMarkerThumbsReady(List<BaseMarker> baseMarkers,
|
|
||||||
ExplorePlacesInfo explorePlacesInfo) {
|
|
||||||
if (null != exploreMapFragmentView) {
|
|
||||||
exploreMapFragmentView.addMarkersToMap(baseMarkers);
|
|
||||||
lockUnlockNearby(false); // So that new location updates wont come
|
|
||||||
exploreMapFragmentView.setProgressBarVisibility(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public View.OnClickListener onSearchThisAreaClicked() {
|
|
||||||
return v -> {
|
|
||||||
// Lock map operations during search this area operation
|
|
||||||
exploreMapFragmentView.setSearchThisAreaButtonVisibility(false);
|
|
||||||
|
|
||||||
if (searchCloseToCurrentLocation()) {
|
|
||||||
updateMap(LOCATION_SIGNIFICANTLY_CHANGED);
|
|
||||||
} else {
|
|
||||||
updateMap(SEARCH_CUSTOM_AREA);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if search this area button is used around our current location, so that we can
|
|
||||||
* continue following our current location again
|
|
||||||
*
|
|
||||||
* @return Returns true if search this area button is used around our current location
|
|
||||||
*/
|
|
||||||
public boolean searchCloseToCurrentLocation() {
|
|
||||||
if (null == exploreMapFragmentView.getLastMapFocus()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Location mylocation = new Location("");
|
|
||||||
Location dest_location = new Location("");
|
|
||||||
dest_location.setLatitude(exploreMapFragmentView.getMapFocus().getLatitude());
|
|
||||||
dest_location.setLongitude(exploreMapFragmentView.getMapFocus().getLongitude());
|
|
||||||
mylocation.setLatitude(exploreMapFragmentView.getLastMapFocus().getLatitude());
|
|
||||||
mylocation.setLongitude(exploreMapFragmentView.getLastMapFocus().getLongitude());
|
|
||||||
Float distance = mylocation.distanceTo(dest_location);
|
|
||||||
|
|
||||||
return !(distance > 2000.0 * 3 / 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,223 @@
|
||||||
|
package fr.free.nrw.commons.explore.map
|
||||||
|
|
||||||
|
import android.location.Location
|
||||||
|
import android.view.View
|
||||||
|
import fr.free.nrw.commons.BaseMarker
|
||||||
|
import fr.free.nrw.commons.MapController.ExplorePlacesInfo
|
||||||
|
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
||||||
|
import fr.free.nrw.commons.explore.map.ExploreMapController.Companion.loadAttractionsFromLocationToBaseMarkerOptions
|
||||||
|
import fr.free.nrw.commons.explore.map.ExploreMapController.NearbyBaseMarkerThumbCallback
|
||||||
|
import fr.free.nrw.commons.kvstore.JsonKvStore
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType
|
||||||
|
import fr.free.nrw.commons.nearby.Place
|
||||||
|
import io.reactivex.Observable
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
import java.lang.reflect.Proxy
|
||||||
|
import java.util.concurrent.Callable
|
||||||
|
|
||||||
|
class ExploreMapPresenter(
|
||||||
|
var bookmarkLocationDao: BookmarkLocationsDao
|
||||||
|
) : ExploreMapContract.UserActions, NearbyBaseMarkerThumbCallback {
|
||||||
|
|
||||||
|
private var isNearbyLocked = false
|
||||||
|
private val currentLatLng: LatLng? = null
|
||||||
|
private var exploreMapController: ExploreMapController? = null
|
||||||
|
private var exploreMapFragmentView: ExploreMapContract.View? = DUMMY
|
||||||
|
|
||||||
|
override fun updateMap(locationChangeType: LocationChangeType) {
|
||||||
|
Timber.d("Presenter updates map and list$locationChangeType")
|
||||||
|
if (isNearbyLocked) {
|
||||||
|
Timber.d("Nearby is locked, so updateMapAndList returns")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exploreMapFragmentView!!.isNetworkConnectionEstablished()) {
|
||||||
|
Timber.d("Network connection is not established")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Significant changed - Markers and current location will be updated together
|
||||||
|
* Slightly changed - Only current position marker will be updated
|
||||||
|
*/
|
||||||
|
if (locationChangeType == LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED) {
|
||||||
|
Timber.d("LOCATION_SIGNIFICANTLY_CHANGED")
|
||||||
|
var populateLatLng = exploreMapFragmentView!!.getMapCenter()
|
||||||
|
|
||||||
|
//If "Show in Explore" was selected in Nearby, use the previous LatLng
|
||||||
|
if (exploreMapFragmentView is ExploreMapFragment) {
|
||||||
|
val exploreMapFragment = exploreMapFragmentView as ExploreMapFragment
|
||||||
|
if (exploreMapFragment.recentlyCameFromNearbyMap()) {
|
||||||
|
//Ensure this LatLng will not be used again if user searches their GPS location
|
||||||
|
exploreMapFragment.setRecentlyCameFromNearbyMap(false)
|
||||||
|
|
||||||
|
populateLatLng = exploreMapFragment.previousLatLng
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lockUnlockNearby(true)
|
||||||
|
exploreMapFragmentView!!.setProgressBarVisibility(true)
|
||||||
|
exploreMapFragmentView!!.populatePlaces(populateLatLng)
|
||||||
|
} else if (locationChangeType == LocationChangeType.SEARCH_CUSTOM_AREA) {
|
||||||
|
Timber.d("SEARCH_CUSTOM_AREA")
|
||||||
|
lockUnlockNearby(true)
|
||||||
|
exploreMapFragmentView!!.setProgressBarVisibility(true)
|
||||||
|
exploreMapFragmentView!!.populatePlaces(exploreMapFragmentView!!.getMapFocus())
|
||||||
|
} else { // Means location changed slightly, ie user is walking or driving.
|
||||||
|
Timber.d("Means location changed slightly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nearby updates takes time, since they are network operations. During update time, we don't
|
||||||
|
* want to get any other calls from user. So locking nearby.
|
||||||
|
*
|
||||||
|
* @param isNearbyLocked true means lock, false means unlock
|
||||||
|
*/
|
||||||
|
override fun lockUnlockNearby(isNearbyLocked: Boolean) {
|
||||||
|
this.isNearbyLocked = isNearbyLocked
|
||||||
|
if (isNearbyLocked) {
|
||||||
|
exploreMapFragmentView!!.disableFABRecenter()
|
||||||
|
} else {
|
||||||
|
exploreMapFragmentView!!.enableFABRecenter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun attachView(view: ExploreMapContract.View?) {
|
||||||
|
exploreMapFragmentView = view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun detachView() {
|
||||||
|
exploreMapFragmentView = DUMMY
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets click listener of FAB
|
||||||
|
*/
|
||||||
|
override fun setActionListeners(applicationKvStore: JsonKvStore?) {
|
||||||
|
exploreMapFragmentView!!.setFABRecenterAction {
|
||||||
|
exploreMapFragmentView!!.recenterMap(currentLatLng)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun backButtonClicked(): Boolean =
|
||||||
|
exploreMapFragmentView!!.backButtonClicked()
|
||||||
|
|
||||||
|
fun onMapReady(exploreMapController: ExploreMapController?) {
|
||||||
|
this.exploreMapController = exploreMapController
|
||||||
|
if (null != exploreMapFragmentView) {
|
||||||
|
exploreMapFragmentView!!.addSearchThisAreaButtonAction()
|
||||||
|
initializeMapOperations()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun initializeMapOperations() {
|
||||||
|
lockUnlockNearby(false)
|
||||||
|
updateMap(LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadAttractionsFromLocation(
|
||||||
|
currentLatLng: LatLng?,
|
||||||
|
searchLatLng: LatLng?, checkingAroundCurrent: Boolean
|
||||||
|
): Observable<ExplorePlacesInfo?> = Observable.fromCallable(Callable {
|
||||||
|
exploreMapController!!.loadAttractionsFromLocation(
|
||||||
|
currentLatLng,
|
||||||
|
searchLatLng,
|
||||||
|
checkingAroundCurrent
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populates places for custom location, should be used for finding nearby places around a
|
||||||
|
* location where you are not at.
|
||||||
|
*
|
||||||
|
* @param explorePlacesInfo This variable has placeToCenter list information and distances.
|
||||||
|
*/
|
||||||
|
fun updateMapMarkers(
|
||||||
|
explorePlacesInfo: ExplorePlacesInfo
|
||||||
|
) {
|
||||||
|
if (explorePlacesInfo.mediaList != null) {
|
||||||
|
prepareNearbyBaseMarkers(explorePlacesInfo)
|
||||||
|
} else {
|
||||||
|
lockUnlockNearby(false) // So that new location updates wont come
|
||||||
|
exploreMapFragmentView!!.setProgressBarVisibility(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun prepareNearbyBaseMarkers(explorePlacesInfo: ExplorePlacesInfo) {
|
||||||
|
loadAttractionsFromLocationToBaseMarkerOptions(
|
||||||
|
explorePlacesInfo.currentLatLng, // Curlatlang will be used to calculate distances
|
||||||
|
explorePlacesInfo.explorePlaceList,
|
||||||
|
exploreMapFragmentView!!.getContext()!!,
|
||||||
|
this,
|
||||||
|
explorePlacesInfo
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onNearbyBaseMarkerThumbsReady(
|
||||||
|
baseMarkers: List<BaseMarker>?,
|
||||||
|
explorePlacesInfo: ExplorePlacesInfo?
|
||||||
|
) {
|
||||||
|
if (null != exploreMapFragmentView) {
|
||||||
|
exploreMapFragmentView!!.addMarkersToMap(baseMarkers)
|
||||||
|
lockUnlockNearby(false) // So that new location updates wont come
|
||||||
|
exploreMapFragmentView!!.setProgressBarVisibility(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onSearchThisAreaClicked(): View.OnClickListener {
|
||||||
|
return View.OnClickListener {
|
||||||
|
// Lock map operations during search this area operation
|
||||||
|
exploreMapFragmentView!!.setSearchThisAreaButtonVisibility(false)
|
||||||
|
updateMap(if (searchCloseToCurrentLocation()) {
|
||||||
|
LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED
|
||||||
|
} else {
|
||||||
|
LocationChangeType.SEARCH_CUSTOM_AREA
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if search this area button is used around our current location, so that we can
|
||||||
|
* continue following our current location again
|
||||||
|
*
|
||||||
|
* @return Returns true if search this area button is used around our current location
|
||||||
|
*/
|
||||||
|
private fun searchCloseToCurrentLocation(): Boolean {
|
||||||
|
if (null == exploreMapFragmentView!!.getLastMapFocus()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
val mylocation = Location("").apply {
|
||||||
|
latitude = exploreMapFragmentView!!.getLastMapFocus()!!.latitude
|
||||||
|
longitude = exploreMapFragmentView!!.getLastMapFocus()!!.longitude
|
||||||
|
}
|
||||||
|
val dest_location = Location("").apply {
|
||||||
|
latitude = exploreMapFragmentView!!.getMapFocus()!!.latitude
|
||||||
|
longitude = exploreMapFragmentView!!.getMapFocus()!!.longitude
|
||||||
|
}
|
||||||
|
|
||||||
|
val distance = mylocation.distanceTo(dest_location)
|
||||||
|
|
||||||
|
return !(distance > 2000.0 * 3 / 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val DUMMY = Proxy.newProxyInstance(
|
||||||
|
ExploreMapContract.View::class.java.classLoader,
|
||||||
|
arrayOf<Class<*>>(ExploreMapContract.View::class.java)
|
||||||
|
) { _: Any?, method: Method, _: Array<Any?>? ->
|
||||||
|
when {
|
||||||
|
method.name == "onMyEvent" -> null
|
||||||
|
String::class.java == method.returnType -> ""
|
||||||
|
Int::class.java == method.returnType -> 0
|
||||||
|
Int::class.javaPrimitiveType == method.returnType -> 0
|
||||||
|
Boolean::class.java == method.returnType -> java.lang.Boolean.FALSE
|
||||||
|
Boolean::class.javaPrimitiveType == method.returnType -> false
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
} as ExploreMapContract.View
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,202 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.recentsearches;
|
|
||||||
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.content.UriMatcher;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.database.sqlite.SQLiteQueryBuilder;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
import static android.content.UriMatcher.NO_MATCH;
|
|
||||||
import static fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao.Table.ALL_FIELDS;
|
|
||||||
import static fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao.Table.COLUMN_ID;
|
|
||||||
import static fr.free.nrw.commons.explore.recentsearches.RecentSearchesDao.Table.TABLE_NAME;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class contains functions for executing queries for
|
|
||||||
* inserting, searching, deleting, editing recent searches in SqLite DB
|
|
||||||
**/
|
|
||||||
public class RecentSearchesContentProvider extends CommonsDaggerContentProvider {
|
|
||||||
|
|
||||||
// For URI matcher
|
|
||||||
private static final int RECENT_SEARCHES = 1;
|
|
||||||
private static final int RECENT_SEARCHES_ID = 2;
|
|
||||||
private static final String BASE_PATH = "recent_searches";
|
|
||||||
public static final Uri BASE_URI = Uri.parse("content://" + BuildConfig.RECENT_SEARCH_AUTHORITY + "/" + BASE_PATH);
|
|
||||||
private static final UriMatcher uriMatcher = new UriMatcher(NO_MATCH);
|
|
||||||
|
|
||||||
static {
|
|
||||||
uriMatcher.addURI(BuildConfig.RECENT_SEARCH_AUTHORITY, BASE_PATH, RECENT_SEARCHES);
|
|
||||||
uriMatcher.addURI(BuildConfig.RECENT_SEARCH_AUTHORITY, BASE_PATH + "/#", RECENT_SEARCHES_ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Uri uriForId(int id) {
|
|
||||||
return Uri.parse(BASE_URI.toString() + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Inject DBOpenHelper dbOpenHelper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This functions executes query for searching recent searches in SqLite DB
|
|
||||||
**/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
|
|
||||||
String[] selectionArgs, String sortOrder) {
|
|
||||||
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
|
|
||||||
queryBuilder.setTables(TABLE_NAME);
|
|
||||||
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
Cursor cursor;
|
|
||||||
|
|
||||||
switch (uriType) {
|
|
||||||
case RECENT_SEARCHES:
|
|
||||||
cursor = queryBuilder.query(db, projection, selection, selectionArgs,
|
|
||||||
null, null, sortOrder);
|
|
||||||
break;
|
|
||||||
case RECENT_SEARCHES_ID:
|
|
||||||
cursor = queryBuilder.query(db,
|
|
||||||
ALL_FIELDS,
|
|
||||||
"_id = ?",
|
|
||||||
new String[]{uri.getLastPathSegment()},
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
sortOrder
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI" + uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
|
||||||
|
|
||||||
return cursor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getType(@NonNull Uri uri) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This functions executes query for inserting a recentSearch object in SqLite DB
|
|
||||||
**/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
long id;
|
|
||||||
switch (uriType) {
|
|
||||||
case RECENT_SEARCHES:
|
|
||||||
id = sqlDB.insert(TABLE_NAME, null, contentValues);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return Uri.parse(BASE_URI + "/" + id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This functions executes query for deleting a recentSearch object in SqLite DB
|
|
||||||
**/
|
|
||||||
@Override
|
|
||||||
public int delete(@NonNull Uri uri, String s, String[] strings) {
|
|
||||||
int rows;
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
|
|
||||||
switch (uriType) {
|
|
||||||
case RECENT_SEARCHES_ID:
|
|
||||||
Timber.d("Deleting recent searches id %s", uri.getLastPathSegment());
|
|
||||||
rows = db.delete(RecentSearchesDao.Table.TABLE_NAME,
|
|
||||||
"_id = ?",
|
|
||||||
new String[]{uri.getLastPathSegment()}
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI" + uri);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This functions executes query for inserting multiple recentSearch objects in SqLite DB
|
|
||||||
**/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
|
|
||||||
Timber.d("Hello, bulk insert! (RecentSearchesContentProvider)");
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
sqlDB.beginTransaction();
|
|
||||||
switch (uriType) {
|
|
||||||
case RECENT_SEARCHES:
|
|
||||||
for (ContentValues value : values) {
|
|
||||||
Timber.d("Inserting! %s", value);
|
|
||||||
sqlDB.insert(TABLE_NAME, null, value);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
||||||
}
|
|
||||||
sqlDB.setTransactionSuccessful();
|
|
||||||
sqlDB.endTransaction();
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return values.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This functions executes query for updating a particular recentSearch object in SqLite DB
|
|
||||||
**/
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
|
||||||
public int update(@NonNull Uri uri, ContentValues contentValues, String selection,
|
|
||||||
String[] selectionArgs) {
|
|
||||||
/*
|
|
||||||
SQL Injection warnings: First, note that we're not exposing this to the
|
|
||||||
outside world (exported="false"). Even then, we should make sure to sanitize
|
|
||||||
all user input appropriately. Input that passes through ContentValues
|
|
||||||
should be fine. So only issues are those that pass in via concating.
|
|
||||||
|
|
||||||
In here, the only concat created argument is for id. It is cast to an int,
|
|
||||||
and will error out otherwise.
|
|
||||||
*/
|
|
||||||
int uriType = uriMatcher.match(uri);
|
|
||||||
SQLiteDatabase sqlDB = dbOpenHelper.getWritableDatabase();
|
|
||||||
int rowsUpdated;
|
|
||||||
switch (uriType) {
|
|
||||||
case RECENT_SEARCHES_ID:
|
|
||||||
if (TextUtils.isEmpty(selection)) {
|
|
||||||
int id = Integer.valueOf(uri.getLastPathSegment());
|
|
||||||
rowsUpdated = sqlDB.update(TABLE_NAME,
|
|
||||||
contentValues,
|
|
||||||
COLUMN_ID + " = ?",
|
|
||||||
new String[]{String.valueOf(id)});
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Parameter `selection` should be empty when updating an ID");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unknown URI: " + uri + " with type " + uriType);
|
|
||||||
}
|
|
||||||
getContext().getContentResolver().notifyChange(uri, null);
|
|
||||||
return rowsUpdated;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,174 @@
|
||||||
|
package fr.free.nrw.commons.explore.recentsearches
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.content.UriMatcher
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import fr.free.nrw.commons.BuildConfig
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.ALL_FIELDS
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.COLUMN_ID
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.TABLE_NAME
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains functions for executing queries for
|
||||||
|
* inserting, searching, deleting, editing recent searches in SqLite DB
|
||||||
|
*/
|
||||||
|
class RecentSearchesContentProvider : CommonsDaggerContentProvider() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This functions executes query for searching recent searches in SqLite DB
|
||||||
|
*/
|
||||||
|
override fun query(
|
||||||
|
uri: Uri, projection: Array<String>?, selection: String?,
|
||||||
|
selectionArgs: Array<String>?, sortOrder: String?
|
||||||
|
): Cursor {
|
||||||
|
val queryBuilder = SQLiteQueryBuilder().apply {
|
||||||
|
tables = TABLE_NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
val uriType = uriMatcher.match(uri)
|
||||||
|
|
||||||
|
val cursor = when (uriType) {
|
||||||
|
RECENT_SEARCHES -> queryBuilder.query(
|
||||||
|
requireDb(), projection, selection, selectionArgs,
|
||||||
|
null, null, sortOrder
|
||||||
|
)
|
||||||
|
|
||||||
|
RECENT_SEARCHES_ID -> queryBuilder.query(
|
||||||
|
requireDb(),
|
||||||
|
ALL_FIELDS,
|
||||||
|
"$COLUMN_ID = ?",
|
||||||
|
arrayOf(uri.lastPathSegment),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
sortOrder
|
||||||
|
)
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unknown URI$uri")
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.setNotificationUri(context?.contentResolver, uri)
|
||||||
|
|
||||||
|
return cursor
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getType(uri: Uri): String? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This functions executes query for inserting a recentSearch object in SqLite DB
|
||||||
|
*/
|
||||||
|
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
|
||||||
|
val uriType = uriMatcher.match(uri)
|
||||||
|
val id: Long = when (uriType) {
|
||||||
|
RECENT_SEARCHES -> requireDb().insert(TABLE_NAME, null, contentValues)
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
||||||
|
}
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return "$BASE_URI/$id".toUri()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This functions executes query for deleting a recentSearch object in SqLite DB
|
||||||
|
*/
|
||||||
|
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
|
||||||
|
val rows: Int
|
||||||
|
val uriType = uriMatcher.match(uri)
|
||||||
|
when (uriType) {
|
||||||
|
RECENT_SEARCHES_ID -> {
|
||||||
|
rows = requireDb().delete(
|
||||||
|
TABLE_NAME,
|
||||||
|
"_id = ?",
|
||||||
|
arrayOf(uri.lastPathSegment)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unknown URI - $uri")
|
||||||
|
}
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This functions executes query for inserting multiple recentSearch objects in SqLite DB
|
||||||
|
*/
|
||||||
|
override fun bulkInsert(uri: Uri, values: Array<ContentValues>): Int {
|
||||||
|
val uriType = uriMatcher.match(uri)
|
||||||
|
val sqlDB = requireDb()
|
||||||
|
sqlDB.beginTransaction()
|
||||||
|
when (uriType) {
|
||||||
|
RECENT_SEARCHES -> for (value in values) {
|
||||||
|
sqlDB.insert(TABLE_NAME, null, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unknown URI: $uri")
|
||||||
|
}
|
||||||
|
sqlDB.setTransactionSuccessful()
|
||||||
|
sqlDB.endTransaction()
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return values.size
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This functions executes query for updating a particular recentSearch object in SqLite DB
|
||||||
|
*/
|
||||||
|
override fun update(
|
||||||
|
uri: Uri, contentValues: ContentValues?, selection: String?,
|
||||||
|
selectionArgs: Array<String>?
|
||||||
|
): Int {
|
||||||
|
/*
|
||||||
|
SQL Injection warnings: First, note that we're not exposing this to the
|
||||||
|
outside world (exported="false"). Even then, we should make sure to sanitize
|
||||||
|
all user input appropriately. Input that passes through ContentValues
|
||||||
|
should be fine. So only issues are those that pass in via concating.
|
||||||
|
|
||||||
|
In here, the only concat created argument is for id. It is cast to an int,
|
||||||
|
and will error out otherwise.
|
||||||
|
*/
|
||||||
|
val uriType = uriMatcher.match(uri)
|
||||||
|
val rowsUpdated: Int
|
||||||
|
when (uriType) {
|
||||||
|
RECENT_SEARCHES_ID -> if (selection.isNullOrEmpty()) {
|
||||||
|
val id = uri.lastPathSegment!!.toInt()
|
||||||
|
rowsUpdated = requireDb().update(
|
||||||
|
TABLE_NAME,
|
||||||
|
contentValues,
|
||||||
|
"$COLUMN_ID = ?",
|
||||||
|
arrayOf(id.toString())
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"Parameter `selection` should be empty when updating an ID"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unknown URI: $uri with type $uriType")
|
||||||
|
}
|
||||||
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
|
return rowsUpdated
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// For URI matcher
|
||||||
|
private const val RECENT_SEARCHES = 1
|
||||||
|
private const val RECENT_SEARCHES_ID = 2
|
||||||
|
private const val BASE_PATH = "recent_searches"
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
val BASE_URI: Uri = "content://${BuildConfig.RECENT_SEARCH_AUTHORITY}/$BASE_PATH".toUri()
|
||||||
|
|
||||||
|
private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)
|
||||||
|
|
||||||
|
init {
|
||||||
|
uriMatcher.addURI(BuildConfig.RECENT_SEARCH_AUTHORITY, BASE_PATH, RECENT_SEARCHES)
|
||||||
|
uriMatcher.addURI(BuildConfig.RECENT_SEARCH_AUTHORITY, "$BASE_PATH/#", RECENT_SEARCHES_ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun uriForId(id: Int): Uri = "$BASE_URI/$id".toUri()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,275 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.recentsearches;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.ContentProviderClient;
|
|
||||||
import android.content.ContentValues;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.explore.models.RecentSearch;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Named;
|
|
||||||
import javax.inject.Provider;
|
|
||||||
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class doesn't execute queries in database directly instead it contains the logic behind
|
|
||||||
* inserting, deleting, searching data from recent searches database.
|
|
||||||
**/
|
|
||||||
public class RecentSearchesDao {
|
|
||||||
|
|
||||||
private final Provider<ContentProviderClient> clientProvider;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public RecentSearchesDao(@Named("recentsearch") Provider<ContentProviderClient> clientProvider) {
|
|
||||||
this.clientProvider = clientProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on click of media/ categories for storing them in recent searches
|
|
||||||
* @param recentSearch a recent searches object that is to be added in SqLite DB
|
|
||||||
*/
|
|
||||||
public void save(RecentSearch recentSearch) {
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
if (recentSearch.getContentUri() == null) {
|
|
||||||
recentSearch.setContentUri(db.insert(RecentSearchesContentProvider.BASE_URI, toContentValues(recentSearch)));
|
|
||||||
} else {
|
|
||||||
db.update(recentSearch.getContentUri(), toContentValues(recentSearch), null, null);
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on confirmation of delete recent searches.
|
|
||||||
* It deletes all recent searches from the database
|
|
||||||
*/
|
|
||||||
public void deleteAll() {
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
RecentSearchesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
null,
|
|
||||||
new String[]{},
|
|
||||||
Table.COLUMN_LAST_USED + " DESC"
|
|
||||||
);
|
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
|
||||||
try {
|
|
||||||
RecentSearch recentSearch = find(fromCursor(cursor).getQuery());
|
|
||||||
if (recentSearch.getContentUri() == null) {
|
|
||||||
throw new RuntimeException("tried to delete item with no content URI");
|
|
||||||
} else {
|
|
||||||
Timber.d("QUERY_NAME %s - delete tried", recentSearch.getContentUri());
|
|
||||||
db.delete(recentSearch.getContentUri(), null, null);
|
|
||||||
Timber.d("QUERY_NAME %s - query deleted", recentSearch.getQuery());
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Timber.e(e, "query deleted");
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes a recent search from the database
|
|
||||||
*/
|
|
||||||
public void delete(RecentSearch recentSearch) {
|
|
||||||
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
if (recentSearch.getContentUri() == null) {
|
|
||||||
throw new RuntimeException("tried to delete item with no content URI");
|
|
||||||
} else {
|
|
||||||
db.delete(recentSearch.getContentUri(), null, null);
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find persisted search query in database, based on its name.
|
|
||||||
* @param name Search query Ex- "butterfly"
|
|
||||||
* @return recently searched query from database, or null if not found
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public RecentSearch find(String name) {
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query(
|
|
||||||
RecentSearchesContentProvider.BASE_URI,
|
|
||||||
Table.ALL_FIELDS,
|
|
||||||
Table.COLUMN_NAME + "=?",
|
|
||||||
new String[]{name},
|
|
||||||
null);
|
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
|
||||||
return fromCursor(cursor);
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
// This feels lazy, but to hell with checked exceptions. :)
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve recently-searched queries, ordered by descending date.
|
|
||||||
* @return a list containing recent searches
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
public List<String> recentSearches(int limit) {
|
|
||||||
List<String> items = new ArrayList<>();
|
|
||||||
Cursor cursor = null;
|
|
||||||
ContentProviderClient db = clientProvider.get();
|
|
||||||
try {
|
|
||||||
cursor = db.query( RecentSearchesContentProvider.BASE_URI, Table.ALL_FIELDS,
|
|
||||||
null, new String[]{}, Table.COLUMN_LAST_USED + " DESC");
|
|
||||||
// fixme add a limit on the original query instead of falling out of the loop?
|
|
||||||
while (cursor != null && cursor.moveToNext() && cursor.getPosition() < limit) {
|
|
||||||
items.add(fromCursor(cursor).getQuery());
|
|
||||||
}
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} finally {
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
db.release();
|
|
||||||
}
|
|
||||||
return items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* It creates an Recent Searches object from data stored in the SQLite DB by using cursor
|
|
||||||
* @param cursor
|
|
||||||
* @return RecentSearch object
|
|
||||||
*/
|
|
||||||
@NonNull
|
|
||||||
@SuppressLint("Range")
|
|
||||||
RecentSearch fromCursor(Cursor cursor) {
|
|
||||||
// Hardcoding column positions!
|
|
||||||
return new RecentSearch(
|
|
||||||
RecentSearchesContentProvider.uriForId(cursor.getInt(cursor.getColumnIndex(Table.COLUMN_ID))),
|
|
||||||
cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME)),
|
|
||||||
new Date(cursor.getLong(cursor.getColumnIndex(Table.COLUMN_LAST_USED)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class contains the database table architechture for recent searches,
|
|
||||||
* It also contains queries and logic necessary to the create, update, delete this table.
|
|
||||||
*/
|
|
||||||
private ContentValues toContentValues(RecentSearch recentSearch) {
|
|
||||||
ContentValues cv = new ContentValues();
|
|
||||||
cv.put(RecentSearchesDao.Table.COLUMN_NAME, recentSearch.getQuery());
|
|
||||||
cv.put(RecentSearchesDao.Table.COLUMN_LAST_USED, recentSearch.getLastSearched().getTime());
|
|
||||||
return cv;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class contains the database table architechture for recent searches,
|
|
||||||
* It also contains queries and logic necessary to the create, update, delete this table.
|
|
||||||
*/
|
|
||||||
public static class Table {
|
|
||||||
public static final String TABLE_NAME = "recent_searches";
|
|
||||||
public static final String COLUMN_ID = "_id";
|
|
||||||
static final String COLUMN_NAME = "name";
|
|
||||||
static final String COLUMN_LAST_USED = "last_used";
|
|
||||||
|
|
||||||
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
|
||||||
public static final String[] ALL_FIELDS = {
|
|
||||||
COLUMN_ID,
|
|
||||||
COLUMN_NAME,
|
|
||||||
COLUMN_LAST_USED,
|
|
||||||
};
|
|
||||||
|
|
||||||
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
|
|
||||||
|
|
||||||
static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
|
|
||||||
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
|
|
||||||
+ COLUMN_NAME + " STRING,"
|
|
||||||
+ COLUMN_LAST_USED + " INTEGER"
|
|
||||||
+ ");";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method creates a RecentSearchesTable in SQLiteDatabase
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
*/
|
|
||||||
public static void onCreate(SQLiteDatabase db) {
|
|
||||||
db.execSQL(CREATE_TABLE_STATEMENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method deletes RecentSearchesTable from SQLiteDatabase
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
*/
|
|
||||||
public static void onDelete(SQLiteDatabase db) {
|
|
||||||
db.execSQL(DROP_TABLE_STATEMENT);
|
|
||||||
onCreate(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on migrating from a older version to a newer version
|
|
||||||
* @param db SQLiteDatabase
|
|
||||||
* @param from Version from which we are migrating
|
|
||||||
* @param to Version to which we are migrating
|
|
||||||
*/
|
|
||||||
public static void onUpdate(SQLiteDatabase db, int from, int to) {
|
|
||||||
if (from == to) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from < 6) {
|
|
||||||
// doesn't exist yet
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from == 6) {
|
|
||||||
// table added in version 7
|
|
||||||
onCreate(db);
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (from == 7) {
|
|
||||||
from++;
|
|
||||||
onUpdate(db, from, to);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,188 @@
|
||||||
|
package fr.free.nrw.commons.explore.recentsearches
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.ContentProviderClient
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.os.RemoteException
|
||||||
|
import androidx.core.content.contentValuesOf
|
||||||
|
import fr.free.nrw.commons.explore.models.RecentSearch
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesContentProvider.Companion.BASE_URI
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesContentProvider.Companion.uriForId
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.ALL_FIELDS
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.COLUMN_ID
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.COLUMN_LAST_USED
|
||||||
|
import fr.free.nrw.commons.explore.recentsearches.RecentSearchesTable.COLUMN_NAME
|
||||||
|
import fr.free.nrw.commons.utils.getInt
|
||||||
|
import fr.free.nrw.commons.utils.getLong
|
||||||
|
import fr.free.nrw.commons.utils.getString
|
||||||
|
import java.util.Date
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
import javax.inject.Provider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class doesn't execute queries in database directly instead it contains the logic behind
|
||||||
|
* inserting, deleting, searching data from recent searches database.
|
||||||
|
*/
|
||||||
|
class RecentSearchesDao @Inject constructor(
|
||||||
|
@param:Named("recentsearch") private val clientProvider: Provider<ContentProviderClient>
|
||||||
|
) {
|
||||||
|
/**
|
||||||
|
* This method is called on click of media/ categories for storing them in recent searches
|
||||||
|
* @param recentSearch a recent searches object that is to be added in SqLite DB
|
||||||
|
*/
|
||||||
|
fun save(recentSearch: RecentSearch) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
val contentValues = toContentValues(recentSearch)
|
||||||
|
if (recentSearch.contentUri == null) {
|
||||||
|
recentSearch.contentUri = db.insert(BASE_URI, contentValues)
|
||||||
|
} else {
|
||||||
|
db.update(recentSearch.contentUri!!, contentValues, null, null)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on confirmation of delete recent searches.
|
||||||
|
* It deletes all recent searches from the database
|
||||||
|
*/
|
||||||
|
fun deleteAll() {
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI,
|
||||||
|
ALL_FIELDS,
|
||||||
|
null,
|
||||||
|
arrayOf(),
|
||||||
|
"$COLUMN_LAST_USED DESC"
|
||||||
|
)
|
||||||
|
while (cursor != null && cursor.moveToNext()) {
|
||||||
|
try {
|
||||||
|
val recentSearch = find(fromCursor(cursor).query)
|
||||||
|
if (recentSearch!!.contentUri == null) {
|
||||||
|
throw RuntimeException("tried to delete item with no content URI")
|
||||||
|
} else {
|
||||||
|
db.delete(recentSearch.contentUri!!, null, null)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a recent search from the database
|
||||||
|
*/
|
||||||
|
fun delete(recentSearch: RecentSearch) {
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
if (recentSearch.contentUri == null) {
|
||||||
|
throw RuntimeException("tried to delete item with no content URI")
|
||||||
|
} else {
|
||||||
|
db.delete(recentSearch.contentUri!!, null, null)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find persisted search query in database, based on its name.
|
||||||
|
* @param name Search query Ex- "butterfly"
|
||||||
|
* @return recently searched query from database, or null if not found
|
||||||
|
*/
|
||||||
|
fun find(name: String): RecentSearch? {
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI,
|
||||||
|
ALL_FIELDS,
|
||||||
|
"$COLUMN_NAME=?",
|
||||||
|
arrayOf(name),
|
||||||
|
null
|
||||||
|
)
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
return fromCursor(cursor)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
// This feels lazy, but to hell with checked exceptions. :)
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve recently-searched queries, ordered by descending date.
|
||||||
|
* @return a list containing recent searches
|
||||||
|
*/
|
||||||
|
fun recentSearches(limit: Int): List<String> {
|
||||||
|
val items: MutableList<String> = mutableListOf()
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
val db = clientProvider.get()
|
||||||
|
try {
|
||||||
|
cursor = db.query(
|
||||||
|
BASE_URI, ALL_FIELDS,
|
||||||
|
null, arrayOf(), "$COLUMN_LAST_USED DESC"
|
||||||
|
)
|
||||||
|
// fixme add a limit on the original query instead of falling out of the loop?
|
||||||
|
while (cursor != null && cursor.moveToNext() && cursor.position < limit) {
|
||||||
|
items.add(fromCursor(cursor).query)
|
||||||
|
}
|
||||||
|
} catch (e: RemoteException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
db.release()
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It creates an Recent Searches object from data stored in the SQLite DB by using cursor
|
||||||
|
* @param cursor
|
||||||
|
* @return RecentSearch object
|
||||||
|
*/
|
||||||
|
fun fromCursor(cursor: Cursor): RecentSearch {
|
||||||
|
var query = cursor.getString(COLUMN_NAME)
|
||||||
|
|
||||||
|
if (query == null) {
|
||||||
|
query = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return RecentSearch(
|
||||||
|
uriForId(cursor.getInt(COLUMN_ID)),
|
||||||
|
query,
|
||||||
|
Date(cursor.getLong(COLUMN_LAST_USED))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains the database table architechture for recent searches,
|
||||||
|
* It also contains queries and logic necessary to the create, update, delete this table.
|
||||||
|
*/
|
||||||
|
private fun toContentValues(recentSearch: RecentSearch): ContentValues = contentValuesOf(
|
||||||
|
COLUMN_NAME to recentSearch.query,
|
||||||
|
COLUMN_LAST_USED to recentSearch.lastSearched.time
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -1,149 +0,0 @@
|
||||||
package fr.free.nrw.commons.explore.recentsearches;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.Toast;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import fr.free.nrw.commons.R;
|
|
||||||
import fr.free.nrw.commons.databinding.FragmentSearchHistoryBinding;
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
|
||||||
import fr.free.nrw.commons.explore.SearchActivity;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Displays the recent searches screen.
|
|
||||||
*/
|
|
||||||
public class RecentSearchesFragment extends CommonsDaggerSupportFragment {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
RecentSearchesDao recentSearchesDao;
|
|
||||||
List<String> recentSearches;
|
|
||||||
ArrayAdapter adapter;
|
|
||||||
|
|
||||||
private FragmentSearchHistoryBinding binding;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
||||||
Bundle savedInstanceState) {
|
|
||||||
binding = FragmentSearchHistoryBinding.inflate(inflater, container, false);
|
|
||||||
|
|
||||||
recentSearches = recentSearchesDao.recentSearches(10);
|
|
||||||
|
|
||||||
if (recentSearches.isEmpty()) {
|
|
||||||
binding.recentSearchesDeleteButton.setVisibility(View.GONE);
|
|
||||||
binding.recentSearchesTextView.setText(R.string.no_recent_searches);
|
|
||||||
}
|
|
||||||
|
|
||||||
binding.recentSearchesDeleteButton.setOnClickListener(v -> {
|
|
||||||
showDeleteRecentAlertDialog(requireContext());
|
|
||||||
});
|
|
||||||
|
|
||||||
adapter = new ArrayAdapter<>(requireContext(), R.layout.item_recent_searches,
|
|
||||||
recentSearches);
|
|
||||||
binding.recentSearchesList.setAdapter(adapter);
|
|
||||||
binding.recentSearchesList.setOnItemClickListener((parent, view, position, id) -> (
|
|
||||||
(SearchActivity) getContext()).updateText(recentSearches.get(position)));
|
|
||||||
binding.recentSearchesList.setOnItemLongClickListener((parent, view, position, id) -> {
|
|
||||||
showDeleteAlertDialog(requireContext(), position);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
updateRecentSearches();
|
|
||||||
|
|
||||||
return binding.getRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showDeleteRecentAlertDialog(@NonNull final Context context) {
|
|
||||||
new AlertDialog.Builder(context)
|
|
||||||
.setMessage(getString(R.string.delete_recent_searches_dialog))
|
|
||||||
.setPositiveButton(android.R.string.yes,
|
|
||||||
(dialog, which) -> setDeleteRecentPositiveButton(context, dialog))
|
|
||||||
.setNegativeButton(android.R.string.no, null)
|
|
||||||
.setCancelable(false)
|
|
||||||
.create()
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setDeleteRecentPositiveButton(@NonNull final Context context,
|
|
||||||
final DialogInterface dialog) {
|
|
||||||
recentSearchesDao.deleteAll();
|
|
||||||
if (binding != null) {
|
|
||||||
binding.recentSearchesDeleteButton.setVisibility(View.GONE);
|
|
||||||
binding.recentSearchesTextView.setText(R.string.no_recent_searches);
|
|
||||||
Toast.makeText(getContext(), getString(R.string.search_history_deleted),
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
recentSearches = recentSearchesDao.recentSearches(10);
|
|
||||||
adapter = new ArrayAdapter<>(context, R.layout.item_recent_searches,
|
|
||||||
recentSearches);
|
|
||||||
binding.recentSearchesList.setAdapter(adapter);
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
dialog.dismiss();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showDeleteAlertDialog(@NonNull final Context context, final int position) {
|
|
||||||
new AlertDialog.Builder(context)
|
|
||||||
.setMessage(R.string.delete_search_dialog)
|
|
||||||
.setPositiveButton(getString(R.string.delete).toUpperCase(Locale.ROOT),
|
|
||||||
((dialog, which) -> setDeletePositiveButton(context, dialog, position)))
|
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
|
||||||
.setCancelable(false)
|
|
||||||
.create()
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setDeletePositiveButton(@NonNull final Context context,
|
|
||||||
final DialogInterface dialog, final int position) {
|
|
||||||
recentSearchesDao.delete(recentSearchesDao.find(recentSearches.get(position)));
|
|
||||||
recentSearches = recentSearchesDao.recentSearches(10);
|
|
||||||
adapter = new ArrayAdapter<>(context, R.layout.item_recent_searches,
|
|
||||||
recentSearches);
|
|
||||||
if (binding != null){
|
|
||||||
binding.recentSearchesList.setAdapter(adapter);
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
dialog.dismiss();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called on back press of activity so we are updating the list from database to
|
|
||||||
* refresh the recent searches list.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
updateRecentSearches();
|
|
||||||
super.onResume();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called when search query is null to update Recent Searches
|
|
||||||
*/
|
|
||||||
public void updateRecentSearches() {
|
|
||||||
recentSearches = recentSearchesDao.recentSearches(10);
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
|
|
||||||
if (!recentSearches.isEmpty()) {
|
|
||||||
if (binding!= null) {
|
|
||||||
binding.recentSearchesDeleteButton.setVisibility(View.VISIBLE);
|
|
||||||
binding.recentSearchesTextView.setText(R.string.search_recent_header);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
|
|
||||||
if (binding != null) {
|
|
||||||
binding = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,153 @@
|
||||||
|
package fr.free.nrw.commons.explore.recentsearches
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.DialogInterface
|
||||||
|
import android.content.DialogInterface.OnClickListener
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.AdapterView
|
||||||
|
import android.widget.AdapterView.OnItemClickListener
|
||||||
|
import android.widget.AdapterView.OnItemLongClickListener
|
||||||
|
import android.widget.ArrayAdapter
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.appcompat.app.AlertDialog
|
||||||
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.databinding.FragmentSearchHistoryBinding
|
||||||
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
|
||||||
|
import fr.free.nrw.commons.explore.SearchActivity
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the recent searches screen.
|
||||||
|
*/
|
||||||
|
class RecentSearchesFragment : CommonsDaggerSupportFragment() {
|
||||||
|
@JvmField
|
||||||
|
@Inject
|
||||||
|
var recentSearchesDao: RecentSearchesDao? = null
|
||||||
|
|
||||||
|
private var recentSearches: List<String> = emptyList()
|
||||||
|
private lateinit var adapter: ArrayAdapter<String>
|
||||||
|
private var binding: FragmentSearchHistoryBinding? = null
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View {
|
||||||
|
binding = FragmentSearchHistoryBinding.inflate(inflater, container, false)
|
||||||
|
|
||||||
|
recentSearches = recentSearchesDao!!.recentSearches(10)
|
||||||
|
|
||||||
|
if (recentSearches.isEmpty()) {
|
||||||
|
binding!!.recentSearchesDeleteButton.visibility = View.GONE
|
||||||
|
binding!!.recentSearchesTextView.setText(R.string.no_recent_searches)
|
||||||
|
}
|
||||||
|
|
||||||
|
binding!!.recentSearchesDeleteButton.setOnClickListener { v: View? ->
|
||||||
|
showDeleteRecentAlertDialog(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
adapter = ArrayAdapter(requireContext(), R.layout.item_recent_searches, recentSearches)
|
||||||
|
binding!!.recentSearchesList.adapter = adapter
|
||||||
|
binding!!.recentSearchesList.onItemClickListener =
|
||||||
|
OnItemClickListener { _: AdapterView<*>?, _: View?, position: Int, _: Long ->
|
||||||
|
(context as SearchActivity).updateText(recentSearches[position])
|
||||||
|
}
|
||||||
|
binding!!.recentSearchesList.onItemLongClickListener =
|
||||||
|
OnItemLongClickListener { _: AdapterView<*>?, _: View?, position: Int, _: Long ->
|
||||||
|
showDeleteAlertDialog(requireContext(), position)
|
||||||
|
true
|
||||||
|
}
|
||||||
|
updateRecentSearches()
|
||||||
|
|
||||||
|
return binding!!.root
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showDeleteRecentAlertDialog(context: Context) {
|
||||||
|
AlertDialog.Builder(context)
|
||||||
|
.setMessage(getString(R.string.delete_recent_searches_dialog))
|
||||||
|
.setPositiveButton(R.string.yes) { dialog: DialogInterface, _: Int ->
|
||||||
|
setDeleteRecentPositiveButton(context, dialog)
|
||||||
|
}
|
||||||
|
.setNegativeButton(R.string.no, null)
|
||||||
|
.setCancelable(false)
|
||||||
|
.create()
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setDeleteRecentPositiveButton(context: Context, dialog: DialogInterface) {
|
||||||
|
recentSearchesDao!!.deleteAll()
|
||||||
|
if (binding != null) {
|
||||||
|
binding!!.recentSearchesDeleteButton.visibility = View.GONE
|
||||||
|
binding!!.recentSearchesTextView.setText(R.string.no_recent_searches)
|
||||||
|
Toast.makeText(
|
||||||
|
getContext(), getString(R.string.search_history_deleted),
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
recentSearches = recentSearchesDao!!.recentSearches(10)
|
||||||
|
adapter = ArrayAdapter(context, R.layout.item_recent_searches, recentSearches)
|
||||||
|
binding!!.recentSearchesList.adapter = adapter
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showDeleteAlertDialog(context: Context, position: Int) {
|
||||||
|
AlertDialog.Builder(context)
|
||||||
|
.setMessage(R.string.delete_search_dialog)
|
||||||
|
.setPositiveButton(
|
||||||
|
getString(R.string.delete).uppercase(),
|
||||||
|
{ dialog: DialogInterface, _: Int ->
|
||||||
|
setDeletePositiveButton(context, dialog, position)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.setCancelable(false)
|
||||||
|
.create()
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun setDeletePositiveButton(context: Context, dialog: DialogInterface, position: Int) {
|
||||||
|
recentSearchesDao!!.delete(recentSearchesDao!!.find(recentSearches[position])!!)
|
||||||
|
recentSearches = recentSearchesDao!!.recentSearches(10)
|
||||||
|
adapter = ArrayAdapter(
|
||||||
|
context, R.layout.item_recent_searches,
|
||||||
|
recentSearches
|
||||||
|
)
|
||||||
|
if (binding != null) {
|
||||||
|
binding!!.recentSearchesList.adapter = adapter
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on back press of activity so we are updating the list from database to
|
||||||
|
* refresh the recent searches list.
|
||||||
|
*/
|
||||||
|
override fun onResume() {
|
||||||
|
updateRecentSearches()
|
||||||
|
super.onResume()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when search query is null to update Recent Searches
|
||||||
|
*/
|
||||||
|
fun updateRecentSearches() {
|
||||||
|
recentSearches = recentSearchesDao!!.recentSearches(10)
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
|
||||||
|
if (recentSearches.isNotEmpty()) {
|
||||||
|
if (binding != null) {
|
||||||
|
binding!!.recentSearchesDeleteButton.visibility = View.VISIBLE
|
||||||
|
binding!!.recentSearchesTextView.setText(R.string.search_recent_header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
binding = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
package fr.free.nrw.commons.explore.recentsearches
|
||||||
|
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains the database table architechture for recent searches, It also contains
|
||||||
|
* queries and logic necessary to the create, update, delete this table.
|
||||||
|
*/
|
||||||
|
object RecentSearchesTable {
|
||||||
|
const val TABLE_NAME: String = "recent_searches"
|
||||||
|
const val COLUMN_ID: String = "_id"
|
||||||
|
const val COLUMN_NAME: String = "name"
|
||||||
|
const val COLUMN_LAST_USED: String = "last_used"
|
||||||
|
|
||||||
|
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
|
||||||
|
@JvmField
|
||||||
|
val ALL_FIELDS = arrayOf(
|
||||||
|
COLUMN_ID,
|
||||||
|
COLUMN_NAME,
|
||||||
|
COLUMN_LAST_USED,
|
||||||
|
)
|
||||||
|
|
||||||
|
const val DROP_TABLE_STATEMENT: String = "DROP TABLE IF EXISTS $TABLE_NAME"
|
||||||
|
|
||||||
|
const val CREATE_TABLE_STATEMENT: String = ("CREATE TABLE $TABLE_NAME ($COLUMN_ID INTEGER PRIMARY KEY,$COLUMN_NAME STRING,$COLUMN_LAST_USED INTEGER);")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method creates a RecentSearchesTable in SQLiteDatabase
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
*/
|
||||||
|
fun onCreate(db: SQLiteDatabase) = db.execSQL(CREATE_TABLE_STATEMENT)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method deletes RecentSearchesTable from SQLiteDatabase
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
*/
|
||||||
|
fun onDelete(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(DROP_TABLE_STATEMENT)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called on migrating from a older version to a newer version
|
||||||
|
*
|
||||||
|
* @param db SQLiteDatabase
|
||||||
|
* @param from Version from which we are migrating
|
||||||
|
* @param to Version to which we are migrating
|
||||||
|
*/
|
||||||
|
fun onUpdate(db: SQLiteDatabase, from: Int, to: Int) {
|
||||||
|
if (from == to) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (from < 6) {
|
||||||
|
// doesn't exist yet
|
||||||
|
onUpdate(db, from + 1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (from == 6) {
|
||||||
|
// table added in version 7
|
||||||
|
onCreate(db)
|
||||||
|
onUpdate(db, from + 1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (from == 7) {
|
||||||
|
onUpdate(db, from + 1, to)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -296,10 +296,19 @@ object FilePicker : Constants {
|
||||||
* https://github.com/commons-app/apps-android-commons/issues/6357
|
* https://github.com/commons-app/apps-android-commons/issues/6357
|
||||||
*/
|
*/
|
||||||
private fun takePersistableUriPermissions(context: Context, result: ActivityResult) {
|
private fun takePersistableUriPermissions(context: Context, result: ActivityResult) {
|
||||||
result.data?.data?.also { uri ->
|
result.data?.let { intentData ->
|
||||||
val takeFlags: Int = (Intent.FLAG_GRANT_READ_URI_PERMISSION
|
val takeFlags: Int = (Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
// Persist the URI permission for all URIs in the clip data
|
||||||
context.contentResolver.takePersistableUriPermission(uri, takeFlags)
|
// if multiple images are selected,
|
||||||
|
// or for the single URI if only one image is selected
|
||||||
|
intentData.clipData?.let { clipData ->
|
||||||
|
for (i in 0 until clipData.itemCount) {
|
||||||
|
context.contentResolver.takePersistableUriPermission(
|
||||||
|
clipData.getItemAt(i).uri, takeFlags)
|
||||||
|
}
|
||||||
|
} ?: intentData.data?.let { uri ->
|
||||||
|
context.contentResolver.takePersistableUriPermission(uri, takeFlags)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -358,6 +367,7 @@ object FilePicker : Constants {
|
||||||
callbacks: Callbacks
|
callbacks: Callbacks
|
||||||
) {
|
) {
|
||||||
if (result.resultCode == Activity.RESULT_OK && !isPhoto(result.data)) {
|
if (result.resultCode == Activity.RESULT_OK && !isPhoto(result.data)) {
|
||||||
|
takePersistableUriPermissions(activity, result)
|
||||||
try {
|
try {
|
||||||
val files = getFilesFromGalleryPictures(result.data, activity)
|
val files = getFilesFromGalleryPictures(result.data, activity)
|
||||||
callbacks.onImagesPicked(files, ImageSource.GALLERY, restoreType(activity))
|
callbacks.onImagesPicked(files, ImageSource.GALLERY, restoreType(activity))
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,68 @@
|
||||||
package fr.free.nrw.commons.fileusages
|
package fr.free.nrw.commons.fileusages
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show where file is being used on Commons and oher wikis.
|
* Data model for displaying file usage information in the UI, including the title and link to the page.
|
||||||
*/
|
*/
|
||||||
data class FileUsagesUiModel(
|
data class FileUsagesUiModel(
|
||||||
val title: String,
|
val title: String,
|
||||||
val link: String?
|
val link: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a FileUsage object to a UI model for Commons file usages.
|
||||||
|
* Creates a link to the file's page on Commons.
|
||||||
|
*/
|
||||||
fun FileUsage.toUiModel(): FileUsagesUiModel {
|
fun FileUsage.toUiModel(): FileUsagesUiModel {
|
||||||
return FileUsagesUiModel(title = title, link = "https://commons.wikimedia.org/wiki/$title")
|
// Replace spaces with underscores and URL-encode the title for the link
|
||||||
|
val encodedTitle = Uri.encode(title.replace(" ", "_"))
|
||||||
|
return FileUsagesUiModel(
|
||||||
|
title = title,
|
||||||
|
link = "https://commons.wikimedia.org/wiki/$encodedTitle"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a GlobalFileUsage object to a UI model for file usages on other wikis.
|
||||||
|
* Generates a link to the page and prefixes the title with the wiki code (e.g., "(en) Title").
|
||||||
|
*/
|
||||||
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
|
fun GlobalFileUsage.toUiModel(): FileUsagesUiModel {
|
||||||
// link is associated with sub items under wiki group (which is not used ATM)
|
// Log input values for debugging
|
||||||
return FileUsagesUiModel(title = wiki, link = null)
|
Timber.d("Converting GlobalFileUsage: wiki=$wiki, title=$title")
|
||||||
|
|
||||||
|
// Check for invalid or empty inputs
|
||||||
|
if (wiki.isBlank() || title.isBlank()) {
|
||||||
|
Timber.w("Invalid input: wiki=$wiki, title=$title")
|
||||||
|
return FileUsagesUiModel(title = title, link = null)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract wiki code for prefix (e.g., "en" from "en.wikipedia.org" or "enwiki")
|
||||||
|
val wikiCode = when {
|
||||||
|
wiki.contains(".") -> wiki.substringBefore(".") // e.g., "en" from "en.wikipedia.org"
|
||||||
|
wiki == "commonswiki" -> "commons"
|
||||||
|
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki")
|
||||||
|
else -> wiki
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create prefixed title, e.g., "(en) Changi East Depot"
|
||||||
|
val prefixedTitle = "($wikiCode) $title"
|
||||||
|
|
||||||
|
// Determine the domain for the URL
|
||||||
|
val domain = when {
|
||||||
|
wiki.contains(".") -> wiki // Already a full domain, e.g., "en.wikipedia.org"
|
||||||
|
wiki == "commonswiki" -> "commons.wikimedia.org"
|
||||||
|
wiki.endsWith("wiki") -> wiki.removeSuffix("wiki") + ".wikipedia.org"
|
||||||
|
else -> "$wiki.wikipedia.org" // Fallback for simple codes like "en"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize title: replace spaces with underscores and URL-encode
|
||||||
|
val encodedTitle = Uri.encode(title.replace(" ", "_"))
|
||||||
|
|
||||||
|
// Build the full URL
|
||||||
|
val url = "https://$domain/wiki/$encodedTitle"
|
||||||
|
Timber.d("Generated URL: $url")
|
||||||
|
|
||||||
|
return FileUsagesUiModel(title = prefixedTitle, link = url)
|
||||||
}
|
}
|
||||||
|
|
@ -64,8 +64,8 @@ class LocationPermissionsHelper(
|
||||||
activity,
|
activity,
|
||||||
activity.getString(dialogTitleResource),
|
activity.getString(dialogTitleResource),
|
||||||
activity.getString(dialogTextResource),
|
activity.getString(dialogTextResource),
|
||||||
activity.getString(android.R.string.ok),
|
activity.getString(R.string.ok),
|
||||||
activity.getString(android.R.string.cancel),
|
activity.getString(R.string.cancel),
|
||||||
{
|
{
|
||||||
ActivityCompat.requestPermissions(
|
ActivityCompat.requestPermissions(
|
||||||
activity,
|
activity,
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.IntentCompat
|
import androidx.core.content.IntentCompat
|
||||||
import androidx.core.os.BundleCompat
|
import androidx.core.os.BundleCompat
|
||||||
import androidx.core.text.HtmlCompat
|
import androidx.core.text.HtmlCompat
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||||
import fr.free.nrw.commons.CameraPosition
|
import fr.free.nrw.commons.CameraPosition
|
||||||
import fr.free.nrw.commons.CommonsApplication
|
import fr.free.nrw.commons.CommonsApplication
|
||||||
|
|
@ -44,6 +45,9 @@ import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.Compani
|
||||||
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.Companion.LAST_ZOOM
|
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.Companion.LAST_ZOOM
|
||||||
import fr.free.nrw.commons.utils.DialogUtil
|
import fr.free.nrw.commons.utils.DialogUtil
|
||||||
import fr.free.nrw.commons.utils.MapUtils.ZOOM_LEVEL
|
import fr.free.nrw.commons.utils.MapUtils.ZOOM_LEVEL
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomInsets
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomPaddingInsets
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeTopPaddingInsets
|
||||||
import fr.free.nrw.commons.utils.handleGeoCoordinates
|
import fr.free.nrw.commons.utils.handleGeoCoordinates
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
|
@ -330,12 +334,19 @@ class LocationPickerActivity : BaseActivity(), LocationPermissionCallback {
|
||||||
*/
|
*/
|
||||||
private fun getToolbarUI() {
|
private fun getToolbarUI() {
|
||||||
val toolbar: ConstraintLayout = findViewById(R.id.location_picker_toolbar)
|
val toolbar: ConstraintLayout = findViewById(R.id.location_picker_toolbar)
|
||||||
|
WindowCompat.getInsetsController(window, window.decorView)
|
||||||
|
.isAppearanceLightStatusBars = false
|
||||||
|
toolbar.applyEdgeToEdgeTopPaddingInsets()
|
||||||
largeToolbarText = findViewById(R.id.location_picker_toolbar_primary_text_view)
|
largeToolbarText = findViewById(R.id.location_picker_toolbar_primary_text_view)
|
||||||
smallToolbarText = findViewById(R.id.location_picker_toolbar_secondary_text_view)
|
smallToolbarText = findViewById(R.id.location_picker_toolbar_secondary_text_view)
|
||||||
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primaryColor))
|
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primaryColor))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupMapView() {
|
private fun setupMapView() {
|
||||||
|
|
||||||
|
val mapBottomLayout: ConstraintLayout = findViewById(R.id.map_bottom_layout)
|
||||||
|
mapBottomLayout.applyEdgeToEdgeBottomPaddingInsets()
|
||||||
|
|
||||||
requestLocationPermissions()
|
requestLocationPermissions()
|
||||||
|
|
||||||
//If location metadata is available, move map to that location.
|
//If location metadata is available, move map to that location.
|
||||||
|
|
@ -460,6 +471,7 @@ class LocationPickerActivity : BaseActivity(), LocationPermissionCallback {
|
||||||
*/
|
*/
|
||||||
private fun addPlaceSelectedButton() {
|
private fun addPlaceSelectedButton() {
|
||||||
placeSelectedButton = findViewById(R.id.location_chosen_button)
|
placeSelectedButton = findViewById(R.id.location_chosen_button)
|
||||||
|
applyEdgeToEdgeBottomInsets(placeSelectedButton)
|
||||||
placeSelectedButton.setOnClickListener { placeSelected() }
|
placeSelectedButton.setOnClickListener { placeSelected() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -541,6 +541,7 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
binding.progressBarEdit.visibility = View.GONE
|
binding.progressBarEdit.visibility = View.GONE
|
||||||
|
binding.descriptionEdit.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||||
|
|
@ -1026,12 +1027,12 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
|
||||||
val message: String = if (result) {
|
val message: String = if (result) {
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.send_thank_success_message,
|
R.string.send_thank_success_message,
|
||||||
media!!.displayTitle
|
media!!.user
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.send_thank_failure_message,
|
R.string.send_thank_failure_message,
|
||||||
media!!.displayTitle
|
media!!.user
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2128,22 +2129,17 @@ fun FileUsagesContainer(
|
||||||
val uriHandle = LocalUriHandler.current
|
val uriHandle = LocalUriHandler.current
|
||||||
|
|
||||||
Column(modifier = modifier) {
|
Column(modifier = modifier) {
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
) {
|
) {
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(R.string.usages_on_commons_heading),
|
text = stringResource(R.string.usages_on_commons_heading),
|
||||||
textAlign = TextAlign.Center,
|
textAlign = TextAlign.Center,
|
||||||
style = MaterialTheme.typography.titleSmall
|
style = MaterialTheme.typography.titleSmall
|
||||||
)
|
)
|
||||||
|
IconButton(onClick = { isCommonsListExpanded = !isCommonsListExpanded }) {
|
||||||
IconButton(onClick = {
|
|
||||||
isCommonsListExpanded = !isCommonsListExpanded
|
|
||||||
}) {
|
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = if (isCommonsListExpanded) Icons.Default.KeyboardArrowUp
|
imageVector = if (isCommonsListExpanded) Icons.Default.KeyboardArrowUp
|
||||||
else Icons.Default.KeyboardArrowDown,
|
else Icons.Default.KeyboardArrowDown,
|
||||||
|
|
@ -2157,11 +2153,8 @@ fun FileUsagesContainer(
|
||||||
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
|
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
|
||||||
LinearProgressIndicator()
|
LinearProgressIndicator()
|
||||||
}
|
}
|
||||||
|
|
||||||
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
|
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
|
||||||
|
|
||||||
val data = commonsContainerState.data
|
val data = commonsContainerState.data
|
||||||
|
|
||||||
if (data.isNullOrEmpty()) {
|
if (data.isNullOrEmpty()) {
|
||||||
ListItem(headlineContent = {
|
ListItem(headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
|
|
@ -2181,7 +2174,7 @@ fun FileUsagesContainer(
|
||||||
headlineContent = {
|
headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
modifier = Modifier.clickable {
|
modifier = Modifier.clickable {
|
||||||
uriHandle.openUri(usage.link!!)
|
usage.link?.let { uriHandle.openUri(it) }
|
||||||
},
|
},
|
||||||
text = usage.title,
|
text = usage.title,
|
||||||
style = MaterialTheme.typography.titleSmall.copy(
|
style = MaterialTheme.typography.titleSmall.copy(
|
||||||
|
|
@ -2189,11 +2182,11 @@ fun FileUsagesContainer(
|
||||||
textDecoration = TextDecoration.Underline
|
textDecoration = TextDecoration.Underline
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
|
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
|
||||||
ListItem(headlineContent = {
|
ListItem(headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
|
|
@ -2203,12 +2196,10 @@ fun FileUsagesContainer(
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
|
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
|
@ -2219,10 +2210,7 @@ fun FileUsagesContainer(
|
||||||
textAlign = TextAlign.Center,
|
textAlign = TextAlign.Center,
|
||||||
style = MaterialTheme.typography.titleSmall
|
style = MaterialTheme.typography.titleSmall
|
||||||
)
|
)
|
||||||
|
IconButton(onClick = { isOtherWikisListExpanded = !isOtherWikisListExpanded }) {
|
||||||
IconButton(onClick = {
|
|
||||||
isOtherWikisListExpanded = !isOtherWikisListExpanded
|
|
||||||
}) {
|
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = if (isOtherWikisListExpanded) Icons.Default.KeyboardArrowUp
|
imageVector = if (isOtherWikisListExpanded) Icons.Default.KeyboardArrowUp
|
||||||
else Icons.Default.KeyboardArrowDown,
|
else Icons.Default.KeyboardArrowDown,
|
||||||
|
|
@ -2236,11 +2224,8 @@ fun FileUsagesContainer(
|
||||||
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
|
MediaDetailViewModel.FileUsagesContainerState.Loading -> {
|
||||||
LinearProgressIndicator()
|
LinearProgressIndicator()
|
||||||
}
|
}
|
||||||
|
|
||||||
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
|
is MediaDetailViewModel.FileUsagesContainerState.Success -> {
|
||||||
|
|
||||||
val data = globalContainerState.data
|
val data = globalContainerState.data
|
||||||
|
|
||||||
if (data.isNullOrEmpty()) {
|
if (data.isNullOrEmpty()) {
|
||||||
ListItem(headlineContent = {
|
ListItem(headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
|
|
@ -2259,16 +2244,20 @@ fun FileUsagesContainer(
|
||||||
},
|
},
|
||||||
headlineContent = {
|
headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
|
modifier = Modifier.clickable {
|
||||||
|
usage.link?.let { uriHandle.openUri(it) }
|
||||||
|
},
|
||||||
text = usage.title,
|
text = usage.title,
|
||||||
style = MaterialTheme.typography.titleSmall.copy(
|
style = MaterialTheme.typography.titleSmall.copy(
|
||||||
|
color = Color(0xFF5A6AEC),
|
||||||
textDecoration = TextDecoration.Underline
|
textDecoration = TextDecoration.Underline
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
|
is MediaDetailViewModel.FileUsagesContainerState.Error -> {
|
||||||
ListItem(headlineContent = {
|
ListItem(headlineContent = {
|
||||||
Text(
|
Text(
|
||||||
|
|
@ -2278,10 +2267,8 @@ fun FileUsagesContainer(
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
|
MediaDetailViewModel.FileUsagesContainerState.Initial -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -166,7 +166,7 @@ class MediaDetailPagerFragment : CommonsDaggerSupportFragment(), OnPageChangeLis
|
||||||
val mediaDetailFragment = adapter!!.currentMediaDetailFragment
|
val mediaDetailFragment = adapter!!.currentMediaDetailFragment
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_bookmark_current_image -> {
|
R.id.menu_bookmark_current_image -> {
|
||||||
val bookmarkExists = bookmarkDao!!.updateBookmark(bookmark)
|
val bookmarkExists = bookmarkDao!!.updateBookmark(bookmark!!)
|
||||||
val snackbar = if (bookmarkExists) Snackbar.make(
|
val snackbar = if (bookmarkExists) Snackbar.make(
|
||||||
requireView(),
|
requireView(),
|
||||||
R.string.add_bookmark,
|
R.string.add_bookmark,
|
||||||
|
|
@ -436,7 +436,7 @@ ${m.pageTitle.canonicalUri}"""
|
||||||
bookmark = Bookmark(
|
bookmark = Bookmark(
|
||||||
m.filename,
|
m.filename,
|
||||||
m.getAuthorOrUser(),
|
m.getAuthorOrUser(),
|
||||||
BookmarkPicturesContentProvider.uriForName(m.filename)
|
BookmarkPicturesContentProvider.uriForName(m.filename!!)
|
||||||
)
|
)
|
||||||
updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_image))
|
updateBookmarkState(menu.findItem(R.id.menu_bookmark_current_image))
|
||||||
val contributionState = provider.getContributionStateAt(position)
|
val contributionState = provider.getContributionStateAt(position)
|
||||||
|
|
|
||||||
|
|
@ -114,13 +114,13 @@ class MoreBottomSheetFragment : BottomSheetDialogFragment() {
|
||||||
val level = store.getString("userAchievementsLevel", "0")
|
val level = store.getString("userAchievementsLevel", "0")
|
||||||
if (level == "0"){
|
if (level == "0"){
|
||||||
binding?.moreProfile?.text = getString(
|
binding?.moreProfile?.text = getString(
|
||||||
R.string.profileLevel,
|
R.string.profile_withoutLevel,
|
||||||
getUserName(),
|
getUserName(),
|
||||||
getString(R.string.see_your_achievements) // Second argument
|
getString(R.string.see_your_achievements) // Second argument
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
binding?.moreProfile?.text = getString(
|
binding?.moreProfile?.text = getString(
|
||||||
R.string.profileLevel,
|
R.string.profile_withLevel,
|
||||||
getUserName(),
|
getUserName(),
|
||||||
level
|
level
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ public class NearbyFilterSearchRecyclerViewAdapter
|
||||||
label.setSelected(!label.isSelected());
|
label.setSelected(!label.isSelected());
|
||||||
holder.placeTypeLayout.setSelected(label.isSelected());
|
holder.placeTypeLayout.setSelected(label.isSelected());
|
||||||
|
|
||||||
|
NearbyFilterState.setSelectedLabels(new ArrayList<>(selectedLabels));
|
||||||
callback.filterByMarkerType(selectedLabels, 0, false, false);
|
callback.filterByMarkerType(selectedLabels, 0, false, false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -152,6 +153,7 @@ public class NearbyFilterSearchRecyclerViewAdapter
|
||||||
label.setSelected(false);
|
label.setSelected(false);
|
||||||
selectedLabels.remove(label);
|
selectedLabels.remove(label);
|
||||||
}
|
}
|
||||||
|
NearbyFilterState.setSelectedLabels(new ArrayList<>(selectedLabels));
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,6 +165,7 @@ public class NearbyFilterSearchRecyclerViewAdapter
|
||||||
selectedLabels.add(label);
|
selectedLabels.add(label);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
NearbyFilterState.setSelectedLabels(new ArrayList<>(selectedLabels));
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ public class NearbyFilterState {
|
||||||
private int checkBoxTriState;
|
private int checkBoxTriState;
|
||||||
private ArrayList<Label> selectedLabels;
|
private ArrayList<Label> selectedLabels;
|
||||||
|
|
||||||
private static NearbyFilterState nearbyFılterStateInstance;
|
private static NearbyFilterState nearbyFilterStateInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define initial filter values here
|
* Define initial filter values here
|
||||||
|
|
@ -23,10 +23,10 @@ public class NearbyFilterState {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NearbyFilterState getInstance() {
|
public static NearbyFilterState getInstance() {
|
||||||
if (nearbyFılterStateInstance == null) {
|
if (nearbyFilterStateInstance == null) {
|
||||||
nearbyFılterStateInstance = new NearbyFilterState();
|
nearbyFilterStateInstance = new NearbyFilterState();
|
||||||
}
|
}
|
||||||
return nearbyFılterStateInstance;
|
return nearbyFilterStateInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setSelectedLabels(ArrayList<Label> selectedLabels) {
|
public static void setSelectedLabels(ArrayList<Label> selectedLabels) {
|
||||||
|
|
|
||||||
|
|
@ -881,6 +881,12 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
|
||||||
fun initNearbyFilter() {
|
fun initNearbyFilter() {
|
||||||
binding!!.nearbyFilterList.root.visibility = View.GONE
|
binding!!.nearbyFilterList.root.visibility = View.GONE
|
||||||
hideBottomSheet()
|
hideBottomSheet()
|
||||||
|
binding!!.nearbyFilter.searchViewLayout.searchView.apply {
|
||||||
|
setIconifiedByDefault(false)
|
||||||
|
isIconified = false
|
||||||
|
setQuery("", false)
|
||||||
|
clearFocus()
|
||||||
|
}
|
||||||
binding!!.nearbyFilter.searchViewLayout.searchView.setOnQueryTextFocusChangeListener { v, hasFocus ->
|
binding!!.nearbyFilter.searchViewLayout.searchView.setOnQueryTextFocusChangeListener { v, hasFocus ->
|
||||||
setLayoutHeightAlignedToWidth(
|
setLayoutHeightAlignedToWidth(
|
||||||
1.25,
|
1.25,
|
||||||
|
|
@ -924,6 +930,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
|
||||||
return _isDarkTheme
|
return _isDarkTheme
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
restoreStoredFilterSelection()
|
||||||
binding!!.nearbyFilterList.root
|
binding!!.nearbyFilterList.root
|
||||||
.layoutParams.width = getScreenWidth(
|
.layoutParams.width = getScreenWidth(
|
||||||
requireActivity(),
|
requireActivity(),
|
||||||
|
|
@ -942,6 +949,22 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun restoreStoredFilterSelection() {
|
||||||
|
val adapter = nearbyFilterSearchRecyclerViewAdapter ?: return
|
||||||
|
val savedLabels = ArrayList(NearbyFilterState.getInstance().selectedLabels)
|
||||||
|
adapter.selectedLabels.clear()
|
||||||
|
val savedSet = savedLabels.toSet()
|
||||||
|
Label.valuesAsList().forEach { label ->
|
||||||
|
val isSelected = savedSet.contains(label)
|
||||||
|
label.setSelected(isSelected)
|
||||||
|
if (isSelected) {
|
||||||
|
adapter.selectedLabels.add(label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NearbyFilterState.setSelectedLabels(ArrayList(adapter.selectedLabels))
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
override fun setCheckBoxAction() {
|
override fun setCheckBoxAction() {
|
||||||
binding!!.nearbyFilterList.checkboxTriStates.addAction()
|
binding!!.nearbyFilterList.checkboxTriStates.addAction()
|
||||||
binding!!.nearbyFilterList.checkboxTriStates.state = CheckBoxTriStates.UNKNOWN
|
binding!!.nearbyFilterList.checkboxTriStates.state = CheckBoxTriStates.UNKNOWN
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ class NearbyResultItem(
|
||||||
private val wikipediaArticle: ResultTuple?,
|
private val wikipediaArticle: ResultTuple?,
|
||||||
private val commonsArticle: ResultTuple?,
|
private val commonsArticle: ResultTuple?,
|
||||||
private val location: ResultTuple?,
|
private val location: ResultTuple?,
|
||||||
@field:SerializedName("itemLabel")
|
@field:SerializedName("label") private val label: ResultTuple?,
|
||||||
private val label: ResultTuple?,
|
@field:SerializedName("itemLabel") private val itemLabel: ResultTuple?,
|
||||||
@field:SerializedName("streetAddress") private val address: ResultTuple?,
|
@field:SerializedName("streetAddress") private val address: ResultTuple?,
|
||||||
private val icon: ResultTuple?,
|
private val icon: ResultTuple?,
|
||||||
@field:SerializedName("class") private val className: ResultTuple?,
|
@field:SerializedName("class") private val className: ResultTuple?,
|
||||||
|
|
@ -16,7 +16,7 @@ class NearbyResultItem(
|
||||||
@field:SerializedName("commonsCategory") private val commonsCategory: ResultTuple?,
|
@field:SerializedName("commonsCategory") private val commonsCategory: ResultTuple?,
|
||||||
@field:SerializedName("pic") private val pic: ResultTuple?,
|
@field:SerializedName("pic") private val pic: ResultTuple?,
|
||||||
@field:SerializedName("destroyed") private val destroyed: ResultTuple?,
|
@field:SerializedName("destroyed") private val destroyed: ResultTuple?,
|
||||||
@field:SerializedName("itemDescription") private val description: ResultTuple?,
|
@field:SerializedName("description") private val description: ResultTuple?,
|
||||||
@field:SerializedName("endTime") private val endTime: ResultTuple?,
|
@field:SerializedName("endTime") private val endTime: ResultTuple?,
|
||||||
@field:SerializedName("monument") private val monument: ResultTuple?,
|
@field:SerializedName("monument") private val monument: ResultTuple?,
|
||||||
@field:SerializedName("dateOfOfficialClosure") private val dateOfOfficialClosure: ResultTuple?,
|
@field:SerializedName("dateOfOfficialClosure") private val dateOfOfficialClosure: ResultTuple?,
|
||||||
|
|
@ -30,7 +30,15 @@ class NearbyResultItem(
|
||||||
|
|
||||||
fun getLocation(): ResultTuple = location ?: ResultTuple()
|
fun getLocation(): ResultTuple = location ?: ResultTuple()
|
||||||
|
|
||||||
fun getLabel(): ResultTuple = label ?: ResultTuple()
|
/**
|
||||||
|
* Returns label for display (pins, popup), using fallback to itemLabel if needed.
|
||||||
|
*/
|
||||||
|
fun getLabel(): ResultTuple = label ?: itemLabel ?: ResultTuple()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns only the original label field, for Wikidata edits.
|
||||||
|
*/
|
||||||
|
fun getOriginalLabel(): ResultTuple = label ?: ResultTuple()
|
||||||
|
|
||||||
fun getIcon(): ResultTuple = icon ?: ResultTuple()
|
fun getIcon(): ResultTuple = icon ?: ResultTuple()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import androidx.core.view.ViewGroupCompat
|
||||||
import androidx.recyclerview.widget.DividerItemDecoration
|
import androidx.recyclerview.widget.DividerItemDecoration
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
|
@ -19,8 +20,10 @@ import fr.free.nrw.commons.databinding.ActivityNotificationBinding
|
||||||
import fr.free.nrw.commons.notification.models.Notification
|
import fr.free.nrw.commons.notification.models.Notification
|
||||||
import fr.free.nrw.commons.notification.models.NotificationType
|
import fr.free.nrw.commons.notification.models.NotificationType
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeTopInsets
|
||||||
import fr.free.nrw.commons.utils.NetworkUtils
|
import fr.free.nrw.commons.utils.NetworkUtils
|
||||||
import fr.free.nrw.commons.utils.ViewUtil
|
import fr.free.nrw.commons.utils.ViewUtil
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomPaddingInsets
|
||||||
import fr.free.nrw.commons.utils.handleWebUrl
|
import fr.free.nrw.commons.utils.handleWebUrl
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
|
@ -56,6 +59,9 @@ class NotificationActivity : BaseActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
isRead = intent.getStringExtra("title") == "read"
|
isRead = intent.getStringExtra("title") == "read"
|
||||||
binding = ActivityNotificationBinding.inflate(layoutInflater)
|
binding = ActivityNotificationBinding.inflate(layoutInflater)
|
||||||
|
ViewGroupCompat.installCompatInsetsDispatch(binding.root)
|
||||||
|
applyEdgeToEdgeTopInsets(binding.toolbar.toolbar)
|
||||||
|
binding.listView.applyEdgeToEdgeBottomPaddingInsets()
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
mNotificationWorkerFragment = supportFragmentManager.findFragmentByTag(
|
mNotificationWorkerFragment = supportFragmentManager.findFragmentByTag(
|
||||||
tagNotificationWorkerFragment
|
tagNotificationWorkerFragment
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,13 @@ import fr.free.nrw.commons.databinding.ActivityProfileBinding
|
||||||
import fr.free.nrw.commons.profile.achievements.AchievementsFragment
|
import fr.free.nrw.commons.profile.achievements.AchievementsFragment
|
||||||
import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment
|
import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.utils.DialogUtil
|
import fr.free.nrw.commons.utils.DialogUtil
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This activity will set two tabs, achievements and
|
* This activity will set two tabs, achievements and
|
||||||
|
|
@ -46,7 +48,7 @@ class ProfileActivity : BaseActivity() {
|
||||||
private var contributionsFragment: ContributionsFragment? = null
|
private var contributionsFragment: ContributionsFragment? = null
|
||||||
|
|
||||||
fun setScroll(canScroll: Boolean) {
|
fun setScroll(canScroll: Boolean) {
|
||||||
binding.viewPager.setCanScroll(canScroll)
|
binding.viewPager.canScroll = canScroll
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
|
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
|
||||||
|
|
@ -61,6 +63,7 @@ class ProfileActivity : BaseActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
binding = ActivityProfileBinding.inflate(layoutInflater)
|
binding = ActivityProfileBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding.root)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
setSupportActionBar(binding.toolbarBinding.toolbar)
|
setSupportActionBar(binding.toolbarBinding.toolbar)
|
||||||
|
|
||||||
|
|
@ -120,7 +123,7 @@ class ProfileActivity : BaseActivity() {
|
||||||
val rootView = window.decorView.findViewById<View>(android.R.id.content)
|
val rootView = window.decorView.findViewById<View>(android.R.id.content)
|
||||||
val screenShot = getScreenShot(rootView)
|
val screenShot = getScreenShot(rootView)
|
||||||
if (screenShot == null) {
|
if (screenShot == null) {
|
||||||
Log.e("ERROR", "ScreenShot is null")
|
Timber.e("ScreenShot is null")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
showAlert(screenShot)
|
showAlert(screenShot)
|
||||||
|
|
|
||||||
|
|
@ -311,7 +311,7 @@ class LeaderboardFragment : CommonsDaggerSupportFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SelectionListener(private val handler: () -> Unit): AdapterView.OnItemSelectedListener {
|
private class SelectionListener(private val handler: () -> Unit): AdapterView.OnItemSelectedListener {
|
||||||
override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) =
|
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) =
|
||||||
handler()
|
handler()
|
||||||
|
|
||||||
override fun onNothingSelected(p0: AdapterView<*>?) = Unit
|
override fun onNothingSelected(p0: AdapterView<*>?) = Unit
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,11 @@ package fr.free.nrw.commons.quiz
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
|
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
|
||||||
|
|
||||||
import com.facebook.drawee.drawable.ProgressBarDrawable
|
import com.facebook.drawee.drawable.ProgressBarDrawable
|
||||||
|
|
@ -15,6 +17,7 @@ import fr.free.nrw.commons.databinding.ActivityQuizBinding
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
|
|
||||||
|
|
||||||
class QuizActivity : AppCompatActivity() {
|
class QuizActivity : AppCompatActivity() {
|
||||||
|
|
@ -37,7 +40,11 @@ class QuizActivity : AppCompatActivity() {
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
enableEdgeToEdge()
|
||||||
binding = ActivityQuizBinding.inflate(layoutInflater)
|
binding = ActivityQuizBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding.root)
|
||||||
|
WindowCompat.getInsetsController(window, window.decorView)
|
||||||
|
.isAppearanceLightStatusBars = true
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
quizController.initialize(this)
|
quizController.initialize(this)
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ class QuizChecker @Inject constructor(
|
||||||
activity.getString(R.string.quiz),
|
activity.getString(R.string.quiz),
|
||||||
activity.getString(R.string.quiz_alert_message, revertPercentageForMessage),
|
activity.getString(R.string.quiz_alert_message, revertPercentageForMessage),
|
||||||
activity.getString(R.string.about_translate_proceed),
|
activity.getString(R.string.about_translate_proceed),
|
||||||
activity.getString(android.R.string.cancel),
|
activity.getString(R.string.cancel),
|
||||||
{ startQuizActivity(activity) },
|
{ startQuizActivity(activity) },
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,11 @@ import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
|
|
||||||
import fr.free.nrw.commons.databinding.ActivityQuizResultBinding
|
import fr.free.nrw.commons.databinding.ActivityQuizResultBinding
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
@ -22,6 +24,7 @@ import java.io.FileOutputStream
|
||||||
|
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.contributions.MainActivity
|
import fr.free.nrw.commons.contributions.MainActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,7 +38,11 @@ class QuizResultActivity : AppCompatActivity() {
|
||||||
|
|
||||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
enableEdgeToEdge()
|
||||||
binding = ActivityQuizResultBinding.inflate(layoutInflater)
|
binding = ActivityQuizResultBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding!!.root)
|
||||||
|
WindowCompat.getInsetsController(window, window.decorView)
|
||||||
|
.isAppearanceLightStatusBars = true
|
||||||
setContentView(binding?.root)
|
setContentView(binding?.root)
|
||||||
|
|
||||||
setSupportActionBar(binding?.toolbar?.toolbar)
|
setSupportActionBar(binding?.toolbar?.toolbar)
|
||||||
|
|
@ -186,7 +193,7 @@ class QuizResultActivity : AppCompatActivity() {
|
||||||
alertadd.setPositiveButton(R.string.about_translate_proceed) { dialog, _ ->
|
alertadd.setPositiveButton(R.string.about_translate_proceed) { dialog, _ ->
|
||||||
shareScreen(screenshot)
|
shareScreen(screenshot)
|
||||||
}
|
}
|
||||||
alertadd.setNegativeButton(android.R.string.cancel) { dialog, _ ->
|
alertadd.setNegativeButton(R.string.cancel) { dialog, _ ->
|
||||||
dialog.cancel()
|
dialog.cancel()
|
||||||
}
|
}
|
||||||
alertadd.show()
|
alertadd.show()
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,13 @@ package fr.free.nrw.commons.recentlanguages
|
||||||
|
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.database.sqlite.SQLiteDatabase
|
|
||||||
import android.database.sqlite.SQLiteQueryBuilder
|
import android.database.sqlite.SQLiteQueryBuilder
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.TextUtils
|
|
||||||
import fr.free.nrw.commons.BuildConfig
|
import fr.free.nrw.commons.BuildConfig
|
||||||
import fr.free.nrw.commons.data.DBOpenHelper
|
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
|
||||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.COLUMN_NAME
|
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.COLUMN_NAME
|
||||||
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.TABLE_NAME
|
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.TABLE_NAME
|
||||||
import javax.inject.Inject
|
import androidx.core.net.toUri
|
||||||
import timber.log.Timber
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -23,27 +19,17 @@ class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val BASE_PATH = "recent_languages"
|
private const val BASE_PATH = "recent_languages"
|
||||||
val BASE_URI: Uri =
|
val BASE_URI: Uri = "content://${BuildConfig.RECENT_LANGUAGE_AUTHORITY}/$BASE_PATH".toUri()
|
||||||
Uri.parse(
|
|
||||||
"content://${BuildConfig.RECENT_LANGUAGE_AUTHORITY}/$BASE_PATH"
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append language code to the base URI
|
* Append language code to the base URI
|
||||||
* @param languageCode Code of a language
|
* @param languageCode Code of a language
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun uriForCode(languageCode: String): Uri {
|
fun uriForCode(languageCode: String): Uri = "$BASE_URI/$languageCode".toUri()
|
||||||
return Uri.parse("$BASE_URI/$languageCode")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
override fun getType(uri: Uri): String? = null
|
||||||
lateinit var dbOpenHelper: DBOpenHelper
|
|
||||||
|
|
||||||
override fun getType(uri: Uri): String? {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries the SQLite database for the recently used languages
|
* Queries the SQLite database for the recently used languages
|
||||||
|
|
@ -60,11 +46,12 @@ class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
|
||||||
selectionArgs: Array<String>?,
|
selectionArgs: Array<String>?,
|
||||||
sortOrder: String?
|
sortOrder: String?
|
||||||
): Cursor? {
|
): Cursor? {
|
||||||
val queryBuilder = SQLiteQueryBuilder()
|
val queryBuilder = SQLiteQueryBuilder().apply {
|
||||||
queryBuilder.tables = TABLE_NAME
|
tables = TABLE_NAME
|
||||||
val db = dbOpenHelper.readableDatabase
|
}
|
||||||
|
|
||||||
val cursor = queryBuilder.query(
|
val cursor = queryBuilder.query(
|
||||||
db,
|
requireDb(),
|
||||||
projection,
|
projection,
|
||||||
selection,
|
selection,
|
||||||
selectionArgs,
|
selectionArgs,
|
||||||
|
|
@ -89,12 +76,11 @@ class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
|
||||||
selection: String?,
|
selection: String?,
|
||||||
selectionArgs: Array<String>?
|
selectionArgs: Array<String>?
|
||||||
): Int {
|
): Int {
|
||||||
val sqlDB = dbOpenHelper.writableDatabase
|
|
||||||
val rowsUpdated: Int
|
val rowsUpdated: Int
|
||||||
if (selection.isNullOrEmpty()) {
|
if (selection.isNullOrEmpty()) {
|
||||||
val id = uri.lastPathSegment?.toInt()
|
val id = uri.lastPathSegment?.toInt()
|
||||||
?: throw IllegalArgumentException("Invalid URI: $uri")
|
?: throw IllegalArgumentException("Invalid URI: $uri")
|
||||||
rowsUpdated = sqlDB.update(
|
rowsUpdated = requireDb().update(
|
||||||
TABLE_NAME,
|
TABLE_NAME,
|
||||||
contentValues,
|
contentValues,
|
||||||
"$COLUMN_NAME = ?",
|
"$COLUMN_NAME = ?",
|
||||||
|
|
@ -114,14 +100,13 @@ class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
|
||||||
* @param contentValues : new values to be entered to the database
|
* @param contentValues : new values to be entered to the database
|
||||||
*/
|
*/
|
||||||
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
|
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
|
||||||
val sqlDB = dbOpenHelper.writableDatabase
|
val id = requireDb().insert(
|
||||||
val id = sqlDB.insert(
|
|
||||||
TABLE_NAME,
|
TABLE_NAME,
|
||||||
null,
|
null,
|
||||||
contentValues
|
contentValues
|
||||||
)
|
)
|
||||||
context?.contentResolver?.notifyChange(uri, null)
|
context?.contentResolver?.notifyChange(uri, null)
|
||||||
return Uri.parse("$BASE_URI/$id")
|
return "$BASE_URI/$id".toUri()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -129,9 +114,7 @@ class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
|
||||||
* @param uri : contains the URI for recently used languages
|
* @param uri : contains the URI for recently used languages
|
||||||
*/
|
*/
|
||||||
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
|
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
|
||||||
val db = dbOpenHelper.readableDatabase
|
val rows = requireDb().delete(
|
||||||
Timber.d("Deleting recently used language %s", uri.lastPathSegment)
|
|
||||||
val rows = db.delete(
|
|
||||||
TABLE_NAME,
|
TABLE_NAME,
|
||||||
"language_code = ?",
|
"language_code = ?",
|
||||||
arrayOf(uri.lastPathSegment)
|
arrayOf(uri.lastPathSegment)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import fr.free.nrw.commons.databinding.ActivityReviewBinding
|
||||||
import fr.free.nrw.commons.delete.DeleteHelper
|
import fr.free.nrw.commons.delete.DeleteHelper
|
||||||
import fr.free.nrw.commons.media.MediaDetailFragment
|
import fr.free.nrw.commons.media.MediaDetailFragment
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
|
import fr.free.nrw.commons.utils.applyEdgeToEdgeAllInsets
|
||||||
import fr.free.nrw.commons.utils.DialogUtil
|
import fr.free.nrw.commons.utils.DialogUtil
|
||||||
import fr.free.nrw.commons.utils.ViewUtil
|
import fr.free.nrw.commons.utils.ViewUtil
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
|
@ -73,6 +74,7 @@ class ReviewActivity : BaseActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityReviewBinding.inflate(layoutInflater)
|
binding = ActivityReviewBinding.inflate(layoutInflater)
|
||||||
|
applyEdgeToEdgeAllInsets(binding.root)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
setSupportActionBar(binding.toolbarBinding?.toolbar)
|
setSupportActionBar(binding.toolbarBinding?.toolbar)
|
||||||
|
|
@ -107,10 +109,8 @@ class ReviewActivity : BaseActivity() {
|
||||||
setUpMediaDetailFragment()
|
setUpMediaDetailFragment()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.skipImage.setOnTouchListener { _, event ->
|
binding.skipImageInfo?.setOnTouchListener { _, event ->
|
||||||
if (event.action == MotionEvent.ACTION_UP &&
|
if (event.action == MotionEvent.ACTION_UP) {
|
||||||
event.rawX >= (binding.skipImage.right - binding.skipImage.compoundDrawables[2].bounds.width())
|
|
||||||
) {
|
|
||||||
showSkipImageInfo()
|
showSkipImageInfo()
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -238,7 +238,7 @@ class ReviewActivity : BaseActivity() {
|
||||||
this,
|
this,
|
||||||
getString(R.string.skip_image).uppercase(Locale.ROOT),
|
getString(R.string.skip_image).uppercase(Locale.ROOT),
|
||||||
getString(R.string.skip_image_explanation),
|
getString(R.string.skip_image_explanation),
|
||||||
getString(android.R.string.ok),
|
getString(R.string.ok),
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
|
|
@ -250,7 +250,7 @@ class ReviewActivity : BaseActivity() {
|
||||||
this,
|
this,
|
||||||
getString(R.string.title_activity_review),
|
getString(R.string.title_activity_review),
|
||||||
getString(R.string.review_image_explanation),
|
getString(R.string.review_image_explanation),
|
||||||
getString(android.R.string.ok),
|
getString(R.string.ok),
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue