개정판 54a28343
Thumbnail 로딩 방식 수정
Change-Id: Iad91569fc06e4eecfe9d5a7a9ece9567ad161874
KCOM/PageManager/PageStorage.cs | ||
---|---|---|
11 | 11 |
public class PageStorage |
12 | 12 |
{ |
13 | 13 |
private const int DEFUALT_TALK_PAGE_COUNT = 10; |
14 |
|
|
14 |
bool IsRunWork = false; |
|
15 | 15 |
BackgroundWorker backgroundWorker; |
16 | 16 |
List<PageItem> fileItems = new List<PageItem>(); |
17 | 17 |
string _localStorage; |
18 |
string _fileExt; |
|
18 | 19 |
string _BaseUri; |
19 | 20 |
int _TotalPages; |
21 |
int _TakeCount; |
|
20 | 22 |
|
21 | 23 |
|
22 | 24 |
BitmapFrame PageImage; |
23 | 25 |
|
24 |
public PageStorage(string BaseUri, string localStoragePath,int totalPages)
|
|
26 |
public PageStorage(string BaseUri, string localStoragePath,string fileExt,int totalPages,int takeCount = 5)
|
|
25 | 27 |
{ |
26 | 28 |
try |
27 | 29 |
{ |
28 | 30 |
backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true }; |
29 | 31 |
backgroundWorker.DoWork += BackgroundWorker_DoWork; |
30 | 32 |
|
33 |
_fileExt = fileExt; |
|
31 | 34 |
_BaseUri = BaseUri; |
32 | 35 |
_localStorage = localStoragePath; |
33 | 36 |
_TotalPages = totalPages; |
37 |
_TakeCount = takeCount; |
|
34 | 38 |
|
35 | 39 |
System.IO.Directory.CreateDirectory(_localStorage); |
36 | 40 |
|
... | ... | |
74 | 78 |
|
75 | 79 |
} |
76 | 80 |
|
77 |
public async Task<BitmapFrame> GetPageAsync(int PageNo,bool IsRestart =false) |
|
81 |
public async Task<BitmapFrame> GetPageImageAsync(int PageNo,bool IsRestart =false)
|
|
78 | 82 |
{ |
79 | 83 |
try |
80 | 84 |
{ |
85 |
|
|
86 |
var localUri = await GetPageUriAsync(PageNo); |
|
87 |
|
|
88 |
PageImage = BitmapFrame.Create(localUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
|
89 |
} |
|
90 |
catch (Exception ex) |
|
91 |
{ |
|
92 |
throw new Exception("GetPageImageAsync(int PageNo,bool IsRestart =false)", ex); |
|
93 |
} |
|
94 |
finally |
|
95 |
{ |
|
96 |
} |
|
97 |
|
|
98 |
return PageImage; |
|
99 |
} |
|
100 |
|
|
101 |
public async Task<Uri> GetPageUriAsync(int PageNo, bool IsRestart = false) |
|
102 |
{ |
|
103 |
Uri result = null; |
|
104 |
|
|
105 |
try |
|
106 |
{ |
|
81 | 107 |
System.Diagnostics.Debug.WriteLine("GetPageAsync"); |
108 |
|
|
109 |
var pageItem = await DownloadPageAsync(PageNo); |
|
82 | 110 |
|
83 |
while (backgroundWorker.IsBusy)
|
|
111 |
if (!IsRunWork && pageItem != null)
|
|
84 | 112 |
{ |
85 |
await Task.Delay(100);
|
|
113 |
var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount);
|
|
86 | 114 |
|
87 |
if (backgroundWorker.IsBusy)
|
|
115 |
if (takePageNoList.Any(x => fileItems.Count(y => y.PageNo == x) == 0))
|
|
88 | 116 |
{ |
89 |
backgroundWorker.CancelAsync(); |
|
90 |
} |
|
117 |
while (backgroundWorker.IsBusy) |
|
118 |
{ |
|
119 |
if (backgroundWorker.IsBusy) |
|
120 |
{ |
|
121 |
backgroundWorker.CancelAsync(); |
|
122 |
} |
|
91 | 123 |
|
92 |
} |
|
124 |
await Task.Delay(100); |
|
125 |
} |
|
93 | 126 |
|
94 |
var pageItem = await DownloadPageAsync(PageNo);
|
|
127 |
backgroundWorker.RunWorkerAsync(new int[] { PageNo + 1, _TakeCount });
|
|
95 | 128 |
|
96 |
backgroundWorker.RunWorkerAsync(new int[] { PageNo + 1, 5 }); |
|
97 |
|
|
98 |
PageImage = BitmapFrame.Create(pageItem.LocalUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
|
129 |
if (_TotalPages == _TakeCount) |
|
130 |
{ |
|
131 |
IsRunWork = true; |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
result = pageItem.LocalUri; |
|
99 | 137 |
} |
100 |
catch (Exception) |
|
138 |
catch (Exception ex)
|
|
101 | 139 |
{ |
102 | 140 |
if (!IsRestart) |
103 | 141 |
{ |
104 | 142 |
await Task.Delay(100); |
105 |
PageImage = await GetPageAsync(PageNo,true); |
|
143 |
|
|
144 |
result = await GetPageUriAsync(PageNo, true); |
|
106 | 145 |
} |
107 | 146 |
//throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
108 | 147 |
} |
... | ... | |
110 | 149 |
{ |
111 | 150 |
} |
112 | 151 |
|
113 |
return PageImage;
|
|
152 |
return result;
|
|
114 | 153 |
} |
115 | 154 |
|
116 | 155 |
public void DownloadPagesAsync(int StartPageNo, int TalkCount = 5) |
... | ... | |
129 | 168 |
try |
130 | 169 |
{ |
131 | 170 |
DownloadPageAsync(i).RunAndForget(); |
171 |
|
|
132 | 172 |
} |
133 | 173 |
catch (Exception ex) |
134 | 174 |
{ |
... | ... | |
138 | 178 |
{ |
139 | 179 |
} |
140 | 180 |
|
141 |
|
|
142 | 181 |
} |
143 | 182 |
} |
144 | 183 |
} |
... | ... | |
148 | 187 |
} |
149 | 188 |
finally |
150 | 189 |
{ |
151 |
|
|
190 |
GC.Collect(2); |
|
191 |
GC.Collect(2); |
|
152 | 192 |
} |
153 | 193 |
} |
154 | 194 |
|
... | ... | |
183 | 223 |
|
184 | 224 |
System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
185 | 225 |
|
186 |
string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + ".png");
|
|
226 |
string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + "." + _fileExt);
|
|
187 | 227 |
|
188 | 228 |
Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())); |
189 | 229 |
|
... | ... | |
212 | 252 |
fileItems.Add(result); |
213 | 253 |
}; |
214 | 254 |
|
215 |
await client.DownloadFileTaskAsync(originalUri, downloadFilePath);
|
|
255 |
System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath);
|
|
216 | 256 |
|
217 |
GC.Collect(); |
|
218 |
GC.WaitForPendingFinalizers(); |
|
219 |
GC.Collect(); |
|
257 |
await client.DownloadFileTaskAsync(originalUri, downloadFilePath); |
|
220 | 258 |
|
221 |
System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
|
259 |
//System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath);
|
|
222 | 260 |
} |
223 | 261 |
} |
224 | 262 |
catch (Exception ex) |
내보내기 Unified diff