<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/mainRoot"
+ android:id="@+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AndroidReaderActivity">
- <EditText
- android:id="@+id/editText"
- android:layout_width="177dp"
- android:layout_height="38dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPersonName"
- android:text="@string/enter_text"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:onClick="onClick"
- android:text="Download if needed"
- app:layout_constraintBottom_toBottomOf="@+id/editText"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintStart_toEndOf="@+id/editText" />
-
<Button
- android:id="@+id/button2"
+ android:id="@+id/Main_btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
- android:layout_marginTop="28dp"
- android:onClick="buttonClick"
- android:text="Add all sources"
+ android:layout_marginTop="8dp"
+ android:onClick="onAdd"
+ android:text="Add story"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.472"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/editText" />
+ app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
- android:id="@+id/dropZone"
- android:layout_width="363dp"
- android:layout_height="wrap_content"
+ android:id="@+id/Main_pnlStories"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
+ android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
- android:layout_marginTop="88dp"
+ android:layout_marginTop="8dp"
android:orientation="vertical"
+ app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/editText"></LinearLayout>
+ app:layout_constraintTop_toBottomOf="@+id/Main_btnAdd"></LinearLayout>
</android.support.constraint.ConstraintLayout>
+++ /dev/null
-<?xml version="1.0" encoding="utf-8"?>
-<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="be.nikiroo.testy.be.nikiroo.fanfix.reader.android.SayIt">
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="285dp"
- android:layout_height="250dp"
- android:text="TextView"
- tools:layout_editor_absoluteX="38dp"
- tools:layout_editor_absoluteY="130dp" />
-</android.support.constraint.ConstraintLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/coverWidget"
+ android:id="@+id/Book"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_height="wrap_content">
<TextView
- android:id="@+id/title"
+ android:id="@+id/Book_lblTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
- android:text="TITLE"
- app:layout_constraintEnd_toStartOf="@+id/cover"
+ android:textStyle="bold"
+ app:layout_constraintEnd_toStartOf="@+id/Book_imgCover"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="@+id/cover" />
+ app:layout_constraintTop_toTopOf="@+id/Book_imgCover" />
<ImageView
- android:id="@+id/cover"
+ android:id="@+id/Book_imgCover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
+
+ <TextView
+ android:id="@+id/Book_lblAuthor"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginEnd="8dp"
+ android:layout_marginTop="8dp"
+ android:textColor="@android:color/darker_gray"
+ app:layout_constraintEnd_toStartOf="@+id/Book_imgCover"
+ app:layout_constraintStart_toEndOf="@+id/Book_lblBy"
+ app:layout_constraintTop_toBottomOf="@+id/Book_lblTitle" />
+
+ <TextView
+ android:id="@+id/Book_lblBy"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_marginTop="8dp"
+ android:text="By "
+ android:textColor="@android:color/darker_gray"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/Book_lblTitle" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
<!-- TODO: Update blank fragment layout -->
- <LinearLayout
- android:id="@+id/AndroidReaderGroup_root"
+ <ListView
+ android:id="@+id/Group_root"
android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"></LinearLayout>
+ android:layout_height="match_parent" />
</FrameLayout>
+++ /dev/null
-<?xml version="1.0" encoding="utf-8"?>
-<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
- <background android:drawable="@drawable/ic_launcher_background"/>
- <foreground android:drawable="@drawable/ic_launcher_foreground"/>
-</adaptive-icon>
\ No newline at end of file
+++ /dev/null
-<?xml version="1.0" encoding="utf-8"?>
-<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
- <background android:drawable="@drawable/ic_launcher_background"/>
- <foreground android:drawable="@drawable/ic_launcher_foreground"/>
-</adaptive-icon>
\ No newline at end of file
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- <activity android:name="be.nikiroo.fanfix.reader.android.SayIt" />
</application>
</manifest>
\ No newline at end of file
package be.nikiroo.fanfix.reader.android;
import android.app.Activity;
+import android.app.AlertDialog;
import android.app.FragmentTransaction;
-import android.content.Intent;
-import android.os.AsyncTask;
+import android.content.Context;
+import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
+import android.text.InputType;
import android.view.View;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
- config();
+ reader = config();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
- int i = 1;
+ @Override
+ protected void onStart() {
+ super.onStart();
+ refresh();
+ }
- public void buttonClick(View view) {
- AndroidReaderGroup group = null;
- if (group == null) {
- group = new AndroidReaderGroup();
- }
+ private void refresh() {
+ AndroidReaderGroup group = new AndroidReaderGroup();
FragmentTransaction trans = getFragmentManager().beginTransaction();
- trans.replace(R.id.dropZone, group);
+ trans.replace(R.id.Main_pnlStories, group);
trans.commit();
getFragmentManager().executePendingTransactions();
- group.fill(reader, null);
+ group.fill(reader.getLibrary().getList(), reader);
}
- public void onClick(View view) {
- new AsyncTask<Void, Void, String>() {
- @Override
- protected void onPreExecute() {
- EditText editText = findViewById(R.id.editText);
- editText.setText("Downloading...");
- }
-
- @Override
- protected String doInBackground(Void... voids) {
- try {
- URL[] urls = new URL[] {
- new URL("https://e621.net/pool/show/13124"),
- new URL("https://e621.net/pool/show/13121"), };
-
- for (int i = 0; i < urls.length; i++) {
- if (reader.getLibrary().getList().size() <= i) {
- reader.getLibrary().imprt(urls[i], null);
- }
- }
-
- String message = "";
- for (MetaData meta : reader.getLibrary().getList()) {
- message += meta.getTitle() + "\n";
+ public void onAdd(View view) {
+ final View root = findViewById(R.id.Main);
+
+ ask(this,
+ "Import new story",
+ "Enter the story URL (the program will then download it -- the interface will not be usable until it is downloaded",
+ "Download", new AnswerListener() {
+ @Override
+ public void onAnswer(final String answer) {
+ root.setEnabled(false);
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ URL url = new URL(answer);
+ reader.getLibrary().imprt(url, null);
+ } catch (Throwable e) {
+ // TODO: show error message correctly
+ String mess = "";
+ for (String tab = ""; e != null
+ && e != e.getCause(); e = e
+ .getCause()) {
+ mess += tab + "["
+ + e.getClass().getSimpleName()
+ + "] " + e.getMessage() + "\n";
+ tab += "\t";
+ }
+
+ final String messf = mess;
+ AndroidReaderActivity.this
+ .runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ask(AndroidReaderActivity.this,
+ "Error",
+ "Cannot import URL: \n"
+ + messf,
+ "OK", null);
+ }
+ });
+
+ }
+
+ AndroidReaderActivity.this
+ .runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ refresh();
+ root.setEnabled(true);
+ }
+ });
+ }
+ }).start();
}
+ });
- return message;
- } catch (Exception e) {
- return e.getClass() + ": " + e.getMessage();
- }
- }
-
- @Override
- protected void onPostExecute(String message) {
- EditText editText = findViewById(R.id.editText);
- editText.setText("testy");
-
- Intent intent = new Intent(AndroidReaderActivity.this,
- SayIt.class);
- intent.putExtra(SayIt.MESSAGE, message);
- startActivity(intent);
- }
- }.execute();
+ /*
+ * Intent intent = new Intent(AndroidReaderActivity.this, SayIt.class);
+ * intent.putExtra(SayIt.MESSAGE, message); startActivity(intent);
+ */
}
@Override
}
}
- private void config() {
+ private Reader config() {
if (reader != null) {
- return;
+ return reader;
}
String internal = getExternalFilesDir(null).toString();
Instance.setTraceHandler(new TraceHandler(true, true, 2));
BasicReader.setDefaultReaderType(Reader.ReaderType.ANDROID);
- reader = BasicReader.getReader();
+ return BasicReader.getReader();
+ }
+
+ public static void ask(Context context, String title, String message,
+ String okMessage, final AnswerListener listener) {
+ final EditText input = new EditText(context);
+ input.setFocusable(true);
+ input.setInputType(InputType.TYPE_CLASS_TEXT);
+
+ AlertDialog.Builder alert = new AlertDialog.Builder(context);
+ alert.setTitle(title);
+ alert.setMessage(message);
+ alert.setCancelable(true);
+ alert.setView(input);
+
+ if (listener != null) {
+ alert.setPositiveButton(okMessage,
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ listener.onAnswer(input.getText().toString());
+ }
+ });
+
+ alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
+ @Override
+ public void onCancel(DialogInterface dialog) {
+ listener.onAnswer(null);
+ }
+ });
+ }
+
+ alert.show();
+ }
+
+ private interface AnswerListener {
+ public void onAnswer(String answer);
}
}
\ No newline at end of file
import java.io.IOException;
-import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.data.MetaData;
import be.nikiroo.fanfix.reader.Reader;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.android.ImageUtilsAndroid;
public class AndroidReaderBook extends Fragment {
- private Reader reader;
private OnFragmentInteractionListener listener;
- private MetaData meta;
/**
* This interface must be implemented by activities that contain this
listener = null;
}
- public void fill(final Reader reader, final String luid) {
- View view = getView();
- if (view == null) {
- return;
- }
-
- final ImageView cover = view.findViewById(R.id.cover);
- final TextView title = view.findViewById(R.id.title);
- final FrameLayout frame = view.findViewById(R.id.coverWidget);
+ public void fill(final MetaData meta, final Reader reader) {
+ ViewHolder viewHolder = new ViewHolder(getView());
- new AsyncTask<Void, Void, MetaData>() {
+ viewHolder.title.setText(meta.getTitle());
+ viewHolder.author.setText(meta.getAuthor());
+ viewHolder.frame.setClickable(true);
+ viewHolder.frame.setFocusable(true);
+ viewHolder.frame.setOnClickListener(new View.OnClickListener() {
@Override
- protected MetaData doInBackground(Void[] objects) {
- return Instance.getLibrary().getInfo(luid);
+ public void onClick(View v) {
+ OnFragmentInteractionListener llistener = listener;
+ if (llistener != null) {
+ llistener.onFragmentInteraction(meta);
+ }
}
+ });
+ new AsyncTask<MetaData, Void, Image>() {
@Override
- protected void onPostExecute(MetaData meta) {
- AndroidReaderBook.this.meta = meta;
-
- if (meta != null) {
- title.setText(meta.getTitle());
- try {
- Image coverImage = reader.getLibrary().getCover(
- meta.getLuid());
- if (coverImage != null) {
- Bitmap coverBitmap = ImageUtilsAndroid
- .fromImage(coverImage);
- coverBitmap = Bitmap.createScaledBitmap(
- coverBitmap, 128, 128, true);
- cover.setImageBitmap(coverBitmap);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
+ protected Image doInBackground(MetaData[] metas) {
+ if (metas[0].getCover() != null) {
+ return metas[0].getCover();
}
- frame.setClickable(true);
- frame.setFocusable(true);
- frame.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- OnFragmentInteractionListener llistener = listener;
- if (llistener != null) {
- llistener
- .onFragmentInteraction(AndroidReaderBook.this.meta);
- }
+ return reader.getLibrary().getCover(metas[0].getLuid());
+ }
+
+ @Override
+ protected void onPostExecute(Image coverImage) {
+ ViewHolder viewHolder = new ViewHolder(getView());
+
+ try {
+ if (coverImage != null) {
+ Bitmap coverBitmap = ImageUtilsAndroid
+ .fromImage(coverImage);
+ coverBitmap = Bitmap.createScaledBitmap(coverBitmap,
+ 128, 128, true);
+ viewHolder.cover.setImageBitmap(coverBitmap);
}
- });
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
- }.execute();
+ }.execute(meta);
+ }
+
+ private class ViewHolder {
+ public FrameLayout frame;
+ public TextView title;
+ public TextView author;
+ public ImageView cover;
+
+ public ViewHolder(View book) {
+ frame = book.findViewById(R.id.Book);
+ title = book.findViewById(R.id.Book_lblTitle);
+ author = book.findViewById(R.id.Book_lblAuthor);
+ cover = book.findViewById(R.id.Book_imgCover);
+ }
}
}
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
-import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ListView;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import be.nikiroo.fanfix.data.MetaData;
import be.nikiroo.fanfix.reader.Reader;
*/
public class AndroidReaderGroup extends Fragment {
private OnFragmentInteractionListener listener;
+ private Map<View, AndroidReaderBook> books = new HashMap<View, AndroidReaderBook>();
public interface OnFragmentInteractionListener {
void onFragmentInteraction(MetaData meta);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
- // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_android_reader_group,
container, false);
}
listener = null;
}
- public void fill(final Reader reader, final String source) {
- new AsyncTask<Void, Void, List<MetaData>>() {
+ public void fill(final List<MetaData> metas, final Reader reader) {
+ final List<MetaData> datas = new ArrayList<MetaData>(metas);
+
+ ListView list = getView().findViewById(R.id.Group_root);
+ list.setAdapter(new BaseAdapter() {
+ @Override
+ public int getCount() {
+ return datas.size();
+ }
+
@Override
- protected List<MetaData> doInBackground(Void... voids) {
- return reader.getLibrary().getListBySource(source);
+ public long getItemId(int position) {
+ return -1; // TODO: what is a "row id" in this context?
}
@Override
- protected void onPostExecute(List<MetaData> metas) {
- for (MetaData meta : metas) {
- String tag = "Book_" + meta.getLuid();
- tag = null; // TODO: how does it work?
- AndroidReaderBook book = null;// (AndroidReaderBook)
- // getFragmentManager().findFragmentByTag(tag);
- if (book == null) {
- book = new AndroidReaderBook();
- FragmentTransaction trans = getFragmentManager()
- .beginTransaction();
- trans.add(R.id.AndroidReaderGroup_root, book, tag);
- trans.commit();
- getFragmentManager().executePendingTransactions();
- }
- book.fill(reader, meta.getLuid());
+ public Object getItem(int position) {
+ return datas.get(position);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ AndroidReaderBook book = books.get(convertView);
+ if (book == null) {
+ book = new AndroidReaderBook();
+
+ FragmentTransaction trans = getFragmentManager()
+ .beginTransaction();
+ trans.add(book, null);
+ trans.commit();
+ getFragmentManager().executePendingTransactions();
+
+ books.put(book.getView(), book);
}
+
+ MetaData meta = (MetaData) getItem(position);
+ book.fill(meta, reader);
+
+ return book.getView();
}
- }.execute();
+ });
}
}
+++ /dev/null
-package be.nikiroo.fanfix.reader.android;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.os.Bundle;
-import android.widget.TextView;
-
-
-public class SayIt extends Activity {
- public static final String MESSAGE = "be.nikiroo.testy.message";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_say_it);
-
- // Get the Intent that started this activity and extract the string
- Intent intent = getIntent();
- String message = intent.getStringExtra(SayIt.MESSAGE);
-
- // Capture the layout's TextView and set the string as its text
- TextView textView = findViewById(R.id.textView);
- textView.setText(message);
- }
-}