mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-17 19:35:36 -04:00
Remember the books served by Kiwix hotspot
This commit is contained in:
parent
2a39953fdb
commit
13a63d58ac
@ -25,6 +25,8 @@ import androidx.core.content.ContextCompat;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import org.kiwix.kiwixmobile.core.CoreApp;
|
||||
@ -42,21 +44,22 @@ public class SharedPreferenceUtil {
|
||||
public static final String PREF_NIGHTMODE = "pref_nightmode";
|
||||
public static final String PREF_WIFI_ONLY = "pref_wifi_only";
|
||||
public static final String PREF_KIWIX_MOBILE = "kiwix-mobile";
|
||||
public static final String PREF_BACK_TO_TOP = "pref_backtotop";
|
||||
public static final String PREF_HIDE_TOOLBAR = "pref_hidetoolbar";
|
||||
public static final String PREF_ZOOM = "pref_zoom_slider";
|
||||
public static final String PREF_ZOOM_ENABLED = "pref_zoom_enabled";
|
||||
public static final String PREF_FULLSCREEN = "pref_fullscreen";
|
||||
public static final String PREF_NEW_TAB_BACKGROUND = "pref_newtab_background";
|
||||
public static final String PREF_FULL_TEXT_SEARCH = "pref_full_text_search";
|
||||
public static final String PREF_STORAGE_TITLE = "pref_selected_title";
|
||||
public static final String PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup";
|
||||
public static final String PREF_IS_FIRST_RUN = "isFirstRun";
|
||||
public static final String PREF_SHOW_INTRO = "showIntro";
|
||||
private static final String PREF_BACK_TO_TOP = "pref_backtotop";
|
||||
private static final String PREF_HIDE_TOOLBAR = "pref_hidetoolbar";
|
||||
private static final String PREF_FULLSCREEN = "pref_fullscreen";
|
||||
private static final String PREF_NEW_TAB_BACKGROUND = "pref_newtab_background";
|
||||
private static final String PREF_STORAGE_TITLE = "pref_selected_title";
|
||||
private static final String PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup";
|
||||
private static final String PREF_IS_FIRST_RUN = "isFirstRun";
|
||||
private static final String PREF_SHOW_BOOKMARKS_CURRENT_BOOK = "show_bookmarks_current_book";
|
||||
private static final String PREF_SHOW_HISTORY_CURRENT_BOOK = "show_history_current_book";
|
||||
private SharedPreferences sharedPreferences;
|
||||
private static final String PREF_HOSTED_BOOKS = "hosted_books";
|
||||
private final PublishProcessor<String> prefStorages = PublishProcessor.create();
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
@Inject
|
||||
public SharedPreferenceUtil(Context context) {
|
||||
@ -201,4 +204,14 @@ public class SharedPreferenceUtil {
|
||||
.putBoolean(PREF_SHOW_BOOKMARKS_CURRENT_BOOK, prefShowBookmarksFromCurrentBook)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public Set<String> getHostedBooks() {
|
||||
return sharedPreferences.getStringSet(PREF_HOSTED_BOOKS, new HashSet<>());
|
||||
}
|
||||
|
||||
public void setHostedBooks(Set<String> hostedBooks) {
|
||||
sharedPreferences.edit()
|
||||
.putStringSet(PREF_HOSTED_BOOKS, hostedBooks)
|
||||
.apply();
|
||||
}
|
||||
}
|
||||
|
@ -34,8 +34,10 @@ import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import kotlin.Unit;
|
||||
@ -56,31 +58,28 @@ import static org.kiwix.kiwixmobile.core.wifi_hotspot.HotspotService.ACTION_STAR
|
||||
import static org.kiwix.kiwixmobile.core.wifi_hotspot.HotspotService.ACTION_STOP_SERVER;
|
||||
|
||||
public class ZimHostActivity extends BaseActivity implements
|
||||
ZimHostCallbacks, ZimHostContract.View {
|
||||
ZimHostCallbacks, ZimHostContract.View {
|
||||
|
||||
public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths";
|
||||
private static final String TAG = "ZimHostActivity";
|
||||
private static final String IP_STATE_KEY = "ip_state_key";
|
||||
@BindView(R2.id.startServerButton)
|
||||
Button startServerButton;
|
||||
@BindView(R2.id.server_textView)
|
||||
TextView serverTextView;
|
||||
@BindView(R2.id.recycler_view_zim_host)
|
||||
RecyclerView recyclerViewZimHost;
|
||||
|
||||
@Inject
|
||||
ZimHostContract.Presenter presenter;
|
||||
|
||||
@Inject
|
||||
AlertDialogShower alertDialogShower;
|
||||
|
||||
private static final String TAG = "ZimHostActivity";
|
||||
private static final String IP_STATE_KEY = "ip_state_key";
|
||||
public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths";
|
||||
|
||||
private BooksOnDiskAdapter booksAdapter;
|
||||
private BookOnDiskDelegate.BookDelegate bookDelegate;
|
||||
private HotspotService hotspotService;
|
||||
private String ip;
|
||||
private ServiceConnection serviceConnection;
|
||||
private ProgressDialog progressDialog;
|
||||
private HashSet<String> selectedBooksId = new HashSet<>();
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@ -108,8 +107,6 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
recyclerViewZimHost.setAdapter(booksAdapter);
|
||||
presenter.attachView(this);
|
||||
|
||||
presenter.loadBooks();
|
||||
|
||||
serviceConnection = new ServiceConnection() {
|
||||
|
||||
@Override
|
||||
@ -122,28 +119,20 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
public void onServiceDisconnected(ComponentName arg0) {
|
||||
}
|
||||
};
|
||||
|
||||
startServerButton.setOnClickListener(v -> {
|
||||
//Get the path of ZIMs user has selected
|
||||
if (!ServerUtils.isServerStarted) {
|
||||
if (getSelectedBooksPath().size() > 0) {
|
||||
startHotspotHelper();
|
||||
} else {
|
||||
Toast.makeText(ZimHostActivity.this, R.string.no_books_selected_toast_message,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
startHotspotHelper();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startHotspotHelper() {
|
||||
if (ServerUtils.isServerStarted) {
|
||||
startService(createHotspotIntent(ACTION_STOP_SERVER));
|
||||
} else {
|
||||
startHotspotManuallyDialog();
|
||||
}
|
||||
@OnClick(R2.id.startServerButton) void startStopServer() {
|
||||
if (ServerUtils.isServerStarted) {
|
||||
stopServer();
|
||||
} else if (getSelectedBooksPath().size() > 0) {
|
||||
startHotspotManuallyDialog();
|
||||
} else {
|
||||
Toast.makeText(this, R.string.no_books_selected_toast_message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopServer() {
|
||||
startService(createHotspotIntent(ACTION_STOP_SERVER));
|
||||
}
|
||||
|
||||
private ArrayList<String> getSelectedBooksPath() {
|
||||
@ -160,14 +149,14 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
}
|
||||
|
||||
private void select(@NonNull BooksOnDiskListItem.BookOnDisk bookOnDisk) {
|
||||
ArrayList<BooksOnDiskListItem> booksList = new ArrayList<>();
|
||||
for (BooksOnDiskListItem item : booksAdapter.getItems()) {
|
||||
if (item.equals(bookOnDisk)) {
|
||||
item.setSelected(!item.isSelected());
|
||||
}
|
||||
booksList.add(item);
|
||||
bookOnDisk.setSelected(!bookOnDisk.isSelected());
|
||||
if (bookOnDisk.isSelected()) {
|
||||
selectedBooksId.add(bookOnDisk.getBook().getId());
|
||||
} else {
|
||||
selectedBooksId.remove(bookOnDisk.getBook().getId());
|
||||
}
|
||||
booksAdapter.setItems(booksList);
|
||||
booksAdapter.notifyDataSetChanged();
|
||||
saveHostedBooks();
|
||||
}
|
||||
|
||||
@Override protected void onStart() {
|
||||
@ -201,14 +190,15 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
}
|
||||
}
|
||||
|
||||
private void saveHostedBooks() {
|
||||
sharedPreferenceUtil.setHostedBooks(selectedBooksId);
|
||||
}
|
||||
|
||||
private void layoutServerStarted() {
|
||||
serverTextView.setText(getString(R.string.server_started_message, ip));
|
||||
startServerButton.setText(getString(R.string.stop_server_label));
|
||||
startServerButton.setBackgroundColor(getResources().getColor(R.color.stopServer));
|
||||
bookDelegate.setSelectionMode(SelectionMode.NORMAL);
|
||||
for (BooksOnDiskListItem item : booksAdapter.getItems()) {
|
||||
item.setSelected(false);
|
||||
}
|
||||
booksAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@ -290,6 +280,7 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
}
|
||||
|
||||
@Override public void addBooks(@Nullable List<BooksOnDiskListItem> books) {
|
||||
selectPreviouslyHostedBooks(books);
|
||||
booksAdapter.setItems(books);
|
||||
}
|
||||
|
||||
@ -305,4 +296,27 @@ public class ZimHostActivity extends BaseActivity implements
|
||||
Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
|
||||
private void selectPreviouslyHostedBooks(@Nullable List<BooksOnDiskListItem> books) {
|
||||
selectedBooksId.addAll(sharedPreferenceUtil.getHostedBooks());
|
||||
if (books != null && !books.isEmpty()) {
|
||||
if (selectedBooksId.isEmpty()) {
|
||||
// Select all books if no book ids are stored
|
||||
for (BooksOnDiskListItem book : books) {
|
||||
if (book instanceof BooksOnDiskListItem.BookOnDisk) {
|
||||
selectedBooksId.add(((BooksOnDiskListItem.BookOnDisk) book).getBook().getId());
|
||||
book.setSelected(true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (BooksOnDiskListItem book : books) {
|
||||
if (book instanceof BooksOnDiskListItem.BookOnDisk) {
|
||||
book.setSelected(
|
||||
selectedBooksId.contains(((BooksOnDiskListItem.BookOnDisk) book).getBook().getId())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user