44import android .content .res .AssetManager ;
55import android .graphics .Bitmap ;
66import android .util .Log ;
7+ import android .view .MenuItem ;
78import android .webkit .JavascriptInterface ;
89import android .webkit .WebView ;
910import android .webkit .WebViewClient ;
1011
11- import com .google .android .material .navigation .NavigationView ;
12-
1312import androidx .swiperefreshlayout .widget .SwipeRefreshLayout ;
1413
14+ import com .google .android .material .navigation .NavigationView ;
15+
1516import java .io .ByteArrayOutputStream ;
1617import java .io .IOException ;
1718import java .io .InputStream ;
19+ import java .util .HashMap ;
20+ import java .util .Map ;
1821
1922import de .codebucket .mkkm .ui .MainActivity ;
2023
@@ -28,6 +31,17 @@ public class KKMWebviewClient extends WebViewClient {
2831 private NavigationView mNavigationView ;
2932
3033 private boolean hasInjected = false ;
34+ private Map <String , Integer > navIds = new HashMap <String , Integer >(){{
35+ // R.id.nav_tickets
36+ put ("home" , R .id .nav_tickets );
37+ put ("control" , R .id .nav_tickets );
38+
39+ // R.id.nav_purchase
40+ put ("ticket" , R .id .nav_purchase );
41+
42+ // R.id.nav_account
43+ put ("account" , R .id .nav_account );
44+ }};
3145
3246 public KKMWebviewClient (MainActivity context ) {
3347 mContext = context ;
@@ -40,6 +54,11 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
4054 super .onPageStarted (view , url , favicon );
4155 mSwipeLayout .setEnabled (true );
4256 mSwipeLayout .setRefreshing (true );
57+
58+ // Reset injection if url is webapp
59+ if (url .startsWith (WEBAPP_URL )) {
60+ hasInjected = false ;
61+ }
4362 }
4463
4564 @ Override
@@ -75,6 +94,16 @@ public void onPageFinished(WebView view, String url) {
7594 view .addJavascriptInterface (new ScriptInjectorCallback (), "ScriptInjector" );
7695 view .evaluateJavascript (inject , null );
7796 }
97+
98+ // Set navigation item if page has changed
99+ String key = url .substring (WEBAPP_URL .length () + 4 ).split ("/" )[0 ];
100+ if (navIds .containsKey (key )) {
101+ MenuItem item = mNavigationView .getMenu ().findItem (navIds .get (key ));
102+ if (item != null && !item .isChecked ()) {
103+ ((MainActivity ) mContext ).setTitle (item .getTitle ());
104+ mNavigationView .setCheckedItem (item );
105+ }
106+ }
78107 }
79108
80109 public static String getPageUrl (String page ) {
0 commit comments