Skip to content

Commit f7ca67f

Browse files
Checkbox always visible to user in Wifi Hotspot and user can select and unselect zim files when server already started
1 parent d764f34 commit f7ca67f

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class WebServerHelper @Inject constructor(
4343
private var isServerStarted = false
4444
private var validIpAddressDisposable: Disposable? = null
4545

46-
fun startServerHelper(selectedBooksPath: ArrayList<String>): Boolean {
46+
fun startServerHelper(selectedBooksPath: ArrayList<String>, restartServer: Boolean): Boolean {
4747
val ip = getIpAddress()
4848
return if (ip.isNullOrEmpty()) {
4949
false
50-
} else if (startAndroidWebServer(selectedBooksPath)) {
50+
} else if (startAndroidWebServer(selectedBooksPath, restartServer)) {
5151
true
5252
} else {
5353
isServerStarted
@@ -61,17 +61,27 @@ class WebServerHelper @Inject constructor(
6161
}
6262
}
6363

64-
private fun startAndroidWebServer(selectedBooksPath: ArrayList<String>): Boolean {
64+
private fun startAndroidWebServer(
65+
selectedBooksPath: ArrayList<String>,
66+
restartServer: Boolean
67+
): Boolean {
6568
if (!isServerStarted) {
66-
ServerUtils.port = DEFAULT_PORT
67-
kiwixServer = kiwixServerFactory.createKiwixServer(selectedBooksPath).also {
68-
updateServerState(it.startServer(ServerUtils.port))
69-
Log.d(TAG, "Server status$isServerStarted")
70-
}
69+
startKiwixServer(selectedBooksPath)
70+
} else if (restartServer) {
71+
kiwixServer?.stopServer()
72+
startKiwixServer(selectedBooksPath)
7173
}
7274
return isServerStarted
7375
}
7476

77+
private fun startKiwixServer(selectedBooksPath: ArrayList<String>) {
78+
ServerUtils.port = DEFAULT_PORT
79+
kiwixServer = kiwixServerFactory.createKiwixServer(selectedBooksPath).also {
80+
updateServerState(it.startServer(ServerUtils.port))
81+
Log.d(TAG, "Server status$isServerStarted")
82+
}
83+
}
84+
7585
private fun updateServerState(isStarted: Boolean) {
7686
isServerStarted = isStarted
7787
ServerUtils.isServerStarted = isStarted

app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
8282
private lateinit var serviceConnection: ServiceConnection
8383
private var progressDialog: ProgressDialog? = null
8484
private var activityZimHostBinding: ActivityZimHostBinding? = null
85-
private var allBooks: List<BooksOnDiskListItem>? = null
8685
private val selectedBooksPath: ArrayList<String>
8786
get() {
8887
return booksAdapter.items
@@ -101,16 +100,6 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
101100
as ArrayList<String>
102101
}
103102

104-
private val selectedBooks: List<BooksOnDiskListItem>
105-
get() {
106-
return booksAdapter.items
107-
.filter(BooksOnDiskListItem::isSelected)
108-
.filterIsInstance<BookOnDisk>()
109-
.map {
110-
it
111-
}
112-
}
113-
114103
override fun onCreateView(
115104
inflater: LayoutInflater,
116105
container: ViewGroup?,
@@ -277,6 +266,9 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
277266
}
278267
booksAdapter.items = booksList
279268
saveHostedBooks(booksList)
269+
if (ServerUtils.isServerStarted) {
270+
startWifiHotspot(true)
271+
}
280272
}
281273

282274
override fun onStart() {
@@ -331,8 +323,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
331323
activityZimHostBinding?.startServerButton?.setBackgroundColor(
332324
resources.getColor(R.color.stopServerRed)
333325
)
334-
booksAdapter.items = selectedBooks
335-
bookDelegate.selectionMode = SelectionMode.NORMAL
326+
bookDelegate.selectionMode = SelectionMode.MULTI
336327
booksAdapter.notifyDataSetChanged()
337328
}
338329

@@ -358,7 +349,6 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
358349
activityZimHostBinding?.startServerButton?.setBackgroundColor(
359350
resources.getColor(R.color.startServerGreen)
360351
)
361-
allBooks?.let { booksAdapter.items = it }
362352
bookDelegate.selectionMode = SelectionMode.MULTI
363353
booksAdapter.notifyDataSetChanged()
364354
}
@@ -433,15 +423,18 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
433423

434424
override fun addBooks(books: List<BooksOnDiskListItem>) {
435425
booksAdapter.items = books
436-
allBooks = books
437426
}
438427

439428
override fun onIpAddressValid() {
440429
progressDialog?.dismiss()
430+
startWifiHotspot(false)
431+
}
432+
433+
private fun startWifiHotspot(restartServer: Boolean) {
441434
requireActivity().startService(
442435
createHotspotIntent(ACTION_START_SERVER).putStringArrayListExtra(
443436
SELECTED_ZIM_PATHS_KEY, selectedBooksPath
444-
)
437+
).putExtra(RESTART_SERVER, restartServer)
445438
)
446439
}
447440

@@ -452,6 +445,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
452445

453446
companion object {
454447
const val SELECTED_ZIM_PATHS_KEY = "selected_zim_paths"
448+
const val RESTART_SERVER = "restart_server"
455449
const val PERMISSION_REQUEST_CODE_COARSE_LOCATION = 10
456450
}
457451
}

app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotService.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.kiwix.kiwixmobile.core.utils.ServerUtils.getSocketAddress
3030
import org.kiwix.kiwixmobile.webserver.WebServerHelper
3131
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks
3232
import org.kiwix.kiwixmobile.webserver.ZimHostFragment
33+
import org.kiwix.kiwixmobile.webserver.ZimHostFragment.Companion.RESTART_SERVER
3334
import java.lang.ref.WeakReference
3435
import javax.inject.Inject
3536

@@ -69,21 +70,30 @@ class HotspotService :
6970
super.onDestroy()
7071
}
7172

73+
@Suppress("NestedBlockDepth")
7274
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
7375
when (intent.action) {
74-
ACTION_START_SERVER ->
76+
ACTION_START_SERVER -> {
77+
val restartServer = intent.getBooleanExtra(RESTART_SERVER, false)
7578
intent.getStringArrayListExtra(ZimHostFragment.SELECTED_ZIM_PATHS_KEY)?.let {
76-
if (webServerHelper?.startServerHelper(it) == true) {
79+
if (webServerHelper?.startServerHelper(
80+
it,
81+
restartServer
82+
) == true
83+
) {
7784
zimHostCallbacks?.onServerStarted(getSocketAddress())
7885
startForegroundNotificationHelper()
79-
Toast.makeText(
80-
this, R.string.server_started_successfully_toast_message,
81-
Toast.LENGTH_SHORT
82-
).show()
86+
if (!restartServer) {
87+
Toast.makeText(
88+
this, R.string.server_started_successfully_toast_message,
89+
Toast.LENGTH_SHORT
90+
).show()
91+
}
8392
} else {
8493
onServerFailedToStart()
8594
}
8695
} ?: kotlin.run(::onServerFailedToStart)
96+
}
8797
ACTION_STOP_SERVER -> {
8898
Toast.makeText(
8999
this, R.string.server_stopped_successfully_toast_message,

0 commit comments

Comments
 (0)