Skip to content

Commit

Permalink
ability to add saved threads from directory
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceCA committed Dec 30, 2016
1 parent 5862e07 commit 50ce483
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 33 deletions.
1 change: 1 addition & 0 deletions res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="mark_code">Code</string>
<string name="pref_post_thumbnail_scale_title">Thumbnail-Größe</string>
<string name="pref_key_post_thumbnail_scale_toast">Es wird empfohlen, den Cache zu löschen</string>
<string name="newtab_select_local_directory">Speicherort angeben</string>
</resources>
1 change: 1 addition & 0 deletions res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="mark_code">Код</string>
<string name="pref_post_thumbnail_scale_title">Размер эскизов</string>
<string name="pref_key_post_thumbnail_scale_toast">Рекомендуется очистить кэш</string>
<string name="newtab_select_local_directory">Указать расположение каталога</string>
</resources>
1 change: 1 addition & 0 deletions res/values-uk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="mark_code">Код</string>
<string name="pref_post_thumbnail_scale_title">Розмір ескізів</string>
<string name="pref_key_post_thumbnail_scale_toast">Рекомендується очистити кеш</string>
<string name="newtab_select_local_directory">Вказати розташування каталогу</string>
</resources>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,5 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="app_import_overwrite_warning_message">All data will be irreversibly overwritten</string>
<string name="pref_post_thumbnail_scale_title">Post thumbnail size</string>
<string name="pref_key_post_thumbnail_scale_toast">It\'s recommended to clear cache after thumbnail scale has been changed</string>
<string name="newtab_select_local_directory">Specify directory location</string>
</resources>
120 changes: 87 additions & 33 deletions src/nya/miku/wishmaster/ui/NewTabFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package nya.miku.wishmaster.ui;

import java.io.File;
import java.io.FilenameFilter;
import java.util.List;

import nya.miku.wishmaster.R;
Expand All @@ -30,6 +31,7 @@
import nya.miku.wishmaster.lib.dslv.DragSortController;
import nya.miku.wishmaster.lib.dslv.DragSortListView;
import nya.miku.wishmaster.ui.tabs.LocalHandler;
import nya.miku.wishmaster.ui.tabs.TabModel;
import nya.miku.wishmaster.ui.tabs.UrlHandler;
import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -64,6 +66,7 @@
public class NewTabFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {
private static final String TAG = "NewTabFragment";
private static final int REQUEST_FILE = 500;
private static final int REQUEST_DIR = 501;

private MainActivity activity;
private Resources resources;
Expand Down Expand Up @@ -239,6 +242,7 @@ private void openLocal() {
private int drawablePadding = (int) (resources.getDisplayMetrics().density * 5 + 0.5f);

{
add(new Object());
add(new Object());
for (Database.SavedThreadEntry entity : MainApplication.getInstance().database.getSavedThreads()) {
File file = new File(entity.filepath);
Expand All @@ -249,26 +253,34 @@ private void openLocal() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (position == 0) {
v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_1, parent, false) : convertView;
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setText(R.string.newtab_select_local_file);
} else {
Database.SavedThreadEntry item = (Database.SavedThreadEntry) getItem(position);
v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false) : convertView;
TextView t1 = (TextView) v.findViewById(android.R.id.text1);
TextView t2 = (TextView) v.findViewById(android.R.id.text2);
t1.setSingleLine();
t2.setSingleLine();
t1.setEllipsize(TextUtils.TruncateAt.END);
t2.setEllipsize(TextUtils.TruncateAt.START);
t1.setText(item.title);
t2.setText(item.filepath);
ChanModule chan = MainApplication.getInstance().getChanModule(item.chan);
if (chan != null) {
t1.setCompoundDrawablesWithIntrinsicBounds(chan.getChanFavicon(), null, null, null);
t1.setCompoundDrawablePadding(drawablePadding);
}
TextView tv;
switch (position) {
case 0:
v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_1, parent, false) : convertView;
tv = (TextView) v.findViewById(android.R.id.text1);
tv.setText(R.string.newtab_select_local_file);
break;
case 1:
v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_1, parent, false) : convertView;
tv = (TextView) v.findViewById(android.R.id.text1);
tv.setText(R.string.newtab_select_local_directory);
break;
default:
Database.SavedThreadEntry item = (Database.SavedThreadEntry) getItem(position);
v = convertView == null ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false) : convertView;
TextView t1 = (TextView) v.findViewById(android.R.id.text1);
TextView t2 = (TextView) v.findViewById(android.R.id.text2);
t1.setSingleLine();
t2.setSingleLine();
t1.setEllipsize(TextUtils.TruncateAt.END);
t2.setEllipsize(TextUtils.TruncateAt.START);
t1.setText(item.title);
t2.setText(item.filepath);
ChanModule chan = MainApplication.getInstance().getChanModule(item.chan);
if (chan != null) {
t1.setCompoundDrawablesWithIntrinsicBounds(chan.getChanFavicon(), null, null, null);
t1.setCompoundDrawablePadding(drawablePadding);
}
}
return v;
}
Expand All @@ -280,22 +292,23 @@ public int getViewTypeCount() {

@Override
public int getItemViewType(int position) {
return position == 0 ? HEAD_ITEM : NORMAL_ITEM;
return position <= 1 ? HEAD_ITEM : NORMAL_ITEM;
}
};

