Skip to content

Commit

Permalink
tried 3 different entry points for app navigation, xml, compose, xml …
Browse files Browse the repository at this point in the history
…+ compose
  • Loading branch information
Mr-Smithy-x committed Mar 8, 2024
1 parent 81bcc17 commit ac4f143
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 63 deletions.
11 changes: 6 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
android:taskAffinity=""
android:name="io.vonley.mi.ui.interop.MainActivityXml"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
Expand All @@ -41,11 +47,6 @@
android:name="io.vonley.mi.ui.compose.MainActivity"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
<meta-data
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/io/vonley/mi/MiApplication.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vonley.mi

import android.app.Application
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.nsd.NsdManager
Expand All @@ -19,7 +20,8 @@ class MiApplication : Application() {
override fun onCreate() {
super.onCreate()
FirebaseApp.initializeApp(this)
startService(Intent(this, PSXService::class.java))
val intent = Intent(this, PSXService::class.java)
startService(intent)
}

companion object {
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/io/vonley/mi/di/network/impl/MiServerImpl.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.vonley.mi.di.network.impl

import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.qualifiers.ApplicationContext
Expand Down Expand Up @@ -245,7 +248,7 @@ class MiServerImpl constructor(
context,
System.currentTimeMillis().toInt(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val style = NotificationCompat.BigTextStyle()
Expand Down Expand Up @@ -280,6 +283,20 @@ class MiServerImpl constructor(
super.start()
delay(3000)
withContext(Dispatchers.Main) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return@withContext
}
NotificationManagerCompat.from(context).notify(0, create())
}
} catch (e: Throwable) {
Expand Down
109 changes: 59 additions & 50 deletions app/src/main/java/io/vonley/mi/ui/compose/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.material.Button
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -28,6 +29,7 @@ import androidx.navigation.compose.rememberNavController
import dagger.hilt.android.AndroidEntryPoint
import io.vonley.mi.common.templates.nav.BottomNavigationBar
import io.vonley.mi.common.templates.nav.TopBar
import io.vonley.mi.ui.ComposableFun
import io.vonley.mi.ui.MainViewModel
import io.vonley.mi.ui.TabItem
import io.vonley.mi.ui.screens.consoles.presentation.ConsolesView
Expand All @@ -47,6 +49,46 @@ class MainActivity : ComponentActivity() {

}

@Composable
fun MainView() {
val navController = rememberNavController()
scaffold(navController = navController) {
NavHost(navController = navController, TabItem.Home.route) {
composable(TabItem.Home.route) {
HomeView()
}
composable(TabItem.Consoles.route) {
ConsolesView()
}
composable(TabItem.Packages.route) {
RepositoryView()
}
composable(TabItem.Ftp.route) {
FTPView()
}
composable(TabItem.Settings.route) {
SettingsView()
}
}
}
}

@Composable
fun scaffold(navController: NavHostController, builder: ComposableFun) {
Scaffold(
topBar = { TopBar() },
bottomBar = {
BottomNavigationBar(navController)
},
content = { padding -> // We have to pass the scaffold inner padding to our content. That's why we use Box.
Box(modifier = Modifier.padding(padding)) {
builder()
}
},
backgroundColor = Color.White // Set background color to avoid the white flashing when you switch between screens
)
}


@Composable
fun Root() {
Expand All @@ -63,22 +105,25 @@ fun Root() {
val viewModel =
it.sharedViewModel<MainViewModel>(navController = navController)
Button(
modifier = Modifier.padding(16.dp)
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
onClick = {
navController.navigate(route = "overview") {
popUpTo(route = "auth") {
//A, B[Our Nav Graph Here], C, D[Our Screen Is Here] -> E (overview)
//A -> E
inclusive =
true // it would also pop this current view out the backstack
navController.navigate(route = "overview") {
popUpTo(route = "auth") {
//A, B[Our Nav Graph Here], C, D[Our Screen Is Here] -> E (overview)
//A -> E
inclusive =
true // it would also pop this current view out the backstack
}
}
}
}) {
}) {

Text(text = "Example", modifier = Modifier
.padding(16.dp)
.wrapContentHeight(), fontSize=22.sp)
Text(
text = "Example", modifier = Modifier
.padding(16.dp)
.wrapContentHeight(), fontSize = 22.sp
)
}
}

Expand All @@ -98,8 +143,8 @@ fun Root() {
startDestination = "main",
route = "overview"
) {
composable(route = "main") {
MainScreen(navController)
composable("main") {
MainView()
}
}
}
Expand All @@ -114,42 +159,6 @@ inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navControll
return viewModel(viewModelStoreOwner = parentEntry)
}

@Composable
fun MainScreen(navController: NavHostController = rememberNavController()) {
Scaffold(
topBar = { TopBar() },
bottomBar = { BottomNavigationBar(navController) },
content = { padding -> // We have to pass the scaffold inner padding to our content. That's why we use Box.
Box(modifier = Modifier.padding(padding)) {
Navigation(navController = navController)
}
},
backgroundColor = Color.White // Set background color to avoid the white flashing when you switch between screens
)
}


@Composable
fun Navigation(navController: NavHostController) {
NavHost(navController, startDestination = TabItem.Home.route) {
composable(TabItem.Home.route) {
HomeView()
}
composable(TabItem.Consoles.route) {
ConsolesView()
}
composable(TabItem.Packages.route) {
RepositoryView()
}
composable(TabItem.Ftp.route) {
FTPView()
}
composable(TabItem.Settings.route) {
SettingsView()
}
}
}


@Preview(showBackground = true)
@Composable
Expand Down
70 changes: 65 additions & 5 deletions app/src/main/java/io/vonley/mi/ui/interop/MainActivityXml.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package io.vonley.mi.ui.interop

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.viewinterop.AndroidViewBinding
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.navigateUp
import dagger.hilt.android.AndroidEntryPoint
import io.vonley.mi.R
import io.vonley.mi.databinding.ActivityMainXmlBinding
import java.util.zip.Inflater

@AndroidEntryPoint
class MainActivityXml : AppCompatActivity(), NavController.OnDestinationChangedListener {
Expand All @@ -25,14 +28,71 @@ class MainActivityXml : AppCompatActivity(), NavController.OnDestinationChangedL
private val navController
get() = navHostFragment.navController


private lateinit var appBarConfiguration: AppBarConfiguration

override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment_container)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PreviewHomeFragmentView() { binding ->
this.navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
this.navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setSupportActionBar(binding.bottomAppBar)
NavigationUI.setupWithNavController(binding.bottomNavigation, navController)
navController.addOnDestinationChangedListener(this)
navController.addOnDestinationChangedListener { controller, destination, arguments ->
val i = when (destination.id) {
R.id.fragment_payload -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_upload_two
)
)
View.VISIBLE
}

R.id.fragment_ftp -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.ic_svg_upload
)
)
View.VISIBLE
}

