Skip to content

Commit dc53ff3

Browse files
authored
Kotlinize the playground demo app (#329)
Kotlinize the playground demo app
1 parent 7bc3567 commit dc53ff3

32 files changed

+1446
-1818
lines changed

demo-playground/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
apply plugin: 'com.android.application'
18+
apply plugin: 'kotlin-android'
1819

1920
android {
2021
compileSdkVersion rootProject.ext.compileSdkVersion
@@ -45,6 +46,7 @@ dependencies {
4546
compile "com.android.support:preference-v7:${rootProject.ext.supportLibVersion}"
4647
compile "com.android.support:preference-v14:${rootProject.ext.supportLibVersion}"
4748
compile "com.android.support:design:${rootProject.ext.supportLibVersion}"
49+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
4850

4951
testCompile "junit:junit:${rootProject.ext.junitVersion}"
5052

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox
18+
19+
import android.content.Context
20+
21+
/**
22+
* Convert pixel to dp. Preserve the negative value as it's used for representing
23+
* MATCH_PARENT(-1) and WRAP_CONTENT(-2).
24+
* Ignore the round error that might happen in dividing the pixel by the density.
25+
*
26+
* @param pixel the value in pixel
27+
*
28+
* @return the converted value in dp
29+
*/
30+
fun Context.pixelToDp(pixel: Int): Int {
31+
val displayMetrics = this.resources.displayMetrics
32+
return if (pixel < 0) pixel else Math.round(pixel / displayMetrics.density)
33+
}
34+
35+
/**
36+
* Convert dp to pixel. Preserve the negative value as it's used for representing
37+
* MATCH_PARENT(-1) and WRAP_CONTENT(-2).
38+
*
39+
* @param dp the value in dp
40+
*
41+
* @return the converted value in pixel
42+
*/
43+
fun Context.dpToPixel(dp: Int): Int {
44+
val displayMetrics = this.resources.displayMetrics
45+
return if (dp < 0) dp else Math.round(dp * displayMetrics.density)
46+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox
18+
19+
import android.support.v7.app.AppCompatActivity
20+
import android.support.v7.widget.RecyclerView
21+
import android.view.LayoutInflater
22+
import android.view.ViewGroup
23+
import com.google.android.apps.flexbox.R
24+
25+
/**
26+
* [RecyclerView.Adapter] implementation for [FlexItemViewHolder].
27+
*/
28+
internal class FlexItemAdapter(private val activity: AppCompatActivity, private val flexContainer: FlexContainer) : RecyclerView.Adapter<FlexItemViewHolder>() {
29+
30+
private val layoutParams = mutableListOf<FlexboxLayoutManager.LayoutParams>()
31+
32+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FlexItemViewHolder {
33+
val view = LayoutInflater.from(parent.context)
34+
.inflate(R.layout.viewholder_flex_item, parent, false)
35+
36+
return FlexItemViewHolder(view)
37+
}
38+
39+
override fun onBindViewHolder(holder: FlexItemViewHolder, position: Int) {
40+
val adapterPosition = holder.adapterPosition
41+
// TODO: More optimized set the click listener inside the view holder
42+
holder.itemView.setOnClickListener(FlexItemClickListener(activity,
43+
FlexItemChangedListenerImplRecyclerView(flexContainer, this),
44+
adapterPosition))
45+
holder.bindTo(layoutParams[position])
46+
}
47+
48+
fun addItem(lp: FlexboxLayoutManager.LayoutParams) {
49+
layoutParams.add(lp)
50+
notifyItemInserted(layoutParams.size - 1)
51+
}
52+
53+
fun removeItem(position: Int) {
54+
if (position < 0 || position >= layoutParams.size) {
55+
return
56+
}
57+
layoutParams.removeAt(position)
58+
notifyItemRemoved(layoutParams.size)
59+
notifyItemRangeChanged(position, layoutParams.size)
60+
}
61+
62+
val items get() = layoutParams
63+
64+
override fun getItemCount() = layoutParams.size
65+
}

demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListener.java renamed to demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListener.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Google Inc. All rights reserved.
2+
* Copyright 2017 Google Inc. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.android.flexbox;
17+
package com.google.android.flexbox
1818

1919
/**
2020
* A listener that listens to the change of a flex item
2121
*/
22-
public interface FlexItemChangedListener {
22+
internal interface FlexItemChangedListener {
2323

24-
void onFlexItemChanged(FlexItem flexItem, int viewIndex);
24+
fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int)
2525
}

demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListenerImpl.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox
18+
19+
import android.view.ViewGroup
20+
21+
/**
22+
* Default implementation for the [FlexItemChangedListener].
23+
*/
24+
internal class FlexItemChangedListenerImpl(private val flexContainer: FlexContainer) : FlexItemChangedListener {
25+
26+
override fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int) {
27+
val view = flexContainer.getFlexItemAt(viewIndex)
28+
view.layoutParams = flexItem as ViewGroup.LayoutParams
29+
}
30+
}

demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListenerImplRecyclerView.java renamed to demo-playground/src/main/java/com/google/android/flexbox/FlexItemChangedListenerImplRecyclerView.kt

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.android.flexbox;
17+
package com.google.android.flexbox
1818

19-
import android.support.v7.widget.RecyclerView;
20-
import android.view.View;
21-
import android.view.ViewGroup;
19+
import android.support.v7.widget.RecyclerView
20+
import android.view.ViewGroup
2221

2322
/**
24-
* Implementation for the {@link FlexItemChangedListener}.
23+
* Implementation for the [FlexItemChangedListener].
2524
* It expects RecyclerView as the underlying flex container implementation.
2625
*/
27-
public class FlexItemChangedListenerImplRecyclerView implements FlexItemChangedListener {
26+
internal class FlexItemChangedListenerImplRecyclerView(private val flexContainer: FlexContainer,
27+
private val adapter: RecyclerView.Adapter<*>) : FlexItemChangedListener {
2828

29-
private FlexContainer mFlexContainer;
30-
31-
private RecyclerView.Adapter mAdapter;
32-
33-
public FlexItemChangedListenerImplRecyclerView(FlexContainer flexContainer,
34-
RecyclerView.Adapter adapter) {
35-
mFlexContainer = flexContainer;
36-
mAdapter = adapter;
37-
}
38-
39-
@Override
40-
public void onFlexItemChanged(FlexItem flexItem, int viewIndex) {
41-
View view = mFlexContainer.getFlexItemAt(viewIndex);
42-
view.setLayoutParams((ViewGroup.LayoutParams) flexItem);
43-
mAdapter.notifyDataSetChanged();
29+
override fun onFlexItemChanged(flexItem: FlexItem, viewIndex: Int) {
30+
val view = flexContainer.getFlexItemAt(viewIndex)
31+
view.layoutParams = flexItem as ViewGroup.LayoutParams
32+
adapter.notifyDataSetChanged()
4433
// TODO: An Exception is thrown if notifyItemChanged(int) is used.
4534
// Investigate that, but using LinearLayoutManager also produces the same Exception
4635
// java.lang.IllegalArgumentException: Called attach on a child which is not detached:

demo-playground/src/main/java/com/google/android/flexbox/FlexItemClickListener.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.flexbox
18+
19+
import android.support.v7.app.AppCompatActivity
20+
import android.view.View
21+
22+
/**
23+
* Implementation of the [android.view.View.OnClickListener] when a flex item is clicked in
24+
* the Flexbox Playground demo app.
25+
*/
26+
internal class FlexItemClickListener(private val activity: AppCompatActivity, private val flexItemChangedListener: FlexItemChangedListener,
27+
private val viewIndex: Int) : View.OnClickListener {
28+
29+
override fun onClick(v: View) {
30+
val fragment = FlexItemEditFragment
31+
.newInstance(v.layoutParams as FlexItem, viewIndex)
32+
fragment.setFlexItemChangedListener(flexItemChangedListener)
33+
fragment.show(activity.supportFragmentManager, EDIT_DIALOG_TAG)
34+
}
35+
36+
companion object {
37+
38+
private const val EDIT_DIALOG_TAG = "edit_dialog_tag"
39+
}
40+
}

0 commit comments

Comments
 (0)