if (savedThreadsAdapter.getCount() == 1) {
selectFile();
return;
}
DialogInterface.OnClickListener listListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
selectFile();
} else {
Database.SavedThreadEntry item = (Database.SavedThreadEntry) savedThreadsAdapter.getItem(which);
LocalHandler.open(item.filepath, activity);
switch (which) {
case 0:
selectFile();
break;
case 1:
selectDirectory();
break;
default:
Database.SavedThreadEntry item = (Database.SavedThreadEntry) savedThreadsAdapter.getItem(which);
LocalHandler.open(item.filepath, activity);
}
}
};
Expand All @@ -313,12 +326,53 @@ private void selectFile() {
selectFile.putExtra(FileDialogActivity.START_PATH, MainApplication.getInstance().settings.getDownloadDirectory().getAbsolutePath());
startActivityForResult(selectFile, REQUEST_FILE);
}

private void selectDirectory() {
Intent selectFile = new Intent(activity, FileDialogActivity.class);
selectFile.putExtra(FileDialogActivity.SELECTION_MODE, FileDialogActivity.SELECTION_MODE_OPEN);
selectFile.putExtra(FileDialogActivity.CAN_SELECT_DIR, true);
selectFile.putExtra(FileDialogActivity.FORMAT_FILTER, new String[] { ".zip", ".mhtml", ".html" });
selectFile.putExtra(FileDialogActivity.START_PATH, MainApplication.getInstance().settings.getDownloadDirectory().getAbsolutePath());
startActivityForResult(selectFile, REQUEST_DIR);
}

private File[] listFiles(File path, final String[] ext, final boolean includeDirs){
return path.listFiles(new FilenameFilter() {

@Override
public boolean accept(File dir, String filename) {
boolean result = false;
for (String s : ext) if ((result = result || filename.endsWith(s))) break;
result = result || (includeDirs && (new File(dir.getAbsolutePath(), filename)).isDirectory());
return result;
}
});
}

private void addSavedThreads(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
addSavedThreads(listFiles(file, new String[] {".html"}, false));
} else {
TabModel model = LocalHandler.getTabModel(file.getAbsolutePath(), activity.getResources());
if (model != null) {
MainApplication.getInstance().database.addSavedThread(model.pageModel.chanName, model.title, model.localFilePath);
}
}
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_FILE && resultCode == Activity.RESULT_OK) {
String path = data.getStringExtra(FileDialogActivity.RESULT_PATH);
LocalHandler.open(path, activity);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_FILE) {
String path = data.getStringExtra(FileDialogActivity.RESULT_PATH);
LocalHandler.open(path, activity);
}
if (requestCode == REQUEST_DIR) {
File path = new File(data.getStringExtra(FileDialogActivity.RESULT_PATH));
if (path.isDirectory()) addSavedThreads(listFiles(path, new String[] {".html", ".zip", ".mhtml"}, true));
}
}
}

Expand Down

0 comments on commit 50ce483

Please sign in to comment.