sample/src/main/java/com/geekorum/aboutoss/sampleapp/MainActivity.kt
changeset 18 ac393491d2eb
parent 15 4e26459b4642
child 34 ce299aacc068
equal deleted inserted replaced
17:6ff6774c8121 18:ac393491d2eb
    19  * You should have received a copy of the GNU General Public License
    19  * You should have received a copy of the GNU General Public License
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
    21  */
    21  */
    22 package com.geekorum.aboutoss.sampleapp
    22 package com.geekorum.aboutoss.sampleapp
    23 
    23 
       
    24 import android.content.Intent
    24 import android.os.Bundle
    25 import android.os.Bundle
    25 import androidx.activity.ComponentActivity
    26 import androidx.activity.ComponentActivity
    26 import androidx.activity.compose.setContent
    27 import androidx.activity.compose.setContent
    27 import androidx.compose.foundation.layout.fillMaxSize
    28 import androidx.compose.foundation.layout.*
    28 import androidx.compose.material3.MaterialTheme
    29 import androidx.compose.material3.*
    29 import androidx.compose.material3.Surface
       
    30 import androidx.compose.material3.Text
       
    31 import androidx.compose.runtime.Composable
    30 import androidx.compose.runtime.Composable
    32 import androidx.compose.ui.Modifier
    31 import androidx.compose.ui.Modifier
    33 import androidx.compose.ui.tooling.preview.Preview
    32 import androidx.compose.ui.tooling.preview.Preview
       
    33 import androidx.compose.ui.unit.dp
    34 import com.geekorum.aboutoss.sampleapp.ui.theme.AboutOssTheme
    34 import com.geekorum.aboutoss.sampleapp.ui.theme.AboutOssTheme
       
    35 import com.geekorum.aboutoss.sampleapp.ui.theme.OpenSourceLicenseTheme
       
    36 import com.geekorum.aboutoss.ui.material3.OpenSourceLicensesActivity
       
    37 import com.geekorum.aboutoss.ui.material.OpenSourceLicensesActivity as Material2OpenSourceLicensesActivity
    35 
    38 
    36 class MainActivity : ComponentActivity() {
    39 class MainActivity : ComponentActivity() {
    37     override fun onCreate(savedInstanceState: Bundle?) {
    40     override fun onCreate(savedInstanceState: Bundle?) {
    38         super.onCreate(savedInstanceState)
    41         super.onCreate(savedInstanceState)
    39         setContent {
    42         setContent {
    40             AboutOssTheme {
    43             AboutOssTheme {
    41                 // A surface container using the 'background' color from the theme
    44                 Sample(
    42                 Surface(
    45                     onMaterial2Click = {
    43                     modifier = Modifier.fillMaxSize(),
    46                         startMaterial2LicenseActivity()
    44                     color = MaterialTheme.colorScheme.background
    47                     },
    45                 ) {
    48                     onMaterial3Click = {
    46                     Greeting("Android")
    49                         startMaterial3LicenseActivity()
    47                 }
    50                     }
       
    51                 )
    48             }
    52             }
       
    53         }
       
    54     }
       
    55 
       
    56     private fun startMaterial2LicenseActivity() {
       
    57         val intent = if (BuildConfig.DEBUG) {
       
    58             // we launch a custom activity in debug to display some fake licenses
       
    59             // see Material2AcPrebuiltLicencesMaterial2Activitytivity for more info
       
    60             Intent(this, PrebuiltLicencesMaterial2Activity::class.java)
       
    61         } else {
       
    62             Intent(this, Material2OpenSourceLicensesActivity::class.java)
       
    63         }
       
    64         startActivity(intent)
       
    65     }
       
    66 
       
    67     private fun startMaterial3LicenseActivity() {
       
    68         // Don't use default MaterialTheme but supply our own
       
    69         OpenSourceLicensesActivity.themeProvider = { content ->
       
    70             OpenSourceLicenseTheme(content)
       
    71         }
       
    72         val intent = if (BuildConfig.DEBUG) {
       
    73             // we launch a custom activity in debug to display some fake licenses
       
    74             // see PrebuiltLicencesMaterial3Activity for more info
       
    75             Intent(this, PrebuiltLicencesMaterial3Activity::class.java)
       
    76         } else {
       
    77             Intent(this, OpenSourceLicensesActivity::class.java)
       
    78         }
       
    79         startActivity(intent)
       
    80     }
       
    81 }
       
    82 
       
    83 @Composable
       
    84 private fun Sample(
       
    85     onMaterial2Click: () -> Unit,
       
    86     onMaterial3Click: () -> Unit,
       
    87 ) {
       
    88     Surface(
       
    89         modifier = Modifier.fillMaxSize(),
       
    90         color = MaterialTheme.colorScheme.background
       
    91     ) {
       
    92         Column(Modifier.fillMaxSize()) {
       
    93             LaunchActivitySection(onMaterial2Click, onMaterial3Click)
       
    94             CustomViewer(modifier = Modifier.padding(horizontal = 16.dp))
    49         }
    95         }
    50     }
    96     }
    51 }
    97 }
    52 
    98 
    53 @Composable
    99 @Composable
    54 fun Greeting(name: String, modifier: Modifier = Modifier) {
   100 private fun LaunchActivitySection(
    55     Text(
   101     onMaterial2Click: () -> Unit,
    56         text = "Hello $name!",
   102     onMaterial3Click: () -> Unit,
    57         modifier = modifier
   103     modifier: Modifier = Modifier
    58     )
   104 ) {
       
   105     Column(modifier.padding(16.dp)) {
       
   106         Text("This section launch a new activity to display licences information")
       
   107         Row(
       
   108             horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier
       
   109                 .fillMaxWidth()
       
   110                 .padding(32.dp)
       
   111         ) {
       
   112             Material2Card(onClick = onMaterial2Click)
       
   113             Material3Card(onClick = onMaterial3Click)
       
   114         }
       
   115     }
    59 }
   116 }
       
   117 
       
   118 
       
   119 @Composable
       
   120 @OptIn(ExperimentalMaterial3Api::class)
       
   121 private fun Material2Card(
       
   122     onClick: () -> Unit,
       
   123     modifier: Modifier = Modifier
       
   124 ) {
       
   125     Card(modifier = modifier, onClick = onClick) {
       
   126         Column(Modifier.padding(16.dp)) {
       
   127             Text("Material2 UI", style = MaterialTheme.typography.labelLarge)
       
   128         }
       
   129     }
       
   130 }
       
   131 
       
   132 @Composable
       
   133 @OptIn(ExperimentalMaterial3Api::class)
       
   134 private fun Material3Card(
       
   135     onClick: () -> Unit,
       
   136     modifier: Modifier = Modifier
       
   137 ) {
       
   138     Card(modifier = modifier, onClick = onClick) {
       
   139         Column(Modifier.padding(16.dp)) {
       
   140             Text("Material3 UI", style = MaterialTheme.typography.labelLarge)
       
   141         }
       
   142     }
       
   143 }
       
   144 
    60 
   145 
    61 @Preview(showBackground = true)
   146 @Preview(showBackground = true)
    62 @Composable
   147 @Composable
    63 fun GreetingPreview() {
   148 fun LauncherActivitySectionPreview() {
    64     AboutOssTheme {
   149     AboutOssTheme {
    65         Greeting("Android")
   150         LaunchActivitySection(onMaterial2Click = {}, onMaterial3Click = {})
    66     }
   151     }
    67 }
   152 }