Skip to content

Commit

Permalink
Change UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Kadmiv committed Oct 10, 2018
1 parent 016aa10 commit 91bc59f
Show file tree
Hide file tree
Showing 36 changed files with 288 additions and 210 deletions.
3 changes: 1 addition & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 13 additions & 38 deletions app/src/main/java/com/gaijin/sinopticparser/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.constraint.Group;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
Expand All @@ -17,7 +15,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;

import com.gaijin.sinopticparser.views.fragments.City;
import com.gaijin.sinopticparser.views.adapters.DayPagerAdapter;
Expand All @@ -31,15 +28,14 @@
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.realm.Realm;
import io.realm.RealmResults;


public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,View.OnLongClickListener, Variables {
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, Variables {

@BindView(R.id.toolbar)
Toolbar toolbar;
Expand All @@ -52,7 +48,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On


// Object of DB
Realm realm = null;
Realm realmDB = null;
// Adapter for PagerView
DayPagerAdapter pagerAdapter;
// Change cities submenu in NavigationView
Expand Down Expand Up @@ -81,7 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {
navigationView.setNavigationItemSelectedListener(this);

// Load city from BD
ArrayList<RealmCity> citiesList = loadBD();
ArrayList<RealmCity> citiesList = loadCitiesFromBD();

// Find and load information in web site Sinoptic for all cities
ArrayList<City> cities = realmListToList(citiesList);
Expand All @@ -107,21 +103,21 @@ private ArrayList<City> realmListToList(ArrayList<RealmCity> citiesList) {
*
* @return list of cities for further processing
*/
private ArrayList<RealmCity> loadBD() {
private ArrayList<RealmCity> loadCitiesFromBD() {

// Initialize Realm (just once per application)
Realm.init(this);

// Get a Realm instance for this thread
realm = Realm.getDefaultInstance();
realm.beginTransaction();
cityList = realm.where(RealmCity.class).findAll();
realmDB = Realm.getDefaultInstance();
realmDB.beginTransaction();
cityList = realmDB.where(RealmCity.class).findAll();

ArrayList<RealmCity> list = new ArrayList<>();
for (RealmCity city : cityList) {
list.add(city);
}
realm.commitTransaction();
realmDB.commitTransaction();

createCitiesMenu(list);

Expand Down Expand Up @@ -232,22 +228,6 @@ public void onBackPressed() {
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
Expand All @@ -263,7 +243,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
intent = new Intent(this, SearchCityActivity.class);
startActivityForResult(intent, 1);
break;
case R.id.settings:
case R.id.action_settings:
//Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
//intent = new Intent(this, SearchCityActivity.class);
break;
Expand Down Expand Up @@ -305,11 +285,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String cityInfo = data.getStringExtra("cityInfo");
RealmCity city = new RealmCity(cityInfo.split("\\|"));
Log.d("MyLog", "Returned city is " + cityInfo);
realm.beginTransaction();
realm.insert(city);
realm.commitTransaction();
realmDB.beginTransaction();
realmDB.insert(city);
realmDB.commitTransaction();

ArrayList<RealmCity> citiesList = loadBD();
ArrayList<RealmCity> citiesList = loadCitiesFromBD();

loadWeatherForCities(realmListToList(citiesList));
break;
Expand All @@ -318,9 +298,4 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}


@Override
public boolean onLongClick(View v) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void parseDayInfo(Element body) {
SinopticParser sinoptic = new SinopticParser();
//sinoptic.setLanguach("");

System.out.println("\n" + toString() + "\n");

weatherOnDay = sinoptic.getTimesOfDay(body.html());

System.out.println("\n" + this.toString() + "\n");
}

private String parseDayTemperature(Element temperature, String type) {
Expand Down Expand Up @@ -151,7 +151,7 @@ public String toString() {
String info = String.format(" ICON: %s\n MIN: %s\n MAX: %s\n TIME OF DAY: %s\n DAY DESCRIPTION: %s\n ",
iconOfDay, minTemp, maxTemp, timeOfDay, allDayDescription);

if (weatherOnDay != null) {
if (weatherOnDay.size() > 0) {
for (int i = 0; i < weatherOnDay.size(); i++) {
info += String.format("\n\nTime of day %s\n", i + 1) + weatherOnDay.get(i).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class SinopticParser {
private final String currentTag = "cur";
private final List<String> nowList = Arrays.asList(IMAGE_OF_NOW, TEMP_OF_NOW);

private final List<String> METRICS_1 = Arrays.asList("Температура: %sC", "чувствуется как: %sC",
"Давление: %s мм", "Влажность: %s %%", "Ветер: %s", "Вероятность осадков: %s %%", "Время: %s");


//View weather for now
private SeparateTime nowView;
Expand All @@ -53,9 +50,7 @@ public ArrayList<SeparateTime> getTimesOfDay(String html) {
Elements now = body.getElementsByClass(LEFT_PART);

// On this part we check lang for metrics
List<String> metrics = METRICS_1;

ArrayList<SeparateTime> views = parseAllDay(body, metrics);
ArrayList<SeparateTime> views = parseAllDay(body);
try {
parseNow(now, nowList);
} catch (Exception exe) {
Expand All @@ -68,11 +63,10 @@ public ArrayList<SeparateTime> getTimesOfDay(String html) {
/**
* Function parse html representation of day to separate time views
*
* @param body - html representation of day
* @param metrics - metrics for finding
* @param body - html representation of day
* @return - list of SeparateTime class which contains information for each separate time of day
*/
private ArrayList<SeparateTime> parseAllDay(Element body, List<String> metrics) {
private ArrayList<SeparateTime> parseAllDay(Element body) {

ArrayList<SeparateTime> viewList = new ArrayList<>();

Expand All @@ -99,11 +93,11 @@ private ArrayList<SeparateTime> parseAllDay(Element body, List<String> metrics)
if (timeOfDay.size() == 0) {
continue;
} else {
newView = parseSeparateTime(timeOfDay, metrics);
newView = parseSeparateTime(timeOfDay);
nowView = newView;
}
} else {
newView = parseSeparateTime(timeOfDay, metrics);
newView = parseSeparateTime(timeOfDay);
}

viewList.add(newView);
Expand All @@ -116,10 +110,9 @@ private ArrayList<SeparateTime> parseAllDay(Element body, List<String> metrics)
* This function parse information for one separate time of day information
*
* @param timeOfDay - separate time of day information
* @param metrics - metrics for finding
* @return - SeparateTime object which contains all information for one separate time of day
*/
private SeparateTime parseSeparateTime(Elements timeOfDay, List<String> metrics) {
private SeparateTime parseSeparateTime(Elements timeOfDay) {
SeparateTime newView = new SeparateTime();

/*Parse image*/
Expand All @@ -129,33 +122,51 @@ private SeparateTime parseSeparateTime(Elements timeOfDay, List<String> metrics)
Element description = timeOfDay.get(1).select("div").first();
String shortDescription = description.attr("title");
newView.setShortDescription(shortDescription);

/*Parse temperature*/
String temp_1 = String.format(metrics.get(0), timeOfDay.get(2).text());
newView.setTemp(temp_1);
// String temp_1 = String.format(metrics.get(0), timeOfDay.get(2).text());
String temp_1 = timeOfDay.get(2).text();
newView.setTemp_1(temp_1);

/*Parse temperature 2*/
String temp_2 = String.format(metrics.get(1), timeOfDay.get(3).text());
// String temp_2 = String.format(metrics.get(1), timeOfDay.get(3).text());
String temp_2 = timeOfDay.get(3).text();
newView.setTemp_2(temp_2);

/*Parse Atmospher Pressure*/
String pressure = String.format(metrics.get(2), timeOfDay.get(4).text());
// String pressure = String.format(metrics.get(2), timeOfDay.get(4).text());
String pressure = timeOfDay.get(4).text();
newView.setAtmoPressure(pressure);

/*Parse Humidity*/
String humidity = String.format(metrics.get(3), timeOfDay.get(5).text());
// String humidity = String.format(metrics.get(3), timeOfDay.get(5).text());
String humidity = timeOfDay.get(5).text();
newView.setHumidity(humidity);

/*Parse Wind*/
Element wind = timeOfDay.get(6).select("div").first();
String winds = wind.attr("data-tooltip");
winds = String.format(metrics.get(4), winds);

String winds = "0";
try {
Log.d("MyLog", "data-tooltip " + wind.attr("data-tooltip").toString());
winds = wind.attr("data-tooltip").split(" ")[1];
} catch (Exception ex) {
}
newView.setWind(winds);
String windDirection = wind.attr("class").split(" ")[2];
windDirection = windDirection.substring(windDirection.length() - 2).replace("-", "");
newView.setWindDirection(windDirection);

/*Parse Precipitation*/
String precipit = timeOfDay.get(7).text();
if (precipit.equals("-")) {
precipit = "0";
}
precipit = String.format(metrics.get(5), precipit);
// precipit = String.format(metrics.get(5), precipit);
newView.setPrecipitation(precipit);

/*Parse Local time*/
String localTime = timeOfDay.get(0).text().replace(" ", "");
localTime = String.format(metrics.get(6), localTime);
newView.setTime(localTime);

return newView;
Expand All @@ -170,16 +181,16 @@ private SeparateTime parseSeparateTime(Elements timeOfDay, List<String> metrics)
private void parseNow(Elements nowView, List<String> dayViewList) {

/*First parse image of weather*/
String srcImage = parseSourceImage(nowView);
Element image = nowView.select("img").first();
String shortDescription = image.attr("alt");
this.nowView.setImage(srcImage);
this.nowView.setShortDescription(shortDescription);
// String srcImage = parseSourceImage(nowView);
// Element image = nowView.select("img").first();
// String shortDescription = image.attr("alt");
// this.nowView.setImage(srcImage);
// this.nowView.setShortDescription(shortDescription);

/*Next find temp of now*/
Elements temp = nowView.get(0).getElementsByClass(dayViewList.get(1));
String nowTemp = temp.get(0).text();
this.nowView.setTemp(nowTemp);
String nowTemp = temp.get(0).text().replace("C", "");
this.nowView.setTemp_1(nowTemp);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/gaijin/sinopticparser/views/SinopticApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.gaijin.sinopticparser.views;

import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.gaijin.sinopticparser.MainActivity;
import com.gaijin.sinopticparser.db.RealmCity;

import java.util.regex.Matcher;

import io.realm.RealmResults;

/**
* Created by Kachulyak Ivan.
*/
public class SinopticApp extends Application {

RealmResults<RealmCity> cityList;
SharedPreferences sharedCity;
final String cityKey = "city_key";

@Override
public void onCreate() {
super.onCreate();
Log.d("MyLog"," Create Sinoptic App");
Intent startMainActivity = new Intent(this, MainActivity.class);
//startMainActivity.putExtra("FirstLoadedCity",)
}
}
Loading

0 comments on commit 91bc59f

Please sign in to comment.