Skip to content

Instantly share code, notes, and snippets.

View DDihanov's full-sized avatar

Dimitar Dihanov DDihanov

View GitHub Profile
import java.io.File
val input = File("src/main/kotlin/day13/Day13.txt").readLines().mapNotNull {
when (it) {
"" -> null
else -> it
}
}
sealed class Data {
@DDihanov
DDihanov / SpannedStringFormatter
Last active July 2, 2021 07:29
Formats URLSpans with a %s placeholder into new URLSpans with vararg URLs
// usage:
// spannedStringFormatter.format(context.getText(R.string.mystring), "url1", "url2"))
// where R.string.mystring is <string name="mystring"><![CDATA[ Link one <a href="%s"><b> Link one</b></a> and link two <a href="%s"><b>Link two</b></a>.]]></string>
class SpannedStringFormatter {
/**
* replace all the %s in the spanned string with corresponding URLs
*/
fun format(format: CharSequence, vararg urls: String?): SpannedString {
// make a spannable copy so that we can change the spans (Spanned is immutable)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@id/home_flow"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@id/dashboard_flow"
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
class MainActivity : AppCompatActivity(), ToFlowNavigatable {
private val navigator: Navigator = Navigator()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
interface ToFlowNavigatable {
fun navigateToFlow(flow: NavigationFlow)
}
sealed class NavigationFlow {
object HomeFlow : NavigationFlow()
object DashboardFlow : NavigationFlow()
}
class Navigator {
lateinit var navController: NavController
fun navigateToFlow(navigationFlow: NavigationFlow) = when (navigationFlow) {
NavigationFlow.HomeFlow -> navController.navigate(MainNavGraphDirections.actionGlobalHomeFlow())
NavigationFlow.DashboardFlow -> navController.navigate(MainNavGraphDirections.actionGlobalDashboardFlow())
}
}
class StartFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_start, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {