-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathMainActivity.kt
More file actions
56 lines (49 loc) · 2.11 KB
/
MainActivity.kt
File metadata and controls
56 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.jetnews.ui
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.navigation3.runtime.NavKey
import com.example.jetnews.JetnewsApplication
import com.example.jetnews.ui.navigation.HomeKey
import com.example.jetnews.ui.navigation.POST_ID
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
val appContainer = (application as JetnewsApplication).container
setContent {
val widthSizeClass = calculateWindowSizeClass(this).widthSizeClass
JetnewsApp(appContainer, widthSizeClass, getDeepLinkKey(intent))
}
}
}
private fun getDeepLinkKey(intent: Intent): HomeKey? {
val uri: Uri = intent.data ?: return null
val pathParams = uri.pathSegments
if (pathParams.lastOrNull() != "home") return null
val queryParams = uri.getQueryParameters(POST_ID)
if (queryParams.isEmpty() || queryParams.size > 1) return null
// "https://developer.android.com/jetnews/home?postId={$POST_ID}"
return HomeKey(postId = Uri.decode(queryParams.firstOrNull()))
}