Skip to content

Commit

Permalink
convert widget classes to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
nikit19 authored and colintheshots committed Jan 24, 2019
1 parent b189185 commit 26f68eb
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 312 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.widget

import android.content.Context
import android.os.Build
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceViewHolder
import android.util.AttributeSet
import android.widget.Switch
import org.mozilla.focus.R
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.utils.Browsers
import org.mozilla.focus.utils.SupportUtils

class DefaultBrowserPreference : Preference {
private var switchView: Switch? = null

// Instantiated from XML
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}

// Instantiated from XML
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}

private fun init() {
widgetLayoutResource = R.layout.preference_default_browser

val appName = context.resources.getString(R.string.app_name)
val title = context.resources.getString(R.string.preference_default_browser2, appName)

setTitle(title)
}

override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)

switchView = holder.findViewById(R.id.switch_widget) as Switch

update()
}

fun update() {
if (switchView != null) {
val browsers = Browsers(context, Browsers.TRADITIONAL_BROWSER_URL)
switchView?.isChecked = browsers.isDefaultBrowser(context)
}
}

public override fun onClick() {
val context = context

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
SupportUtils.openDefaultAppsSettings(context)
TelemetryWrapper.makeDefaultBrowserSettings()
} else {
SupportUtils.openDefaultBrowserSumoPage(context)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.FloatingActionButton
import android.text.TextPaint
import android.util.AttributeSet

import org.mozilla.focus.R

class FloatingSessionsButton : FloatingActionButton {

private var textPaint: TextPaint? = null
private var tabCount: Int = 0

constructor(context: Context) : super(context) {
init()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}

private fun init() {
val paint = Paint()
paint.color = Color.WHITE
paint.typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)

val textSize = resources.getDimensionPixelSize(R.dimen.tabs_button_text_size)

textPaint = TextPaint(paint)
textPaint?.textAlign = Paint.Align.CENTER
textPaint?.textSize = textSize.toFloat()

setImageResource(R.drawable.tab_number_border)
}

fun updateSessionsCount(tabCount: Int) {
this.tabCount = tabCount

contentDescription = resources.getString(R.string.content_description_tab_counter, tabCount.toString())

val params = layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as FloatingActionButtonBehavior?

val shouldBeVisible = tabCount >= 2

behavior?.setEnabled(shouldBeVisible)

if (shouldBeVisible) {
show()
invalidate()
} else {
hide()
}
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

val x = canvas.width / 2f
val y = canvas.height / 2f - (textPaint!!.descent() + textPaint!!.ascent()) / 2f

val text = if (tabCount < TOO_MANY_TABS) tabCount.toString() else TOO_MANY_TABS_SYMBOL

canvas.drawText(text, x, y, textPaint!!)
}

companion object {
/**
* The Answer to the Ultimate Question of Life, the Universe, and Everything. And the number of
* tabs that is just too many.
*/
private val TOO_MANY_TABS = 42
private val TOO_MANY_TABS_SYMBOL = ":("
}
}
Loading

0 comments on commit 26f68eb

Please sign in to comment.