mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Refactoring based on code review feedback.
This commit is contained in:
parent
6d77243329
commit
07577d73a1
2 changed files with 13 additions and 17 deletions
|
|
@ -16,6 +16,7 @@ import java.util.Map;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
|
@ -34,34 +35,26 @@ import timber.log.Timber;
|
|||
public class MediaDataExtractor {
|
||||
private final MediaWikiApi mediaWikiApi;
|
||||
private boolean fetched;
|
||||
|
||||
private String filename;
|
||||
private ArrayList<String> categories;
|
||||
private Map<String, String> descriptions;
|
||||
private String license;
|
||||
private @Nullable LatLng coordinates;
|
||||
private LicenseList licenseList;
|
||||
|
||||
/**
|
||||
* @param mwApi instance of MediaWikiApi
|
||||
* @param filename of the target media object, should include 'File:' prefix
|
||||
*/
|
||||
public MediaDataExtractor(String filename, LicenseList licenseList, MediaWikiApi mwApi) {
|
||||
this.filename = filename;
|
||||
@Inject
|
||||
public MediaDataExtractor(MediaWikiApi mwApi) {
|
||||
this.categories = new ArrayList<>();
|
||||
this.descriptions = new HashMap<>();
|
||||
this.fetched = false;
|
||||
this.licenseList = licenseList;
|
||||
this.mediaWikiApi = mwApi;
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Actually fetch the data over the network.
|
||||
* todo: use local caching?
|
||||
*
|
||||
* Warning: synchronous i/o, call on a background thread
|
||||
*/
|
||||
public void fetch() throws IOException {
|
||||
public void fetch(String filename, LicenseList licenseList) throws IOException {
|
||||
if (fetched) {
|
||||
throw new IllegalStateException("Tried to call MediaDataExtractor.fetch() again.");
|
||||
}
|
||||
|
|
@ -72,7 +65,7 @@ public class MediaDataExtractor {
|
|||
extractCategories(result.getWikiSource());
|
||||
|
||||
// Description template info is extracted from preprocessor XML
|
||||
processWikiParseTree(result.getParseTreeXmlSource());
|
||||
processWikiParseTree(result.getParseTreeXmlSource(), licenseList);
|
||||
fetched = true;
|
||||
}
|
||||
|
||||
|
|
@ -91,7 +84,7 @@ public class MediaDataExtractor {
|
|||
}
|
||||
}
|
||||
|
||||
private void processWikiParseTree(String source) throws IOException {
|
||||
private void processWikiParseTree(String source, LicenseList licenseList) throws IOException {
|
||||
Document doc;
|
||||
try {
|
||||
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import java.util.Date;
|
|||
import java.util.Locale;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import dagger.android.support.DaggerFragment;
|
||||
import fr.free.nrw.commons.License;
|
||||
|
|
@ -56,7 +57,9 @@ public class MediaDetailFragment extends DaggerFragment {
|
|||
return mf;
|
||||
}
|
||||
|
||||
@Inject MediaWikiApi mwApi;
|
||||
@Inject
|
||||
Provider<MediaDataExtractor> mediaDataExtractorProvider;
|
||||
|
||||
private MediaWikiImageView image;
|
||||
private MediaDetailSpacer spacer;
|
||||
private int initialListTop = 0;
|
||||
|
|
@ -192,13 +195,13 @@ public class MediaDetailFragment extends DaggerFragment {
|
|||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
extractor = new MediaDataExtractor(media.getFilename(), licenseList, mwApi);
|
||||
extractor = mediaDataExtractorProvider.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... voids) {
|
||||
try {
|
||||
extractor.fetch();
|
||||
extractor.fetch(media.getFilename(), licenseList);
|
||||
return Boolean.TRUE;
|
||||
} catch (IOException e) {
|
||||
Timber.d(e);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue