Browse Source

Query available instances asynchronously. Correctly filter states and lists as available by previous selections

master
Robby Zambito 6 months ago
parent
commit
d48e8b9d2d
6 changed files with 79 additions and 54 deletions
  1. +21
    -14
      lib/endpoints/unauthenticated/available_instances.dart
  2. +10
    -5
      lib/main.dart
  3. +7
    -2
      lib/views/unauthenticated/auth_screen.dart
  4. +30
    -29
      lib/views/unauthenticated/instance_selector.dart
  5. +7
    -1
      lib/views/unauthenticated/login.dart
  6. +4
    -3
      lib/views/unauthenticated/sign_up.dart

+ 21
- 14
lib/endpoints/unauthenticated/available_instances.dart View File

@ -13,21 +13,28 @@
// You should have received a copy of the GNU General Public License
// along with Hamlet Social. If not, see <https://www.gnu.org/licenses/>.
Future<List<_InstanceInfo>> availableInstances() async {
import 'dart:ui';
return <_InstanceInfo>[
_InstanceInfo(
country: "us",
state: "ny",
city: "cazenovia"
),
import 'package:country_codes/country_codes.dart';
import 'package:hamlet_social/data/instance.dart';
Future<List<HamletInstance>> availableInstances() async {
return <HamletInstance>[
HamletInstance(
city: "Cazenovia",
state: "New York",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Syracuse",
state: "New York",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Pittsburgh",
state: "Pennsylvania",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Berlin",
country: CountryCodes.detailsForLocale(Locale('de', 'DE'))),
];
}
class _InstanceInfo {
String country;
String state;
String city;
_InstanceInfo({this.country, this.state, this.city});
}

+ 10
- 5
lib/main.dart View File

@ -15,6 +15,8 @@
import 'package:country_codes/country_codes.dart';
import 'package:flutter/material.dart';
import 'package:hamlet_social/data/instance.dart';
import 'package:hamlet_social/endpoints/unauthenticated/available_instances.dart';
import 'package:hamlet_social/views/authenticated/timeline_screen.dart';
import 'package:hamlet_social/views/unauthenticated/auth_screen.dart';
import 'package:pleroma_dart/pleroma_dart.dart';
@ -39,19 +41,22 @@ Future main() async {
await authBloc.initalized;
List<HamletInstance> hamletInstances = await availableInstances();
runApp(MyApp(
bloc: authBloc,
pleroma: pleroma,
));
bloc: authBloc, pleroma: pleroma, hamletInstances: hamletInstances));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
const MyApp({Key key, @required this.bloc, this.pleroma}) : super(key: key);
const MyApp(
{Key key, @required this.bloc, this.pleroma, this.hamletInstances})
: super(key: key);
final AuthBloc bloc;
final Pleroma pleroma;
final List<HamletInstance> hamletInstances;
@override
Widget build(BuildContext context) {
@ -82,7 +87,7 @@ class MyApp extends StatelessWidget {
initialRoute: '/',
routes: {},
home: bloc.account.value == null
? AuthScreen()
? AuthScreen(hamletInstances: hamletInstances)
: TimelineScreen(account: bloc.account.value),
),
);

+ 7
- 2
lib/views/unauthenticated/auth_screen.dart View File

@ -16,6 +16,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:hamlet_social/data/instance.dart';
import 'package:hamlet_social/views/authenticated/timeline_screen.dart';
import 'package:hamlet_social/views/unauthenticated/instance_selector.dart';
import 'package:pleroma_flutter/pleroma_flutter.dart';
@ -26,7 +27,9 @@ import 'login.dart';
import 'sign_up.dart';
class AuthScreen extends StatefulWidget {
AuthScreen({Key key}) : super(key: key);
AuthScreen({Key key, this.hamletInstances}) : super(key: key);
List<HamletInstance> hamletInstances;
@override
State<StatefulWidget> createState() => _AuthScreenState();
@ -74,7 +77,9 @@ class _AuthScreenState extends State {
key: _authBuilderKey,
bloc: bloc,
onLogin: _onLogin,
child: !_isSignUp ? SignUpForm() : LoginForm(),
child: !_isSignUp
? SignUpForm(widget.hamletInstances)
: LoginForm(widget.hamletInstances),
),
);
}

