mirror of
https://github.com/wahyd4/libr-android.git
synced 2026-08-09 04:56:19 +10:00
Refactor DownloadImageTask
This commit is contained in:
+2
-1
@@ -7,7 +7,8 @@
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.Sherlock.Light">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name"
|
||||
android:theme="@style/Theme.Sherlock.Light.DarkActionBar">
|
||||
<activity android:name=".ui.MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
@@ -69,4 +69,14 @@ public class Book {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String buildFilename() {
|
||||
StringBuilder builder = new StringBuilder(isbn);
|
||||
int pos = image.lastIndexOf('.');
|
||||
if (pos > 0) {
|
||||
String suffix = image.substring(pos);
|
||||
builder.append(suffix);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.github.hoverruan.libr.mobile.domain.BookList;
|
||||
import com.github.hoverruan.libr.mobile.domain.BookParser;
|
||||
import com.github.hoverruan.libr.mobile.util.DownloadImageTask;
|
||||
import com.github.hoverruan.libr.mobile.util.DownloadJsonTask;
|
||||
import static com.github.hoverruan.libr.mobile.util.FileUtils.newFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
@@ -37,7 +38,7 @@ public class MainActivity extends SherlockListActivity {
|
||||
|
||||
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
||||
|
||||
setSupportProgressBarIndeterminateVisibility(true);
|
||||
// setSupportProgressBarIndeterminateVisibility(true);
|
||||
new DownloadBooksInfo().execute("http://libr.herokuapp.com/api/books");
|
||||
}
|
||||
|
||||
@@ -100,7 +101,7 @@ public class MainActivity extends SherlockListActivity {
|
||||
setListAdapter(new BookListAdapter(MainActivity.this));
|
||||
}
|
||||
}
|
||||
setSupportProgressBarIndeterminateVisibility(false);
|
||||
// setSupportProgressBarIndeterminateVisibility(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,15 +115,15 @@ public class MainActivity extends SherlockListActivity {
|
||||
}
|
||||
|
||||
public void load() {
|
||||
File imageFile = DownloadImageTask.calculateImageFile(MainActivity.this, "cover", book.getIsbn());
|
||||
if (!imageFile.exists()) {
|
||||
new DownloadImageTask(MainActivity.this, "cover", book.getIsbn()) {
|
||||
File coverFile = newFile(MainActivity.this.getCacheDir(), "cover", book.buildFilename());
|
||||
if (!coverFile.exists()) {
|
||||
new DownloadImageTask(MainActivity.this, coverFile) {
|
||||
protected void onPostExecute(File downloadedImageFile) {
|
||||
loadImage(downloadedImageFile);
|
||||
}
|
||||
}.execute(book.getImage());
|
||||
} else {
|
||||
loadImage(imageFile);
|
||||
loadImage(coverFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.github.hoverruan.libr.mobile.util;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Hover Ruan
|
||||
*/
|
||||
public class FileUtils {
|
||||
public static File newFile(File root, String... paths) {
|
||||
File targetFile = root;
|
||||
|
||||
for (String path : paths) {
|
||||
targetFile = new File(targetFile, path);
|
||||
}
|
||||
|
||||
return targetFile;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.github.hoverruan.libr.mobile.domain;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Hover Ruan
|
||||
*/
|
||||
public class BookTest {
|
||||
@Test
|
||||
public void should_build_Filename_with_ISBN_and_image_url() {
|
||||
String suffix = ".jpg";
|
||||
String image = "http://img3.douban.com/mpic/s4483293" + suffix;
|
||||
String isbn = "9787111316657";
|
||||
|
||||
Book book = new Book();
|
||||
book.setImage(image);
|
||||
book.setIsbn(isbn);
|
||||
|
||||
String filename = book.buildFilename();
|
||||
assertThat(filename, is(isbn + suffix));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user