/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.swisscom.safeconnect.R;
import com.swisscom.safeconnect.model.Subscription;
import java.text.DateFormat;
import java.text.DecimalFormatSymbols;
import java.util.Date;
import java.util.Locale;
/**
* Created by vadim on 19.09.14.
*/
public class AboView extends RelativeLayout {
private TextView tvAboName;
private TextView tvAboPrice;
private TextView tvAboPricePeriod;
private TextView tvValidTill;
private View btnBuy;
private Subscription subscription;
private PipeProgressBar prBuy;
private static DecimalFormatSymbols decimalFormatSymbols
= new DecimalFormatSymbols(Locale.getDefault());
public enum PricePeriod {
MONTH,
YEAR;
public String toString(Context context) {
switch (this) {
case MONTH: return context.getResources().getString(R.string.lab_month);
case YEAR: return context.getResources().getString(R.string.lab_year);
default: return super.toString();
}
}
}
public AboView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.abo_descr, this, true);
tvAboName = (TextView) findViewById(R.id.tv_abo_name);
tvAboPrice = (TextView) findViewById(R.id.tv_abo_price);
tvAboPricePeriod = (TextView) findViewById(R.id.tv_abo_price_period);
tvValidTill = (TextView) findViewById(R.id.tv_valid_till);
btnBuy = findViewById(R.id.btn_buy);
prBuy = (PipeProgressBar) findViewById(R.id.pr_buy);
}
public void setAboName(String name) {
tvAboName.setText(name.replaceAll("\\(.*\\)", ""));
}
public void setAboPrice(String price) {
if (price.indexOf(decimalFormatSymbols.getDecimalSeparator()) >= 0) {
tvAboPrice.setText(price);
} else {
tvAboPrice.setText(price + ".\u2014");
}
}
public void setAboPricePeriod(String currency, PricePeriod period) {
tvAboPricePeriod.setText(currency + "/" + period.toString(getContext()));
}
public void setOnBuyListener(OnClickListener listener) {
btnBuy.setOnClickListener(listener);
}
public void setBtnBuyEnabled(boolean enabled) {
btnBuy.setEnabled(enabled);
}
public void setProgressVisible(boolean visible) {
prBuy.setVisibility(visible? View.VISIBLE: View.INVISIBLE);
}
public void setSubscription(Subscription s, Subscription current, PricePeriod period) {
subscription = s;
setAboName(s.getTitle());
setAboPrice(s.getPrice());
setAboPricePeriod(s.getCurrency(), period);
Date date = new Date(subscription.getDaysValid()*86400000L + (current == null?
System.currentTimeMillis(): current.getValidTill()*1000));
tvValidTill.setText(getContext().getString(R.string.lab_subscription_valid_until,
DateFormat.getDateInstance().format(date)));
}
public Subscription getSubscription() {
return subscription;
}
}