Skip to content
Open

Done #57

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ plugins {
id 'org.jetbrains.kotlin.android'
}

kotlin {
jvmToolchain(17)
}

android {
compileSdk 34
compileSdk 35
namespace "otus.gpb.homework.fragments"

defaultConfig {
Expand All @@ -34,15 +30,20 @@ android {
kotlinOptions {
jvmTarget = '17'
}

buildFeatures {
viewBinding = true
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.16.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
implementation 'androidx.fragment:fragment-ktx:1.8.6'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityA"
android:label="Activity A"
android:exported="false" />
<activity
android:name=".ActivityB"
android:label="Activity B"
android:exported="false" />
</application>

</manifest>
20 changes: 20 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ActivityA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ActivityA : AppCompatActivity() {

private lateinit var fragmentA: FragmentA

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_a)
if (savedInstanceState == null) {
fragmentA = FragmentA()
supportFragmentManager.beginTransaction()
.replace(R.id.activity_a_container, fragmentA)
.commit()
}
}
}
42 changes: 42 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ActivityB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ActivityB : AppCompatActivity() {
private val saveColor = "saveColor"
private var baColor: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)

if (savedInstanceState != null) {
baColor = savedInstanceState.getInt(saveColor)
}

val fba = FragmentBA()

if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
supportFragmentManager.beginTransaction()
.replace(R.id.activity_ba_container, fba)
.replace(R.id.activity_bb_container, FragmentBB())
.commit()
} else {
supportFragmentManager.beginTransaction()
.replace(R.id.activity_b_container, fba)
.commit()
}

fba.sendColor(baColor)
supportFragmentManager.observeColor(this) {color ->
baColor = color
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(saveColor, baColor)
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import otus.gpb.homework.fragments.ColorGenerator.generateColor

class FragmentA : Fragment() {

override fun onAttach(context: Context) {
super.onAttach(context)

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
} else {
isEnabled = false
@Suppress("DEPRECATION")
requireActivity().onBackPressed()
}
}
}

requireActivity().onBackPressedDispatcher.addCallback(callback)
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.open_fragment_aa).setOnClickListener {

childFragmentManager.beginTransaction()
.replace(R.id.fragment_a_container, FragmentAA.newInstance(generateColor()))
.addToBackStack(null)
.commit()
}
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import otus.gpb.homework.fragments.ColorGenerator.generateColor

private const val SET_COLOR = "setColor"

open class FragmentAA : Fragment() {
private var color: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(SET_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_aa, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.setBackgroundColor(color)
view.findViewById<Button>(R.id.open_fragment_ab).setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_a_container, FragmentAB.newInstance(generateColor()))
.addToBackStack(null)
.commit()
}
}

companion object {
@JvmStatic
fun newInstance(color: Int) =
FragmentAA().apply {
arguments = Bundle().apply {
putInt(SET_COLOR, color)
}
}
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

private const val SET_COLOR = "setColor"

class FragmentAB : Fragment() {
private var color: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
color = it.getInt(SET_COLOR)
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ab, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.setBackgroundColor(color)
}

companion object {
@JvmStatic
fun newInstance(color: Int) =
FragmentAB().apply {
arguments = Bundle().apply {
putInt(SET_COLOR, color)
}
}
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment

class FragmentBA: Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ba, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

applyColor {
c->view.setBackgroundColor(c)
}

val openBB = view.findViewById<Button>(R.id.open_fragment_bb)
openBB?.isVisible =
resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE

openBB.setOnClickListener{
parentFragmentManager.beginTransaction()
.replace(R.id.activity_b_container, FragmentBB())
.commit()
}
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package otus.gpb.homework.fragments

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import otus.gpb.homework.fragments.ColorGenerator.generateColor

class FragmentBB: Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bb, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.send_color).setOnClickListener {
if (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE) {
parentFragmentManager.beginTransaction()
.replace(R.id.activity_b_container, FragmentBA())
.commit()
}
sendColor(generateColor())
}
}
}
29 changes: 29 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package otus.gpb.homework.fragments

import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.LifecycleOwner

private const val F_RESULT_KEY = "fResult"
private const val FM_RESULT_KEY = "fmResult"
private const val COLOR_KEY = "color"

fun Fragment.sendColor(color: Int) {
setFragmentResult(F_RESULT_KEY, bundleOf(COLOR_KEY to color))
setFragmentResult(FM_RESULT_KEY, bundleOf(COLOR_KEY to color))
}

fun Fragment.applyColor(callback: (Int)->Unit) {
setFragmentResultListener(F_RESULT_KEY) { _, bundle ->
callback(bundle.getInt(COLOR_KEY, 0))
}
}

fun FragmentManager.observeColor(lifecycleOwner: LifecycleOwner, callback: (Int)->Unit) {
setFragmentResultListener(FM_RESULT_KEY, lifecycleOwner) {_, bundle ->
callback(bundle.getInt(COLOR_KEY, 0))
}
}
Loading