R.id.fragment_console -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_add
)
)
View.VISIBLE
}

R.id.fragment_home -> {
binding.fab.setOnClickListener(null)
binding.fab.setImageDrawable(
ContextCompat.getDrawable(
this@MainActivityXml,
R.drawable.icon_svg_info
)
)
View.VISIBLE
}

else -> View.GONE
}
binding.fab.visibility = i
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main_xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
app:itemIconTint="@color/tab_selected_color"
app:itemTextColor="@color/tab_selected_color"
app:labelVisibilityMode="auto"
app:menu="@menu/bottom_menu" />
app:menu="@menu/bottom_menu_compose" />

</com.google.android.material.bottomappbar.BottomAppBar>

Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/menu/bottom_menu_compose.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/fragment_home_view"
android:title="Home"
android:icon="@drawable/icon_svg_home"/>
<item
android:id="@+id/fragment_console_view"
android:title="Console"
android:icon="@drawable/icon_svg_monitor"/>
<item
android:id="@+id/fragment_packages_view"
android:title="Payload"
android:icon="@drawable/icon_svg_folder"/>
<item
android:id="@+id/fragment_ftp_view"
android:title="FTP"
android:icon="@drawable/icon_svg_upload_two"/>
<item
android:id="@+id/fragment_settings_view"
android:title="Setting"
android:icon="@drawable/icon_svg_settings"/>
</menu>

0 comments on commit ac4f143

Please sign in to comment.