+ 30
- 29
lib/views/unauthenticated/instance_selector.dart View File

@ -19,8 +19,13 @@ import 'package:flutter/material.dart';
import 'package:direct_select/direct_select.dart';
import 'package:hamlet_social/data/instance.dart';
import 'package:country_codes/country_codes.dart';
import 'package:hamlet_social/endpoints/unauthenticated/available_instances.dart';
class InstanceSelector extends StatefulWidget {
final List<HamletInstance> _hamletInstances;
InstanceSelector(this._hamletInstances);
@override
State<StatefulWidget> createState() => _InstanceSelectorState();
}
@ -29,31 +34,14 @@ class _InstanceSelectorState extends State {
var _isCustomInstance = false;
final _formKey = GlobalKey<FormState>();
final List<HamletInstance> _hamletInstanceList = [
HamletInstance(
city: "Cazenovia",
state: "New York",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Syracuse",
state: "New York",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Pittsburgh",
state: "Pennsylvania",
country: CountryCodes.detailsForLocale(Locale('en', 'US'))),
HamletInstance(
city: "Berlin",
country: CountryCodes.detailsForLocale(Locale('de', 'DE'))),
// "United States of America",
// "Canada",
// "Germany",
// "Spain",
// "Italy",
];
List<Widget> _buildCountryList() {
final res = _hamletInstanceList
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.map((i) => i.country.localizedName)
.toSet()
.toList();
@ -66,7 +54,13 @@ class _InstanceSelectorState extends State {
int _selectedCountryIndex = 0;
List<Widget> _buildStateList() {
final res = _hamletInstanceList
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.where((i) =>
i.country.localizedName ==
(_buildCountryList()[_selectedCountryIndex] as Text).data)
@ -75,23 +69,30 @@ class _InstanceSelectorState extends State {
.toSet()
.toList();
print("res: ${res.toString()}");
return res.map((s) => Text(s)).toList();
}
int _selectedStateIndex = 0;
List<Widget> _buildCityList() {
final res = _hamletInstanceList
if (this.widget._hamletInstances.isEmpty) {
return [Text("None")];
}
final res = this
.widget
._hamletInstances
.where((i) =>
i.country.localizedName ==
(_buildCountryList()[_selectedCountryIndex] as Text).data)
(_buildCountryList()[_selectedCountryIndex] as Text).data &&
(_buildStateList().isEmpty ||
i.state ==
(_buildStateList()[_selectedStateIndex] as Text).data))
.map((i) => i.city)
.toSet()
.toList();
res.sort();
return res.map((c) => Text(c)).toList();
}

+ 7
- 1
lib/views/unauthenticated/login.dart View File

@ -14,11 +14,15 @@
// along with Hamlet Social. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
import 'package:hamlet_social/data/instance.dart';
import 'package:hamlet_social/views/unauthenticated/instance_selector.dart';
// This should probably just redirect the user to the login
// page of the instance in an embeded web page
class LoginForm extends StatefulWidget {
List<HamletInstance> hamletInstances;
LoginForm(this.hamletInstances);
@override
_LoginFormState createState() => _LoginFormState();
}
@ -28,6 +32,8 @@ class _LoginFormState extends State {
@override
Widget build(BuildContext context) {
return !_isInstanceSelected ? InstanceSelector() : Text("Instance selected");
return !_isInstanceSelected
? InstanceSelector(widget.hamletInstances)
: Text("Instance selected");
}
}

+ 4
- 3
lib/views/unauthenticated/sign_up.dart View File

@ -1,12 +1,13 @@
import 'package:flutter/material.dart';
import 'package:hamlet_social/data/instance.dart';
class SignUpForm extends StatefulWidget {
List<HamletInstance> hamletInstances;
SignUpForm(this.hamletInstances);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
throw UnimplementedError();
}
}

Loading…
Cancel
Save