convert MainActivity to kotlin

This commit is contained in:
tibbi 2016-09-16 21:32:02 +02:00
parent 0993212094
commit 228ab622e5
2 changed files with 91 additions and 111 deletions

View File

@ -1,111 +0,0 @@
package com.simplemobiletools.calendar.activities;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.simplemobiletools.calendar.Config;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.Formatter;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.adapters.MyPagerAdapter;
import com.simplemobiletools.calendar.fragments.MonthFragment;
import com.simplemobiletools.calendar.views.MyViewPager;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends SimpleActivity implements MonthFragment.NavigationListener {
@BindView(R.id.view_pager) MyViewPager mPager;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
private static final int PREFILLED_MONTHS = 73;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
final String today = new DateTime().toString(Formatter.DAYCODE_PATTERN);
fillViewPager(today);
}
@Override
protected void onDestroy() {
super.onDestroy();
Config.newInstance(getApplicationContext()).setIsFirstRun(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
case R.id.about:
startActivity(new Intent(getApplicationContext(), AboutActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@OnClick(R.id.calendar_fab)
public void fabClicked(View view) {
final Intent intent = new Intent(getApplicationContext(), EventActivity.class);
final String tomorrowCode = Formatter.getDayCodeFromDateTime(new DateTime(DateTimeZone.getDefault()).plusDays(1));
intent.putExtra(Constants.DAY_CODE, tomorrowCode);
startActivity(intent);
}
private void fillViewPager(String targetMonth) {
final List<String> codes = getMonths(targetMonth);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), codes, this);
mPager.setAdapter(adapter);
mPager.setCurrentItem(codes.size() / 2);
}
private List<String> getMonths(String code) {
final List<String> months = new ArrayList<>(PREFILLED_MONTHS);
final DateTime today = Formatter.getDateTimeFromCode(code);
for (int i = -PREFILLED_MONTHS / 2; i <= PREFILLED_MONTHS / 2; i++) {
months.add(Formatter.getDayCodeFromDateTime(today.plusMonths(i)));
}
return months;
}
@Override
public void goLeft() {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
@Override
public void goRight() {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
@Override
public void goToDateTime(DateTime dateTime) {
fillViewPager(Formatter.getDayCodeFromDateTime(dateTime));
}
}

View File

@ -0,0 +1,91 @@
package com.simplemobiletools.calendar.activities
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import butterknife.OnClick
import com.simplemobiletools.calendar.Config
import com.simplemobiletools.calendar.Constants
import com.simplemobiletools.calendar.Formatter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.adapters.MyPagerAdapter
import com.simplemobiletools.calendar.fragments.MonthFragment
import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.util.*
class MainActivity : SimpleActivity(), MonthFragment.NavigationListener {
private val PREFILLED_MONTHS = 73
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val today = DateTime().toString(Formatter.DAYCODE_PATTERN)
fillViewPager(today)
}
override fun onDestroy() {
super.onDestroy()
Config.newInstance(applicationContext).isFirstRun = false
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.settings -> {
startActivity(Intent(applicationContext, SettingsActivity::class.java))
return true
}
R.id.about -> {
startActivity(Intent(applicationContext, AboutActivity::class.java))
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
@OnClick(R.id.calendar_fab)
fun fabClicked(view: View) {
val intent = Intent(applicationContext, EventActivity::class.java)
val tomorrowCode = Formatter.getDayCodeFromDateTime(DateTime(DateTimeZone.getDefault()).plusDays(1))
intent.putExtra(Constants.DAY_CODE, tomorrowCode)
startActivity(intent)
}
private fun fillViewPager(targetMonth: String) {
val codes = getMonths(targetMonth)
val adapter = MyPagerAdapter(supportFragmentManager, codes, this)
view_pager.adapter = adapter
view_pager.currentItem = codes.size / 2
}
private fun getMonths(code: String): List<String> {
val months = ArrayList<String>(PREFILLED_MONTHS)
val today = Formatter.getDateTimeFromCode(code)
for (i in -PREFILLED_MONTHS / 2..PREFILLED_MONTHS / 2) {
months.add(Formatter.getDayCodeFromDateTime(today.plusMonths(i)))
}
return months
}
override fun goLeft() {
view_pager.currentItem = view_pager.currentItem - 1
}
override fun goRight() {
view_pager.currentItem = view_pager.currentItem + 1
}
override fun goToDateTime(dateTime: DateTime) {
fillViewPager(Formatter.getDayCodeFromDateTime(dateTime))
}
}