Skip to content

Commit d079999

Browse files
committed
Added documentation to code
1 parent 027a1a5 commit d079999

File tree

16 files changed

+275
-22
lines changed

16 files changed

+275
-22
lines changed

app/src/main/java/com/foremlibrary/app/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ private const val WEB_VIEW_FRAGMENT_RESULT_LISTENER_KEY =
1212
private const val DEV_TO = "https://dev.to"
1313
private const val MMA_LIFE = "https://www.thismmalife.com/"
1414

15+
/** Helper activity which connects with foremwebview module.*/
1516
class MainActivity : AppCompatActivity() {
1617

1718
private lateinit var backImageView: ImageView

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ buildscript {
22
repositories {
33
google()
44
mavenCentral()
5-
// maven { url 'https://jitpack.io' }
65
}
76
dependencies {
87
classpath "com.android.tools.build:gradle:4.1.3"

foremwebview/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ android {
1111
minSdk 21
1212
targetSdk 31
1313
versionCode 1
14-
versionName "1.0.5"
14+
versionName "1.0.6"
1515

1616
buildConfigField("String", "ANDROID_BRIDGE", "\"AndroidBridge\"")
17-
buildConfigField("String", "FOREM_AGENT_EXTENSION", "\"ForemWebView/1.0.5\"")
17+
buildConfigField("String", "FOREM_AGENT_EXTENSION", "\"ForemWebView/1.0.6\"")
1818
buildConfigField("String", "PASSPORT_AGENT_EXTENSION", "\"ForemWebView/passport\"")
1919

2020
consumerProguardFiles "consumer-rules.pro"
@@ -67,7 +67,7 @@ afterEvaluate {
6767

6868
groupId = 'com.github.forem'
6969
artifactId = 'ForemWebView-android'
70-
version = '1.0.5'
70+
version = '1.0.6'
7171
}
7272
}
7373
}

foremwebview/src/main/java/com/forem/webview/AndroidWebViewBridge.kt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.google.gson.Gson
1717
import org.json.JSONObject
1818
import java.util.*
1919

20+
/** Bridge between WebView and its client where all the javascript interfaces are designed. */
2021
class AndroidWebViewBridge(
2122
private val context: Activity,
2223
private val webViewClient: ForemWebViewClient
@@ -37,18 +38,35 @@ class AndroidWebViewBridge(
3738
}
3839
}
3940

41+
/**
42+
* Function which gets called once the webview has fully loaded a valid forem instance which
43+
* contains the details of forem like name, logo, etc.
44+
*
45+
* @param foremMetaDataJsonObject an object which contains meta data of forem.
46+
*/
4047
@JavascriptInterface
4148
fun processForemMetaData(foremMetaDataJsonObject: String) {
4249
var foremMetaDataMap: Map<String, String> = HashMap()
4350
foremMetaDataMap = Gson().fromJson(foremMetaDataJsonObject, foremMetaDataMap.javaClass)
4451
webViewClient.foremMetaDataReceived(foremMetaDataMap)
4552
}
4653

54+
/**
55+
* Function which gets called by website when an error occurs.
56+
*
57+
* @param errorTag tag required for log message.
58+
* @param errorMessage message required for log message.
59+
*/
4760
@JavascriptInterface
4861
fun logError(errorTag: String, errorMessage: String) {
4962
Log.e(errorTag, errorMessage)
5063
}
5164

65+
/**
66+
* Function which gets called once the user has successfully logged-in.
67+
*
68+
* @param message contains user related meta data like name, id, etc.
69+
*/
5270
@JavascriptInterface
5371
fun userLoginMessage(message: String) {
5472
val userDataObject = JSONObject(message)
@@ -59,17 +77,28 @@ class AndroidWebViewBridge(
5977
}
6078
}
6179

80+
/**
81+
* Function which gets called once the user has successfully logged-out.
82+
*
83+
* @param message contains user related data.
84+
*/
6285
@JavascriptInterface
6386
fun userLogoutMessage(message: String) {
6487
// User logged-out
6588
webViewClient.userLoggedOut()
6689
}
6790

91+
/**
92+
* Function which gets triggered when user does any action related to podcast.
93+
*
94+
* @param message contains information related to podcast like its url, play action, pause
95+
* action, title, etc. which can be used to load th podcast in audio service.
96+
*/
6897
@JavascriptInterface
6998
fun podcastMessage(message: String) {
7099
// Reference: https://stackoverflow.com/questions/9446868/access-ui-from-javascript-on-android
71100
context.runOnUiThread {
72-
var map: Map<String, String> = java.util.HashMap()
101+
var map: Map<String, String> = HashMap()
73102
map = Gson().fromJson(message, map.javaClass)
74103
when (map["action"]) {
75104
"load" -> loadPodcast(map["url"])
@@ -115,7 +144,7 @@ class AndroidWebViewBridge(
115144
}
116145
}
117146

118-
fun podcastTimeUpdate() {
147+
private fun podcastTimeUpdate() {
119148
audioService?.let {
120149
val time = it.currentTimeInSec() / 1000
121150
val duration = it.durationInSec() / 1000
@@ -133,6 +162,11 @@ class AndroidWebViewBridge(
133162
}
134163
}
135164

165+
/**
166+
* Function which gets triggered when user first clicks on video in webview player.
167+
*
168+
* @param message contains information like video url and current-time in video.
169+
*/
136170
@JavascriptInterface
137171
fun videoMessage(message: String) {
138172
var map: Map<String, String> = HashMap()
@@ -157,6 +191,8 @@ class AndroidWebViewBridge(
157191
/**
158192
* This method is used to open the native share-sheet of Android when simple text is to be
159193
* shared from the web-view.
194+
*
195+
* @param text contains the text which needs to be shared natively.
160196
*/
161197
@JavascriptInterface
162198
fun shareText(text: String) {
@@ -171,23 +207,41 @@ class AndroidWebViewBridge(
171207
context.startActivity(shareIntent)
172208
}
173209

210+
/**
211+
* This function copies the text natively to clipboard.
212+
*
213+
* @param copyText contains the text which gets saved to clipboard natively.
214+
*/
174215
@JavascriptInterface
175216
fun copyToClipboard(copyText: String) {
176217
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
177218
val clipData = ClipData.newPlainText("Forem", copyText)
178219
clipboard?.setPrimaryClip(clipData)
179220
}
180221

222+
/**
223+
* Shows toast in android app triggered via website.
224+
*
225+
* @param message required to be displayed in Toast.
226+
*/
181227
@JavascriptInterface
182228
fun showToast(message: String) {
183229
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
184230
}
185231

232+
/**
233+
* Helps in sending the timer value for the video to the backend.
234+
*
235+
* @param seconds new updated value of current time in video.
236+
*/
186237
fun updateTimer(seconds: String) {
187238
val message = mapOf("action" to "tick", "currentTime" to seconds)
188239
webViewClient.sendBridgeMessage(BridgeMessageType.VIDEO, message)
189240
}
190241

242+
/**
243+
* Helps in sending the information to webview that the video has been paused.
244+
*/
191245
fun videoPlayerPaused() {
192246
webViewClient.sendBridgeMessage(BridgeMessageType.VIDEO, mapOf("action" to "pause"))
193247
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.forem.webview
22

3+
/** Enum to determine common actions within podcast and video. */
34
enum class BridgeMessageType(val messageType: String) {
5+
/** PODCAST enum. */
46
PODCAST("podcast"),
7+
/** VIDEO enum. */
58
VIDEO("video")
69
}

foremwebview/src/main/java/com/forem/webview/FileChooserListener.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package com.forem.webview
33
import android.net.Uri
44
import android.webkit.ValueCallback
55

6+
/** Interface to open file chooser natively in android. */
67
interface FileChooserListener {
8+
/**
9+
* Function which passes the filePathCallback which gets used again once the images are selected
10+
* natively.
11+
*/
712
fun openFileChooser(filePathCallback: ValueCallback<Array<Uri>>?)
813
}

foremwebview/src/main/java/com/forem/webview/ForemWebChromeClient.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.webkit.ValueCallback
55
import android.webkit.WebChromeClient
66
import android.webkit.WebView
77

8+
/** Class which extends WebChromeClient to override all out-of WebView actions. */
89
class ForemWebChromeClient(
910
private val fileChooserListener: FileChooserListener
1011
) : WebChromeClient() {

foremwebview/src/main/java/com/forem/webview/ForemWebViewClient.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import org.json.JSONObject
1717
import java.net.URL
1818
import java.util.Locale
1919

20+
/**
21+
* Class which extends WebViewClient to override all functions and implement custom functionalities
22+
* within the WebView.
23+
*/
2024
@SuppressLint("DefaultLocale")
2125
class ForemWebViewClient(
2226
private val activity: Activity?,
@@ -37,6 +41,11 @@ class ForemWebViewClient(
3741
setBaseUrl(originalUrl)
3842
}
3943

44+
/**
45+
* Sets the base url.
46+
*
47+
* @param baseUrl is the url which is basically the host.
48+
*/
4049
fun setBaseUrl(baseUrl: String) {
4150
this.baseUrl = baseUrl
4251
}
@@ -47,6 +56,12 @@ class ForemWebViewClient(
4756

4857
private var token: String? = null
4958

59+
/**
60+
* Function called via [AndroidWebViewBridge] when it successfully fetches the meta data of
61+
* forem instance.
62+
*
63+
* @param foremMetaDataMap is a map of forem related meta data.
64+
*/
5065
fun foremMetaDataReceived(foremMetaDataMap: Map<String, String>) {
5166
if (UrlChecks.checkUrlIsCorrect(baseUrl)) {
5267
val host = URL(baseUrl).host
@@ -59,15 +74,27 @@ class ForemWebViewClient(
5974
}
6075
}
6176

77+
/**
78+
* Function which gets called when user is logged-in successfully.
79+
*
80+
* @param userId unique user if associated with current user.
81+
*/
6282
fun userLoggedIn(userId: Int) {
6383
this.userId = userId
6484
generateFirebaseToken()
6585
}
6686

87+
/**
88+
* Gets called when user logs-out of the website.
89+
*/
6790
fun userLoggedOut() {
6891
unregisterDevice()
6992
}
7093

94+
/**
95+
* Function to set the clearHistory boolean to true.
96+
* @param
97+
*/
7198
fun clearHistory() {
7299
clearHistory = true
73100
}
@@ -232,6 +259,13 @@ class ForemWebViewClient(
232259
}
233260
}
234261

262+
/**
263+
* This function gets called from [AndroidWebViewBridge] whenever some information needs to be
264+
* sent to the website from the android device.
265+
*
266+
* @param type can be either podcast of video.
267+
* @param message contains the information along with actions which needs to be sent.
268+
*/
235269
fun sendBridgeMessage(type: BridgeMessageType, message: Map<String, Any>) {
236270
val payload = mutableMapOf<String, Any>()
237271
payload.putAll(message)

foremwebview/src/main/java/com/forem/webview/ForemWebViewSession.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ package com.forem.webview
22

33
import android.util.Log
44

5+
/**
6+
* Helper class to play and pause video in [VideoPlayerActivity] and send that information to
7+
* website.
8+
*/
59
class ForemWebViewSession {
610

11+
/** The [AndroidWebViewBridge] instance required to call javascript interface functions. */
712
var androidWebViewBridge: AndroidWebViewBridge? = null
813

914
private var instance: ForemWebViewSession? = null
1015

16+
/**
17+
* Function which creates a new instance of this class or shares the old instance.
18+
*
19+
* @return instance of [ForemWebViewSession].
20+
*/
1121
fun getInstance(): ForemWebViewSession? {
1222
if (instance == null) {
1323
synchronized(ForemWebViewSession::class.java) {
@@ -19,13 +29,19 @@ class ForemWebViewSession {
1929
return instance
2030
}
2131

32+
/** Gets called whenever the video gets paused or the [VideoPlayerActivity] is destroyed. */
2233
fun videoPlayerPaused() {
2334
if (androidWebViewBridge == null) {
2435
Log.e("ForemWebViewSession", "videoPlayerPaused: androidWebViewBridge is null")
2536
}
2637
androidWebViewBridge?.videoPlayerPaused()
2738
}
2839

40+
/**
41+
* This function regularly updates the timer for the video on backend.
42+
*
43+
* @return seconds at which the video is at currently.
44+
*/
2945
fun videoPlayerTimerUpdate(seconds: String) {
3046
if (androidWebViewBridge == null) {
3147
Log.e("ForemWebViewSession", "videoPlayerTimerUpdate: androidWebViewBridge is null")

foremwebview/src/main/java/com/forem/webview/UrlChecks.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.net.URISyntaxException
88
import java.net.URL
99
import java.util.Locale
1010

11+
/** Helper object which checks different urls with helper functions. */
1112
object UrlChecks {
1213
private val overrideUrlList = listOf(
1314
"account.forem.com",
@@ -23,6 +24,13 @@ object UrlChecks {
2324
"https://accounts.google.co.in"
2425
)
2526

27+
/**
28+
* Function which determines the url type w.r.t. to host url.
29+
*
30+
* @param url which will get compared with the host.
31+
* @param host is the main url for comparison.
32+
* @return an emum described within from [UrlType]
33+
*/
2634
@SuppressLint("DefaultLocale")
2735
fun getURLType(url: String, host: String): UrlType {
2836
// TODO(#178): Special case- if URL is is https://m.facebook.com/ and host is www.facebook.com
@@ -51,6 +59,12 @@ object UrlChecks {
5159
}
5260
}
5361

62+
/**
63+
* Checks if text is correct valid url or not.
64+
* @param url which needs to be checked.
65+
* @throws MalformedURLException or URISyntaxException or Exception if url is not valid.
66+
* @return true if its a valid url.
67+
*/
5468
fun checkUrlIsCorrect(url: String): Boolean {
5569
return try {
5670
val checkURL = URL(url)
@@ -65,6 +79,14 @@ object UrlChecks {
6579
}
6680
}
6781

82+
/**
83+
* Checks if the url is valid and then append/adjust to https:// prefix
84+
*
85+
* @param url which needs to be corrected.
86+
* @return a valid https url
87+
*
88+
* @sample: if url is "http://example.com" it will return "https://example.com"
89+
*/
6890
fun checkAndAppendHttpsToUrl(url: String): String {
6991
return if (url.isValidUrl()) {
7092
when {

0 commit comments

Comments
 (0)